Skip to content
Snippets Groups Projects
Commit d573d041 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

changes in "device_component" logic

parent ccdd5bd8
No related branches found
No related tags found
2 merge requests!235Release TeraFlowSDN 3.0,!150Resolve "(TID) Network device inventory management"
...@@ -174,7 +174,7 @@ message Device { ...@@ -174,7 +174,7 @@ message Device {
DeviceOperationalStatusEnum device_operational_status = 5; DeviceOperationalStatusEnum device_operational_status = 5;
repeated DeviceDriverEnum device_drivers = 6; repeated DeviceDriverEnum device_drivers = 6;
repeated EndPoint device_endpoints = 7; 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 DeviceId controller_id = 9; // Identifier of node controlling the actual device
} }
...@@ -184,6 +184,7 @@ message Component { //Defined previously to this section ...@@ -184,6 +184,7 @@ message Component { //Defined previously to this section
string type = 3; string type = 3;
repeated string child = 4; // list[sub-component.name] repeated string child = 4; // list[sub-component.name]
map<string, string> attributes = 5; // dict[attr.name => json.dumps(attr.value)] map<string, string> attributes = 5; // dict[attr.name => json.dumps(attr.value)]
string parent = 6;
} }
// ----------------------------------------------------- // -----------------------------------------------------
......
...@@ -140,7 +140,9 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -140,7 +140,9 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
@safe_and_metered_rpc_method(METRICS_POOL, LOGGER) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def ListDevices(self, request : Empty, context : grpc.ServicerContext) -> DeviceList: 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) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def GetDevice(self, request : ContextId, context : grpc.ServicerContext) -> Device: def GetDevice(self, request : ContextId, context : grpc.ServicerContext) -> Device:
......
...@@ -47,36 +47,26 @@ def compose_components_data( ...@@ -47,36 +47,26 @@ def compose_components_data(
resource_value = data["resource_value"] resource_value = data["resource_value"]
if '/inventory' in resource_key: if '/inventory' in resource_key:
resource_value_data = json.loads(resource_value) resource_value_data = json.loads(resource_value)
name = resource_value_data.get('name') name = resource_value_data.pop('name', None)
type = resource_value_data.get('class') type = resource_value_data.pop('class', None)
uuid = resource_value_data.get('uuid') uuid = resource_value_data.pop('uuid', None)
parent = resource_value_data.pop('parent-component-references', None)
if name is not None: resource_value_data.pop('component-reference', 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'
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 = { dict_component = {
'name' : name, 'component_uuid': component_uuid,
'type' : type, 'device_uuid' : device_uuid,
'attributes' : attributes, 'name' : name,
'created_at': now, 'type' : type,
'updated_at': now, '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) dict_components.append(dict_component)
else:
continue
return dict_components return dict_components
...@@ -199,9 +199,10 @@ def device_set(db_engine : Engine, request : Device) -> Tuple[Dict, bool]: ...@@ -199,9 +199,10 @@ def device_set(db_engine : Engine, request : Device) -> Tuple[Dict, bool]:
stmt = stmt.on_conflict_do_update( stmt = stmt.on_conflict_do_update(
index_elements=[ComponentModel.component_uuid], index_elements=[ComponentModel.component_uuid],
set_=dict( set_=dict(
name = str(stmt.excluded.name), name = stmt.excluded.name,
type = str(stmt.excluded.type), type = stmt.excluded.type,
attributes = str(stmt.excluded.attributes), attributes = stmt.excluded.attributes,
parent = stmt.excluded.parent,
updated_at = stmt.excluded.updated_at, updated_at = stmt.excluded.updated_at,
) )
) )
......
...@@ -27,6 +27,7 @@ class ComponentModel(_Base): #Inherit ...@@ -27,6 +27,7 @@ class ComponentModel(_Base): #Inherit
name = Column(String, nullable=False) #String field that stores the name of the component 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 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 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 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 updated_at = Column(DateTime, nullable=False) #Stores the last upadted timestamp for the component
...@@ -39,10 +40,11 @@ class ComponentModel(_Base): #Inherit ...@@ -39,10 +40,11 @@ class ComponentModel(_Base): #Inherit
} }
def dump(self) -> Dict: def dump(self) -> Dict:
data = json.loads(self.attributes)
data['component_uuid'] = self.component_uuid data['component_uuid'] = self.component_uuid
data['name'] = self.name data['name'] = self.name
data['type'] = self.type data['type'] = self.type
data['attributes'] = self.attributes data['parent'] = self.parent
return data return data
def dump_name(self) -> Dict: def dump_name(self) -> Dict:
......
...@@ -56,7 +56,7 @@ class DeviceModel(_Base): ...@@ -56,7 +56,7 @@ class DeviceModel(_Base):
]} ]}
def dump_components(self) -> List[Dict]: 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, def dump(self,
include_endpoints : bool = True, include_config_rules : bool = True, include_components : bool = True, include_endpoints : bool = True, include_config_rules : bool = True, include_components : bool = True,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment