diff --git a/Dockerfile b/Dockerfile index f049425..4532dfc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,8 @@ WORKDIR /usr/src/app COPY . /usr/src/app +COPY ./sense-o-auth.yaml.templat[e] /root/.sense-o-auth.yaml + # create a venv. RUN python3 -m venv /opt/venv --upgrade-deps diff --git a/env.template b/env.template index 8658ec8..f380308 100644 --- a/env.template +++ b/env.template @@ -40,3 +40,8 @@ OXP_PROVISION_URL=http://192.168.201.205:8088/SDX-LC/1.0.0/provision OXP_PULL_URL=http://192.168.201.205:8088/SDX-LC/1.0.0/topology OXP_PULL_INTERVAL=180 OXP_CONNECTION_URL=http://192.168.201.205:8088/SDX-LC/1.0.0/connection + +#OXP Client +CLIENT_PACKAGE=sense_sdx +CLIENT_FUNCTION=client.py +CLIENT_TOPOLOGY_TRANSLATOR=topology_translate \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 13d7cb7..056f079 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,9 @@ dependencies = [ "connexion[swagger-ui] == 2.14.2", "asgiref >= 3.7.2", "pymongo > 3.0", - "sdx-datamodel @ git+https://github.com/atlanticwave-sdx/datamodel@3.0.0.dev5", + "dotenv", + "sense-sdx @ git+https://github.com/atlanticwave-sdx/sense-sdx.git", + "sdx-datamodel @ git+https://github.com/atlanticwave-sdx/datamodel@3.0.0.dev13", ] [project.optional-dependencies] diff --git a/sdx_lc/app.py b/sdx_lc/app.py index 5223a14..93d0a01 100644 --- a/sdx_lc/app.py +++ b/sdx_lc/app.py @@ -57,7 +57,8 @@ def create_app(): logger.info( f"SDX Local Controller starting up (" f"name: {os.getenv('SDXLC_NAME')}, " - f"domain: {os.getenv('SDXLC_DOMAIN')})" + f"domain: {os.getenv('SDXLC_DOMAIN')})," + f"client package:{os.getenv('CLIENT_PACKAGE')}" ) # Run swagger service diff --git a/sdx_lc/jobs/pull_topo_changes.py b/sdx_lc/jobs/pull_topo_changes.py index 7adce92..f825d7e 100644 --- a/sdx_lc/jobs/pull_topo_changes.py +++ b/sdx_lc/jobs/pull_topo_changes.py @@ -1,3 +1,5 @@ +import importlib +import importlib.util import json import logging import os.path @@ -15,6 +17,10 @@ from messaging.rpc_queue_producer import RpcProducer from utils.db_utils import DbUtils +CLIENT_PACKAGE = os.getenv("CLIENT_PACKAGE", None) +CLIENT_FUNCTION = os.getenv("CLIENT_FUNCTION", None) +CLIENT_TOPOLOGY_TRANSLATOR = os.getenv("CLIENT_TOPOLOGY_TRANSLATOR", None) + OXPO_USER = os.environ.get("OXPO_USER", None) OXPO_PASS = os.environ.get("OXPO_PASS", None) OXP_PULL_URL = os.environ.get("OXP_PULL_URL") @@ -54,32 +60,41 @@ def process_domain_controller_topo(db_instance): else: logger.debug("Latest topology does not exist") - try: - response = requests.get(OXP_PULL_URL, auth=(OXPO_USER, OXPO_PASS)) - pulled_topology = response.content - except (requests.ConnectionError, requests.HTTPError): - logger.debug("Error connecting to domain controller...") - continue - - if not response.ok: - continue - - logger.debug("Pulled request from domain controller") - - try: - json_pulled_topology = response.json() - except ValueError: - logger.debug("Cannot parse pulled topology, invalid JSON") - continue - - try: - pulled_topo_version = json_pulled_topology["version"] - except KeyError: - logger.debug("Error getting topo version") - continue - - if latest_topology_exists and latest_topo_version == pulled_topo_version: - continue + if not CLIENT_PACKAGE: + try: + response = requests.get(OXP_PULL_URL, auth=(OXPO_USER, OXPO_PASS)) + pulled_topology = response.content + if not response.ok: + continue + logger.debug("Pulled request from domain controller") + json_pulled_topology = response.json() + pulled_topo_version = json_pulled_topology["version"] + except (requests.ConnectionError, requests.HTTPError): + logger.debug("Error connecting to domain controller...") + continue + except ValueError: + logger.debug("Cannot parse pulled topology, invalid JSON") + continue + except KeyError: + logger.debug("Error getting topo version") + continue + if latest_topology_exists and latest_topo_version == pulled_topo_version: + continue + else: + try: + client_module = importlib.import_module(CLIENT_PACKAGE) + path = client_module.__dict__.get("__path__")[0] + spec = importlib.util.spec_from_file_location( + CLIENT_TOPOLOGY_TRANSLATOR, path + "/" + CLIENT_FUNCTION + ) + client = importlib.util.module_from_spec(spec) + spec.loader.exec_module(client) + pulled_topology = client.topology_translate() + json_pulled_topology = json.loads(pulled_topology) + json_pulled_topology["version"] = 1 + except Exception as e: + logger.error("Client Error:{e} \}") + continue logger.debug("Pulled topo with different version. Adding pulled topo to db") db_instance.add_key_value_pair_to_db(