Commit d573d041 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

changes in "device_component" logic

parent ccdd5bd8
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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;
}

// -----------------------------------------------------
+3 −1
Original line number Diff line number Diff line
@@ -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:
+18 −28
Original line number Diff line number Diff line
@@ -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 = {
                'component_uuid': component_uuid,
                'device_uuid'   : device_uuid,
                'name'          : name,
                'type'          : type,
                'attributes' : attributes,
                '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
+4 −3
Original line number Diff line number Diff line
@@ -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,
                )
            )
+3 −1
Original line number Diff line number Diff line
@@ -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:
Loading