From e05e698a619738d69b13beb3148c1e421c813303 Mon Sep 17 00:00:00 2001 From: Sviatoslav Boichuk Date: Tue, 22 Oct 2024 10:26:04 +0300 Subject: [PATCH 1/2] Do not send device capabilities change message to NB for unassigfned ifra connections --- src/cgw_connection_server.rs | 75 +++++++++++++----------------------- 1 file changed, 27 insertions(+), 48 deletions(-) diff --git a/src/cgw_connection_server.rs b/src/cgw_connection_server.rs index 8635ebe..f3f4717 100644 --- a/src/cgw_connection_server.rs +++ b/src/cgw_connection_server.rs @@ -1535,6 +1535,33 @@ impl CGWConnectionServer { debug!("Detected foreign infra {} connection. Group: {}, Group Shard Owner: {}", device_mac.to_hex_string(), group_id, group_owner_id); } + + let changes = + cgw_detect_device_chages(&device.get_device_capabilities(), &caps); + match changes { + Some(diff) => { + if let Ok(resp) = cgw_construct_device_capabilities_changed_msg( + device_mac, + device.get_device_group_id(), + &diff, + ) { + self.enqueue_mbox_message_from_cgw_to_nb_api( + device.get_device_group_id(), + resp, + ); + } else { + error!( + "Failed to construct device_capabilities_changed message!" + ); + } + } + None => { + debug!( + "Capabilities for device: {} was not changed", + device_mac.to_hex_string() + ) + } + } } else { if let Ok(resp) = cgw_construct_unassigned_infra_connection_msg( device_mac, @@ -1551,32 +1578,6 @@ impl CGWConnectionServer { ); } - let changes = - cgw_detect_device_chages(&device.get_device_capabilities(), &caps); - match changes { - Some(diff) => { - if let Ok(resp) = cgw_construct_device_capabilities_changed_msg( - device_mac, - device.get_device_group_id(), - &diff, - ) { - self.enqueue_mbox_message_from_cgw_to_nb_api( - device.get_device_group_id(), - resp, - ); - } else { - error!( - "Failed to construct device_capabilities_changed message!" - ); - } - } - None => { - debug!( - "Capabilities for device: {} was not changed", - device_mac.to_hex_string() - ) - } - } device.update_device_capabilities(&caps); match serde_json::to_string(device) { Ok(device_json) => { @@ -1593,28 +1594,6 @@ impl CGWConnectionServer { } } } else { - let default_caps: CGWDeviceCapabilities = Default::default(); - let changes = cgw_detect_device_chages(&default_caps, &caps); - match changes { - Some(diff) => { - if let Ok(resp) = cgw_construct_device_capabilities_changed_msg( - device_mac, 0, &diff, - ) { - self.enqueue_mbox_message_from_cgw_to_nb_api(0, resp); - } else { - error!( - "Failed to construct device_capabilities_changed message!" - ); - } - } - None => { - debug!( - "Capabilities for device: {} was not changed", - device_mac.to_hex_string() - ) - } - } - let device: CGWDevice = CGWDevice::new( device_type, CGWDeviceState::CGWDeviceConnected, From 33d44a2ac0fd651d403dd066c7f662b910358662 Mon Sep 17 00:00:00 2001 From: Sviatoslav Boichuk Date: Tue, 22 Oct 2024 14:58:37 +0300 Subject: [PATCH 2/2] Send unassigned infra device capabilities change when receive infra add message --- src/cgw_connection_server.rs | 53 +++++++++++++++++++++++++++++++++--- src/cgw_remote_discovery.rs | 6 ++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/cgw_connection_server.rs b/src/cgw_connection_server.rs index f3f4717..1cdb36c 100644 --- a/src/cgw_connection_server.rs +++ b/src/cgw_connection_server.rs @@ -1114,14 +1114,18 @@ impl CGWConnectionServer { .create_ifras_list(gid, mac_list.clone(), devices_cache_lock) .await { - Ok(()) => { - // All mac's GIDs been successfully changed; + Ok(success_ifras) => { + // Not all mac's GIDs might been successfully changed; // Notify all of them about the change. self.clone() - .notify_devices_on_gid_change(mac_list.clone(), gid); + .notify_devices_on_gid_change(success_ifras.clone(), gid); if let Ok(resp) = cgw_construct_infra_group_device_add_response( - gid, mac_list, uuid, true, None, + gid, + success_ifras.clone(), + uuid, + true, + None, ) { self.enqueue_mbox_message_from_cgw_to_nb_api(gid, resp); } else { @@ -1129,6 +1133,47 @@ impl CGWConnectionServer { "Failed to construct infra_group_device_add message!" ); } + + let devices_cache_read = self.devices_cache.read().await; + for mac in success_ifras { + if let Some(dev) = devices_cache_read.get_device(&mac) { + if dev.get_device_state() + == CGWDeviceState::CGWDeviceConnected + { + let dev_gid = dev.get_device_group_id(); + let changes = cgw_detect_device_chages( + &CGWDeviceCapabilities::default(), + &dev.get_device_capabilities(), + ); + match changes { + Some(diff) => { + if let Ok(resp) = + cgw_construct_device_capabilities_changed_msg( + mac, + dev_gid, + &diff, + ) + { + self.enqueue_mbox_message_from_cgw_to_nb_api( + dev_gid, + resp, + ); + } else { + error!( + "Failed to construct device_capabilities_changed message!" + ); + } + } + None => { + debug!( + "Capabilities for device: {} was not changed", + mac.to_hex_string() + ) + } + } + } + } + } } Err(macs) => { if let Error::RemoteDiscoveryFailedInfras(mac_addresses) = macs diff --git a/src/cgw_remote_discovery.rs b/src/cgw_remote_discovery.rs index f6f7d70..1ee58e0 100644 --- a/src/cgw_remote_discovery.rs +++ b/src/cgw_remote_discovery.rs @@ -880,7 +880,7 @@ impl CGWRemoteDiscovery { gid: i32, infras: Vec, cache: Arc>, - ) -> Result<()> { + ) -> Result> { // TODO: assign list to shards; currently - only created bulk, no assignment let mut futures = Vec::with_capacity(infras.len()); // Results store vec of MACs we failed to add @@ -906,6 +906,7 @@ impl CGWRemoteDiscovery { return Err(Error::RemoteDiscoveryFailedInfras(infras)); } + let mut success_infras: Vec = Vec::with_capacity(futures.len()); let mut failed_infras: Vec = Vec::with_capacity(futures.len()); for x in infras.iter() { let db_accessor_clone = self.db_accessor.clone(); @@ -974,6 +975,7 @@ impl CGWRemoteDiscovery { } } } + success_infras.push(device_mac); assigned_infras_num += 1; } } @@ -995,7 +997,7 @@ impl CGWRemoteDiscovery { return Err(Error::RemoteDiscoveryFailedInfras(failed_infras)); } - Ok(()) + Ok(success_infras) } pub async fn destroy_ifras_list(