zenoh-flow 0.5.0-alpha.2

Zenoh-Flow: a Zenoh-based data flow programming framework for computations that span from the cloud to the device.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
//
// Copyright (c) 2021 - 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

#![allow(unused)]

#[cfg(feature = "data_bincode")]
extern crate bincode;

#[cfg(feature = "data_cbor")]
extern crate serde_cbor;

#[cfg(feature = "data_json")]
extern crate serde_json;

use crate::model::record::DataFlowRecord;
use crate::model::registry::RegistryNode;
use crate::runtime::{RuntimeConfig, RuntimeInfo, RuntimeStatus};
use crate::zfresult::ErrorKind;
use crate::Result;
use crate::{bail, zferror};
use async_std::pin::Pin;
use async_std::stream::Stream;
use async_std::task::{Context, Poll};
use futures::StreamExt;
use futures_lite::FutureExt;
use pin_project_lite::pin_project;
use serde::{de::DeserializeOwned, Serialize};
use std::convert::TryFrom;
use std::sync::Arc;
use uhlc::HLC;
use uuid::Uuid;
use zenoh::prelude::r#async::*;
use zenoh::query::Reply;

use super::Job;

//NOTE: this should be pub(crate)

/// Root prefix for key expressions when running as a router plugin.
pub static ROOT_PLUGIN_RUNTIME_PREFIX: &str = "@/router/";
/// Root suffix for key expression when running as router plugin.
pub static ROOT_PLUGIN_RUNTIME_SUFFIX: &str = "plugin/zenoh-flow";
/// Root for key expression when running as standalone.
pub static ROOT_STANDALONE: &str = "zenoh-flow";

/// Token for the runtime in the key expression.
pub static KEY_RUNTIMES: &str = "runtimes";
/// Token for the registry in the key expression.
pub static KEY_REGISTRY: &str = "registry";

/// TOken for the flow in the key expression.
pub static KEY_FLOWS: &str = "flows";
/// Token for the graphs in the key expression.
pub static KEY_GRAPHS: &str = "graphs";

/// Token for the leaf with information in the key expression.
pub static KEY_INFO: &str = "info";
/// Token for the leaf with status information in the key expression/
pub static KEY_STATUS: &str = "status";
/// Token for the leaf with configuration in the key expression.
pub static KEY_CONFIGURATION: &str = "configuration";

/// Token for job queue in the key expression.
pub static KEY_JOB_QUEUE: &str = "job-queue";

/// Token for the submitted jobs job queue in the key expression.
pub static KEY_JOB_SUBMITTED: &str = "sumbitted";

/// Token for the started jobs job queue in the key expression.
pub static KEY_JOB_STARTED: &str = "started";

/// Token for the done jobs job queue in the key expression.
pub static KEY_JOB_DONE: &str = "done";

/// Token for the failed jobs job queue in the key expression.
pub static KEY_JOB_FAILED: &str = "failed";

/// Generates the runtime info key expression.
#[macro_export]
macro_rules! RT_INFO_PATH {
    ($prefix:expr, $rtid:expr) => {
        format!(
            "{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_INFO
        )
    };
}

/// Generates the runtime status key expression.
#[macro_export]
macro_rules! RT_STATUS_PATH {
    ($prefix:expr, $rtid:expr) => {
        format!(
            "{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_STATUS
        )
    };
}
/// Generates the runtime configuration key expression.
#[macro_export]
macro_rules! RT_CONFIGURATION_PATH {
    ($prefix:expr, $rtid:expr) => {
        format!(
            "{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_CONFIGURATION
        )
    };
}

/// Generates the flow instance key expression.
#[macro_export]
macro_rules! RT_FLOW_PATH {
    ($prefix:expr, $rtid:expr, $fid:expr, $iid:expr) => {
        format!(
            "{}/{}/{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_FLOWS,
            $fid,
            $iid
        )
    };
}

/// Generates the flow selector by instance id.
#[macro_export]
macro_rules! RT_FLOW_SELECTOR_BY_INSTANCE {
    ($prefix:expr, $rtid:expr, $iid:expr) => {
        format!(
            "{}/{}/{}/{}/*/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_FLOWS,
            $iid
        )
    };
}

/// Generates the flow selector by flow id.
#[macro_export]
macro_rules! RT_FLOW_SELECTOR_BY_FLOW {
    ($prefix:expr, $rtid:expr, $fid:expr) => {
        format!(
            "{}/{}/{}/{}/{}/*",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_FLOWS,
            $fid
        )
    };
}

/// Generate the selector for all flows.
#[macro_export]
macro_rules! RT_FLOW_SELECTOR_ALL {
    ($prefix:expr, $rtid:expr) => {
        format!(
            "{}/{}/{}/{}/*/*",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rtid,
            $crate::runtime::resources::KEY_FLOWS
        )
    };
}

/// Generates the flow selector by instance, for all runtimes.
#[macro_export]
macro_rules! FLOW_SELECTOR_BY_INSTANCE {
    ($prefix:expr, $iid:expr) => {
        format!(
            "{}/{}/*/{}/*/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $crate::runtime::resources::KEY_FLOWS,
            $iid
        )
    };
}
/// Generates the flow selector by flow, for all runtimes.
#[macro_export]
macro_rules! FLOW_SELECTOR_BY_FLOW {
    ($prefix:expr, $fid:expr) => {
        format!(
            "{}/{}/*/{}/{}/*",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $crate::runtime::resources::KEY_FLOWS,
            $fid
        )
    };
}

/// Generates the graph key expression.
#[macro_export]
macro_rules! REG_GRAPH_SELECTOR {
    ($prefix:expr, $fid:expr) => {
        format!(
            "{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_REGISTRY,
            $crate::runtime::resources::KEY_GRAPHS,
            $fid
        )
    };
}

/// Generates the sumbitted jobs key expression (selector)
#[macro_export]
macro_rules! JQ_SUMBITTED_SEL {
    ($prefix:expr, $rid:expr) => {
        format!(
            "{}/{}/{}/{}/{}/*",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rid,
            $crate::runtime::resources::KEY_JOB_QUEUE,
            $crate::runtime::resources::KEY_JOB_SUBMITTED
        )
    };
}

/// Generates the sumbitted job key expression
#[macro_export]
macro_rules! JQ_SUMBITTED_JOB {
    ($prefix:expr, $rid:expr, $jid: expr) => {
        format!(
            "{}/{}/{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rid,
            $crate::runtime::resources::KEY_JOB_QUEUE,
            $crate::runtime::resources::KEY_JOB_SUBMITTED,
            $jid
        )
    };
}

/// Generates the started job key expression
#[macro_export]
macro_rules! JQ_STARTED_JOB {
    ($prefix:expr, $rid:expr, $jid: expr) => {
        format!(
            "{}/{}/{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rid,
            $crate::runtime::resources::KEY_JOB_QUEUE,
            $crate::runtime::resources::KEY_JOB_STARTED,
            $jid
        )
    };
}

/// Generates the done job key expression
#[macro_export]
macro_rules! JQ_DONE_JOB {
    ($prefix:expr, $rid:expr, $jid: expr) => {
        format!(
            "{}/{}/{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rid,
            $crate::runtime::resources::KEY_JOB_QUEUE,
            $crate::runtime::resources::KEY_JOB_DONE,
            $jid
        )
    };
}

/// Generates the done job key expression
#[macro_export]
macro_rules! JQ_FAILED_JOB {
    ($prefix:expr, $rid:expr, $jid: expr) => {
        format!(
            "{}/{}/{}/{}/{}/{}",
            $prefix,
            $crate::runtime::resources::KEY_RUNTIMES,
            $rid,
            $crate::runtime::resources::KEY_JOB_QUEUE,
            $crate::runtime::resources::KEY_JOB_FAILED,
            $jid
        )
    };
}

/// Deserialize data from Zenoh storage.
/// The format used depends on the features.
/// It can be JSON (default), bincode or CBOR.
///
/// # Errors
/// If it fails to deserialize an error
/// variant will be returned.
pub fn deserialize_data<T>(raw_data: &[u8]) -> Result<T>
where
    T: DeserializeOwned,
{
    #[cfg(feature = "data_bincode")]
    return Ok(bincode::deserialize::<T>(&raw_data)?);

    #[cfg(feature = "data_cbor")]
    return Ok(serde_cbor::from_slice::<T>(&raw_data)?);

    #[cfg(feature = "data_json")]
    return Ok(serde_json::from_str::<T>(std::str::from_utf8(raw_data)?)?);
}

/// Serializes data for zenoh
///
/// # Errors
/// If it fails to serialize an error
/// variant will be returned.
#[cfg(feature = "data_bincode")]

pub fn serialize_data<T: ?Sized>(data: &T) -> FResult<Vec<u8>>
where
    T: Serialize,
{
    Ok(bincode::serialize(data)?)
}

/// Serializes data for zenoh
///
/// # Errors
/// If it fails to serialize an error
/// variant will be returned.
#[cfg(feature = "data_json")]
pub fn serialize_data<T: ?Sized>(data: &T) -> Result<Vec<u8>>
where
    T: Serialize,
{
    Ok(serde_json::to_string(data)?.into_bytes())
}

/// Serializes data for zenoh
///
/// # Errors
/// If it fails to serialize an error
/// variant will be returned.
#[cfg(feature = "data_cbor")]
pub fn serialize_data<T>(data: &T) -> FResult<Vec<u8>>
where
    T: Serialize,
{
    Ok(serde_cbor::to_vec(data)?)
}
//

/// Converts data from Zenoh samples,
/// useful when converting data from a subscriber
///
/// # Errors
/// It can return an error in the following cases
/// - fails to deserialize
/// - the sample is not an APP_OCTET_STREAM
/// - the sample is a Delete
pub fn convert<T>(sample: Sample) -> Result<T>
where
    T: DeserializeOwned,
{
    match sample.kind {
        SampleKind::Put => match sample.value.encoding {
            Encoding::APP_OCTET_STREAM => {
                match deserialize_data::<T>(&sample.value.payload.contiguous()) {
                    Ok(data) => Ok(data),
                    Err(e) => Err(e),
                }
            }
            _ => {
                log::warn!(
                    "Received sample with wrong encoding {:?}, dropping",
                    sample.value.encoding
                );
                Err(zferror!(
                    ErrorKind::DeserializationError,
                    "Received sample with wrong encoding {:?}, dropping",
                    sample.value.encoding
                )
                .into())
            }
        },
        SampleKind::Delete => {
            log::warn!("Received delete sample drop it");
            Err(zferror!(
                ErrorKind::DeserializationError,
                "Received delete sample dropping it"
            )
            .into())
        }
    }
}

/// The `DataStore` provides all the methods to access/store/listen and update
/// all the information stored in zenoh storages.
#[derive(Clone)]
pub struct DataStore {
    //Name TBD
    z: Arc<zenoh::Session>,
}

impl DataStore {
    /// Creates a new `DataStore` from an `Arc<zenoh::Session>`
    pub fn new(z: Arc<zenoh::Session>) -> Self {
        Self { z }
    }

    /// Gets the [`RuntimeInfo`](`RuntimeInfo`) for the given `rtid`.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_runtime_info(&self, rtid: &ZenohId) -> Result<RuntimeInfo> {
        let selector = RT_INFO_PATH!(ROOT_STANDALONE, rtid);

        self.get_from_zenoh::<RuntimeInfo>(&selector).await
    }

    /// Gets the  [`RuntimeInfo`](`RuntimeInfo`) for all the runtimes in the
    /// infrastructure
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_all_runtime_info(&self) -> Result<Vec<RuntimeInfo>> {
        let selector = RT_INFO_PATH!(ROOT_STANDALONE, "*");

        self.get_vec_from_zenoh::<RuntimeInfo>(&selector).await
    }

    /// Gets the  [`RuntimeInfo`](`RuntimeInfo`) for the runtime with the
    /// given name `rtid`.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_runtime_info_by_name(&self, rtid: &str) -> Result<RuntimeInfo> {
        let selector = RT_INFO_PATH!(ROOT_STANDALONE, "*");
        let rts = self.get_vec_from_zenoh::<RuntimeInfo>(&selector).await?;
        for rt in &rts {
            if *rt.name == *rtid {
                return Ok(rt.clone());
            }
        }
        bail!(ErrorKind::NotFound)
    }

    /// Removes the information for the given runtime `rtid`.
    ///
    /// # Errors
    /// If zenoh delete fails an error variant is returned.
    pub async fn remove_runtime_info(&self, rtid: &ZenohId) -> Result<()> {
        let path = RT_INFO_PATH!(ROOT_STANDALONE, rtid);

        self.z.delete(&path).res().await
    }

    /// Stores the given  [`RuntimeInfo`](`RuntimeInfo`) for the given `rtid`
    /// in Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_runtime_info(&self, rtid: &ZenohId, rt_info: &RuntimeInfo) -> Result<()> {
        let path = RT_INFO_PATH!(ROOT_STANDALONE, rtid);

        let encoded_info = serialize_data(rt_info)?;
        self.z.put(&path, encoded_info).res().await
    }

    /// Gets [`RuntimeConfig`](`RuntimeConfig`) for the given `rtid`
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_runtime_config(&self, rtid: &ZenohId) -> Result<RuntimeConfig> {
        let selector = RT_CONFIGURATION_PATH!(ROOT_STANDALONE, rtid);
        self.get_from_zenoh::<RuntimeConfig>(&selector).await
    }

    /// Subscribes to configuration changes for the given `rtid`
    /// **NOTE:** not implemented.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - zenoh subscribe fails
    /// - fails to deserialize
    pub async fn subscribe_runtime_config(
        &self,
        rtid: &ZenohId,
    ) -> Result<zenoh::subscriber::Subscriber<'static, flume::Receiver<Sample>>> {
        // let selector = RT_CONFIGURATION_PATH!(ROOT_STANDALONE, rtid))?;
        //
        // Ok(self.z
        //     .subscribe(&selector)
        //     .await
        //     .map(|change_stream| ZFRuntimeConfigStream { change_stream })?)
        bail!(ErrorKind::Unimplemented)
    }

    /// Removes the configuration for the given `rtid`.
    ///
    /// # Errors
    /// If zenoh delete fails an error variant is returned.
    pub async fn remove_runtime_config(&self, rtid: &ZenohId) -> Result<()> {
        let path = RT_CONFIGURATION_PATH!(ROOT_STANDALONE, rtid);

        self.z.delete(&path).res().await
    }

    /// Stores the given [`RuntimeConfig`](`RuntimeConfig`) for the given
    /// `rtid` in Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_runtime_config(&self, rtid: &ZenohId, rt_info: &RuntimeConfig) -> Result<()> {
        let path = RT_CONFIGURATION_PATH!(ROOT_STANDALONE, rtid);

        let encoded_info = serialize_data(rt_info)?;
        self.z.put(&path, encoded_info).res().await
    }

    /// Gets the `RuntimeStatus` for the given runtime `rtid`.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_runtime_status(&self, rtid: &ZenohId) -> Result<RuntimeStatus> {
        let selector = RT_STATUS_PATH!(ROOT_STANDALONE, rtid);
        self.get_from_zenoh::<RuntimeStatus>(&selector).await
    }

    /// Gets the [`RuntimeStatus`](`RuntimeStatus`) for the given `rtid`.
    ///
    /// # Errors
    /// If zenoh delete fails an error variant is returned.
    pub async fn remove_runtime_status(&self, rtid: &ZenohId) -> Result<()> {
        let path = RT_STATUS_PATH!(ROOT_STANDALONE, rtid);

        self.z.delete(&path).res().await
    }

    /// Stores the given [`RuntimeStatus`](`RuntimeStatus`) for the given `rtid`
    /// in Zenoh.
    ///
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_runtime_status(&self, rtid: &ZenohId, rt_info: &RuntimeStatus) -> Result<()> {
        let path = RT_STATUS_PATH!(ROOT_STANDALONE, rtid);

        let encoded_info = serialize_data(rt_info)?;
        self.z.put(&path, encoded_info).res().await
    }

    /// Gets the [`DataFlowRecord`](`DataFlowRecord`) running on the given runtime `rtid` for the
    /// given instance `iid`.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_runtime_flow_by_instance(
        &self,
        rtid: &ZenohId,
        iid: &Uuid,
    ) -> Result<DataFlowRecord> {
        let selector = RT_FLOW_SELECTOR_BY_INSTANCE!(ROOT_STANDALONE, rtid, iid);

        self.get_from_zenoh::<DataFlowRecord>(&selector).await
    }

    /// Gets the [`DataFlowRecord`](`DataFlowRecord`) running across the
    /// infrastructure for the instance `iid`.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_flow_by_instance(&self, iid: &Uuid) -> Result<DataFlowRecord> {
        let selector = RT_FLOW_SELECTOR_BY_INSTANCE!(ROOT_STANDALONE, "*", iid);
        self.get_from_zenoh::<DataFlowRecord>(&selector).await
    }

    /// Gets all the [`DataFlowRecord`](`DataFlowRecord`) for the given
    /// instance `iid` running on the given runtime `rtid`.
    ///
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_runtime_flow_instances(
        &self,
        rtid: &ZenohId,
        fid: &str,
    ) -> Result<Vec<DataFlowRecord>> {
        let selector = RT_FLOW_SELECTOR_BY_FLOW!(ROOT_STANDALONE, rtid, fid);

        self.get_vec_from_zenoh::<DataFlowRecord>(&selector).await
    }

    /// Gets all the [`DataFlowRecord`](`DataFlowRecord`) running across
    /// the infrastructure for the given flow `fid`.
    ///
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_flow_instances(&self, fid: &str) -> Result<Vec<DataFlowRecord>> {
        let selector = FLOW_SELECTOR_BY_FLOW!(ROOT_STANDALONE, fid);
        self.get_vec_from_zenoh::<DataFlowRecord>(&selector).await
    }

    /// Gets all the [`DataFlowRecord`](`DataFlowRecord`) running across the
    /// infrastructure.
    pub async fn get_all_instances(&self) -> Result<Vec<DataFlowRecord>> {
        let selector = FLOW_SELECTOR_BY_FLOW!(ROOT_STANDALONE, "*");
        self.get_vec_from_zenoh::<DataFlowRecord>(&selector).await
    }

    /// Gets all the runtimes UUID where the given instance `iid` is running.
    pub async fn get_flow_instance_runtimes(&self, iid: &Uuid) -> Result<Vec<ZenohId>> {
        let selector = RT_FLOW_SELECTOR_BY_INSTANCE!(ROOT_STANDALONE, "*", iid);

        let mut ds = self.z.get(&selector).res().await?;

        let mut runtimes = Vec::new();

        for kv in ds.into_iter() {
            if let Ok(sample) = &kv.sample {
                let id = sample
                    .key_expr
                    .as_str()
                    .split('/')
                    .nth(2) // The way the key_expr are built, the 3rd "token" is the instance id
                    .ok_or_else(|| {
                        log::error!(
                            "Could not extract the instance id from key expression: {}",
                            sample.key_expr.as_str()
                        );
                        zferror!(ErrorKind::DeserializationError)
                    })?;
                runtimes.push(id.parse::<ZenohId>()?);
            }
        }

        Ok(runtimes)
    }

    /// Removes information on the given instance `iid` of the given flow `fid`
    /// running on the given runtime `rtid` from Zenoh.
    ///
    /// # Errors
    /// If zenoh delete fails an error variant is returned.
    pub async fn remove_runtime_flow_instance(
        &self,
        rtid: &ZenohId,
        fid: &str,
        iid: &Uuid,
    ) -> Result<()> {
        let path = RT_FLOW_PATH!(ROOT_STANDALONE, rtid, fid, iid);

        self.z.delete(&path).res().await
    }

    /// Stores the given [`DataFlowRecord`](`DataFlowRecord`) running on the
    /// given runtime `rtid` in Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_runtime_flow(
        &self,
        rtid: &ZenohId,
        flow_instance: &DataFlowRecord,
    ) -> Result<()> {
        let path = RT_FLOW_PATH!(
            ROOT_STANDALONE,
            rtid,
            flow_instance.flow,
            flow_instance.uuid
        );

        let encoded_info = serialize_data(flow_instance)?;
        self.z.put(&path, encoded_info).res().await
    }

    // Registry Related, registry is not yet in place.

    /// Stores the given [`RegistryNode`](`RegistryNode`) in the registry's
    /// Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_graph(&self, graph: &RegistryNode) -> Result<()> {
        let path = REG_GRAPH_SELECTOR!(ROOT_STANDALONE, &graph.id);

        let encoded_info = serialize_data(graph)?;
        self.z.put(&path, encoded_info).res().await
    }

    /// Gets the [`RegistryNode`](`RegistryNode`) associated with the given
    /// `graph_id` from registry's Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_graph(&self, graph_id: &str) -> Result<RegistryNode> {
        let selector = REG_GRAPH_SELECTOR!(ROOT_STANDALONE, graph_id);
        self.get_from_zenoh::<RegistryNode>(&selector).await
    }

    /// Gets all the nodes [`RegistryNode`](`RegistryNode`) within the
    /// registry's Zenoh.
    ///
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    pub async fn get_all_graphs(&self) -> Result<Vec<RegistryNode>> {
        let selector = REG_GRAPH_SELECTOR!(ROOT_STANDALONE, "*");
        self.get_vec_from_zenoh::<RegistryNode>(&selector).await
    }

    /// Removes the given node `graph_id` from registry's Zenoh.
    pub async fn delete_graph(&self, graph_id: &str) -> Result<()> {
        let path = REG_GRAPH_SELECTOR!(ROOT_STANDALONE, &graph_id);

        self.z.delete(&path).res().await
    }

    // Job Queue

    /// Subscribes to the job queue of the given `rtid`
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - zenoh subscribe fails
    /// - fails to deserialize
    pub async fn subscribe_sumbitted_jobs(
        &self,
        rtid: &ZenohId,
    ) -> Result<zenoh::subscriber::Subscriber<'static, flume::Receiver<Sample>>> {
        let selector = JQ_SUMBITTED_SEL!(ROOT_STANDALONE, rtid);
        self.z.declare_subscriber(&selector).res().await
    }

    /// Submits the given [`Job`](`Job`) in the queue
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_submitted_job(&self, rtid: &ZenohId, job: &Job) -> Result<()> {
        let path = JQ_SUMBITTED_JOB!(ROOT_STANDALONE, rtid, &job.id);
        let encoded_info = serialize_data(job)?;
        self.z.put(&path, encoded_info).res().await
    }

    pub async fn del_submitted_job(&self, rtid: &ZenohId, id: &Uuid) -> Result<()> {
        let path = JQ_SUMBITTED_JOB!(ROOT_STANDALONE, rtid, id);
        self.z.delete(&path).res().await
    }

    /// Sets as started the given [`Job`](`Job`) in the queue
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_started_job(&self, rtid: &ZenohId, job: &Job) -> Result<()> {
        let path = JQ_STARTED_JOB!(ROOT_STANDALONE, rtid, &job.id);
        let encoded_info = serialize_data(job)?;
        self.z.put(&path, encoded_info).res().await
    }

    /// Sets as completed the given [`Job`](`Job`) in the queue
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_done_job(&self, rtid: &ZenohId, job: &Job) -> Result<()> {
        let path = JQ_DONE_JOB!(ROOT_STANDALONE, rtid, &job.id);
        let encoded_info = serialize_data(job)?;
        self.z.put(&path, encoded_info).res().await
    }

    /// Sets as failed the given [`Job`](`Job`) in the queue
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - fails to serialize
    /// - zenoh put fails
    pub async fn add_failed_job(&self, rtid: &ZenohId, job: &Job) -> Result<()> {
        let path = JQ_FAILED_JOB!(ROOT_STANDALONE, rtid, &job.id);
        let encoded_info = serialize_data(job)?;
        self.z.put(&path, encoded_info).res().await
    }

    // Helpers

    /// Helper function to get a generic data `T` and deserializing it
    /// from Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - no data present in zenoh
    /// - fails to deserialize
    /// - wrong zenoh encoding
    async fn get_from_zenoh<T>(&self, path: &str) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let mut ds = self.z.get(path).res().await?;
        let data = ds.into_iter().collect::<Vec<Reply>>();
        match data.len() {
            0 => Err(zferror!(ErrorKind::Empty).into()),
            _ => {
                let kv = &data[0];
                match &kv.sample {
                    Ok(sample) => match &sample.value.encoding {
                        &Encoding::APP_OCTET_STREAM => {
                            let ni = deserialize_data::<T>(&sample.value.payload.contiguous())?;
                            Ok(ni)
                        }
                        _ => Err(zferror!(ErrorKind::DeserializationError).into()),
                    },
                    _ => Err(zferror!(ErrorKind::DeserializationError).into()),
                }
            }
        }
    }

    /// Helper function to get a vector of genetic `T` and deserializing
    /// it from Zenoh.
    ///
    /// # Errors
    /// An error variant is returned in case of:
    /// - wrong encoding
    /// - fails to deserialize
    async fn get_vec_from_zenoh<T>(&self, selector: &str) -> Result<Vec<T>>
    where
        T: DeserializeOwned,
    {
        let mut ds = self.z.get(selector).res().await?;

        let mut zf_data: Vec<T> = Vec::new();

        for kv in ds.into_iter() {
            match &kv.sample {
                Ok(sample) => match &sample.value.encoding {
                    &Encoding::APP_OCTET_STREAM => {
                        let ni = deserialize_data::<T>(&sample.value.payload.contiguous())?;
                        zf_data.push(ni);
                    }
                    _ => return Err(zferror!(ErrorKind::DeserializationError).into()),
                },
                _ => return Err(zferror!(ErrorKind::DeserializationError).into()),
            }
        }
        Ok(zf_data)
    }
}