Commit 5ea83f3b authored by Andrea Sgambelluri's avatar Andrea Sgambelluri
Browse files

Endpoints Auto Descivory

parent 10a2f3ff
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -20,7 +20,7 @@
export TFS_REGISTRY_IMAGES="http://localhost:32000/tfs/"
export TFS_REGISTRY_IMAGES="http://localhost:32000/tfs/"


# Set the list of components, separated by spaces, you want to build images for, and deploy.
# Set the list of components, separated by spaces, you want to build images for, and deploy.
export TFS_COMPONENTS="context device pathcomp service slice nbi webui load_generator"
export TFS_COMPONENTS="context device pathcomp opticalcontroller service slice nbi webui "


# Uncomment to activate Monitoring
# Uncomment to activate Monitoring
#export TFS_COMPONENTS="${TFS_COMPONENTS} monitoring"
#export TFS_COMPONENTS="${TFS_COMPONENTS} monitoring"
+3 −3
Original line number Original line Diff line number Diff line
@@ -305,7 +305,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def GetOpticalConfig(self, request : Empty, context : grpc.ServicerContext) -> OpticalConfigList:
    def GetOpticalConfig(self, request : Empty, context : grpc.ServicerContext) -> OpticalConfigList:
        result = get_opticalconfig(self.db_engine)
        result = get_opticalconfig(self.db_engine)
        return OpticalConfigList(OpticalConfigs=result)
        return OpticalConfigList(opticalconfigs=result)


    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def SetOpticalConfig(self, request : OpticalConfig, context : grpc.ServicerContext) -> OpticalConfigId:
    def SetOpticalConfig(self, request : OpticalConfig, context : grpc.ServicerContext) -> OpticalConfigId:
@@ -316,5 +316,5 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
    def SelectOpticalConfig(self, request : OpticalConfigId, context : grpc.ServicerContext) -> OpticalConfig:
    def SelectOpticalConfig(self, request : OpticalConfigId, context : grpc.ServicerContext) -> OpticalConfig:
        result = select_opticalconfig(self.db_engine, request)
        result = select_opticalconfig(self.db_engine, request)
        optical_config_id = OpticalConfigId()
        optical_config_id = OpticalConfigId()
        optical_config_id.CopyFrom(result.OpticalConfig_id)
        optical_config_id.CopyFrom(result.opticalconfig_id)
        return OpticalConfig(config=result.config, OpticalConfig_id=optical_config_id)
        return OpticalConfig(config=result.config, opticalconfig_id=optical_config_id)
+51 −23
Original line number Original line Diff line number Diff line
@@ -18,7 +18,8 @@ from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy_cockroachdb import run_transaction
from sqlalchemy_cockroachdb import run_transaction
from common.proto.context_pb2 import OpticalConfig, OpticalConfigId
from common.proto.context_pb2 import OpticalConfig, OpticalConfigId
from .models.OpticalConfigModel import OpticalConfigModel
from .models.OpticalConfigModel import OpticalConfigModel , OpticalChannelModel
from context.service.database.uuids.OpticalConfig import channel_get_uuid


LOGGER = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)


@@ -26,44 +27,61 @@ def get_opticalconfig(db_engine : Engine):
    def callback(session:Session):
    def callback(session:Session):
        optical_configs = list()
        optical_configs = list()
        results = session.query(OpticalConfigModel).all()
        results = session.query(OpticalConfigModel).all()
        
        for obj in results:
        for obj in results:
            LOGGER.info(f"opticaln config obj from context {obj.dump()}")
     
            optical_config = OpticalConfig()
            optical_config = OpticalConfig()
            optical_config.config = json.dump(obj.config)
            optical_config.config = json.dumps(obj.dump())
            optical_config.opticalconfig_id.opticalconfig_uuid = obj.opticalconfig_uuid
            optical_config.opticalconfig_id.opticalconfig_uuid = obj.dump_id()["opticalconfig_uuid"]
            optical_configs.append(optical_config)
            optical_configs.append(optical_config)
        return optical_configs
        return optical_configs
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    obj = run_transaction(sessionmaker(bind=db_engine), callback)
    return obj
    return obj


def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
    LOGGER.info(f"request {request} ")
    opticalconfig_id = OpticalConfigId()
    opticalconfig_id = OpticalConfigId()
    opticalconfig_id.opticalconfig_uuid = request.opticalconfig_id.opticalconfig_uuid
    opticalconfig_id.opticalconfig_uuid = request.opticalconfig_id.opticalconfig_uuid
    my_config_data = []
    OpticalConfig_data = []
    if request.config:
    if request.config:
        channels = []
        channels = []
        transceivers = []
        transceivers = []
        config = json.loads(request.config)
        config = json.loads(request.config)
        if 'channels' in config and len(config['channels']) > 0:
                 
            channels = [channel['name']['index'] for channel in config['channels']]
        if 'transceivers' in config and len(config['transceivers']['transceiver']) > 0:
        if 'transceivers' in config and len(config['transceivers']['transceiver']) > 0:
            transceivers = [transceiver for transceiver in config['transceivers']['transceiver']]
            transceivers = [transceiver for transceiver in config['transceivers']['transceiver']]
            
            
        my_config_data = [
        if 'channels' in config and len(config['channels']) > 0:
            #channels = [channel['name']['index'] for channel in config['channels']]
            for channel_params in config['channels']:
                 channels.append(
                                {
                                    "channel_uuid":channel_get_uuid(channel_params['name']['index']),
                                    "opticalconfig_uuid": request.opticalconfig_id.opticalconfig_uuid,
                                    "channel_name"          : channel_params['name']['index'],
                                    "frequency"         : int(channel_params["frequency"]) if "frequency" in channel_params  else 0,
                                    "operational_mode"  : int(channel_params["operational-mode"]) if "operational-mode" in channel_params else 0,
                                    "target_output_power"      : channel_params["target-output-power"] if "target-output-power" in channel_params else '',
                                 } 
                             )
                  
        OpticalConfig_data.append(
           {
           {
                                    "opticalconfig_uuid": request.opticalconfig_id.opticalconfig_uuid,
                                    "opticalconfig_uuid": request.opticalconfig_id.opticalconfig_uuid,
                "channels"          : channels,
                                    "transcievers"      : transceivers,
                                    "transcievers"      : transceivers,
                                    "interfaces"        : json.dumps(config["interfaces"]["interface"]),
                                    "interfaces"        : json.dumps(config["interfaces"]["interface"]),
                                    "channel_namespace" : config["channel_namespace"],
                                    "channel_namespace" : config["channel_namespace"],
                "endpoints"         : [json.dumps(endpoint) for endpoint in config["endpoints"]],
                                    "endpoints"         : [json.dumps(endpoint) for endpoint in config["endpoints"]],}
                "frequency"         : config["frequency"] if "frequency" in config  else 0,
                                   
                "operational_mode"  : config["operational_mode"] if "operational_mode" in config else 0,
        )
                "output_power"      : config["output_power"] if "output_power" in config else '',
            }
        ]
            
            
       
    LOGGER.info(f"optical config to set {OpticalConfig_data} ")
    LOGGER.info(f"channels {channels}")
    def callback(session:Session)->bool:
    def callback(session:Session)->bool:
        stmt = insert(OpticalConfigModel).values(my_config_data)
        stmt = insert(OpticalConfigModel).values(OpticalConfig_data)
        
        stmt = stmt.on_conflict_do_update(
        stmt = stmt.on_conflict_do_update(
            index_elements=[OpticalConfigModel.opticalconfig_uuid],
            index_elements=[OpticalConfigModel.opticalconfig_uuid],
            set_=dict(
            set_=dict(
@@ -71,7 +89,17 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
            )
            )
        )
        )
        stmt = stmt.returning(OpticalConfigModel.opticalconfig_uuid)
        stmt = stmt.returning(OpticalConfigModel.opticalconfig_uuid)
        id = session.execute(stmt).fetchone()
        opticalconfig_id = session.execute(stmt).fetchone()
        if (len(channels)>0) :
            
                stmt = insert(OpticalChannelModel).values(channels)
        
                stmt = stmt.on_conflict_do_nothing(
                    index_elements=[OpticalChannelModel.channel_uuid , OpticalConfigModel.opticalconfig_uuid],
                    
                )
                stmt = stmt.returning(OpticalChannelModel.channel_uuid)
                opticalChannel_id = session.execute(stmt).fetchone()
    opticalconfig_id = run_transaction(sessionmaker(bind=db_engine), callback)
    opticalconfig_id = run_transaction(sessionmaker(bind=db_engine), callback)
    return {'opticalconfig_uuid': opticalconfig_id}
    return {'opticalconfig_uuid': opticalconfig_id}


+35 −7
Original line number Original line Diff line number Diff line
@@ -13,30 +13,58 @@
# limitations under the License.
# limitations under the License.


import json
import json
from sqlalchemy import Column, String, Integer
from sqlalchemy import Column, String, Integer , ForeignKey
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.orm import relationship
from ._Base import _Base
from ._Base import _Base


class OpticalConfigModel(_Base):
class OpticalConfigModel(_Base):
    __tablename__ = 'optical_config'
    __tablename__ = 'optical_config'
    opticalconfig_uuid = Column(String, primary_key=True)
    opticalconfig_uuid = Column(String, primary_key=True)
    channels           = Column(ARRAY(String), nullable=True)
   
    transcievers       = Column(ARRAY(String), nullable=True)
    transcievers       = Column(ARRAY(String), nullable=True)
    interfaces         = Column(String, nullable=True)
    interfaces         = Column(String, nullable=True)
    channel_namespace  = Column(String, nullable=True)
    channel_namespace  = Column(String, nullable=True)
    endpoints          = Column(ARRAY(String), nullable=True)
    endpoints          = Column(ARRAY(String), nullable=True)
    frequency          = Column(Integer, nullable=True)
    channels           = relationship("OpticalChannelModel")
    operational_mode   = Column(Integer, nullable=True)

    output_power       = Column(String, nullable=True)
    
    def dump_id (self ):
        return {
            "opticalconfig_uuid":self.opticalconfig_uuid
        }


    def dump(self):
    def dump(self):
        return {
        return {
            "channels"          : [{'name': {'index': channel}} for channel in self.channels],
            "channels"          : [channel.dump() for channel in self.channels],
            "transceivers"      : {"transceiver": [transciever for transciever in self.transcievers]},
            "transceivers"      : {"transceiver": [transciever for transciever in self.transcievers]},
            "interfaces"        : {"interface": json.loads(self.interfaces)},
            "interfaces"        : {"interface": json.loads(self.interfaces)},
            "channel_namespace" : self.channel_namespace,
            "channel_namespace" : self.channel_namespace,
            "endpoints"         : [json.loads(endpoint) for endpoint in self.endpoints],
            "endpoints"         : [json.loads(endpoint) for endpoint in self.endpoints],
            
        }
        
        

class OpticalChannelModel(_Base):
    __tablename__ = 'optical_channel'
    channel_uuid =      Column(String, primary_key=True)
    channel_name =      Column (String,nullable=True)
    frequency          = Column(Integer, nullable=True)
    operational_mode   = Column(Integer, nullable=True)
    target_output_power       = Column(String, nullable=True)
    opticalconfig_uuid     = Column(ForeignKey('optical_config.opticalconfig_uuid',         ondelete='CASCADE' ), primary_key=True)
    opticalconfig     = relationship('OpticalConfigModel',     back_populates='channels')
    def dump_id (self ):
        return {
            "channel_uuid":self.channel_uuid
        }

    def dump(self):
        return {
            "name"      :{'index':self.channel_name},
            "frequency"         : self.frequency,
            "frequency"         : self.frequency,
            "output_power"      : self.output_power,
            "target_output_power"      : self.target_output_power,
            "operational_mode"  : self.operational_mode,
            "operational_mode"  : self.operational_mode,
        }
        }
        
+78 −0
Original line number Original line Diff line number Diff line

import operator
from sqlalchemy import CheckConstraint, Column, DateTime, Float, ForeignKey, Integer, String ,Boolean
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.types import ARRAY
from sqlalchemy.orm import relationship
from typing import Dict
from ._Base import _Base

class OpticalLinkModel(_Base):
    __tablename__ = 'opticallink'

    optical_link_uuid           = Column(UUID(as_uuid=False), primary_key=True)
    optical_link_name           = Column(String, nullable=False)
    length = Column(Integer, nullable=True)
    source  = Column(String, nullable=True)
    target  = Column(String, nullable=True)
    optical_link_fiber= relationship("FiberModel") 
    created_at          = Column(DateTime, nullable=False)
    updated_at          = Column(DateTime, nullable=False)

 
   

    def dump_id(self) -> Dict:
        return {'optical_link_uuid': {'uuid': self.link_uuid}}

    def dump(self) -> Dict:
        result = {
            'optical_link_id'   : self.dump_id(),
            'name'              : self.optical_link_name,
            'details': {
                "length"        : self.length,
                'source'        : self.source,
                "target"        : self.target,
                'fibers'        : [ fiber.dump() for fiber in self.optical_link_fiber ]
            }
          
        }
       
        return result

class FiberModel(_Base):
    __tablename__ = 'fiber'
    fiber_uuid  = Column(UUID(as_uuid=False), primary_key=True)
    fiber_length = Column(Integer, nullable=True)
    source_port  = Column(String, nullable=True)
    destination_port  = Column(String, nullable=True)
    local_peer_port = Column(String, nullable=True)
    remote_peer_port = Column(String, nullable=True)
    used = Column(Boolean ,nullable=true)
    c_slots = Column (ARRAY(Integer),nullable=True)
    l_slots = Column (ARRAY(Integer),nullable=True)
    s_slots = Column (ARRAY(Integer),nullable=True)
    optical_link_uuid     = Column(ForeignKey('opticallink.optical_link_uuid',  ondelete='CASCADE' ), primary_key=True)
	optical_link     = relationship('OpticalLinkModel',     back_populates='optical_link_fibers')
 
 
 	def dump_id(self) -> Dict:
        return {'fiber_uuid': {'uuid': self.fiber_uuid}}
    
    
    def dump(self) -> Dict:
        result = {
        'ID'               : self.dump_id(),
        'length'           : self.fiber_length,
        "src_port"         : self.source_port,
        "dst_port"         : self.destination_port,
        "local_peer_port"  : self.local_peer_port,
        "remote_peer_port" : self.remote_peer_port,
        "used"             : self.used,
        "c_slots"          : self.c_slots ,
        "l_slots"          : self.l_slots,
        "s_slots"          : self.s_slots
          
        }
       
        return result
 No newline at end of file
Loading