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

Code cleanup

parent f991f748
No related branches found
No related tags found
2 merge requests!235Release TeraFlowSDN 3.0,!150Resolve "(TID) Network device inventory management"
...@@ -187,8 +187,6 @@ message Component { //Defined previously to this section ...@@ -187,8 +187,6 @@ message Component { //Defined previously to this section
string parent = 5; string parent = 5;
} }
// -----------------------------------------------------
message DeviceConfig { message DeviceConfig {
repeated ConfigRule config_rules = 1; repeated ConfigRule config_rules = 1;
} }
......
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) # Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
...@@ -16,7 +14,6 @@ ...@@ -16,7 +14,6 @@
import datetime, json, logging import datetime, json, logging
from sqlalchemy import delete from sqlalchemy import delete
#from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from typing import Dict, List, Optional, Set from typing import Dict, List, Optional, Set
...@@ -63,7 +60,7 @@ def compose_components_data( ...@@ -63,7 +60,7 @@ def compose_components_data(
'device_uuid' : device_uuid, 'device_uuid' : device_uuid,
'name' : name, 'name' : name,
'type' : type_, 'type' : type_,
'attributes' : json.dumps(attributes), #Store the remaining fields in 'attributes' 'attributes' : json.dumps(attributes),
'parent' : parent, 'parent' : parent,
'created_at' : now, 'created_at' : now,
'updated_at' : now, 'updated_at' : now,
......
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import datetime, logging import datetime, logging, json
import json
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.engine import Engine from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, selectinload, sessionmaker from sqlalchemy.orm import Session, selectinload, sessionmaker
...@@ -55,7 +54,6 @@ def device_list_objs(db_engine : Engine) -> DeviceList: ...@@ -55,7 +54,6 @@ def device_list_objs(db_engine : Engine) -> DeviceList:
.options(selectinload(DeviceModel.config_rules))\ .options(selectinload(DeviceModel.config_rules))\
.options(selectinload(DeviceModel.components))\ .options(selectinload(DeviceModel.components))\
.all() .all()
#.options(selectinload(DeviceModel.components))\
return [obj.dump() for obj in obj_list] return [obj.dump() for obj in obj_list]
devices = run_transaction(sessionmaker(bind=db_engine), callback) devices = run_transaction(sessionmaker(bind=db_engine), callback)
return DeviceList(devices=devices) return DeviceList(devices=devices)
...@@ -67,7 +65,6 @@ def device_get(db_engine : Engine, request : DeviceId) -> Device: ...@@ -67,7 +65,6 @@ def device_get(db_engine : Engine, request : DeviceId) -> Device:
.options(selectinload(DeviceModel.endpoints))\ .options(selectinload(DeviceModel.endpoints))\
.options(selectinload(DeviceModel.config_rules))\ .options(selectinload(DeviceModel.config_rules))\
.filter_by(device_uuid=device_uuid).one_or_none() .filter_by(device_uuid=device_uuid).one_or_none()
#.options(selectinload(DeviceModel.components))\
return None if obj is None else obj.dump() return None if obj is None else obj.dump()
obj = run_transaction(sessionmaker(bind=db_engine), callback) obj = run_transaction(sessionmaker(bind=db_engine), callback)
if obj is None: if obj is None:
......
...@@ -19,20 +19,19 @@ from sqlalchemy.orm import relationship ...@@ -19,20 +19,19 @@ from sqlalchemy.orm import relationship
from typing import Dict from typing import Dict
from ._Base import _Base from ._Base import _Base
class ComponentModel(_Base): #Inherited functionality from the base class _Base class ComponentModel(_Base):
__tablename__ = 'device_component' #Name of the table in the DB associtaed with this model __tablename__ = 'device_component'
component_uuid = Column(UUID(as_uuid=False), primary_key=True) #Unique identifier that serves as a primary key for this table component_uuid = Column(UUID(as_uuid=False), primary_key=True)
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) device_uuid = Column(ForeignKey('device.device_uuid',ondelete='CASCADE' ), nullable=False, index=True)
name = Column(String, nullable=False) #String field that stores the name of the component name = Column(String, nullable=False)
type = Column(String, nullable=False) #String field that stores the type of the component type = Column(String, nullable=False)
attributes = Column(String, nullable=False) #String field that stores the attributes of the component attributes = Column(String, nullable=False)
parent = Column(String, nullable=False) #String field that stores the parent of the component parent = Column(String, nullable=False)
created_at = Column(DateTime, nullable=False) #Stores the creaton timestamp for the component created_at = Column(DateTime, nullable=False)
updated_at = Column(DateTime, nullable=False) #Stores the last upadted timestamp for the component updated_at = Column(DateTime, nullable=False)
device = relationship('DeviceModel', back_populates='components')# lazy='selectin'#Defines a relationship between ComponentModel and DeviceModel device = relationship('DeviceModel', back_populates='components')
#Represents a 1:n relationship where 1 device -> N components // The relationship is defined by the FK device_uuid
def dump_id(self) -> Dict: def dump_id(self) -> Dict:
return{ return{
'device_id' : self.device.dump_id(), 'device_id' : self.device.dump_id(),
......
...@@ -12,6 +12,14 @@ ...@@ -12,6 +12,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # 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 #Method Name: parse
...@@ -22,39 +30,28 @@ ...@@ -22,39 +30,28 @@
# Functionality: # Functionality:
The parse function of the inventerio class has the functionality to parse The parse function of the inventerio class has the functionality to parse
an XML document represented by the xml_data parameter and extract specific an XML document represented by the xml_data parameter and extract specific
information from the XML elements, namely the relevant characteristics of the information from the XML elements, namely the relevant characteristics of the
components. 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 2) Iterate over the XML elements that match the pattern specified by the XPATH_PORTS
expression. These elements represent components in the XML document. expression. These elements represent components in the XML document.
3) For each component element: 3) For each component element:
A dictionary called inventory is initialized that will store the information extracted 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 from the component.The values of the relevant XML elements are extracted and added to
the dictionary. the dictionary.
#Return: #Return:
List[Tuple[str, Dict[str, Any]]] The response list containing the tuples (path, dictionary) 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. 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]]]: def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]:
response = [] response = []
LOGGER.debug("InventoryPrueba") LOGGER.debug("InventoryPrueba")
...@@ -126,7 +123,6 @@ def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: ...@@ -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) add_value_from_tag(inventory['attributes'], 'mfg-date', component_mfg_date)
#Transceiver Information #Transceiver Information
component_serial_t = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:serial-no', namespaces=NAMESPACES) component_serial_t = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:serial-no', namespaces=NAMESPACES)
if not component_serial_t is None: if not component_serial_t is None:
add_value_from_tag(inventory['attributes'], 'serial-num', component_serial_t) 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]]]: ...@@ -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']]]) component_reference.extend([parent_types[inventory['parent-component-references']]])
response.append(('/inventory/{:s}'.format(inventory['name']), inventory)) response.append(('/inventory/{:s}'.format(inventory['name']), inventory))
for tupla in response: for tupla in response:
...@@ -162,4 +156,4 @@ def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: ...@@ -162,4 +156,4 @@ def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]:
inventory['component-reference'] = component_reference inventory['component-reference'] = component_reference
return response return response
\ No newline at end of file
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