Commit 69323cf5 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Pre-merge code cleanup

parent fbe65582
Loading
Loading
Loading
Loading
+99 −75
Original line number Diff line number Diff line
@@ -33,20 +33,25 @@
#    # do test ...
#    descriptor_loader.unload()

import concurrent.futures, json, logging, operator
import concurrent.futures, copy, json, logging, operator
from typing import Any, Dict, List, Optional, Tuple, Union
from common.proto.context_pb2 import (
    Connection, Context, ContextId, Device, DeviceId, Empty, Link, LinkId, Service, ServiceId, Slice, SliceId,
    Topology, TopologyId)
    Connection, Context, ContextId, Device, DeviceId, Empty,
    Link, LinkId, Service, ServiceId, Slice, SliceId,
    Topology, TopologyId
)
from common.tools.object_factory.Context import json_context_id
from context.client.ContextClient import ContextClient
from device.client.DeviceClient import DeviceClient
from service.client.ServiceClient import ServiceClient
from slice.client.SliceClient import SliceClient
from .Tools import (
    format_device_custom_config_rules, format_service_custom_config_rules, format_slice_custom_config_rules,
    get_descriptors_add_contexts, get_descriptors_add_services, get_descriptors_add_slices,
    get_descriptors_add_topologies, split_controllers_and_network_devices, split_devices_by_rules)
    format_device_custom_config_rules, format_service_custom_config_rules,
    format_slice_custom_config_rules, get_descriptors_add_contexts,
    get_descriptors_add_services, get_descriptors_add_slices,
    get_descriptors_add_topologies, split_controllers_and_network_devices,
    split_devices_by_rules
)

LOGGER = logging.getLogger(__name__)
LOGGERS = {
@@ -78,6 +83,30 @@ TypeResults = List[Tuple[str, str, int, List[str]]] # entity_name, action, num_o
TypeNotification = Tuple[str, str] # message, level
TypeNotificationList = List[TypeNotification]

SLICE_TEMPLATE = {
    "slice_id": {
        "context_id": {"context_uuid": {"uuid": "admin"}},
        "slice_uuid": {"uuid": None}
    },
    "name": {},
    "slice_config": {"config_rules": [
        {"action": 1, "custom": {"resource_key": "/settings", "resource_value": {
            "address_families": ["IPV4"], "bgp_as": 65000,
            "bgp_route_target": "65000:333", "mtu": 1512
        }}}
    ]},
    "slice_constraints": [
        {"sla_capacity": {"capacity_gbps": 20.0}},
        {"sla_availability": {"availability": 20.0, "num_disjoint_paths": 1, "all_active": True}},
        {"sla_isolation": {"isolation_level": [0]}}
    ],
    "slice_endpoint_ids": [

    ],
    "slice_status": {"slice_status": 1}
}


class DescriptorLoader:
    def __init__(
        self, descriptors : Optional[Union[str, Dict]] = None, descriptors_file : Optional[str] = None,
@@ -106,43 +135,20 @@ class DescriptorLoader:
        self.__links       = self.__descriptors.get('links'      , [])
        self.__services    = self.__descriptors.get('services'   , [])
        self.__slices      = self.__descriptors.get('slices'     , [])
        self.__slices = self.__descriptors.get('ietf-network-slice-service:network-slice-services', {})
        self.__ietf_slices = self.__descriptors.get('ietf-network-slice-service:network-slice-services', {})
        self.__connections = self.__descriptors.get('connections', [])

        if self.__slices: 
            json_out = {"slices": [
                {
                    "slice_id": {
                        "context_id": {"context_uuid": {"uuid": "admin"}},
                        "slice_uuid": {}
                    },
                    "name": {},
                    "slice_config": {"config_rules": [
                        {"action": 1, "custom": {"resource_key": "/settings", "resource_value": {
                        "address_families": ["IPV4"], "bgp_as": 65000, "bgp_route_target": "65000:333", "mtu": 1512
                        }}}
                    ]},
                    "slice_constraints": [
                        {"sla_capacity": {"capacity_gbps": 20.0}},
                        {"sla_availability": {"availability": 20.0, "num_disjoint_paths": 1, "all_active": True}},
                        {"sla_isolation": {"isolation_level": [0]}}
                    ],
                    "slice_endpoint_ids": [
            
                    ],
                    "slice_status": {"slice_status": 1}
                }
            ]}

            for slice_service in self.__slices["slice-service"]:
                for slice in json_out["slices"]:
                    slice["slice_id"]["slice_uuid"] = { "uuid": slice_service["id"]}
                    slice["name"] = slice_service["description"]
                    sdp = slice_service["sdps"]["sdp"]
                    for elemento in sdp:
                        attcircuits = elemento["attachment-circuits"]["attachment-circuit"]
                        for attcircuit in attcircuits:
                            resource_key = "/device[{sdp_id}]/endpoint[{endpoint_id}]/settings".format(sdp_id = elemento["id"], endpoint_id = attcircuit["ac-tp-id"])
        if len(self.__ietf_slices) > 0:
            for slice_service in self.__ietf_slices["slice-service"]:
                tfs_slice = copy.deepcopy(SLICE_TEMPLATE)
                tfs_slice["slice_id"]["slice_uuid"]["uuid"] = slice_service["id"]
                tfs_slice["name"] = slice_service["description"]
                for sdp in slice_service["sdps"]["sdp"]:
                    sdp_id = sdp["id"]
                    for attcircuit in sdp["attachment-circuits"]["attachment-circuit"]:
                        att_cir_tp_id = attcircuit["ac-tp-id"]
                        RESOURCE_KEY = "/device[{:s}]/endpoint[{:s}]/settings"
                        resource_key = RESOURCE_KEY.format(str(sdp_id), str(att_cir_tp_id))

                        for tag in attcircuit['ac-tags']['ac-tag']:
                            if tag.get('tag-type') == 'ietf-nss:vlan-id':
@@ -150,20 +156,31 @@ class DescriptorLoader:
                            else:
                                vlan_id = 0

                            slice["slice_config"]["config_rules"].append( {"action": 1, "custom": {"resource_key": resource_key, "resource_value": {
                                "router_id": elemento.get("node-id",[]), "sub_interface_index": 0, "vlan_id": vlan_id
                            }}})
                            slice["slice_endpoint_ids"].append({
                                "device_id": {"device_uuid": {"uuid": elemento["id"]}},
                                "endpoint_uuid": {"uuid": attcircuit["ac-tp-id"]},
                        tfs_slice["slice_config"]["config_rules"].append({
                            "action": 1, "custom": {
                                "resource_key": resource_key, "resource_value": {
                                    "router_id": sdp.get("node-id",[]),
                                    "sub_interface_index": 0,
                                    "vlan_id": vlan_id
                                }
                            }
                        })
                        tfs_slice["slice_endpoint_ids"].append({
                            "device_id": {"device_uuid": {"uuid": sdp_id}},
                            "endpoint_uuid": {"uuid": att_cir_tp_id},
                            "topology_id": {"context_id": {"context_uuid": {"uuid": "admin"}}, 
                            "topology_uuid": {"uuid": "admin"}}
                        })
                            slice["slice_constraints"].append({"endpoint_location": {
                                "endpoint_id": {"device_id": {"device_uuid": {"uuid": elemento["id"]}}, "endpoint_uuid": {"uuid": attcircuit["ac-tp-id"]}},
                                "location": {"region": "4"}
                        }})
            self.__slices      = json_out.get('slices'     , [])
                        #tfs_slice["slice_constraints"].append({
                        #    "endpoint_location": {
                        #        "endpoint_id": {
                        #            "device_id": {"device_uuid": {"uuid": sdp["id"]}},
                        #            "endpoint_uuid": {"uuid": attcircuit["ac-tp-id"]}
                        #        },
                        #        "location": {"region": "4"}
                        #    }
                        #})
                self.__slices.append(tfs_slice)

        self.__contexts_add   = None
        self.__topologies_add = None
@@ -293,6 +310,8 @@ class DescriptorLoader:
        self.__ctx_cli.connect()
        self._process_descr('context',    'add',    self.__ctx_cli.SetContext,    Context,    self.__contexts_add  )
        self._process_descr('topology',   'add',    self.__ctx_cli.SetTopology,   Topology,   self.__topologies_add)
        self._process_descr('controller', 'add',    self.__ctx_cli.SetDevice,     Device,     controllers          )
        self._process_descr('device',     'add',    self.__ctx_cli.SetDevice,     Device,     network_devices      )
        self._process_descr('device',     'add',    self.__ctx_cli.SetDevice,     Device,     self.__devices       )
        self._process_descr('link',       'add',    self.__ctx_cli.SetLink,       Link,       self.__links         )
        self._process_descr('service',    'add',    self.__ctx_cli.SetService,    Service,    self.__services      )
@@ -321,6 +340,8 @@ class DescriptorLoader:
        self.__services_add = get_descriptors_add_services(self.__services)
        self.__slices_add = get_descriptors_add_slices(self.__slices)

        controllers_add, network_devices_add = split_controllers_and_network_devices(self.__devices_add)

        self.__ctx_cli.connect()
        self.__dev_cli.connect()
        self.__svc_cli.connect()
@@ -328,7 +349,8 @@ class DescriptorLoader:

        self._process_descr('context',    'add',    self.__ctx_cli.SetContext,      Context,  self.__contexts_add  )
        self._process_descr('topology',   'add',    self.__ctx_cli.SetTopology,     Topology, self.__topologies_add)
        self._process_descr('device',   'add',    self.__dev_cli.AddDevice,       Device,   self.__devices_add   )
        self._process_descr('controller', 'add',    self.__dev_cli.AddDevice,       Device,   controllers_add      )
        self._process_descr('device',     'add',    self.__dev_cli.AddDevice,       Device,   network_devices_add  )
        self._process_descr('device',     'config', self.__dev_cli.ConfigureDevice, Device,   self.__devices_config)
        self._process_descr('link',       'add',    self.__ctx_cli.SetLink,         Link,     self.__links         )
        self._process_descr('service',    'add',    self.__svc_cli.CreateService,   Service,  self.__services_add  )
@@ -336,9 +358,11 @@ class DescriptorLoader:
        self._process_descr('slice',      'add',    self.__slc_cli.CreateSlice,     Slice,    self.__slices_add    )
        self._process_descr('slice',      'update', self.__slc_cli.UpdateSlice,     Slice,    self.__slices        )

        # Update context and topology is useless:
        # - devices and links are assigned to topologies automatically by Context component
        # - topologies, services, and slices are assigned to contexts automatically by Context component
        # By default the Context component automatically assigns devices and links to topologies based on their
        # endpoints, and assigns topologies, services, and slices to contexts based on their identifiers.

        # The following statement is useless; up to now, any use case requires assigning a topology, service, or
        # slice to a different context.
        #self._process_descr('context',  'update', self.__ctx_cli.SetContext,      Context,  self.__contexts      )

        # In some cases, it might be needed to assign devices and links to multiple topologies; the
+2 −0
Original line number Diff line number Diff line
@@ -59,6 +59,8 @@ def format_custom_config_rules(config_rules : List[Dict]) -> List[Dict]:
        if isinstance(custom_resource_value, (dict, list)):
            custom_resource_value = json.dumps(custom_resource_value, sort_keys=True, indent=0)
            config_rule['custom']['resource_value'] = custom_resource_value
        elif not isinstance(custom_resource_value, str):
            config_rule['custom']['resource_value'] = str(custom_resource_value)
    return config_rules

def format_device_custom_config_rules(device : Dict) -> Dict: