Skip to content

Commit db62ed8

Browse files
authored
retrieve uuid from endpoint address, use it with service url as device id (#630)
* retrieve uuid from endpoint address, use it with service url as device id Signed-off-by: Johnson Shih <jshih@microsoft.com> * update patch version Signed-off-by: Johnson Shih <jshih@microsoft.com> --------- Signed-off-by: Johnson Shih <jshih@microsoft.com>
1 parent af878cd commit db62ed8

File tree

3 files changed

+187
-62
lines changed

3 files changed

+187
-62
lines changed

discovery-handlers/onvif/src/discovery_handler.rs

+87-31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::discovery_impl::util;
22
use super::discovery_utils::{
33
OnvifQuery, OnvifQueryImpl, ONVIF_DEVICE_IP_ADDRESS_LABEL_ID,
44
ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID, ONVIF_DEVICE_SERVICE_URL_LABEL_ID,
5+
ONVIF_DEVICE_UUID_LABEL_ID,
56
};
67
use akri_discovery_utils::{
78
discovery::{
@@ -73,7 +74,7 @@ impl DiscoveryHandler for DiscoveryHandlerImpl {
7374
deserialize_discovery_details(&discover_request.discovery_details)
7475
.map_err(|e| tonic::Status::new(tonic::Code::InvalidArgument, format!("{}", e)))?;
7576
tokio::spawn(async move {
76-
let mut previous_cameras = Vec::new();
77+
let mut previous_cameras = HashMap::new();
7778
let mut filtered_camera_devices = HashMap::new();
7879
loop {
7980
// Before each iteration, check if receiver has dropped
@@ -98,17 +99,19 @@ impl DiscoveryHandler for DiscoveryHandlerImpl {
9899
.unwrap();
99100
trace!("discover - discovered:{:?}", &latest_cameras);
100101
// Remove cameras that have gone offline
101-
previous_cameras.iter().for_each(|c| {
102-
if !latest_cameras.contains(c) {
102+
previous_cameras.keys().for_each(|c| {
103+
if !latest_cameras.contains_key(c) {
103104
changed_camera_list = true;
104105
filtered_camera_devices.remove(c);
105106
}
106107
});
107108

108109
let futures: Vec<_> = latest_cameras
109110
.iter()
110-
.filter(|c| !previous_cameras.contains(c))
111-
.map(|c| apply_filters(&discovery_handler_config, c, &onvif_query))
111+
.filter(|(k, _)| !previous_cameras.contains_key(*k))
112+
.map(|(uri, uuid)| {
113+
apply_filters(&discovery_handler_config, uri, uuid, &onvif_query)
114+
})
112115
.collect();
113116
let options = futures_util::future::join_all(futures).await;
114117
// Insert newly discovered camera that are not filtered out
@@ -150,9 +153,13 @@ impl DiscoveryHandler for DiscoveryHandlerImpl {
150153
async fn apply_filters(
151154
discovery_handler_config: &OnvifDiscoveryDetails,
152155
device_service_uri: &str,
156+
device_uuid: &str,
153157
onvif_query: &impl OnvifQuery,
154158
) -> Option<(String, Device)> {
155-
info!("apply_filters - device service url {}", device_service_uri);
159+
info!(
160+
"apply_filters - device service url {}, uuid {}",
161+
device_service_uri, device_uuid
162+
);
156163
let (ip_address, mac_address) = match onvif_query
157164
.get_device_ip_and_mac_address(device_service_uri)
158165
.await
@@ -184,19 +191,20 @@ async fn apply_filters(
184191
return None;
185192
}
186193

187-
let ip_and_mac_joined = format!("{}-{}", &ip_address, &mac_address);
194+
let service_uri_and_uuid_joined = format!("{}-{}", device_service_uri, device_uuid);
188195
let mut properties = HashMap::new();
189196
properties.insert(
190197
ONVIF_DEVICE_SERVICE_URL_LABEL_ID.to_string(),
191198
device_service_uri.to_string(),
192199
);
200+
properties.insert(ONVIF_DEVICE_UUID_LABEL_ID.into(), device_uuid.to_string());
193201
properties.insert(ONVIF_DEVICE_IP_ADDRESS_LABEL_ID.into(), ip_address);
194202
properties.insert(ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID.into(), mac_address);
195203

196204
Some((
197205
device_service_uri.to_string(),
198206
Device {
199-
id: ip_and_mac_joined,
207+
id: service_uri_and_uuid_joined,
200208
properties,
201209
mounts: Vec::default(),
202210
device_specs: Vec::default(),
@@ -239,20 +247,20 @@ mod tests {
239247
.returning(move |_| Ok((ip.to_string(), mac.to_string())));
240248
}
241249

242-
fn expected_device(uri: &str, ip: &str, mac: &str) -> (String, Device) {
250+
fn expected_device(uri: &str, uuid: &str, ip: &str, mac: &str) -> (String, Device) {
243251
let mut properties = HashMap::new();
244252
properties.insert(
245253
ONVIF_DEVICE_SERVICE_URL_LABEL_ID.to_string(),
246254
uri.to_string(),
247255
);
248-
256+
properties.insert(ONVIF_DEVICE_UUID_LABEL_ID.into(), uuid.to_string());
249257
properties.insert(ONVIF_DEVICE_IP_ADDRESS_LABEL_ID.into(), ip.to_string());
250258
properties.insert(ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID.into(), mac.to_string());
251259

252260
(
253261
uri.to_string(),
254262
Device {
255-
id: format!("{}-{}", ip, mac),
263+
id: format!("{}-{}", uri, uuid),
256264
properties,
257265
mounts: Vec::default(),
258266
device_specs: Vec::default(),
@@ -271,6 +279,7 @@ mod tests {
271279
#[tokio::test]
272280
async fn test_apply_filters_no_filters() {
273281
let mock_uri = "device_uri";
282+
let mock_uuid = "device_uuid";
274283
let mock_ip = "mock.ip";
275284
let mock_mac = "mock:mac";
276285

@@ -290,14 +299,20 @@ mod tests {
290299
scopes: None,
291300
discovery_timeout_seconds: 1,
292301
};
293-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
302+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
303+
.await
304+
.unwrap();
294305

295-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
306+
assert_eq!(
307+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
308+
instance
309+
);
296310
}
297311

298312
#[tokio::test]
299313
async fn test_apply_filters_include_ip_exist() {
300314
let mock_uri = "device_uri";
315+
let mock_uuid = "device_uuid";
301316
let mock_ip = "mock.ip";
302317
let mock_mac = "mock:mac";
303318

@@ -320,14 +335,20 @@ mod tests {
320335
scopes: None,
321336
discovery_timeout_seconds: 1,
322337
};
323-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
338+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
339+
.await
340+
.unwrap();
324341

325-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
342+
assert_eq!(
343+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
344+
instance
345+
);
326346
}
327347

328348
#[tokio::test]
329349
async fn test_apply_filters_include_ip_nonexist() {
330350
let mock_uri = "device_uri";
351+
let mock_uuid = "device_uuid";
331352

332353
let mut mock = MockOnvifQuery::new();
333354
configure_scenario(
@@ -348,14 +369,15 @@ mod tests {
348369
scopes: None,
349370
discovery_timeout_seconds: 1,
350371
};
351-
assert!(apply_filters(&onvif_config, mock_uri, &mock)
372+
assert!(apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
352373
.await
353374
.is_none());
354375
}
355376

356377
#[tokio::test]
357378
async fn test_apply_filters_include_ip_similar() {
358379
let mock_uri = "device_uri";
380+
let mock_uuid = "device_uuid";
359381

360382
let mut mock = MockOnvifQuery::new();
361383
configure_scenario(
@@ -376,14 +398,15 @@ mod tests {
376398
scopes: None,
377399
discovery_timeout_seconds: 1,
378400
};
379-
assert!(apply_filters(&onvif_config, mock_uri, &mock)
401+
assert!(apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
380402
.await
381403
.is_none());
382404
}
383405

384406
#[tokio::test]
385407
async fn test_apply_filters_exclude_ip_nonexist() {
386408
let mock_uri = "device_uri";
409+
let mock_uuid = "device_uuid";
387410
let mock_ip = "mock.ip";
388411
let mock_mac = "mock:mac";
389412

@@ -406,14 +429,20 @@ mod tests {
406429
scopes: None,
407430
discovery_timeout_seconds: 1,
408431
};
409-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
432+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
433+
.await
434+
.unwrap();
410435

411-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
436+
assert_eq!(
437+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
438+
instance
439+
);
412440
}
413441

414442
#[tokio::test]
415443
async fn test_apply_filters_exclude_ip_exist() {
416444
let mock_uri = "device_uri";
445+
let mock_uuid = "device_uuid";
417446
let mock_ip = "mock.ip";
418447

419448
let mut mock = MockOnvifQuery::new();
@@ -435,14 +464,15 @@ mod tests {
435464
scopes: None,
436465
discovery_timeout_seconds: 1,
437466
};
438-
assert!(apply_filters(&onvif_config, mock_uri, &mock)
467+
assert!(apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
439468
.await
440469
.is_none());
441470
}
442471

443472
#[tokio::test]
444473
async fn test_apply_filters_exclude_ip_similar() {
445474
let mock_uri = "device_uri";
475+
let mock_uuid = "device_uuid";
446476
let mock_ip = "mock.ip";
447477
let mock_mac = "mock:mac";
448478

@@ -465,14 +495,20 @@ mod tests {
465495
scopes: None,
466496
discovery_timeout_seconds: 1,
467497
};
468-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
498+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
499+
.await
500+
.unwrap();
469501

470-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
502+
assert_eq!(
503+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
504+
instance
505+
);
471506
}
472507

473508
#[tokio::test]
474509
async fn test_apply_filters_include_mac_exist() {
475510
let mock_uri = "device_uri";
511+
let mock_uuid = "device_uuid";
476512
let mock_ip = "mock.ip";
477513
let mock_mac = "mock:mac";
478514

@@ -495,14 +531,20 @@ mod tests {
495531
scopes: None,
496532
discovery_timeout_seconds: 1,
497533
};
498-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
534+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
535+
.await
536+
.unwrap();
499537

500-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
538+
assert_eq!(
539+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
540+
instance
541+
);
501542
}
502543

503544
#[tokio::test]
504545
async fn test_apply_filters_include_mac_nonexist() {
505546
let mock_uri = "device_uri";
547+
let mock_uuid = "device_uuid";
506548

507549
let mut mock = MockOnvifQuery::new();
508550
configure_scenario(
@@ -523,14 +565,15 @@ mod tests {
523565
scopes: None,
524566
discovery_timeout_seconds: 1,
525567
};
526-
assert!(apply_filters(&onvif_config, mock_uri, &mock)
568+
assert!(apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
527569
.await
528570
.is_none());
529571
}
530572

531573
#[tokio::test]
532574
async fn test_apply_filters_exclude_mac_nonexist() {
533575
let mock_uri = "device_uri";
576+
let mock_uuid = "device_uuid";
534577
let mock_ip = "mock.ip";
535578
let mock_mac = "mock:mac";
536579

@@ -553,14 +596,20 @@ mod tests {
553596
scopes: None,
554597
discovery_timeout_seconds: 1,
555598
};
556-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
599+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
600+
.await
601+
.unwrap();
557602

558-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
603+
assert_eq!(
604+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
605+
instance
606+
);
559607
}
560608

561609
#[tokio::test]
562610
async fn test_apply_filters_exclude_mac_exist() {
563611
let mock_uri = "device_uri";
612+
let mock_uuid = "device_uuid";
564613
let mock_mac = "mock:mac";
565614

566615
let mut mock = MockOnvifQuery::new();
@@ -582,14 +631,15 @@ mod tests {
582631
scopes: None,
583632
discovery_timeout_seconds: 1,
584633
};
585-
assert!(apply_filters(&onvif_config, mock_uri, &mock)
634+
assert!(apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
586635
.await
587636
.is_none());
588637
}
589638

590639
#[tokio::test]
591640
async fn test_apply_filters_include_mac_exist_different_letter_cases() {
592641
let mock_uri = "device_uri";
642+
let mock_uuid = "device_uuid";
593643
let mock_ip = "mock.ip";
594644
let mock_mac = "MocK:Mac";
595645

@@ -612,14 +662,20 @@ mod tests {
612662
scopes: None,
613663
discovery_timeout_seconds: 1,
614664
};
615-
let instance = apply_filters(&onvif_config, mock_uri, &mock).await.unwrap();
665+
let instance = apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
666+
.await
667+
.unwrap();
616668

617-
assert_eq!(expected_device(mock_uri, mock_ip, mock_mac), instance);
669+
assert_eq!(
670+
expected_device(mock_uri, mock_uuid, mock_ip, mock_mac),
671+
instance
672+
);
618673
}
619674

620675
#[tokio::test]
621676
async fn test_apply_filters_exclude_mac_exist_different_letter_cases() {
622677
let mock_uri = "device_uri";
678+
let mock_uuid = "device_uuid";
623679
let mock_ip = "mock.ip";
624680
let mock_mac = "MocK:Mac";
625681

@@ -642,7 +698,7 @@ mod tests {
642698
scopes: None,
643699
discovery_timeout_seconds: 1,
644700
};
645-
assert!(apply_filters(&onvif_config, mock_uri, &mock)
701+
assert!(apply_filters(&onvif_config, mock_uri, mock_uuid, &mock)
646702
.await
647703
.is_none());
648704
}

0 commit comments

Comments
 (0)