diff --git a/proto/context.proto b/proto/context.proto index 0d7a667311952a7443424c80e2b015b7c6713203..30fbe01c53815d011f2fc65b0508459b561fb678 100644 --- a/proto/context.proto +++ b/proto/context.proto @@ -187,8 +187,6 @@ message Component { //Defined previously to this section string parent = 5; } -// ----------------------------------------------------- - message DeviceConfig { repeated ConfigRule config_rules = 1; } diff --git a/src/context/service/database/Component.py b/src/context/service/database/Component.py index 3f2776787570a85d8b090e3b20e47bd6c016e04b..ae873855b1c01c80c101eb4e97e3bf7688619fae 100644 --- a/src/context/service/database/Component.py +++ b/src/context/service/database/Component.py @@ -1,5 +1,3 @@ - - # Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +14,6 @@ import datetime, json, logging from sqlalchemy import delete -#from sqlalchemy.dialects import postgresql from sqlalchemy.dialects.postgresql import insert from sqlalchemy.orm import Session from typing import Dict, List, Optional, Set @@ -63,7 +60,7 @@ def compose_components_data( 'device_uuid' : device_uuid, 'name' : name, 'type' : type_, - 'attributes' : json.dumps(attributes), #Store the remaining fields in 'attributes' + 'attributes' : json.dumps(attributes), 'parent' : parent, 'created_at' : now, 'updated_at' : now, diff --git a/src/context/service/database/Device.py b/src/context/service/database/Device.py index ed73b7f521e4be90cca0f28acfe8f499af96549e..6da7c91bbb852a66ff10104bde0b12fe86f814e2 100644 --- a/src/context/service/database/Device.py +++ b/src/context/service/database/Device.py @@ -12,8 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime, logging -import json +import datetime, logging, json from sqlalchemy.dialects.postgresql import insert from sqlalchemy.engine import Engine from sqlalchemy.orm import Session, selectinload, sessionmaker @@ -55,7 +54,6 @@ def device_list_objs(db_engine : Engine) -> DeviceList: .options(selectinload(DeviceModel.config_rules))\ .options(selectinload(DeviceModel.components))\ .all() - #.options(selectinload(DeviceModel.components))\ return [obj.dump() for obj in obj_list] devices = run_transaction(sessionmaker(bind=db_engine), callback) return DeviceList(devices=devices) @@ -67,7 +65,6 @@ def device_get(db_engine : Engine, request : DeviceId) -> Device: .options(selectinload(DeviceModel.endpoints))\ .options(selectinload(DeviceModel.config_rules))\ .filter_by(device_uuid=device_uuid).one_or_none() - #.options(selectinload(DeviceModel.components))\ return None if obj is None else obj.dump() obj = run_transaction(sessionmaker(bind=db_engine), callback) if obj is None: diff --git a/src/context/service/database/models/ComponentModel.py b/src/context/service/database/models/ComponentModel.py index 60927e944357fadc860cd9f0d98d4042939934cc..c9acfaeabe95c55fc464732d54459d99f9b5b054 100644 --- a/src/context/service/database/models/ComponentModel.py +++ b/src/context/service/database/models/ComponentModel.py @@ -19,20 +19,19 @@ from sqlalchemy.orm import relationship from typing import Dict from ._Base import _Base -class ComponentModel(_Base): #Inherited functionality from the base class _Base - __tablename__ = 'device_component' #Name of the table in the DB associtaed with this model - - component_uuid = Column(UUID(as_uuid=False), primary_key=True) #Unique identifier that serves as a primary key for this table - device_uuid = Column(ForeignKey('device.device_uuid',ondelete='CASCADE' ), nullable=False, index=True) #Foreign Key relationship with the field device_uuid from the Device table (CASCADE' behavior for deletion, meaning when a device is deleted, its components will also be dele) - name = Column(String, nullable=False) #String field that stores the name of the component - type = Column(String, nullable=False) #String field that stores the type of the component - attributes = Column(String, nullable=False) #String field that stores the attributes of the component - parent = Column(String, nullable=False) #String field that stores the parent of 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 - - device = relationship('DeviceModel', back_populates='components')# lazy='selectin'#Defines a relationship between ComponentModel and DeviceModel - #Represents a 1:n relationship where 1 device -> N components // The relationship is defined by the FK device_uuid +class ComponentModel(_Base): + __tablename__ = 'device_component' + + component_uuid = Column(UUID(as_uuid=False), primary_key=True) + device_uuid = Column(ForeignKey('device.device_uuid',ondelete='CASCADE' ), nullable=False, index=True) + name = Column(String, nullable=False) + type = Column(String, nullable=False) + attributes = Column(String, nullable=False) + parent = Column(String, nullable=False) + created_at = Column(DateTime, nullable=False) + updated_at = Column(DateTime, nullable=False) + + device = relationship('DeviceModel', back_populates='components') def dump_id(self) -> Dict: return{ 'device_id' : self.device.dump_id(), diff --git a/src/device/service/drivers/openconfig/templates/Inventory.py b/src/device/service/drivers/openconfig/templates/Inventory.py index b7e7c7c120e3d6cb1210ab4672097a8122cdafe5..0d98d72882695038d9d3ddbc2c9b86564d21d5b0 100644 --- a/src/device/service/drivers/openconfig/templates/Inventory.py +++ b/src/device/service/drivers/openconfig/templates/Inventory.py @@ -12,6 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging, lxml.etree as ET +from typing import Any, Dict, List, Tuple +from .Namespace import NAMESPACES +from .Tools import add_value_from_tag + +LOGGER = logging.getLogger(__name__) + +XPATH_PORTS = "//ocp:components/ocp:component" """ #Method Name: parse @@ -22,39 +30,28 @@ # Functionality: -The parse function of the inventerio class has the functionality to parse -an XML document represented by the xml_data parameter and extract specific -information from the XML elements, namely the relevant characteristics of the -components. + The parse function of the inventerio class has the functionality to parse + an XML document represented by the xml_data parameter and extract specific + information from the XML elements, namely the relevant characteristics of the + components. -To generate the template the following steps are performed: + To generate the template the following steps are performed: -1) An empty list called response is created to store the results of the analysis. + 1) An empty list called response is created to store the results of the analysis. -2) Iterate over the XML elements that match the pattern specified by the XPATH_PORTS -expression. These elements represent components in the XML document. + 2) Iterate over the XML elements that match the pattern specified by the XPATH_PORTS + expression. These elements represent components in the XML document. -3) For each component element: -A dictionary called inventory is initialized that will store the information extracted -from the component.The values of the relevant XML elements are extracted and added to -the dictionary. + 3) For each component element: + A dictionary called inventory is initialized that will store the information extracted + from the component.The values of the relevant XML elements are extracted and added to + the dictionary. #Return: -List[Tuple[str, Dict[str, Any]]] The response list containing the tuples (path, dictionary) -with the information extracted from the XML document components is returned. - + List[Tuple[str, Dict[str, Any]]] The response list containing the tuples (path, dictionary) + with the information extracted from the XML document components is returned. """ - -import logging, lxml.etree as ET -from typing import Any, Dict, List, Tuple -from .Namespace import NAMESPACES -from .Tools import add_value_from_tag - -LOGGER = logging.getLogger(__name__) - -XPATH_PORTS = "//ocp:components/ocp:component" - def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: response = [] LOGGER.debug("InventoryPrueba") @@ -126,7 +123,6 @@ def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: add_value_from_tag(inventory['attributes'], 'mfg-date', component_mfg_date) #Transceiver Information - component_serial_t = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:serial-no', namespaces=NAMESPACES) if not component_serial_t is None: add_value_from_tag(inventory['attributes'], 'serial-num', component_serial_t) @@ -152,8 +148,6 @@ def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: component_reference.extend([parent_types[inventory['parent-component-references']]]) - - response.append(('/inventory/{:s}'.format(inventory['name']), inventory)) for tupla in response: @@ -162,4 +156,4 @@ def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: inventory['component-reference'] = component_reference - return response \ No newline at end of file + return response