Commit d29a6712 authored by Mohammad Ismaeel's avatar Mohammad Ismaeel
Browse files

External Api for configuring Transponders

parent 5ea83f3b
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ service ContextService {
  rpc GetOpticalConfig   (Empty          ) returns (OpticalConfigList     ) {}
  rpc SetOpticalConfig   (OpticalConfig  ) returns (OpticalConfigId       ) {}
  rpc SelectOpticalConfig(OpticalConfigId) returns (OpticalConfig         ) {}
  rpc DeleteOpticalConfig(OpticalConfigId) returns (Empty         ) {}

  rpc SetOpticalLink     (OpticalLink    ) returns (Empty                 ) {}
  rpc GetOpticalLink     (OpticalLinkId  ) returns (OpticalLink           ) {}
@@ -638,6 +639,12 @@ message OpticalConfigList {
  repeated OpticalConfig opticalconfigs = 1;
}

message OpticalConfigEvent {
  Event event = 1;
  OpticalConfigId opticalconfig_id = 2;
}


// ---- Optical Link ----

message OpticalLinkId {
+58 −0
Original line number Diff line number Diff line

from common.method_wrappers.ServiceExceptions import InvalidArgumentsException
from typing import Optional, Union
from uuid import UUID, uuid4, uuid5

# Generate a UUIDv5-like from the SHA-1 of "TFS" and no namespace to be used as the NAMESPACE for all
# the context UUIDs generated. For efficiency purposes, the UUID is hardcoded; however, it is produced
# using the following code:
#    from hashlib import sha1
#    from uuid import UUID
#    hash = sha1(bytes('TFS', 'utf-8')).digest()
#    NAMESPACE_TFS = UUID(bytes=hash[:16], version=5)
NAMESPACE_TFS = UUID('200e3a1f-2223-534f-a100-758e29c37f40')

def get_uuid_from_string(str_uuid_or_name : Union[str, UUID], prefix_for_name : Optional[str] = None) -> str:
    # if UUID given, assume it is already a valid UUID
    if isinstance(str_uuid_or_name, UUID): return str_uuid_or_name
    if not isinstance(str_uuid_or_name, str):
        MSG = 'Parameter({:s}) cannot be used to produce a UUID'
        raise Exception(MSG.format(str(repr(str_uuid_or_name))))
    try:
        # try to parse as UUID
        return str(UUID(str_uuid_or_name))
    except: # pylint: disable=bare-except
        # produce a UUID within TFS namespace from parameter
        if prefix_for_name is not None:
            str_uuid_or_name = '{:s}/{:s}'.format(prefix_for_name, str_uuid_or_name)
        return str(uuid5(NAMESPACE_TFS, str_uuid_or_name))

def get_uuid_random() -> str:
    # Generate random UUID. No need to use namespace since "namespace + random = random".
    return str(uuid4())

def channel_get_uuid(
    channel_name :str , allow_random : bool = False
) -> str:
    

    if len(channel_name) > 0:
        return get_uuid_from_string(channel_name)
    if allow_random: return get_uuid_random()

    raise InvalidArgumentsException([
        ('channel uuid', channel_name),
       
    ], extra_details=['Channel name is required to produce a channel UUID'])

def opticalconfig_get_uuid(
    device_name : str = '', allow_random : bool = False
) -> str:
    
    if len(device_name) > 0:
        return get_uuid_from_string(device_name)
    if allow_random: return get_uuid_random()

    raise InvalidArgumentsException([
        ('name', device_name),
    ], extra_details=['At least one is required to produce a OpticalConfig UUID'])
+6 −0
Original line number Diff line number Diff line
@@ -461,3 +461,9 @@ class ContextClient:
        response = self.stub.SelectOpticalConfig(request)
        LOGGER.debug('SelectOpticalConfig result: {:s}'.format(grpc_message_to_json_string(response)))
        return response
    @RETRY_DECORATOR
    def DeleteOpticalConfig(self,request : OpticalConfigId) -> Empty:
        LOGGER.debug('DeleteOpticalConfig request: {:s}'.format(grpc_message_to_json_string(request)))
        response = self.stub.DeleteOpticalConfig(request)
        LOGGER.debug('DeleteOpticalConfig result: {:s}'.format(grpc_message_to_json_string(response)))
        return response
+8 −2
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ from .database.Slice import (
    slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_select, slice_set, slice_unset)
from .database.Topology import (
    topology_delete, topology_get, topology_get_details, topology_list_ids, topology_list_objs, topology_set)
from .database.OpticalConfig import set_opticalconfig, select_opticalconfig, get_opticalconfig
from .database.OpticalConfig import set_opticalconfig, select_opticalconfig, get_opticalconfig ,delete_opticalconfig

LOGGER = logging.getLogger(__name__)

@@ -318,3 +318,9 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
        optical_config_id = OpticalConfigId()
        optical_config_id.CopyFrom(result.opticalconfig_id)
        return OpticalConfig(config=result.config, opticalconfig_id=optical_config_id)
   
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def DeleteOpticalConfig (self, request : OpticalConfigId, context : grpc.ServicerContext) -> Empty:
        delete_opticalconfig(self.db_engine,self.messagebroker, request)
       
        return Empty()
+10 −1
Original line number Diff line number Diff line
@@ -17,7 +17,8 @@ from typing import Dict, Iterator, Set
from common.message_broker.Message import Message
from common.message_broker.MessageBroker import MessageBroker
from common.proto.context_pb2 import (
    ConnectionEvent, ContextEvent, DeviceEvent, EventTypeEnum, LinkEvent, ServiceEvent, SliceEvent, TopologyEvent)
    ConnectionEvent, ContextEvent, DeviceEvent, EventTypeEnum, LinkEvent, ServiceEvent, SliceEvent, TopologyEvent ,
    OpticalConfigEvent)

class EventTopicEnum(enum.Enum):
    CONNECTION  = 'connection'
@@ -28,6 +29,8 @@ class EventTopicEnum(enum.Enum):
    SERVICE     = 'service'
    SLICE       = 'slice'
    TOPOLOGY    = 'topology'
    OPTICALCONFIG = 'optical-config'
  

TOPIC_TO_EVENTCLASS = {
    EventTopicEnum.CONNECTION.value  : ConnectionEvent,
@@ -38,6 +41,8 @@ TOPIC_TO_EVENTCLASS = {
    EventTopicEnum.SERVICE.value     : ServiceEvent,
    EventTopicEnum.SLICE.value       : SliceEvent,
    EventTopicEnum.TOPOLOGY.value    : TopologyEvent,
    EventTopicEnum.OPTICALCONFIG.value : OpticalConfigEvent

}

CONSUME_TIMEOUT = 0.5 # seconds
@@ -61,6 +66,10 @@ def notify_event_topology(messagebroker : MessageBroker, event_type : EventTypeE
def notify_event_device(messagebroker : MessageBroker, event_type : EventTypeEnum, device_id : Dict) -> None:
    notify_event(messagebroker, EventTopicEnum.DEVICE, event_type, {'device_id': device_id})
    
def notify_event_opticalconfig(messagebroker : MessageBroker, event_type : EventTypeEnum, opticalconfig_id : Dict) -> None:
    notify_event(messagebroker, EventTopicEnum.DEVICE, event_type, {'opticalconfig_id': opticalconfig_id})
    

def notify_event_link(messagebroker : MessageBroker, event_type : EventTypeEnum, link_id : Dict) -> None:
    notify_event(messagebroker, EventTopicEnum.LINK, event_type, {'link_id': link_id})

Loading