diff --git a/manifests/osm_clientservice.yaml b/manifests/osm_clientservice.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf536e2fdf6448943fa9727e24c722ca18932759 --- /dev/null +++ b/manifests/osm_clientservice.yaml @@ -0,0 +1,75 @@ +# 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. + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: osm_clientservice +spec: + selector: + matchLabels: + app: osm_clientservice + #replicas: 1 + template: + metadata: + labels: + app: osm_clientservice + spec: + terminationGracePeriodSeconds: 5 + containers: + - name: server + image: labs.etsi.org:5050/tfs/controller/osm_client:latest + imagePullPolicy: Always + ports: + - containerPort: 30210 + - containerPort: 9192 + env: + - name: OSM_ADDRESS + value: "127.0.0.1" + - name: LOG_LEVEL + value: "INFO" + readinessProbe: + exec: + command: ["/bin/grpc_health_probe", "-addr=:30210"] + livenessProbe: + exec: + command: ["/bin/grpc_health_probe", "-addr=:30210"] + resources: + requests: + cpu: 250m + memory: 128Mi + limits: + cpu: 1000m + memory: 1024Mi +--- +apiVersion: v1 +kind: Service +metadata: + name: osm_clientservice + labels: + app: osm_clientservice +spec: + type: ClusterIP + selector: + app: osm_clientservice + ports: + - name: grpc + protocol: TCP + port: 30210 + targetPort: 30210 + - name: metrics + protocol: TCP + port: 9192 + targetPort: +--- \ No newline at end of file diff --git a/proto/osm_client.proto b/proto/osm_client.proto new file mode 100644 index 0000000000000000000000000000000000000000..b447816415dcce6e2ca6632d7ff9cc70e8d15bab --- /dev/null +++ b/proto/osm_client.proto @@ -0,0 +1,71 @@ +// 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. + +syntax = "proto3"; +package osmClient; + +import "context.proto"; + +service OsmService { + rpc NsiCreate (CreateRequest) returns(CreateResponse) {} + rpc NsiList (context.Empty) returns(NsiListResponse) {} + rpc NsiGet (GetRequest) returns(GetResponse) {} + rpc NsiDelete (DeleteRequest) returns(DeleteResponse) {} +} + +message CreateRequest { + string nst_name = 1; + string nsi_name = 2; + string config = 3; + string ssh_key = 4; + string account = 5; +} + +//OSM library doesn't return nsi ID, just an exception +message CreateResponse { + bool succeded = 1; + string errormessage = 2; +} + +message NsiListResponse { + repeated string id = 1; +} + +message GetRequest { + string id = 1; +} + +message GetResponse { + NsiObject nsi = 1; +} + +message NsiObject { + string nst_name = 1; + string nsi_name = 2; + string description = 3; + string VimAccountId = 4; + string Netslice_Subnet_id = 5; + string Netslice_vld_ip = 6; +} + +message DeleteRequest { + string id = 1; +} + +//OSM library doesn't return nsi ID, just an exception +message DeleteResponse { + bool succeded = 1; + string errormessage = 2; +} + diff --git a/src/common/Constants.py b/src/common/Constants.py index 68200764610b5fded328dbec741fdbbf70bc0930..8b5b714b572d84a7a2c57df0047818d058218a9a 100644 --- a/src/common/Constants.py +++ b/src/common/Constants.py @@ -72,6 +72,7 @@ class ServiceNameEnum(Enum): ANALYTICS = 'analytics' ANALYTICSBACKEND = 'analytics-backend' QOSPROFILE = 'qos-profile' + OSMCLIENT = 'osm-client' # Used for test and debugging only DLT_GATEWAY = 'dltgateway' @@ -112,6 +113,7 @@ DEFAULT_SERVICE_GRPC_PORTS = { ServiceNameEnum.ANALYTICS .value : 30080, ServiceNameEnum.ANALYTICSBACKEND .value : 30090, ServiceNameEnum.AUTOMATION .value : 30200, + ServiceNameEnum.OSMCLIENT .value : 30210, # Used for test and debugging only ServiceNameEnum.DLT_GATEWAY .value : 50051, diff --git a/src/osm_client/Config.py b/src/osm_client/Config.py new file mode 100644 index 0000000000000000000000000000000000000000..84feff3d97e1d9d42b6b1e0f58814c116e9536b4 --- /dev/null +++ b/src/osm_client/Config.py @@ -0,0 +1,20 @@ +# 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 common.Settings import get_setting + +DEFAULT_OSM_ADDRESS = '127.0.0.1' +OSM_ADDRESS = get_setting('OSM_ADDRESS', DEFAULT_OSM_ADDRESS) + + \ No newline at end of file diff --git a/src/osm_client/__init__.py b/src/osm_client/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..53d5157f750bfb085125cbd33faff1cec5924e14 --- /dev/null +++ b/src/osm_client/__init__.py @@ -0,0 +1,14 @@ +# 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. + diff --git a/src/osm_client/client/OsmClient.py b/src/osm_client/client/OsmClient.py new file mode 100644 index 0000000000000000000000000000000000000000..a040f661c9c4dfae4f89303e65b1240d253de40f --- /dev/null +++ b/src/osm_client/client/OsmClient.py @@ -0,0 +1,79 @@ +# 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. + +import grpc, logging +from common.Constants import ServiceNameEnum +from common.Settings import get_service_host, get_service_port_grpc +from common.proto.osm_client_pb2_grpc import osmClientServiceStub +from common.proto.context_pb2 import (Empty) +from common.tools.client.RetryDecorator import retry, delay_exponential +from common.tools.grpc.Tools import grpc_message_to_json_string + +from osmclient import client +from osmclient.common.exceptions import ClientException + + +LOGGER = logging.getLogger(__name__) +MAX_RETRIES = 15 +DELAY_FUNCTION = delay_exponential(initial=0.01, increment=2.0, maximum=5.0) +RETRY_DECORATOR = retry(max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect') + +class OsmClient: + def __init__(self, host=None, port=None): + if not host: host = get_service_host(ServiceNameEnum.OSMCLIENT) + if not port: port = get_service_port_grpc(ServiceNameEnum.OSMCLIENT) + self.endpoint = '{:s}:{:s}'.format(str(host), str(port)) + LOGGER.debug('Creating channel to {:s}...'.format(str(self.endpoint))) + self.channel = None + self.stub = None + self.connect() + LOGGER.debug('Channel created') + + + def connect(self): + self.channel = grpc.insecure_channel(self.endpoint) + self.stub = osmClientServiceStub(self.channel) + + def close(self): + if self.channel is not None: self.channel.close() + self.channel = None + self.stub = None + + @RETRY_DECORATOR + def NsiCreate(self, request : CreateRequest) -> CreateResponse: + LOGGER.debug('NsiCreate request: {:s}'.format(grpc_message_to_json_string(request))) + response = self.stub.NsiCreate(request) + LOGGER.debug('NsiCreate result: {:s}'.format(grpc_message_to_json_string(response))) + return response + + @RETRY_DECORATOR + def NsiList(self, request : Empty) -> NsiListResponse: + LOGGER.debug('NsiList request') + response = self.stub.NsiList(request) + LOGGER.debug('NsiList result: {:s}'.format(grpc_message_to_json_string(response))) + return response + + @RETRY_DECORATOR + def NsiGet(self, request : GetRequest) -> GetResponse: + LOGGER.debug('NsiGet request: {:s}'.format(grpc_message_to_json_string(request))) + response = self.stub.NsiGet(request) + LOGGER.debug('NsiGet result: {:s}'.format(grpc_message_to_json_string(response))) + return response + + @RETRY_DECORATOR + def NsiDelete(self, request : DeleteRequest) -> DeleteResponse: + LOGGER.debug('NsiDelete request: {:s}'.format(grpc_message_to_json_string(request))) + response = self.stub.NsiDelete(request) + LOGGER.debug('NsiDelete result: {:s}'.format(grpc_message_to_json_string(response))) + return response \ No newline at end of file diff --git a/src/osm_client/client/__init__.py b/src/osm_client/client/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..53d5157f750bfb085125cbd33faff1cec5924e14 --- /dev/null +++ b/src/osm_client/client/__init__.py @@ -0,0 +1,14 @@ +# 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. + diff --git a/src/osm_client/service/OsmClientService.py b/src/osm_client/service/OsmClientService.py new file mode 100644 index 0000000000000000000000000000000000000000..ccbf1536626a9295e14d51417a1808f89409dacb --- /dev/null +++ b/src/osm_client/service/OsmClientService.py @@ -0,0 +1,33 @@ +# 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 common.Constants import ServiceNameEnum +from common.Settings import get_service_port_grpc +from common.proto.osm_client_pb2_grpc import add_OsmServiceServicer_to_server +from common.tools.service.GenericGrpcService import GenericGrpcService +from osm_client.service.OsmClientServiceServicerImpl import OsmClientServiceServicerImpl + +from osmclient import client +from osmclient.common.exceptions import ClientException + + +class OsmClientService(GenericGrpcService): + def __init__(self, cls_name: str = __name__) -> None: + port = get_service_port_grpc(ServiceNameEnum.OSMCLIENT) + super().__init__(port, cls_name=cls_name) + self.osmClient_servicer = OsmClientServiceServicerImpl() + + + def install_servicers(self): + add_OsmServiceServicer_to_server(self.osmClient_servicer, self.server) diff --git a/src/osm_client/service/OsmClientServiceServicerImpl.py b/src/osm_client/service/OsmClientServiceServicerImpl.py new file mode 100644 index 0000000000000000000000000000000000000000..f45c682a4bc594a5981e879ced32c9338ec1310f --- /dev/null +++ b/src/osm_client/service/OsmClientServiceServicerImpl.py @@ -0,0 +1,70 @@ +# 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. + +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.osm_client_pb2 import CreateRequest, CreateResponse, NsiListResponse, GetRequest, GetResponse, DeleteRequest, DeleteResponse +from common.proto.osm_client_pb2_grpc import osmCLientServiceServicer +from osmclient import client +from osmclient.common.exceptions import ClientException +from osm_client.Config import OSM_ADDRESS + +LOGGER = logging.getLogger(__name__) + +METRICS_POOL = MetricsPool('OSMCLIENT', 'RPC') + +class OsmClientServiceServicerImpl(osmCLientServiceServicer): + def __init__(self): + LOGGER.info('Creating Servicer...') + self.myclient = client.Client(host=OSM_ADDRESS, sol005=True) + 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) + except Exception as e: + resp = CreateResponse(succeded = False, errormessage = str(e)) + else: + resp = CreateResponse(succeded = True) + return resp + + @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 NsiGet(self, request : GetRequest, context : grpc.ServicerContext) -> GetResponse: + nsiObject = self.myclient.nsi.get(request.id) + resp = GetResponse(NsiObject = nsiObject) + return resp + + @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) + 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) + except Exception as e: + resp = DeleteResponse(succeded = False, errormessage = str(e)) + else: + resp = DeleteResponse(succeded = True) + return resp \ No newline at end of file diff --git a/src/osm_client/service/__init__.py b/src/osm_client/service/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..53d5157f750bfb085125cbd33faff1cec5924e14 --- /dev/null +++ b/src/osm_client/service/__init__.py @@ -0,0 +1,14 @@ +# 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. + diff --git a/src/osm_client/service/__main__.py b/src/osm_client/service/__main__.py new file mode 100644 index 0000000000000000000000000000000000000000..d27b5c80adb5c302ff13137a46994fd1ac2b4979 --- /dev/null +++ b/src/osm_client/service/__main__.py @@ -0,0 +1,68 @@ +# 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. + +import logging, signal, sys, threading +from common.Constants import ServiceNameEnum +from common.Settings import ( + ENVVAR_SUFIX_SERVICE_HOST, ENVVAR_SUFIX_SERVICE_PORT_GRPC, + get_env_var_name, get_log_level, + wait_for_environment_variables +) +from .OsmClientService import OsmClientService + +terminate = threading.Event() +LOGGER = None + +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name, unused-argument + LOGGER.warning('Terminate signal received') + terminate.set() + +def main(): + global LOGGER # pylint: disable=global-statement + + log_level = get_log_level() + logging.basicConfig(level=log_level) + LOGGER = logging.getLogger(__name__) + + wait_for_environment_variables([ + get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_HOST ), + get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_PORT_GRPC), + get_env_var_name(ServiceNameEnum.DEVICE, ENVVAR_SUFIX_SERVICE_HOST ), + get_env_var_name(ServiceNameEnum.DEVICE, ENVVAR_SUFIX_SERVICE_PORT_GRPC), + get_env_var_name(ServiceNameEnum.SERVICE, ENVVAR_SUFIX_SERVICE_HOST ), + get_env_var_name(ServiceNameEnum.SERVICE, ENVVAR_SUFIX_SERVICE_PORT_GRPC), + ]) + + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTERM, signal_handler) + + LOGGER.info('Starting...') + + # Starting OsmClient service + grpc_service = OsmClientService() + grpc_service.start() + + LOGGER.debug('Configured Rules:') + + # Wait for Ctrl+C or termination signal + while not terminate.wait(timeout=1.0): pass + + LOGGER.info('Terminating...') + grpc_service.stop() + + LOGGER.info('Bye') + return 0 + +if __name__ == '__main__': + sys.exit(main())