Commit 32469dcd authored by Mohammad Ismaeel's avatar Mohammad Ismaeel
Browse files

Bugs fixed in Tapi model

parent 6230f618
Loading
Loading
Loading
Loading
+34 −37
Original line number Diff line number Diff line
@@ -24,31 +24,28 @@ from common.proto.context_pb2 import (
from common.message_broker.MessageBroker import MessageBroker
from common.method_wrappers.ServiceExceptions import NotFoundException
from common.tools.object_factory.Context import json_context_id
from common.tools.object_factory.Topology import json_topology_id
from .models.enums.KpiSampleType import grpc_to_enum__kpi_sample_type

from .models.DeviceModel import DeviceModel
from common.tools.grpc.Tools import grpc_message_to_json_string

from .models.OpticalLinkModel import OpticalLinkModel
from .models.TapiModel import (ChildTopologyDeviceModel, ChildTopologyOpticalLinkModel
                               , ChildTopologyModel , ChildTopologyEndponitModel , ChildOpticalLinkEndponitModel)
from .uuids.Context import context_get_uuid
from .uuids.Device import device_get_uuid
from .uuids.Link import link_get_uuid
from .uuids.Topology import topology_get_uuid
from .Events import notify_event_context, notify_event_topology
from .uuids.EndPoint import endpoint_get_uuid

from .uuids.Tapi import topology_get_uuid



LOGGER = logging.getLogger(__name__)


def child_topology_list_objs(db_engine : Engine, request : ContextId) -> ChildTopologyList:
    context_uuid = context_get_uuid(request, allow_random=False)

    def callback(session : Session) -> List[Dict]:
        obj_list : List[ChildTopologyModel] = session.query(ChildTopologyModel)\
            .options(selectinload(ChildTopologyModel.child_topology_devices))\
            .options(selectinload(ChildTopologyModel.child_topology_optical_links))\
            .filter_by(context_uuid=context_uuid).all()
            .options(selectinload(ChildTopologyModel.child_topology_optical_links)).all()
          
        return [obj.dump() for obj in obj_list]
    topologies = run_transaction(sessionmaker(bind=db_engine), callback)
    return ChildTopologyList(topology_list=topologies)
@@ -63,20 +60,19 @@ def child_topology_get(db_engine : Engine, request : ChildTopologyId) -> ChildTo
        return None if obj is None else obj.dump()
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    if obj is None:
        context_uuid = context_get_uuid(request.context_id, allow_random=False)
        raw_topology_uuid = '{:s}/{:s}'.format(request.context_id.context_uuid.uuid
                                               , request.topology_uuid.uuid)
        
        raw_topology_uuid = '{:s}'.format(
                                                request.topology_uuid.uuid)
        raise NotFoundException('Topology', raw_topology_uuid, extra_details=[
            'context_uuid generated was: {:s}'.format(context_uuid),
            'topology_uuid generated was: {:s}'.format(topology_uuid),
        ])
    return obj.dump()


def child_topology_set(db_engine : Engine, messagebroker : MessageBroker, request : ChildTopology) -> ChildTopologyId:
    topology_name = request.topology.name
    if len(topology_name) == 0: topology_name = request.topology.topology_id.topology_uuid.uuid
    context_uuid,topology_uuid = topology_get_uuid(request.topology.topology_id, topology_name=topology_name, allow_random=True)
    topology_name = ''
    if len(topology_name) == 0: topology_name = request.topology_id.topology_uuid.uuid
    context_uuid,topology_uuid = topology_get_uuid(request.topology_id, topology_name=topology_name, allow_random=True)

    # By default, ignore request.device_ids and request.link_ids. They are used for retrieving
    # devices and links added into the topology. Explicit addition into the topology is done
@@ -90,39 +86,39 @@ def child_topology_set(db_engine : Engine, messagebroker : MessageBroker, reques
    device_uuids : Set[str] = set()
    related_topology_endpoints : List[Dict] = list()

    for device in request.toplogy.devices:
    for device in request.devices:
        device_uuid = device.device_id.device_uuid.uuid
        related_devices.append({'topology_uuid': topology_uuid, 'device_uuid': device_uuid,'device_name': device.name})
        related_devices.append({'child_topology_uuid': topology_uuid, 'device_uuid': device_uuid,'device_name': device.device_name})
   
      
        for i, endpoint in enumerate(device.endpoints):
           
        for i, endpoint in enumerate(device.device_endpoints):
            endpoint_device_uuid = endpoint.endpoint_id.device_id.device_uuid.uuid
            raw_endpoint_uuid = endpoint.endpoint_id.endpoint_uuid.uuid
            raw_endpoint_name = endpoint.name
            raw_endpoint_name = endpoint.endpoint_name
            related_topology_endpoints.append({
                'endpoint_uuid'    : raw_endpoint_uuid,
                'device_uuid'      : endpoint_device_uuid,
                'topology_uuid'    : topology_uuid,
                'device_uuid'      : device_uuid,
                'child_topology_uuid'    : topology_uuid,
                'endpoint_name'             : raw_endpoint_name,
                
            })    
            
    related_endpoint_opticallinks : List[Dict] = list()
    related_topology_opticallinks : List[Dict] = list()
    for opticallink in request.topology.optical_links:
    for opticallink in request.optical_links:
        link_uuid = opticallink.link_id.link_uuid.uuid
        link_name = opticallink.name
        related_topology_opticallinks.append({'topology_uuid': topology_uuid, 'optical_link_uuid': 
        link_name = opticallink.optical_link_name
        related_topology_opticallinks.append({'child_topology_uuid': topology_uuid, 'optical_link_uuid': 
            link_uuid,'optical_link_name': link_name})
        for endpoint in opticallink.link_endpoint_ids:
        # for endpoint in opticallink.link_endpoint_ids:
            
             related_endpoint_opticallinks.append({'optical_link_uuid': link_uuid,
                                                   'endpoint_uuid': opticallink.endpoint_uuid})
        #      related_endpoint_opticallinks.append({'optical_link_uuid': link_uuid,
        #                                            'endpoint_uuid': opticallink.endpoint_uuid})
  
    now = datetime.datetime.now(datetime.timezone.utc)

    topology_data = [{
        'context_uuid' : context_uuid,
       
        'child_topology_uuid': topology_uuid,
        'topology_name': topology_name,
        'created_at'   : now,
@@ -149,7 +145,7 @@ def child_topology_set(db_engine : Engine, messagebroker : MessageBroker, reques
        if len(related_devices) > 0:
            stmt = insert(ChildTopologyDeviceModel).values(related_devices)
            stmt = stmt.on_conflict_do_nothing(
                index_elements=[ChildTopologyDeviceModel.topology_uuid, ChildTopologyDeviceModel.device_uuid]
                index_elements=[ChildTopologyDeviceModel.device_uuid]
            )
            topology_device_inserts = session.execute(stmt)
            updated_topology_device = int(topology_device_inserts.rowcount) > 0
@@ -157,7 +153,7 @@ def child_topology_set(db_engine : Engine, messagebroker : MessageBroker, reques
        if len(related_topology_opticallinks) > 0:
            stmt = insert(ChildTopologyOpticalLinkModel).values(related_topology_opticallinks)
            stmt = stmt.on_conflict_do_nothing(
                index_elements=[ChildTopologyOpticalLinkModel.topology_uuid, ChildTopologyOpticalLinkModel.optical_link_uuid]
                index_elements=[ ChildTopologyOpticalLinkModel.optical_link_uuid]
            )
            topology_links_inserts = session.execute(stmt)
            updated_topology_links = int(topology_links_inserts.rowcount) > 0
@@ -165,7 +161,7 @@ def child_topology_set(db_engine : Engine, messagebroker : MessageBroker, reques
        if len(related_topology_endpoints) > 0:
            stmt = insert(ChildTopologyEndponitModel).values(related_topology_endpoints)
            stmt = stmt.on_conflict_do_nothing(
                index_elements=[ChildTopologyEndponitModel.topology_uuid, ChildTopologyEndponitModel.endpoint_uuid]
                index_elements=[ ChildTopologyEndponitModel.endpoint_uuid]
            )
            topology_endpoints_inserts = session.execute(stmt)
            updated_topology_endpoints = int(topology_endpoints_inserts.rowcount) > 0    
@@ -183,8 +179,9 @@ def child_topology_set(db_engine : Engine, messagebroker : MessageBroker, reques
        return updated or updated_topology_device 

    updated = run_transaction(sessionmaker(bind=db_engine), callback)
    context_id = json_context_id(context_uuid)
    topology_id = {'topology_uuid':{'uuid': topology_uuid}}
    
    topology_id = ChildTopologyId()
    topology_id.topology_uuid.uuid= topology_uuid
   
    return topology_id

+2 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ class ChildTopologyModel (_Base):
    __tablename__ = 'child_topology'
    
    child_topology_uuid = Column(UUID(as_uuid=False), primary_key=True)
    context_uuid  = Column(UUID(as_uuid=False))
   
    topology_name = Column(String, nullable=False)
    host      = Column(String, nullable=False)
    port      = Column(Integer, nullable=True)
@@ -132,7 +132,7 @@ class ChildTopologyEndponitModel(_Base):
   
    child_topology = relationship('ChildTopologyModel', lazy='selectin', viewonly=True) # back_populates='topology_optical_links'
    device                 = relationship('ChildTopologyDeviceModel',              back_populates='endpoints')
    endpoint_opticallinks = relationship('ChildOpticalLinkEndponitModel' )
    endpoint_opticallinks = relationship('ChildOpticalLinkEndponitModel' , back_populates= 'endpoint' )
    
    def dump_id(self) -> Dict:
        return {
+40 −0
Original line number Diff line number Diff line
# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.

from typing import Tuple
from common.Constants import DEFAULT_TOPOLOGY_NAME
from common.proto.context_pb2 import ChildTopologyId
from common.method_wrappers.ServiceExceptions import InvalidArgumentsException
from ._Builder import get_uuid_from_string, get_uuid_random
from .Context import context_get_uuid

def topology_get_uuid(
    topology_id : ChildTopologyId, topology_name : str = '', allow_random : bool = False, allow_default : bool = False
) -> Tuple[str, str]:
    #context_uuid = context_get_uuid(topology_id.context_id, allow_random=False, allow_default=allow_default)
    raw_topology_uuid = topology_id.topology_uuid.uuid

    if len(raw_topology_uuid) > 0:
        return 'admin',get_uuid_from_string(raw_topology_uuid, prefix_for_name='admin')
    if len(topology_name) > 0:
        return 'admin', get_uuid_from_string(topology_name, prefix_for_name='admin')
    if allow_default:
        return 'admin', get_uuid_from_string(DEFAULT_TOPOLOGY_NAME, prefix_for_name='admin')
    if allow_random:
        return 'admin', get_uuid_random()

    raise InvalidArgumentsException([
        ('topology_id.topology_uuid.uuid', raw_topology_uuid),
        ('name', topology_name),
    ], extra_details=['At least one is required to produce a Topology UUID'])
+36 −20
Original line number Diff line number Diff line
@@ -15,7 +15,8 @@
import base64, json, logging , os  #, re
import requests , threading
from flask import jsonify, redirect, render_template, Blueprint, flash, session, url_for, request , make_response
from common.proto.context_pb2 import (ContextList, EndPoint, TopologyId, TopologyList, Topology , ChildTopology ,)
from common.proto.context_pb2 import (ChildTopologyId, TopologyId, TopologyList, LinkId , ChildTopology ,
                                      DeviceId  , ChildDevice , ChildOpticallink , ChildEndpoint , ChildEndpointId)

from google.protobuf.json_format import MessageToDict , ParseDict

@@ -103,30 +104,45 @@ def add_children () :
                child_topo.host = host
                child_topo.port = port
                devices_raw = data.get('devices',)
                optical_links_raw = data.get('optical_links',)
                optical_links_raw = data.get('opticalLinks',)
                endpoints=[]
                devices=[]
                optical_links = []
                for dev in devices_raw:
                topology_id = ChildTopologyId()
                topology_id.topology_uuid.uuid = topology_uuid
                    
                    endpoints = [{'endpoint_id':ep['endpointId'],
                                'device_id':{'device_uuid':{'uuid':dev['deviceId']['deviceUuid']['uuid']}},
                                'endpoint_name':ep['name'],
                                'topology_id':{'topology_uuid':{'uuid':topology_uuid}}
                                } 
                                 for ep in dev['device_endpoints']]
                    devices.append({'device_id':{'device_uuid':{'uuid':dev['deviceId']['deviceUuid']['uuid']}},
                                    'device_name':dev['name'],
                                    'endpoints':endpoints,
                                    'topology_id':{'topology_uuid':{'uuid':topology_uuid}}
                                    })
                for dev in devices_raw:
                    device = ChildDevice()
                    device_id = DeviceId ()
                    device_id.device_uuid.uuid = dev['deviceId']['deviceUuid']['uuid']
                 
                    for ep in dev['deviceEndpoints'] :

                    
                        endpoint = ChildEndpoint()
                        endpoint_id = ChildEndpointId()
                        endpoint_id.endpoint_uuid.uuid = ep['endpointId']['endpointUuid']['uuid']
                        endpoint.endpoint_id.CopyFrom(endpoint_id)
                        endpoint.endpoint_name= ep['name']
                        endpoint.device_id.CopyFrom(device_id)
                        endpoint.topology_id.CopyFrom(topology_id)
                        endpoints.append(endpoint)
                    device.device_id.CopyFrom(device_id)
                    device.device_name = dev['name']
                    device.topology_id.CopyFrom(topology_id)
                    device.endpoints.extend( endpoints )   
                    devices.append(device)
                for ol in optical_links_raw:
                    optical_links.append({'link_id':{'link_uuid':{'uuid':ol['linkId']['linkUuid']['uuid']}},
                                        'link_name':ol['name'],
                                        'topology_id':{'topology_uuid':{'uuid':topology_uuid}}
                                        })
                child_topo.devices = devices
                child_topo.optical_links = optical_links 
                    optical_link = ChildOpticallink()
                    optical_link_id = LinkId()
                    optical_link_id.link_uuid.uuid = ol['linkId']['linkUuid']['uuid']
                    optical_link.link_id.CopyFrom(optical_link_id)
                    optical_link.optical_link_name = ol['name']
                    optical_link.topology_id.CopyFrom(topology_id)
                    optical_links.append(optical_link)
                    
                child_topo.devices.extend( devices)
                child_topo.optical_links.extend(optical_links) 
                
                client_response = make_response(jsonify({'message':'child topology added'}), 200)
                def update_child_topology(context_client,child_topo) :