diff --git a/manifests/osm_clientservice.yaml b/manifests/osm_clientservice.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ef9eaa2f82d48a35ed6f64be6edb7f3faf50d11d --- /dev/null +++ b/manifests/osm_clientservice.yaml @@ -0,0 +1,74 @@ +# 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: 9192 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..d34980c43378e274c965c10d9d7dbf6a6a468485 --- /dev/null +++ b/src/osm_client/Config.py @@ -0,0 +1,18 @@ +# 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=DEFAULT_OSM_ADDRESS) diff --git a/src/osm_client/Dockerfile b/src/osm_client/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..1c74274047be341435c8587c7894985764d252e6 --- /dev/null +++ b/src/osm_client/Dockerfile @@ -0,0 +1,77 @@ +# 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 python:3.10.16-slim + + +# Install dependencies +RUN apt-get --yes --quiet --quiet update +RUN apt-get --yes --quiet --quiet install wget g++ git build-essential cmake make git \ + libpcre2-dev python3-dev python3-pip python3-cffi curl software-properties-common && \ + rm -rf /var/lib/apt/lists/* + +# Set Python to show logs as they occur +ENV PYTHONUNBUFFERED=0 + + +# Download the gRPC health probe +RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ + wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \ + chmod +x /bin/grpc_health_probe + +# Get generic Python packages +RUN python3 -m pip install --upgrade pip +RUN python3 -m pip install --upgrade setuptools wheel +RUN python3 -m pip install --upgrade pip-tools + +# Get common Python packages +# Note: this step enables sharing the previous Docker build steps among all the Python components +WORKDIR /var/teraflow +COPY common_requirements.in common_requirements.in +RUN pip-compile --quiet --output-file=common_requirements.txt common_requirements.in +RUN python3 -m pip install -r common_requirements.txt + +# Add common files into working directory +WORKDIR /var/teraflow/common +COPY src/common/. ./ +RUN rm -rf proto + +# Create proto sub-folder, copy .proto files, and generate Python code +RUN mkdir -p /var/teraflow/common/proto +WORKDIR /var/teraflow/common/proto +RUN touch __init__.py +COPY proto/*.proto ./ +RUN python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. *.proto +RUN rm *.proto +RUN find . -type f -exec sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' {} \; + +# Create component sub-folders +RUN mkdir -p /var/teraflow/osm_client +WORKDIR /var/teraflow/osm_client +ENV OSM_CLIENT_VERSION=v16.0 +RUN python3 -m pip install -r "https://osm.etsi.org/gitweb/?p=osm/IM.git;a=blob_plain;f=requirements.txt;hb=${OSM_CLIENT_VERSION}" +RUN python3 -m pip install "git+https://osm.etsi.org/gerrit/osm/IM.git@${OSM_CLIENT_VERSION}#egg=osm-im" --upgrade +#Clone OsmCLient code +RUN git clone https://osm.etsi.org/gerrit/osm/osmclient +RUN git -C osmclient checkout ${OSM_CLIENT_VERSION} +# Install osmClient using pip +RUN python3 -m pip install -r osmclient/requirements.txt +RUN python3 -m pip install ./osmclient + +# Add component files into working directory +WORKDIR /var/teraflow +COPY src/osm_client/. osm_client/ + +# Start the service +ENTRYPOINT ["python", "-m", "osm_client.service"] 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..2ef7a414e39cd3063034eecbbe23b44db8e0204e --- /dev/null +++ b/src/osm_client/client/OsmClient.py @@ -0,0 +1,80 @@ +# 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 OsmServiceStub +from common.proto.osm_client_pb2 import CreateRequest, CreateResponse, NsiListResponse, GetRequest, GetResponse, DeleteRequest, DeleteResponse +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 = OsmServiceStub(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 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..81ec534d2a7c5815ff754b66e496e7f2b847fb2e --- /dev/null +++ b/src/osm_client/service/OsmClientService.py @@ -0,0 +1,29 @@ +# 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 + +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..8e9bf1ea6c8d3345467e698ffbf99f7a2bbb2ce7 --- /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 OsmServiceServicer +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(OsmServiceServicer): + 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 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())