diff --git a/src/device/service/driver_api/_Driver.py b/src/device/service/driver_api/_Driver.py index cc9f7a2c63f2f841b864cbe4fa596464a6783cec..8f9c61759611a1ee39242428f8eb5ae1f7e5bab8 100644 --- a/src/device/service/driver_api/_Driver.py +++ b/src/device/service/driver_api/_Driver.py @@ -23,6 +23,7 @@ RESOURCE_INTERFACES = '__interfaces__' RESOURCE_NETWORK_INSTANCES = '__network_instances__' RESOURCE_ROUTING_POLICIES = '__routing_policies__' RESOURCE_ACL = '__acl__' +RESOURCE_INVENTORY = '__inventory__' class _Driver: diff --git a/src/device/service/drivers/openconfig/templates/Inventory.py b/src/device/service/drivers/openconfig/templates/Inventory.py new file mode 100644 index 0000000000000000000000000000000000000000..78744a76dc08ff667500167a848472ea0b408188 --- /dev/null +++ b/src/device/service/drivers/openconfig/templates/Inventory.py @@ -0,0 +1,86 @@ +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# 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" + +def parse(xml_data : ET.Element) -> List[Tuple[str, Dict[str, Any]]]: + response = [] + LOGGER.info("InventoryPrueba") + + for xml_component in xml_data.xpath(XPATH_PORTS, namespaces=NAMESPACES): + #LOGGER.info('xml_component = {:s}'.format(str(ET.tostring(xml_component)))) + inventory = {} + inventory['name'] = '' + inventory['type'] = '' + inventory['datos'] = {} + component_name = xml_component.find('ocp:name', namespaces=NAMESPACES) + if component_name is None or component_name.text is None: continue + add_value_from_tag(inventory, 'name', component_name) + + component_type = xml_component.find('ocp:state/ocp:type', namespaces=NAMESPACES) + component_type.text = component_type.text.replace('oc-platform-types:','') + if component_type is None: continue + add_value_from_tag(inventory, 'type', component_type) + + component_present = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:present', namespaces=NAMESPACES) + if not component_present is None: + add_value_from_tag(inventory['datos'], 'present', component_present) + if 'present' in inventory['datos'] and inventory['datos']['present'] == 'NOT_PRESENT': continue + + component_vendor = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:vendor', namespaces=NAMESPACES) + if not component_vendor is None: + add_value_from_tag(inventory['datos'], 'vendor', component_vendor) + component_connector = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:connector-type', namespaces=NAMESPACES) + if not component_connector is None: + component_connector.text = component_connector.text.replace('oc-opt-types:','') + add_value_from_tag(inventory['datos'], 'connector-type', component_connector) + component_serial = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:serial-no', namespaces=NAMESPACES) + if not component_serial is None: + add_value_from_tag(inventory['datos'], 'serial-no', component_serial) + component_form = xml_component.find('ocptr:transceiver/ocptr:state/ocptr:form-factor', namespaces=NAMESPACES) + if not component_form is None: + component_form.text = component_form.text.replace('oc-platform-types:','') + add_value_from_tag(inventory['datos'], 'form-factor', component_form) + component_parent = xml_component.find('ocp:state/ocp:parent', namespaces=NAMESPACES) + + component_HW = xml_component.find('ocp:state/ocp:hardware-version', namespaces=NAMESPACES) + if not component_HW is None: + add_value_from_tag(inventory['datos'], 'hardware-version', component_HW) + component_SW = xml_component.find('ocp:state/ocp:software-version', namespaces=NAMESPACES) + if not component_SW is None: + add_value_from_tag(inventory['datos'], 'software-version', component_SW) + component_serial = xml_component.find('ocp:state/ocp:serial-no', namespaces=NAMESPACES) + if not component_serial is None: + add_value_from_tag(inventory['datos'], 'serial-no', component_serial) + component_oper_status = xml_component.find('ocp:state/ocp:oper-status', namespaces=NAMESPACES) + if not component_oper_status is None and inventory['type'] != 'CHASSIS' and inventory['type'] != 'CPU': + component_oper_status.text = component_oper_status.text.replace('oc-platform-types:','') + add_value_from_tag(inventory['datos'], 'oper-status', component_oper_status) + component_parent = xml_component.find('ocp:state/ocp:parent', namespaces=NAMESPACES) + if not component_parent is None and inventory['type'] != 'CHASSIS': + add_value_from_tag(inventory['datos'], 'parent', component_parent) + + if 'type' not in inventory: inventory['type'] = '-' + + #LOGGER.info('Inventoty = {:s}'.format(str(inventory))) + if len(inventory) == 0: continue + response.append(('/inventory[{:s}]'.format(inventory['name']), inventory)) + return response diff --git a/src/device/service/drivers/openconfig/templates/Namespace.py b/src/device/service/drivers/openconfig/templates/Namespace.py index eede865502b043b7936d763c980be80a7ea817f8..b70d5c32775075ae299bf18f8ecfc530544efe7b 100644 --- a/src/device/service/drivers/openconfig/templates/Namespace.py +++ b/src/device/service/drivers/openconfig/templates/Namespace.py @@ -28,6 +28,7 @@ NAMESPACE_POLICY_TYPES = 'http://openconfig.net/yang/policy-types' NAMESPACE_POLICY_TYPES_2 = 'http://openconfig.net/yang/policy_types' NAMESPACE_ROUTING_POLICY = 'http://openconfig.net/yang/routing-policy' NAMESPACE_VLAN = 'http://openconfig.net/yang/vlan' +NAMESPACE_PLATFORM_TRANSCEIVER = 'http://openconfig.net/yang/platform/transceiver' NAMESPACES = { 'nc' : NAMESPACE_NETCONF, @@ -44,4 +45,5 @@ NAMESPACES = { 'ocpt2': NAMESPACE_POLICY_TYPES_2, 'ocrp' : NAMESPACE_ROUTING_POLICY, 'ocv' : NAMESPACE_VLAN, + 'ocptr' : NAMESPACE_PLATFORM_TRANSCEIVER, } diff --git a/src/device/service/drivers/openconfig/templates/__init__.py b/src/device/service/drivers/openconfig/templates/__init__.py index c415bfd25725ca950c018e9f0eedfcde6e0df379..a2196b973bea4df67d019f07e9316860d773f8bf 100644 --- a/src/device/service/drivers/openconfig/templates/__init__.py +++ b/src/device/service/drivers/openconfig/templates/__init__.py @@ -16,14 +16,16 @@ import json, logging, lxml.etree as ET, re from typing import Any, Dict, Optional from jinja2 import Environment, PackageLoader, select_autoescape from device.service.driver_api._Driver import ( - RESOURCE_ENDPOINTS, RESOURCE_INTERFACES, RESOURCE_NETWORK_INSTANCES, RESOURCE_ROUTING_POLICIES, RESOURCE_ACL) + RESOURCE_ENDPOINTS, RESOURCE_INTERFACES, RESOURCE_NETWORK_INSTANCES, RESOURCE_ROUTING_POLICIES, RESOURCE_ACL, RESOURCE_INVENTORY) from .EndPoints import parse as parse_endpoints from .Interfaces import parse as parse_interfaces, parse_counters from .NetworkInstances import parse as parse_network_instances from .RoutingPolicy import parse as parse_routing_policy from .Acl import parse as parse_acl +from .Inventory import parse as parse_inventory ALL_RESOURCE_KEYS = [ + RESOURCE_INVENTORY, RESOURCE_ENDPOINTS, RESOURCE_INTERFACES, RESOURCE_ROUTING_POLICIES, # routing policies should come before network instances @@ -32,6 +34,7 @@ ALL_RESOURCE_KEYS = [ ] RESOURCE_KEY_MAPPINGS = { + RESOURCE_INVENTORY : 'inventory', RESOURCE_ENDPOINTS : 'component', RESOURCE_INTERFACES : 'interface', RESOURCE_NETWORK_INSTANCES: 'network_instance', @@ -40,6 +43,7 @@ RESOURCE_KEY_MAPPINGS = { } RESOURCE_PARSERS = { + 'inventory' : parse_inventory, 'component' : parse_endpoints, 'interface' : parse_interfaces, 'network_instance': parse_network_instances, diff --git a/src/device/service/drivers/openconfig/templates/inventory/get.xml b/src/device/service/drivers/openconfig/templates/inventory/get.xml new file mode 100644 index 0000000000000000000000000000000000000000..aa25ed1e3b11e0c324b361eb52d064dac87a64c5 --- /dev/null +++ b/src/device/service/drivers/openconfig/templates/inventory/get.xml @@ -0,0 +1,3 @@ +<components xmlns="http://openconfig.net/yang/platform"> + <component/> +</components>