Commit b667fcdd authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Pre-merge code cleanup

parent 7d529b88
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ ENDPOINT_PREFIX = 'osm_api.'
URL_PREFIX = '/osm-api'

_RESOURCES = [
    ('api.NS Services',      NS_Services,      '/NS_Services'),
    ('api.NS_Services',   NS_Services, '/NS_Services'),
    ('api.NS_Service_id', NS_Service,  '/NS_Service/<string:ns_id>'),
]

+21 −1
Original line number Diff line number Diff line
@@ -14,5 +14,25 @@

from common.Settings import get_setting

DEFAULT_OSM_ADDRESS = '10.1.7.199'
DEFAULT_OSM_ADDRESS = '127.0.0.1'
OSM_ADDRESS = get_setting('OSM_ADDRESS', default=DEFAULT_OSM_ADDRESS)

DEFAULT_OSM_PORT = 80
OSM_PORT = int(get_setting('OSM_PORT', default=DEFAULT_OSM_PORT))

DEFAULT_OSM_USERNAME = 'admin'
OSM_USERNAME = get_setting('OSM_USERNAME', default=DEFAULT_OSM_USERNAME)

DEFAULT_OSM_PASSWORD = 'admin'
OSM_PASSWORD = get_setting('OSM_PASSWORD', default=DEFAULT_OSM_PASSWORD)

DEFAULT_OSM_PROJECT = 'admin'
OSM_PROJECT = get_setting('OSM_PROJECT', default=DEFAULT_OSM_PROJECT)

DEFAULT_OSM_VERIFY_TLS = True
OSM_VERIFY_TLS = get_setting('OSM_VERIFY_TLS')
TRUE_VALUES = {'Y', 'YES', 'TRUE', 'T', 'E', 'ENABLE', 'ENABLED', '1'}
if OSM_VERIFY_TLS is None:
    OSM_VERIFY_TLS = DEFAULT_OSM_VERIFY_TLS
else:
    OSM_VERIFY_TLS = str(OSM_VERIFY_TLS).upper() in TRUE_VALUES
+34 −66
Original line number Diff line number Diff line
@@ -14,8 +14,7 @@

import grpc, logging
from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
from common.tools.grpc.Tools import grpc_message_to_json_string
from common.proto.context_pb2 import (Empty)
from common.proto.context_pb2 import Empty
from common.proto.osm_client_pb2 import (
    CreateRequest, CreateResponse, NsiListResponse, GetRequest, GetResponse,
    DeleteRequest, DeleteResponse , NsiObject
@@ -23,8 +22,9 @@ from common.proto.osm_client_pb2 import (
from common.proto.osm_client_pb2_grpc import OsmServiceServicer
from osmclient import client
from osmclient.common.exceptions import ClientException
from osm_client.Config import OSM_ADDRESS
import os
from osm_client.Config import (
    OSM_ADDRESS, OSM_PORT, OSM_USERNAME, OSM_PASSWORD, OSM_PROJECT, OSM_VERIFY_TLS
)


LOGGER = logging.getLogger(__name__)
@@ -34,66 +34,41 @@ METRICS_POOL = MetricsPool('OSMCLIENT', 'RPC')
class OsmClientServiceServicerImpl(OsmServiceServicer):
    def __init__(self):
        LOGGER.info('Creating Servicer...')
        
        # allow both naming styles
        user    = os.environ.get("OSM_USER") or os.environ.get("OSM_USERNAME")
        pwd     = os.environ.get("OSM_PASSWORD")
        project = os.environ.get("OSM_PROJECT", "admin")
        
        # host can be provided either way
        host    = os.environ.get("OSM_ADDRESS") or os.environ.get("OSM_HOSTNAME")
        
        # TLS verify handling: honor OSM_VERIFY_TLS, otherwise fall back to OSM_DISABLE_SSL_VERIFY
        if "OSM_VERIFY_TLS" in os.environ:
            verify = (os.environ["OSM_VERIFY_TLS"].lower() not in ("false","0","no"))
        elif "OSM_DISABLE_SSL_VERIFY" in os.environ:
            verify = not (os.environ["OSM_DISABLE_SSL_VERIFY"].lower() in ("true","1","yes"))
        else:
            verify = True  # default is to verify
        
        client_kwargs = dict(host=host, sol005=True, verify=verify)
        
        if user and pwd:
            client_kwargs.update(user=user, password=pwd, project=project)
        else:
            LOGGER.warning("OSM_USER/OSM_PASSWORD not set: continuing unauthenticated (likely 401)")
        
        self.myclient = client.Client(**client_kwargs)
        
        self._osm_client = client.Client(
            sol005 = True, host = OSM_ADDRESS, so_port = OSM_PORT, project = OSM_PROJECT,
            user = OSM_USERNAME, password = OSM_PASSWORD, verify = OSM_VERIFY_TLS
        )
        LOGGER.info('osmClient created')

        LOGGER.info('Servicer Created')


    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def NsiCreate(self, request : CreateRequest, context : grpc.ServicerContext) -> CreateResponse:
        try:
            #OSM library doesn't return nsi ID, just an exception
            self.myclient.nsi.create(request.nst_name, request.nsi_name, request.account)
            self._osm_client.nsi.create(request.nst_name, request.nsi_name, request.account)
        except Exception as e:
            resp = CreateResponse(succeded = False, errormessage = str(e))
        else:
            resp =  CreateResponse(succeded = True)
        return resp
    #this is wrong since its a list that contains data not only a single id
    #@safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    #def NsiList(self, request : Empty, context : grpc.ServicerContext) -> NsiListResponse:
    #    nsiIDs = self.myclient.nsi.list()
    #    resp = NsiListResponse(id=nsiIDs)
    #    return resp


    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def NsiList(self, request : Empty, context : grpc.ServicerContext) -> NsiListResponse:
        LOGGER.debug("NsiList request: %s", request)

        try:
            nsi_list = self.myclient.nsi.list()  # list[dict], each dict has "id", maybe others
            nsi_list = self._osm_client.nsi.list()  # list[dict], each dict has "id", maybe others
            LOGGER.debug("OSM returned %d NSIs", len(nsi_list))
            # Extract IDs

            seen = set()
            ids : list[str] = []
            for nsi in nsi_list or []:
                raw_id = nsi.get("id", "")
                sid = "" if raw_id is None else str(raw_id)
                if sid and sid not in seen: # this is a method to check the duplicate id's
                raw_id = nsi.get("id")
                if raw_id is None: continue
                sid = str(raw_id)
                if sid in seen: continue
                seen.add(sid)
                ids.append(sid)
            return NsiListResponse(id=ids)
@@ -105,17 +80,10 @@ class OsmClientServiceServicerImpl(OsmServiceServicer):
            return NsiListResponse(id=[])


    #@safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    #def NsiGet(self, request : GetRequest, context : grpc.ServicerContext) -> GetResponse:
    #    nsiObject =  self.myclient.nsi.get(request.id)
    #    resp = GetResponse(NsiObject = nsiObject)
    #    return resp

    #we should get the network slice info and extract them 
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def NsiGet(self, request : GetRequest, context : grpc.ServicerContext) -> GetResponse:
        try:
            nsi_data = self.myclient.nsi.get(request.id)  # returns dict from OSM
            nsi_data = self._osm_client.nsi.get(request.id)  # returns dict from OSM
            LOGGER.debug("Got NSI: %s", nsi_data)

            nsi_msg = NsiObject(
@@ -140,7 +108,7 @@ class OsmClientServiceServicerImpl(OsmServiceServicer):
    def NsiDelete(self, request : DeleteRequest, context : grpc.ServicerContext) -> DeleteResponse:
        try:
            # OSM library doesn't return nsi ID, just an exception
            self.myclient.nsi.delete(request.id, False, False)
            self._osm_client.nsi.delete(request.id, False, False)
        except Exception as e:
            resp = DeleteResponse(succeded = False, errormessage = str(e))
        else:
+1 −1

File changed.

Contains only whitespace changes.