diff --git a/proto/context.proto b/proto/context.proto index 2bd51a56b4622d3e23dc2491ceab294455bc66f0..1c7518b9f556e911d5b49744e170b4e28fe02f23 100644 --- a/proto/context.proto +++ b/proto/context.proto @@ -174,7 +174,7 @@ message Device { DeviceOperationalStatusEnum device_operational_status = 5; repeated DeviceDriverEnum device_drivers = 6; repeated EndPoint device_endpoints = 7; - map<string, Component> components = 8; // Used for inventory + repeated Component components = 8; // Used for inventory DeviceId controller_id = 9; // Identifier of node controlling the actual device } @@ -184,6 +184,7 @@ message Component { //Defined previously to this section string type = 3; repeated string child = 4; // list[sub-component.name] map<string, string> attributes = 5; // dict[attr.name => json.dumps(attr.value)] + string parent = 6; } // ----------------------------------------------------- diff --git a/src/context/service/ContextServiceServicerImpl.py b/src/context/service/ContextServiceServicerImpl.py index 6d540b4945df8516697c957316294a452186ddb1..23b70be3a9be173853fd3bc3a7c096752b5a785d 100644 --- a/src/context/service/ContextServiceServicerImpl.py +++ b/src/context/service/ContextServiceServicerImpl.py @@ -140,7 +140,9 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def ListDevices(self, request : Empty, context : grpc.ServicerContext) -> DeviceList: - return DeviceList(devices=device_list_objs(self.db_engine)) + devices = device_list_objs(self.db_engine) + LOGGER.info('DEVICES: {:s}'.format(str(devices))) + return DeviceList(devices=devices) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def GetDevice(self, request : ContextId, context : grpc.ServicerContext) -> Device: diff --git a/src/context/service/database/Component.py b/src/context/service/database/Component.py index befaa0ef82e655ba9b4f956c7ec4ea0da10336d7..8774c0cbf6a57ddb22eabc63669bc618f577b809 100644 --- a/src/context/service/database/Component.py +++ b/src/context/service/database/Component.py @@ -47,36 +47,26 @@ def compose_components_data( resource_value = data["resource_value"] if '/inventory' in resource_key: resource_value_data = json.loads(resource_value) - name = resource_value_data.get('name') - type = resource_value_data.get('class') - uuid = resource_value_data.get('uuid') - - if name is not None: - del resource_value_data['name'] - if type is not None: - del resource_value_data['class'] - if uuid is not None: - del resource_value_data['uuid'] - - attributes = resource_value_data #Store the remaining fields in 'attributes' + name = resource_value_data.pop('name', None) + type = resource_value_data.pop('class', None) + uuid = resource_value_data.pop('uuid', None) + parent = resource_value_data.pop('parent-component-references', None) + resource_value_data.pop('component-reference', None) + attributes = { + attr_name:json.dumps(attr_value) + for attr_name,attr_value in resource_value_data.items() + } + component_uuid = get_uuid_from_string(component.custom.resource_key, prefix_for_name=device_uuid) dict_component = { - 'name' : name, - 'type' : type, - 'attributes' : attributes, - 'created_at': now, - 'updated_at': now, + 'component_uuid': component_uuid, + 'device_uuid' : device_uuid, + 'name' : name, + 'type' : type, + 'attributes' : json.dumps(attributes), #Store the remaining fields in 'attributes' + 'parent' : parent, + 'created_at' : now, + 'updated_at' : now, } - - dict_component['device_uuid'] = device_uuid - component_name = '{:s}:{:s}:{:s}'.format('device', 'custom', component.custom.resource_key) - - - component_uuid = get_uuid_from_string(component_name, prefix_for_name=device_uuid) - dict_component['component_uuid'] = component_uuid - dict_components.append(dict_component) - else: - continue - return dict_components diff --git a/src/context/service/database/Device.py b/src/context/service/database/Device.py index 0faecf70fb3c2a22f3b4141c8861088903be658c..0bffce8b4551a7d888ca02aecfe0ff6e950d68e1 100644 --- a/src/context/service/database/Device.py +++ b/src/context/service/database/Device.py @@ -199,9 +199,10 @@ def device_set(db_engine : Engine, request : Device) -> Tuple[Dict, bool]: stmt = stmt.on_conflict_do_update( index_elements=[ComponentModel.component_uuid], set_=dict( - name = str(stmt.excluded.name), - type = str(stmt.excluded.type), - attributes = str(stmt.excluded.attributes), + name = stmt.excluded.name, + type = stmt.excluded.type, + attributes = stmt.excluded.attributes, + parent = stmt.excluded.parent, updated_at = stmt.excluded.updated_at, ) ) diff --git a/src/context/service/database/models/ComponentModel.py b/src/context/service/database/models/ComponentModel.py index ac6d44ba8713304488d1cc4e1e1a99796da9feff..c74b3955d6e3bd7296737d7e0d5f1985fe8a590f 100644 --- a/src/context/service/database/models/ComponentModel.py +++ b/src/context/service/database/models/ComponentModel.py @@ -27,6 +27,7 @@ class ComponentModel(_Base): #Inherit name = Column(String, nullable=False) #String field that stores the name of the component type = Column(String, nullable=False) #String field that stores the name of the component attributes = Column(String, nullable=False) #String field that stores data about the component + parent = Column(String, nullable=False) #String field that stores data about the component created_at = Column(DateTime, nullable=False) #Stores the creaton timestamp for the component updated_at = Column(DateTime, nullable=False) #Stores the last upadted timestamp for the component @@ -39,10 +40,11 @@ class ComponentModel(_Base): #Inherit } def dump(self) -> Dict: + data = json.loads(self.attributes) data['component_uuid'] = self.component_uuid data['name'] = self.name data['type'] = self.type - data['attributes'] = self.attributes + data['parent'] = self.parent return data def dump_name(self) -> Dict: diff --git a/src/context/service/database/models/DeviceModel.py b/src/context/service/database/models/DeviceModel.py index bd52ce76d381906537421096015489758b59d1c8..376dc98c4053f68c511a8c717117d58d9eda1cca 100644 --- a/src/context/service/database/models/DeviceModel.py +++ b/src/context/service/database/models/DeviceModel.py @@ -56,7 +56,7 @@ class DeviceModel(_Base): ]} def dump_components(self) -> List[Dict]: - return {component.name:component.dump() for component in self.components} + return [component.dump() for component in self.components] def dump(self, include_endpoints : bool = True, include_config_rules : bool = True, include_components : bool = True,