From 8e8989793f5e2eec49d450ea87039991b5ad1ef4 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Fri, 19 May 2023 09:57:20 +0000
Subject: [PATCH] Device component:

- Added logic to auto-enable devices when Automation component is not deployed
- Updated retrieval of device controller
---
 .../service/DeviceServiceServicerImpl.py      | 14 +++++++++--
 src/device/service/Tools.py                   | 23 +++++++++++--------
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/device/service/DeviceServiceServicerImpl.py b/src/device/service/DeviceServiceServicerImpl.py
index 38a6b735b..d29d469cb 100644
--- a/src/device/service/DeviceServiceServicerImpl.py
+++ b/src/device/service/DeviceServiceServicerImpl.py
@@ -12,9 +12,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import grpc, logging, time
+import grpc, logging, os, time
 from typing import Dict
 from prometheus_client import Histogram
+from common.Constants import ServiceNameEnum
+from common.Settings import ENVVAR_SUFIX_SERVICE_HOST, get_env_var_name
 from common.method_wrappers.Decorator import MetricTypeEnum, MetricsPool, safe_and_metered_rpc_method
 from common.method_wrappers.ServiceExceptions import NotFoundException, OperationFailedException
 from common.proto.context_pb2 import (
@@ -121,7 +123,15 @@ class DeviceServiceServicerImpl(DeviceServiceServicer):
 
             t9 = time.time()
 
-            device.device_operational_status = DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_DISABLED
+            automation_service_host = get_env_var_name(ServiceNameEnum.AUTOMATION, ENVVAR_SUFIX_SERVICE_HOST)
+            environment_variables = set(os.environ.keys())
+            if automation_service_host in environment_variables:
+                # Automation component is deployed; leave devices disabled. Automation will enable them.
+                device.device_operational_status = DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_DISABLED
+            else:
+                # Automation is not deployed; assume the device is ready while onboarding and set them as enabled.
+                device.device_operational_status = DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED
+
             device_id = context_client.SetDevice(device)
 
             t10 = time.time()
diff --git a/src/device/service/Tools.py b/src/device/service/Tools.py
index cd3af07e3..6a62a75e7 100644
--- a/src/device/service/Tools.py
+++ b/src/device/service/Tools.py
@@ -79,11 +79,13 @@ def check_no_endpoints(device_endpoints) -> None:
                         'interrogation of the physical device.')
 
 def get_device_controller_uuid(device : Device) -> Optional[str]:
-    for config_rule in device.device_config.config_rules:
-        if config_rule.WhichOneof('config_rule') != 'custom': continue
-        if config_rule.custom.resource_key != '_controller': continue
-        device_controller_id = json.loads(config_rule.custom.resource_value)
-        return device_controller_id['uuid']
+    controller_uuid = device.controller_id.device_uuid.uuid
+    if len(controller_uuid) > 0: return controller_uuid
+    #for config_rule in device.device_config.config_rules:
+    #    if config_rule.WhichOneof('config_rule') != 'custom': continue
+    #    if config_rule.custom.resource_key != '_controller': continue
+    #    device_controller_id = json.loads(config_rule.custom.resource_value)
+    #    return device_controller_id['uuid']
     return None
 
 def populate_endpoints(
@@ -142,11 +144,12 @@ def populate_endpoints(
             # Sub-devices should not have a driver assigned. Instead, they should have
             # a config rule specifying their controller.
             #_sub_device.device_drivers.extend(resource_value['drivers'])        # pylint: disable=no-member
-            controller_config_rule = _sub_device.device_config.config_rules.add()
-            controller_config_rule.action = ConfigActionEnum.CONFIGACTION_SET
-            controller_config_rule.custom.resource_key = '_controller'
-            controller = {'uuid': device_uuid, 'name': device_name}
-            controller_config_rule.custom.resource_value = json.dumps(controller, indent=0, sort_keys=True)
+            #controller_config_rule = _sub_device.device_config.config_rules.add()
+            #controller_config_rule.action = ConfigActionEnum.CONFIGACTION_SET
+            #controller_config_rule.custom.resource_key = '_controller'
+            #controller = {'uuid': device_uuid, 'name': device_name}
+            #controller_config_rule.custom.resource_value = json.dumps(controller, indent=0, sort_keys=True)
+            _sub_device.controller_id.device_uuid.uuid = device_uuid
 
             new_sub_devices[_sub_device_uuid] = _sub_device
 
-- 
GitLab