From 84867b76a65efc4e9add52c86eae7e451c4a038d Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Tue, 23 Nov 2021 15:35:35 +0800 Subject: [PATCH] Refactor code layout add o2app to integrate o2ims and o2dms Signed-off-by: Bin Yang Change-Id: Iab47a7e2a40a8ca6e76a084e9ebb902e77899629 --- README-o2imsbuilder.md | 6 +- o2app/adapter/unit_of_work.py | 2 + o2app/bootstrap.py | 3 +- o2app/entrypoints/flask_application.py | 3 +- o2app/entrypoints/resource_watcher.py | 2 +- o2common/adapter/notifications.py | 2 +- o2common/adapter/redis_eventpublisher.py | 4 +- o2common/domain/commands.py | 42 +--------- o2common/domain/events.py | 7 +- .../service/client}/__init__.py | 0 {o2ims => o2common}/service/client/base_client.py | 9 +- {o2app => o2common}/service/messagebus.py | 2 +- o2common/service/unit_of_work.py | 16 ++-- o2common/service/watcher/base.py | 12 ++- {o2ims => o2common}/service/watcher/worker.py | 2 +- {o2ims/service/client => o2dms/domain}/__init__.py | 0 o2dms/domain/dms.py | 2 +- o2ims/adapter/clients/ocloud_sa_client.py | 4 +- o2ims/adapter/clients/orm_stx.py | 6 +- o2ims/adapter/notifications.py | 20 ----- o2ims/adapter/redis_eventpublisher.py | 31 ------- o2ims/adapter/unit_of_work.py | 64 -------------- o2ims/bootstrap.py | 84 ------------------- o2ims/config.py | 93 -------------------- o2ims/domain/base.py | 26 ------ o2ims/domain/commands.py | 5 +- o2ims/domain/events.py | 5 +- o2ims/domain/ocloud.py | 2 +- o2ims/domain/stx_object.py | 2 +- o2ims/entrypoints/flask_application.py | 35 -------- o2ims/entrypoints/redis_eventconsumer.py | 45 ---------- o2ims/entrypoints/resource_watcher.py | 86 ------------------- o2ims/service/auditor/dms_handler.py | 4 +- o2ims/service/auditor/ocloud_handler.py | 4 +- o2ims/service/handlers.py | 39 --------- o2ims/service/messagebus.py | 72 ---------------- o2ims/service/unit_of_work.py | 68 --------------- o2ims/service/watcher/base.py | 98 ---------------------- o2ims/service/watcher/ocloud_watcher.py | 8 +- o2ims/service/watcher/pserver_cpu_watcher.py | 6 +- o2ims/service/watcher/pserver_watcher.py | 6 +- o2ims/service/watcher/resource_watcher.py | 8 +- o2ims/service/watcher/resourcepool_watcher.py | 8 +- o2ims/views/ocloud_route.py | 2 +- o2ims/views/ocloud_view.py | 28 +++---- tests/conftest.py | 13 +-- .../integration-ocloud/test_clientdriver_stx_sa.py | 6 +- tests/integration-ocloud/test_watcher_w_stx_sa.py | 2 +- tests/integration/test_clientdriver_fake_stx_sa.py | 2 +- tests/integration/test_ocloud_repository.py | 2 +- tests/integration/test_ocloud_view.py | 2 +- tests/unit/test_ocloud.py | 2 +- tests/unit/test_watcher.py | 23 ++--- 53 files changed, 105 insertions(+), 920 deletions(-) rename {o2ims/entrypoints => o2common/service/client}/__init__.py (100%) rename {o2ims => o2common}/service/client/base_client.py (76%) rename {o2app => o2common}/service/messagebus.py (98%) rename {o2ims => o2common}/service/watcher/worker.py (94%) rename {o2ims/service/client => o2dms/domain}/__init__.py (100%) delete mode 100644 o2ims/adapter/notifications.py delete mode 100644 o2ims/adapter/redis_eventpublisher.py delete mode 100644 o2ims/adapter/unit_of_work.py delete mode 100644 o2ims/bootstrap.py delete mode 100644 o2ims/config.py delete mode 100644 o2ims/domain/base.py delete mode 100644 o2ims/entrypoints/flask_application.py delete mode 100644 o2ims/entrypoints/redis_eventconsumer.py delete mode 100644 o2ims/entrypoints/resource_watcher.py delete mode 100644 o2ims/service/handlers.py delete mode 100644 o2ims/service/messagebus.py delete mode 100644 o2ims/service/unit_of_work.py delete mode 100644 o2ims/service/watcher/base.py diff --git a/README-o2imsbuilder.md b/README-o2imsbuilder.md index a143301..4ab644f 100644 --- a/README-o2imsbuilder.md +++ b/README-o2imsbuilder.md @@ -49,8 +49,8 @@ exit ### Push O2 service images to local registry (with auth user admin) ```sh -sudo docker tag o2imsdms:latest registry.local:9001/admin/o2imsdms:0.1.1 -sudo docker image push registry.local:9001/admin/o2imsdms:0.1.1 +sudo docker tag o2imsdms:latest registry.local:9001/admin/o2imsdms:0.1.4 +sudo docker image push registry.local:9001/admin/o2imsdms:0.1.4 ``` @@ -67,7 +67,7 @@ o2ims: imagePullSecrets: admin-orano2-registry-secret image: repository: registry.local:9001/admin/o2imsdms - tag: 0.1.1 + tag: 0.1.4 pullPolicy: IfNotPresent logginglevel: "DEBUG" diff --git a/o2app/adapter/unit_of_work.py b/o2app/adapter/unit_of_work.py index 0faba56..2a8663d 100644 --- a/o2app/adapter/unit_of_work.py +++ b/o2app/adapter/unit_of_work.py @@ -46,6 +46,8 @@ class SqlAlchemyUnitOfWork(AbstractUnitOfWork): .ResourcePoolSqlAlchemyRepository(self.session) self.resources = ocloud_repository\ .ResourceSqlAlchemyRepository(self.session) + self.subscriptions = ocloud_repository\ + .SubscriptionSqlAlchemyRepository(self.session) self.deployment_managers = ocloud_repository\ .DeploymentManagerSqlAlchemyRepository(self.session) self.nfdeployment_descs = dms_repository\ diff --git a/o2app/bootstrap.py b/o2app/bootstrap.py index 7a074f6..2e90768 100644 --- a/o2app/bootstrap.py +++ b/o2app/bootstrap.py @@ -20,8 +20,9 @@ from o2common.adapter.notifications import AbstractNotifications,\ SmoO2Notifications from o2common.adapter import redis_eventpublisher from o2common.service import unit_of_work +from o2common.service import messagebus -from o2app.service import handlers, messagebus +from o2app.service import handlers from o2app.adapter.unit_of_work import SqlAlchemyUnitOfWork from o2ims.adapter import orm as o2ims_orm diff --git a/o2app/entrypoints/flask_application.py b/o2app/entrypoints/flask_application.py index 45c0436..65dab57 100644 --- a/o2app/entrypoints/flask_application.py +++ b/o2app/entrypoints/flask_application.py @@ -16,14 +16,13 @@ from flask import Flask from flask_restx import Api from o2app import bootstrap -# from o2ims import config -# from o2ims.views.ocloud_route import configure_routes from o2ims.views import ocloud_route as ims_route from o2dms.views import dms_route # apibase = config.get_o2ims_api_base() app = Flask(__name__) +app.config.SWAGGER_UI_DOC_EXPANSION = 'list' api = Api(app, version='1.0.0', title='O-Cloud O2 Services', description='Swagger OpenAPI document for \ diff --git a/o2app/entrypoints/resource_watcher.py b/o2app/entrypoints/resource_watcher.py index 036596c..8a9b0cc 100644 --- a/o2app/entrypoints/resource_watcher.py +++ b/o2app/entrypoints/resource_watcher.py @@ -16,8 +16,8 @@ import cotyledon from o2app import bootstrap from o2common.service.watcher.base import WatcherTree +from o2common.service.watcher.worker import PollWorker -from o2ims.service.watcher.worker import PollWorker from o2ims.service.watcher.ocloud_watcher import OcloudWatcher from o2ims.service.watcher.ocloud_watcher import DmsWatcher from o2ims.service.watcher.resourcepool_watcher import ResourcePoolWatcher diff --git a/o2common/adapter/notifications.py b/o2common/adapter/notifications.py index 4c49c1b..e6ba8b2 100644 --- a/o2common/adapter/notifications.py +++ b/o2common/adapter/notifications.py @@ -1,6 +1,6 @@ # pylint: disable=too-few-public-methods import abc -from o2ims import config +from o2common.config import config SMO_O2_ENDPOINT = config.get_smo_o2endpoint() diff --git a/o2common/adapter/redis_eventpublisher.py b/o2common/adapter/redis_eventpublisher.py index 4df167e..9fac313 100644 --- a/o2common/adapter/redis_eventpublisher.py +++ b/o2common/adapter/redis_eventpublisher.py @@ -16,8 +16,8 @@ import json from dataclasses import asdict import redis -from o2ims import config -from o2ims.domain import events +from o2common.config import config +from o2common.domain import events from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2common/domain/commands.py b/o2common/domain/commands.py index 8383f47..51d695f 100644 --- a/o2common/domain/commands.py +++ b/o2common/domain/commands.py @@ -13,48 +13,8 @@ # limitations under the License. # pylint: disable=too-few-public-methods -# from datetime import date -# from typing import Optional -from dataclasses import dataclass -# from datetime import datetime -# from o2ims.domain.resource_type import ResourceTypeEnum -from o2ims.domain.stx_object import StxGenericModel +# from dataclasses import dataclass class Command: pass - - -@dataclass -class UpdateStxObject(Command): - data: StxGenericModel - - -@dataclass -class UpdateOCloud(UpdateStxObject): - pass - - -@dataclass -class UpdateDms(UpdateStxObject): - parentid: str - - -@dataclass -class UpdateResourcePool(UpdateStxObject): - parentid: str - - -@dataclass -class UpdateResource(UpdateStxObject): - parentid: str - - -@dataclass -class UpdatePserverCpu(UpdateResource): - pass - - -@dataclass -class UpdatePserver(UpdateResource): - pass diff --git a/o2common/domain/events.py b/o2common/domain/events.py index 591662f..19d7e11 100644 --- a/o2common/domain/events.py +++ b/o2common/domain/events.py @@ -13,13 +13,8 @@ # limitations under the License. # pylint: disable=too-few-public-methods -from dataclasses import dataclass +# from dataclasses import dataclass class Event: pass - - -@dataclass -class OcloudUpdated(Event): - oCloudId: str diff --git a/o2ims/entrypoints/__init__.py b/o2common/service/client/__init__.py similarity index 100% rename from o2ims/entrypoints/__init__.py rename to o2common/service/client/__init__.py diff --git a/o2ims/service/client/base_client.py b/o2common/service/client/base_client.py similarity index 76% rename from o2ims/service/client/base_client.py rename to o2common/service/client/base_client.py index 0c35fee..d346e1c 100644 --- a/o2ims/service/client/base_client.py +++ b/o2common/service/client/base_client.py @@ -14,22 +14,21 @@ import abc # from typing import Optional, List, Set -from typing import List -from o2ims.domain import stx_object as ocloudModel +# from typing import List class BaseClient(abc.ABC): def __init__(self): pass - def list(self, **filters) -> List[ocloudModel.StxGenericModel]: + def list(self, **filters): return self._list(**filters) - def get(self, id) -> ocloudModel.StxGenericModel: + def get(self, id): return self._get(id) @abc.abstractmethod - def _get(self, id) -> ocloudModel.StxGenericModel: + def _get(self, id): raise NotImplementedError @abc.abstractmethod diff --git a/o2app/service/messagebus.py b/o2common/service/messagebus.py similarity index 98% rename from o2app/service/messagebus.py rename to o2common/service/messagebus.py index 297aa8f..6c60c28 100644 --- a/o2app/service/messagebus.py +++ b/o2common/service/messagebus.py @@ -15,7 +15,7 @@ # pylint: disable=broad-except, attribute-defined-outside-init from __future__ import annotations from typing import Callable, Dict, List, Union, Type, TYPE_CHECKING -from o2ims.domain import commands, events +from o2common.domain import commands, events if TYPE_CHECKING: from . import unit_of_work diff --git a/o2common/service/unit_of_work.py b/o2common/service/unit_of_work.py index 3bef2a3..2f6ff5e 100644 --- a/o2common/service/unit_of_work.py +++ b/o2common/service/unit_of_work.py @@ -16,19 +16,13 @@ from __future__ import annotations import abc -from o2ims.domain.ocloud_repo import OcloudRepository,\ - ResourcePoolRepository, ResourceRepository, ResourceTypeRepository,\ - DeploymentManagerRepository -from o2ims.domain.stx_repo import StxObjectRepository - class AbstractUnitOfWork(abc.ABC): - oclouds: OcloudRepository - resource_types: ResourceTypeRepository - resource_pools: ResourcePoolRepository - resources: ResourceRepository - deployment_managers: DeploymentManagerRepository - stxobjects: StxObjectRepository + # oclouds: OcloudRepository + # resource_types: ResourceTypeRepository + # resource_pools: ResourcePoolRepository + # resources: ResourceRepository + # deployment_managers: DeploymentManagerRepository def __enter__(self): return self diff --git a/o2common/service/watcher/base.py b/o2common/service/watcher/base.py index 0daf8d4..6e1eb9e 100644 --- a/o2common/service/watcher/base.py +++ b/o2common/service/watcher/base.py @@ -14,11 +14,9 @@ # from logging import exception # from cgtsclient import exc -from o2ims.service.client.base_client import BaseClient -# from o2ims.domain.stx_object import StxGenericModel -# from o2ims.service.unit_of_work import AbstractUnitOfWork -from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus +from o2common.service.client.base_client import BaseClient +from o2common.domain import commands +from o2common.service.messagebus import MessageBus from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -34,7 +32,7 @@ class BaseWatcher(object): def targetname(self) -> str: return self._targetname() - def probe(self, parent: commands.UpdateStxObject = None): + def probe(self, parent: commands.Command = None): try: cmds = self._probe(parent.data if parent else None) for cmd in cmds: @@ -46,7 +44,7 @@ class BaseWatcher(object): logger.warning("Failed to probe resource due to: " + str(ex)) return [] - def _probe(self, parent: object = None) -> commands.UpdateStxObject: + def _probe(self, parent: object = None) -> commands.Command: raise NotImplementedError def _targetname(self): diff --git a/o2ims/service/watcher/worker.py b/o2common/service/watcher/worker.py similarity index 94% rename from o2ims/service/watcher/worker.py rename to o2common/service/watcher/worker.py index 888ecd6..64d189d 100644 --- a/o2ims/service/watcher/worker.py +++ b/o2common/service/watcher/worker.py @@ -14,7 +14,7 @@ import time import sched -from o2ims.service.watcher.base import WatcherTree +from o2common.service.watcher.base import WatcherTree from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2ims/service/client/__init__.py b/o2dms/domain/__init__.py similarity index 100% rename from o2ims/service/client/__init__.py rename to o2dms/domain/__init__.py diff --git a/o2dms/domain/dms.py b/o2dms/domain/dms.py index d80b4a6..ceef458 100644 --- a/o2dms/domain/dms.py +++ b/o2dms/domain/dms.py @@ -14,7 +14,7 @@ from __future__ import annotations -from o2ims.domain.base import AgRoot +from o2common.domain.base import AgRoot class NfDeploymentDesc(AgRoot): diff --git a/o2ims/adapter/clients/ocloud_sa_client.py b/o2ims/adapter/clients/ocloud_sa_client.py index 0ec88e2..e8f1685 100644 --- a/o2ims/adapter/clients/ocloud_sa_client.py +++ b/o2ims/adapter/clients/ocloud_sa_client.py @@ -15,11 +15,11 @@ # client talking to Stx standalone import uuid -from o2ims.service.client.base_client import BaseClient +from o2common.service.client.base_client import BaseClient from typing import List # Optional, Set from o2ims.domain import stx_object as ocloudModel -from o2ims import config +from o2common.config import config from o2ims.domain.resource_type import ResourceTypeEnum # from dcmanagerclient.api import client diff --git a/o2ims/adapter/clients/orm_stx.py b/o2ims/adapter/clients/orm_stx.py index 46ebdb7..dd65402 100644 --- a/o2ims/adapter/clients/orm_stx.py +++ b/o2ims/adapter/clients/orm_stx.py @@ -31,8 +31,8 @@ from sqlalchemy import ( # from o2ims.domain import stx_object as ocloudModel -from o2ims.service.unit_of_work import AbstractUnitOfWork -from o2ims.adapter.unit_of_work import SqlAlchemyUnitOfWork +from o2common.service.unit_of_work import AbstractUnitOfWork +# from o2ims.adapter.unit_of_work import SqlAlchemyUnitOfWork from o2ims.domain.resource_type import ResourceTypeEnum from o2common.helper import o2logging @@ -53,7 +53,7 @@ stxobject = Table( ) -def start_o2ims_stx_mappers(uow: AbstractUnitOfWork = SqlAlchemyUnitOfWork()): +def start_o2ims_stx_mappers(uow: AbstractUnitOfWork): return # logger.info("Starting O2 IMS Stx mappers") # mapper(ocloudModel.StxGenericModel, stxobject) diff --git a/o2ims/adapter/notifications.py b/o2ims/adapter/notifications.py deleted file mode 100644 index 4c49c1b..0000000 --- a/o2ims/adapter/notifications.py +++ /dev/null @@ -1,20 +0,0 @@ -# pylint: disable=too-few-public-methods -import abc -from o2ims import config - - -SMO_O2_ENDPOINT = config.get_smo_o2endpoint() - - -class AbstractNotifications(abc.ABC): - @abc.abstractmethod - def send(self, message): - raise NotImplementedError - - -class SmoO2Notifications(AbstractNotifications): - def __init__(self, smoO2Endpoint=SMO_O2_ENDPOINT): - self.smoO2Endpoint = smoO2Endpoint - - def send(self, message): - pass diff --git a/o2ims/adapter/redis_eventpublisher.py b/o2ims/adapter/redis_eventpublisher.py deleted file mode 100644 index 4df167e..0000000 --- a/o2ims/adapter/redis_eventpublisher.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 json -from dataclasses import asdict -import redis - -from o2ims import config -from o2ims.domain import events - -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - - -r = redis.Redis(**config.get_redis_host_and_port()) - - -def publish(channel, event: events.Event): - logger.info("publishing: channel=%s, event=%s", channel, event) - r.publish(channel, json.dumps(asdict(event))) diff --git a/o2ims/adapter/unit_of_work.py b/o2ims/adapter/unit_of_work.py deleted file mode 100644 index 8b96fbc..0000000 --- a/o2ims/adapter/unit_of_work.py +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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. - -# pylint: disable=attribute-defined-outside-init -from __future__ import annotations -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -from sqlalchemy.orm.session import Session - -from o2ims import config -from o2ims.adapter import ocloud_repository -from o2ims.adapter.stx_repository import StxObjectSqlAlchemyRepository -from o2ims.service.unit_of_work import AbstractUnitOfWork - - -DEFAULT_SESSION_FACTORY = sessionmaker( - bind=create_engine( - config.get_postgres_uri(), - isolation_level="REPEATABLE READ", - ) -) - - -class SqlAlchemyUnitOfWork(AbstractUnitOfWork): - def __init__(self, session_factory=DEFAULT_SESSION_FACTORY): - self.session_factory = session_factory - - def __enter__(self): - self.session = self.session_factory() # type: Session - self.oclouds = ocloud_repository\ - .OcloudSqlAlchemyRepository(self.session) - self.resource_types = ocloud_repository\ - .ResouceTypeSqlAlchemyRepository(self.session) - self.resource_pools = ocloud_repository\ - .ResourcePoolSqlAlchemyRepository(self.session) - self.resources = ocloud_repository\ - .ResourceSqlAlchemyRepository(self.session) - self.deployment_managers = ocloud_repository\ - .DeploymentManagerSqlAlchemyRepository(self.session) - self.subscriptions = ocloud_repository\ - .SubscriptionSqlAlchemyRepository(self.session) - self.stxobjects = StxObjectSqlAlchemyRepository(self.session) - return super().__enter__() - - def __exit__(self, *args): - super().__exit__(*args) - self.session.close() - - def _commit(self): - self.session.commit() - - def rollback(self): - self.session.rollback() diff --git a/o2ims/bootstrap.py b/o2ims/bootstrap.py deleted file mode 100644 index 595fd91..0000000 --- a/o2ims/bootstrap.py +++ /dev/null @@ -1,84 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 retry import retry -import inspect -from typing import Callable -from o2ims.adapter import orm, redis_eventpublisher -from o2ims.adapter.notifications import AbstractNotifications,\ - SmoO2Notifications - -from o2ims.service import handlers, messagebus, unit_of_work -from o2ims.adapter.unit_of_work import SqlAlchemyUnitOfWork -from o2ims.adapter.clients import orm_stx -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - - -@retry(tries=100, delay=2, backoff=1) -def wait_for_db_ready(engine): - # wait for db up - logger.info("Wait for DB ready ...") - engine.connect() - logger.info("DB is ready") - - -def bootstrap( - start_orm: bool = True, - uow: unit_of_work.AbstractUnitOfWork = SqlAlchemyUnitOfWork(), - notifications: AbstractNotifications = None, - publish: Callable = redis_eventpublisher.publish, -) -> messagebus.MessageBus: - - if notifications is None: - notifications = SmoO2Notifications() - - if start_orm: - with uow: - # get default engine if uow is by default - engine = uow.session.get_bind() - wait_for_db_ready(engine) - orm.start_o2ims_mappers(engine) - - orm_stx.start_o2ims_stx_mappers(uow) - - dependencies = {"uow": uow, "notifications": notifications, - "publish": publish} - injected_event_handlers = { - event_type: [ - inject_dependencies(handler, dependencies) - for handler in event_handlers - ] - for event_type, event_handlers in handlers.EVENT_HANDLERS.items() - } - injected_command_handlers = { - command_type: inject_dependencies(handler, dependencies) - for command_type, handler in handlers.COMMAND_HANDLERS.items() - } - - return messagebus.MessageBus( - uow=uow, - event_handlers=injected_event_handlers, - command_handlers=injected_command_handlers, - ) - - -def inject_dependencies(handler, dependencies): - params = inspect.signature(handler).parameters - deps = { - name: dependency - for name, dependency in dependencies.items() - if name in params - } - return lambda message: handler(message, **deps) diff --git a/o2ims/config.py b/o2ims/config.py deleted file mode 100644 index 55d4755..0000000 --- a/o2ims/config.py +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 os -import sys - -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - - -def get_postgres_uri(): - host = os.environ.get("DB_HOST", "localhost") - port = 54321 if host == "localhost" else 5432 - password = os.environ.get("DB_PASSWORD", "o2ims123") - user, db_name = "o2ims", "o2ims" - return f"postgresql://{user}:{password}@{host}:{port}/{db_name}" - - -def get_api_url(): - host = os.environ.get("API_HOST", "localhost") - port = 5005 if host == "localhost" else 80 - return f"http://{host}:{port}" - - -def get_root_api_base(): - return "/" - - -def get_o2ims_api_base(): - return get_root_api_base() + 'o2ims_infrastructureInventory/v1' - - -def get_o2dms_api_base(): - return get_root_api_base() + "o2dms/v1" - - -def get_redis_host_and_port(): - host = os.environ.get("REDIS_HOST", "localhost") - port = 63791 if host == "localhost" else 6379 - return dict(host=host, port=port) - - -def get_smo_o2endpoint(): - smo_o2endpoint = os.environ.get( - "SMO_O2_ENDPOINT", "http://localhost/smo_sim") - return smo_o2endpoint - - -def get_stx_access_info(): - # authurl = os.environ.get("STX_AUTH_URL", "http://192.168.204.1:5000/v3") - # username = os.environ.get("STX_USERNAME", "admin") - # pswd = os.environ.get("STX_PASSWORD", "passwd1") - # stx_access_info = (authurl, username, pswd) - try: - client_args = dict( - auth_url=os.environ.get('OS_AUTH_URL', - "http://192.168.204.1:5000/v3"), - username=os.environ.get('OS_USERNAME', "admin"), - api_key=os.environ.get('OS_PASSWORD', "fakepasswd1"), - project_name=os.environ.get('OS_PROJECT_NAME', "admin"), - ) - # dc_client_args = dict( - # auth_url=os.environ['OS_AUTH_URL'], - # username=os.environ['OS_USERNAME'], - # api_key=os.environ['OS_PASSWORD'], - # project_name=os.environ['OS_PROJECT_NAME'], - # user_domain_name=os.environ['OS_USER_DOMAIN_NAME'], - # project_domain_name=os.environ['OS_PROJECT_NAME'], - # project_domain_id=os.environ['OS_PROJECT_DOMAIN_ID'] - # ) - except KeyError: - logger.error('Please source your RC file before execution, ' - 'e.g.: `source ~/downloads/admin-rc.sh`') - sys.exit(1) - - os_client_args = {} - for key, val in client_args.items(): - os_client_args['os_{key}'.format(key=key)] = val - os_client_args['os_password'] = os_client_args.pop('os_api_key') - os_client_args['os_region_name'] = 'RegionOne' - os_client_args['api_version'] = 1 - return os_client_args diff --git a/o2ims/domain/base.py b/o2ims/domain/base.py deleted file mode 100644 index 8a673da..0000000 --- a/o2ims/domain/base.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 datetime import datetime -from typing import List -from .events import Event - - -class AgRoot: - def __init__(self) -> None: - self.hash = "" - # self.id = "" - self.updatetime = datetime.now() - self.createtime = datetime.now() - self.events = [] # type: List[Event] diff --git a/o2ims/domain/commands.py b/o2ims/domain/commands.py index 8383f47..1a31f0e 100644 --- a/o2ims/domain/commands.py +++ b/o2ims/domain/commands.py @@ -19,10 +19,7 @@ from dataclasses import dataclass # from datetime import datetime # from o2ims.domain.resource_type import ResourceTypeEnum from o2ims.domain.stx_object import StxGenericModel - - -class Command: - pass +from o2common.domain.commands import Command @dataclass diff --git a/o2ims/domain/events.py b/o2ims/domain/events.py index 591662f..c97c688 100644 --- a/o2ims/domain/events.py +++ b/o2ims/domain/events.py @@ -14,10 +14,7 @@ # pylint: disable=too-few-public-methods from dataclasses import dataclass - - -class Event: - pass +from o2common.domain.events import Event @dataclass diff --git a/o2ims/domain/ocloud.py b/o2ims/domain/ocloud.py index 0c5d232..0829fb9 100644 --- a/o2ims/domain/ocloud.py +++ b/o2ims/domain/ocloud.py @@ -14,7 +14,7 @@ from __future__ import annotations -from o2ims.domain.base import AgRoot +from o2common.domain.base import AgRoot # from dataclasses import dataclass # from datetime import date # from typing import Optional, List, Set diff --git a/o2ims/domain/stx_object.py b/o2ims/domain/stx_object.py index 9629652..5e4d660 100644 --- a/o2ims/domain/stx_object.py +++ b/o2ims/domain/stx_object.py @@ -15,7 +15,7 @@ # from dataclasses import dataclass import datetime import json -from o2ims.domain.base import AgRoot +from o2common.domain.base import AgRoot from o2ims.domain.resource_type import ResourceTypeEnum, MismatchedModel from o2common.helper import o2logging diff --git a/o2ims/entrypoints/flask_application.py b/o2ims/entrypoints/flask_application.py deleted file mode 100644 index c439173..0000000 --- a/o2ims/entrypoints/flask_application.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 flask import Flask -from flask_restx import Api - -from o2ims import bootstrap -# from o2ims import config -from o2ims.views.ocloud_route import configure_namespace -from o2dms.views import dms_route - - -# apibase = config.get_o2ims_api_base() -app = Flask(__name__) -app.config.SWAGGER_UI_DOC_EXPANSION = 'list' -api = Api(app, version='1.0.0', - title='O-Cloud O2 Interface Application', - description='Swagger OpenAPI document for \ - O-Cloud O2 interface application', - ) -bus = bootstrap.bootstrap() -configure_namespace(api, bus) - -dms_route.configure_namespace(api, bus) diff --git a/o2ims/entrypoints/redis_eventconsumer.py b/o2ims/entrypoints/redis_eventconsumer.py deleted file mode 100644 index 15312bf..0000000 --- a/o2ims/entrypoints/redis_eventconsumer.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 json -import redis - -from o2ims import bootstrap, config -# from o2ims.domain import commands - -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - -r = redis.Redis(**config.get_redis_host_and_port()) - - -def main(): - logger.info("Redis pubsub starting") - bus = bootstrap.bootstrap() - pubsub = r.pubsub(ignore_subscribe_messages=True) - pubsub.subscribe("dms_changed") - - for m in pubsub.listen(): - handle_dms_changed(m, bus) - - -def handle_dms_changed(m, bus): - logger.info("handling %s", m) - # data = json.loads(m["data"]) - # cmd = commands.UpdateDms(ref=data["dmsid"]) - # bus.handle(cmd) - - -if __name__ == "__main__": - main() diff --git a/o2ims/entrypoints/resource_watcher.py b/o2ims/entrypoints/resource_watcher.py deleted file mode 100644 index 9e17485..0000000 --- a/o2ims/entrypoints/resource_watcher.py +++ /dev/null @@ -1,86 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 cotyledon - -from o2ims.service.watcher.worker import PollWorker -from o2ims.service.watcher.ocloud_watcher import OcloudWatcher -from o2ims.service.watcher.ocloud_watcher import DmsWatcher -from o2ims.service.watcher.resourcepool_watcher import ResourcePoolWatcher -from o2ims.adapter.clients.ocloud_sa_client import StxSaDmsClient -from o2ims.adapter.clients.ocloud_sa_client import StxSaOcloudClient -from o2ims.adapter.clients.ocloud_sa_client import StxSaResourcePoolClient - -from o2ims.service.watcher.pserver_watcher import PServerWatcher -from o2ims.adapter.clients.ocloud_sa_client import StxPserverClient - -from o2ims.service.watcher.pserver_cpu_watcher import PServerCpuWatcher -from o2ims.adapter.clients.ocloud_sa_client import StxCpuClient - -from o2ims.service.watcher.base import WatcherTree - -from o2ims import bootstrap -# from o2ims import config -# import redis - -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - -# r = redis.Redis(**config.get_redis_host_and_port()) - - -class WatcherService(cotyledon.Service): - def __init__(self, worker_id, args=None) -> None: - super().__init__(worker_id) - self.args = args - self.bus = bootstrap.bootstrap() - self.worker = PollWorker() - - def run(self): - try: - root = WatcherTree(OcloudWatcher( - StxSaOcloudClient(), self.bus)) - root.addchild( - DmsWatcher(StxSaDmsClient(), self.bus)) - - child_respool = root.addchild( - ResourcePoolWatcher(StxSaResourcePoolClient(), - self.bus)) - child_pserver = child_respool.addchild( - PServerWatcher(StxPserverClient(), self.bus)) - child_pserver.addchild( - PServerCpuWatcher(StxCpuClient(), self.bus)) - - self.worker.add_watcher(root) - - self.worker.start() - except Exception as ex: - logger.warning("WorkerService Exception:" + str(ex)) - finally: - self.worker.stop() - - -def start_watchers(sm: cotyledon.ServiceManager = None): - watchersm = sm if sm else cotyledon.ServiceManager() - watchersm.add(WatcherService, workers=1, args=()) - watchersm.run() - - -def main(): - logger.info("Resource watcher starting") - start_watchers() - - -if __name__ == "__main__": - main() diff --git a/o2ims/service/auditor/dms_handler.py b/o2ims/service/auditor/dms_handler.py index 207f6e2..c3d4a77 100644 --- a/o2ims/service/auditor/dms_handler.py +++ b/o2ims/service/auditor/dms_handler.py @@ -20,10 +20,10 @@ from o2ims.domain.stx_object import StxGenericModel # from typing import List, Dict, Callable, Type # TYPE_CHECKING from o2ims.domain import commands -from o2ims.service.unit_of_work import AbstractUnitOfWork +from o2common.service.unit_of_work import AbstractUnitOfWork from o2ims.domain.resource_type import MismatchedModel from o2ims.domain.ocloud import DeploymentManager -from o2ims import config +from o2common.config import config # if TYPE_CHECKING: # from . import unit_of_work diff --git a/o2ims/service/auditor/ocloud_handler.py b/o2ims/service/auditor/ocloud_handler.py index 9dd4f0e..554b2e1 100644 --- a/o2ims/service/auditor/ocloud_handler.py +++ b/o2ims/service/auditor/ocloud_handler.py @@ -20,11 +20,11 @@ from o2ims.domain.stx_object import StxGenericModel # from typing import List, Dict, Callable, Type # TYPE_CHECKING from o2ims.domain import commands -from o2ims.service.unit_of_work import AbstractUnitOfWork +from o2common.service.unit_of_work import AbstractUnitOfWork from o2ims.domain.resource_type import InvalidOcloudState from o2ims.domain.resource_type import MismatchedModel from o2ims.domain.ocloud import Ocloud -from o2ims import config +from o2common.config import config # if TYPE_CHECKING: # from . import unit_of_work diff --git a/o2ims/service/handlers.py b/o2ims/service/handlers.py deleted file mode 100644 index 830e1ff..0000000 --- a/o2ims/service/handlers.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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. - -# pylint: disable=unused-argument -from __future__ import annotations -# from dataclasses import asdict -from typing import List, Dict, Callable, Type -from o2ims.service.auditor import dms_handler -# TYPE_CHECKING -from o2ims.domain import commands, events -from o2ims.service.auditor import ocloud_handler - -# if TYPE_CHECKING: -# from . import unit_of_work - - -class InvalidResourceType(Exception): - pass - - -EVENT_HANDLERS = { -} # type: Dict[Type[events.Event], List[Callable]] - - -COMMAND_HANDLERS = { - commands.UpdateOCloud: ocloud_handler.update_ocloud, - commands.UpdateDms: dms_handler.update_dms -} # type: Dict[Type[commands.Command], Callable] diff --git a/o2ims/service/messagebus.py b/o2ims/service/messagebus.py deleted file mode 100644 index 297aa8f..0000000 --- a/o2ims/service/messagebus.py +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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. - -# pylint: disable=broad-except, attribute-defined-outside-init -from __future__ import annotations -from typing import Callable, Dict, List, Union, Type, TYPE_CHECKING -from o2ims.domain import commands, events - -if TYPE_CHECKING: - from . import unit_of_work - -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - -Message = Union[commands.Command, events.Event] - - -class MessageBus: - def __init__( - self, - uow: unit_of_work.AbstractUnitOfWork, - event_handlers: Dict[Type[events.Event], List[Callable]], - command_handlers: Dict[Type[commands.Command], Callable], - ): - self.uow = uow - self.event_handlers = event_handlers - self.command_handlers = command_handlers - - def handle(self, message: Message): - self.queue = [message] - while self.queue: - message = self.queue.pop(0) - if not message: - continue - elif isinstance(message, events.Event): - self.handle_event(message) - elif isinstance(message, commands.Command): - self.handle_command(message) - else: - raise Exception(f"{message} was not an Event or Command") - - def handle_event(self, event: events.Event): - for handler in self.event_handlers[type(event)]: - try: - logger.debug("handling event %s with handler %s", - event, handler) - handler(event) - self.queue.extend(self.uow.collect_new_events()) - except Exception: - logger.exception("Exception handling event %s", event) - continue - - def handle_command(self, command: commands.Command): - logger.debug("handling command %s", command) - try: - handler = self.command_handlers[type(command)] - handler(command) - self.queue.extend(self.uow.collect_new_events()) - except Exception as ex: - logger.exception("Exception handling command %s", command) - raise ex diff --git a/o2ims/service/unit_of_work.py b/o2ims/service/unit_of_work.py deleted file mode 100644 index 0445996..0000000 --- a/o2ims/service/unit_of_work.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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. - -# pylint: disable=attribute-defined-outside-init -from __future__ import annotations -import abc - -from o2ims.domain.ocloud_repo import OcloudRepository,\ - ResourcePoolRepository, ResourceRepository, ResourceTypeRepository,\ - DeploymentManagerRepository -from o2ims.domain.stx_repo import StxObjectRepository - - -class AbstractUnitOfWork(abc.ABC): - oclouds: OcloudRepository - resource_types: ResourceTypeRepository - resource_pools: ResourcePoolRepository - resources: ResourceRepository - deployment_managers: DeploymentManagerRepository - stxobjects: StxObjectRepository - - def __enter__(self): - return self - - def __exit__(self, *args): - self.rollback() - - def commit(self): - self._commit() - - def collect_new_events(self): - for entry in self.oclouds.seen: - while entry.events: - yield entry.events.pop(0) - for entry in self.resource_pools.seen: - while entry.events: - yield entry.events.pop(0) - for entry in self.resources.seen: - while entry.events: - yield entry.events.pop(0) - for entry in self.resource_types.seen: - while entry.events: - yield entry.events.pop(0) - # for entry in self.stxobjects.seen: - # while entry.events: - # yield entry.events.pop(0) - for entry in self.deployment_managers.seen: - while entry.events: - yield entry.events.pop(0) - - @abc.abstractmethod - def _commit(self): - raise NotImplementedError - - @abc.abstractmethod - def rollback(self): - raise NotImplementedError diff --git a/o2ims/service/watcher/base.py b/o2ims/service/watcher/base.py deleted file mode 100644 index 0daf8d4..0000000 --- a/o2ims/service/watcher/base.py +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright (C) 2021 Wind River Systems, Inc. -# -# 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 logging import exception -# from cgtsclient import exc -from o2ims.service.client.base_client import BaseClient -# from o2ims.domain.stx_object import StxGenericModel -# from o2ims.service.unit_of_work import AbstractUnitOfWork -from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus -from o2common.helper import o2logging -logger = o2logging.get_logger(__name__) - - -class BaseWatcher(object): - def __init__(self, client: BaseClient, - bus: MessageBus) -> None: - super().__init__() - self._client = client - self._bus = bus - # self._uow = bus.uow - - def targetname(self) -> str: - return self._targetname() - - def probe(self, parent: commands.UpdateStxObject = None): - try: - cmds = self._probe(parent.data if parent else None) - for cmd in cmds: - self._bus.handle(cmd) - - # return self._probe(parent) - return cmds - except Exception as ex: - logger.warning("Failed to probe resource due to: " + str(ex)) - return [] - - def _probe(self, parent: object = None) -> commands.UpdateStxObject: - raise NotImplementedError - - def _targetname(self): - raise NotImplementedError - - # def _compare_and_update(self, newmodel: StxGenericModel) -> bool: - # with self._uow: - # # localmodel = self._uow.stxobjects.get(ocloudmodel.id) - # localmodel = self._uow.stxobjects.get(str(newmodel.id)) - # if not localmodel: - # logger.info("add entry:" + newmodel.name) - # self._uow.stxobjects.add(newmodel) - # elif localmodel.is_outdated(newmodel): - # logger.info("update entry:" + newmodel.name) - # localmodel.update_by(newmodel) - # self._uow.stxobjects.update(localmodel) - # self._uow.commit() - - -# node to organize watchers in tree hierachy -class WatcherTree(object): - def __init__(self, watcher: BaseWatcher) -> None: - super().__init__() - self.watcher = watcher - self.children = {} - - def addchild(self, watcher: BaseWatcher) -> object: - child = WatcherTree(watcher) - self.children[watcher.targetname()] = child - return child - - def removechild(self, targetname: str) -> object: - return self.children.pop(targetname) - - # probe all resources by parent, depth = 0 for indefinite recursive - def probe(self, parentresource=None, depth: int = 0): - logger.debug("probe resources with watcher: " - + self.watcher.targetname()) - childdepth = depth - 1 if depth > 0 else 0 - resources = self.watcher.probe(parentresource) - logger.debug("probe returns " + str(len(resources)) + " resources") - - if depth == 1: - # stop recursive - return - - for res in resources: - for targetname in self.children.keys(): - self.children[targetname].probe(res, childdepth) diff --git a/o2ims/service/watcher/ocloud_watcher.py b/o2ims/service/watcher/ocloud_watcher.py index 31e2f1b..1d0c0ae 100644 --- a/o2ims/service/watcher/ocloud_watcher.py +++ b/o2ims/service/watcher/ocloud_watcher.py @@ -13,12 +13,12 @@ # limitations under the License. # from o2ims.domain.resource_type import ResourceTypeEnum -from o2ims.service.client.base_client import BaseClient +from o2common.service.client.base_client import BaseClient from o2ims.domain.stx_object import StxGenericModel -# from o2ims.service.unit_of_work import AbstractUnitOfWork -from o2ims.service.watcher.base import BaseWatcher +# from o2common.service.unit_of_work import AbstractUnitOfWork +from o2common.service.watcher.base import BaseWatcher from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus +from o2common.service.messagebus import MessageBus from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2ims/service/watcher/pserver_cpu_watcher.py b/o2ims/service/watcher/pserver_cpu_watcher.py index 66f2294..cf471a4 100644 --- a/o2ims/service/watcher/pserver_cpu_watcher.py +++ b/o2ims/service/watcher/pserver_cpu_watcher.py @@ -13,11 +13,11 @@ # limitations under the License. from o2ims.domain.stx_object import StxGenericModel -from o2ims.service.client.base_client import BaseClient -# from o2ims.service.unit_of_work import AbstractUnitOfWork +from o2common.service.client.base_client import BaseClient +# from o2common.service.unit_of_work import AbstractUnitOfWork from o2ims.service.watcher.resource_watcher import ResourceWatcher from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus +from o2common.service.messagebus import MessageBus from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2ims/service/watcher/pserver_watcher.py b/o2ims/service/watcher/pserver_watcher.py index befc2f6..ca3e898 100644 --- a/o2ims/service/watcher/pserver_watcher.py +++ b/o2ims/service/watcher/pserver_watcher.py @@ -13,11 +13,11 @@ # limitations under the License. from o2ims.domain.stx_object import StxGenericModel -from o2ims.service.client.base_client import BaseClient -# from o2ims.service.unit_of_work import AbstractUnitOfWork +from o2common.service.client.base_client import BaseClient +# from o2common.service.unit_of_work import AbstractUnitOfWork from o2ims.service.watcher.resource_watcher import ResourceWatcher from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus +from o2common.service.messagebus import MessageBus from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2ims/service/watcher/resource_watcher.py b/o2ims/service/watcher/resource_watcher.py index 7ca4093..86416e3 100644 --- a/o2ims/service/watcher/resource_watcher.py +++ b/o2ims/service/watcher/resource_watcher.py @@ -13,11 +13,11 @@ # limitations under the License. from o2ims.domain.stx_object import StxGenericModel -from o2ims.service.client.base_client import BaseClient -# from o2ims.service.unit_of_work import AbstractUnitOfWork -from o2ims.service.watcher.base import BaseWatcher +from o2common.service.client.base_client import BaseClient +# from o2common.service.unit_of_work import AbstractUnitOfWork +from o2common.service.watcher.base import BaseWatcher from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus +from o2common.service.messagebus import MessageBus from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2ims/service/watcher/resourcepool_watcher.py b/o2ims/service/watcher/resourcepool_watcher.py index a8fd4f6..214041f 100644 --- a/o2ims/service/watcher/resourcepool_watcher.py +++ b/o2ims/service/watcher/resourcepool_watcher.py @@ -13,11 +13,11 @@ # limitations under the License. from o2ims.domain.stx_object import StxGenericModel -from o2ims.service.client.base_client import BaseClient -# from o2ims.service.unit_of_work import AbstractUnitOfWork -from o2ims.service.watcher.base import BaseWatcher +from o2common.service.client.base_client import BaseClient +# from o2common.service.unit_of_work import AbstractUnitOfWork +from o2common.service.watcher.base import BaseWatcher from o2ims.domain import commands -from o2ims.service.messagebus import MessageBus +from o2common.service.messagebus import MessageBus from o2common.helper import o2logging logger = o2logging.get_logger(__name__) diff --git a/o2ims/views/ocloud_route.py b/o2ims/views/ocloud_route.py index a6f1847..5a2b259 100644 --- a/o2ims/views/ocloud_route.py +++ b/o2ims/views/ocloud_route.py @@ -16,7 +16,7 @@ import uuid from flask_restx import Resource from o2ims.views import ocloud_view, api -from o2ims import config +from o2common.config import config from o2ims.domain.ocloud import Subscription from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\ ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index 90850e8..9a1a595 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -16,11 +16,11 @@ from sqlalchemy import select from o2ims.adapter.orm import ocloud, resource, resourcetype, \ resourcepool, deploymentmanager, subscription -from o2ims.adapter import unit_of_work +from o2common.service import unit_of_work from o2ims.domain.ocloud import Subscription -def oclouds(uow: unit_of_work.SqlAlchemyUnitOfWork): +def oclouds(uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(ocloud)) return [dict(r) for r in res] @@ -34,14 +34,14 @@ def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork): return None if first is None else dict(first) -def resource_types(uow: unit_of_work.SqlAlchemyUnitOfWork): +def resource_types(uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(resourcetype)) return [dict(r) for r in res] def resource_type_one(resourceTypeId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(resourcetype).where( resourcetype.c.resourceTypeId == resourceTypeId)) @@ -49,14 +49,14 @@ def resource_type_one(resourceTypeId: str, return None if first is None else dict(first) -def resource_pools(uow: unit_of_work.SqlAlchemyUnitOfWork): +def resource_pools(uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(resourcepool)) return [dict(r) for r in res] def resource_pool_one(resourcePoolId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(resourcepool).where( resourcepool.c.resourcePoolId == resourcePoolId)) @@ -64,14 +64,14 @@ def resource_pool_one(resourcePoolId: str, return None if first is None else dict(first) -def resources(resourcePoolId: str, uow: unit_of_work.SqlAlchemyUnitOfWork): +def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(resource).where( resource.c.resourcePoolId == resourcePoolId)) return [dict(r) for r in res] -def resource_one(resourceId: str, uow: unit_of_work.SqlAlchemyUnitOfWork): +def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork): with uow: # topq = uow.session.query(resource).filter( # resource.c.resourceId == resourceId).cte('cte', recursive=True) @@ -85,14 +85,14 @@ def resource_one(resourceId: str, uow: unit_of_work.SqlAlchemyUnitOfWork): return None if first is None else dict(first) -def deployment_managers(uow: unit_of_work.SqlAlchemyUnitOfWork): +def deployment_managers(uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(deploymentmanager)) return [dict(r) for r in res] def deployment_manager_one(deploymentManagerId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(deploymentmanager).where( deploymentmanager.c.deploymentManagerId == deploymentManagerId)) @@ -100,14 +100,14 @@ def deployment_manager_one(deploymentManagerId: str, return None if first is None else dict(first) -def subscriptions(uow: unit_of_work.SqlAlchemyUnitOfWork): +def subscriptions(uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(subscription)) return [dict(r) for r in res] def subscription_one(subscriptionId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: res = uow.session.execute(select(subscription).where( subscription.c.subscriptionId == subscriptionId)) @@ -116,14 +116,14 @@ def subscription_one(subscriptionId: str, def subscription_create(subscription: Subscription, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: uow.subscriptions.add(subscription) uow.commit() def subscription_delete(subscriptionId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: uow.subscriptions.delete(subscriptionId) uow.commit() diff --git a/tests/conftest.py b/tests/conftest.py index a3359a9..60f3d2e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,12 +14,15 @@ from sqlalchemy.orm import sessionmaker, clear_mappers from tenacity import retry, stop_after_delay from unittest.mock import MagicMock -from o2ims import config +from o2common.config import config + from o2ims.adapter.orm import metadata, start_o2ims_mappers -from o2ims.adapter.clients.orm_stx import start_o2ims_stx_mappers -from o2ims.adapter import unit_of_work +# from o2ims.adapter.clients.orm_stx import start_o2ims_stx_mappers + +from o2app.adapter import unit_of_work from o2ims.views.ocloud_route import configure_namespace -from o2ims.bootstrap import bootstrap + +from o2app.bootstrap import bootstrap @pytest.fixture @@ -81,7 +84,7 @@ def sqlite_flask_uow(sqlite_uow): @pytest.fixture def mappers(): start_o2ims_mappers() - start_o2ims_stx_mappers() + # start_o2ims_stx_mappers() yield clear_mappers() diff --git a/tests/integration-ocloud/test_clientdriver_stx_sa.py b/tests/integration-ocloud/test_clientdriver_stx_sa.py index 4cf4c64..7ece56b 100644 --- a/tests/integration-ocloud/test_clientdriver_stx_sa.py +++ b/tests/integration-ocloud/test_clientdriver_stx_sa.py @@ -14,10 +14,8 @@ import sys import pytest -from o2ims.adapter import ocloud_repository as repository -from o2ims.domain import ocloud -from o2ims import config -import uuid + +from o2common.config import config from o2ims.adapter.clients.ocloud_sa_client import StxSaClientImp from cgtsclient.client import get_client diff --git a/tests/integration-ocloud/test_watcher_w_stx_sa.py b/tests/integration-ocloud/test_watcher_w_stx_sa.py index d722ee7..1525ab1 100644 --- a/tests/integration-ocloud/test_watcher_w_stx_sa.py +++ b/tests/integration-ocloud/test_watcher_w_stx_sa.py @@ -14,7 +14,7 @@ from multiprocessing.queues import Queue import pytest -from o2ims.entrypoints.resource_watcher import start_watchers +from o2app.entrypoints.resource_watcher import start_watchers from multiprocessing import Process from multiprocessing import Pipe # pipe = Pipe() diff --git a/tests/integration/test_clientdriver_fake_stx_sa.py b/tests/integration/test_clientdriver_fake_stx_sa.py index 25bc3ad..8dc84d9 100644 --- a/tests/integration/test_clientdriver_fake_stx_sa.py +++ b/tests/integration/test_clientdriver_fake_stx_sa.py @@ -17,7 +17,7 @@ import sys import pytest from o2ims.adapter import ocloud_repository as repository from o2ims.domain import ocloud -from o2ims import config +from o2common.config import config import logging import uuid import json diff --git a/tests/integration/test_ocloud_repository.py b/tests/integration/test_ocloud_repository.py index 75800a3..38bce21 100644 --- a/tests/integration/test_ocloud_repository.py +++ b/tests/integration/test_ocloud_repository.py @@ -18,7 +18,7 @@ import pytest from o2ims.domain import resource_type as rt from o2ims.adapter import ocloud_repository as repository from o2ims.domain import ocloud -from o2ims import config +from o2common.config import config pytestmark = pytest.mark.usefixtures("mappers") diff --git a/tests/integration/test_ocloud_view.py b/tests/integration/test_ocloud_view.py index e295a94..2fb7d73 100644 --- a/tests/integration/test_ocloud_view.py +++ b/tests/integration/test_ocloud_view.py @@ -15,7 +15,7 @@ import uuid import pytest -from o2ims import config +from o2common.config import config from o2ims.views import ocloud_view from o2ims.domain import ocloud from o2ims.domain import resource_type as rt diff --git a/tests/unit/test_ocloud.py b/tests/unit/test_ocloud.py index a556295..a2904b7 100644 --- a/tests/unit/test_ocloud.py +++ b/tests/unit/test_ocloud.py @@ -17,7 +17,7 @@ import uuid from o2ims.domain import ocloud from o2ims.domain import resource_type as rt from o2ims.views import ocloud_view -from o2ims import config +from o2common.config import config def setup_ocloud(): diff --git a/tests/unit/test_watcher.py b/tests/unit/test_watcher.py index 69c3e91..8c1afac 100644 --- a/tests/unit/test_watcher.py +++ b/tests/unit/test_watcher.py @@ -16,22 +16,25 @@ import time from datetime import datetime import json from typing import List -from o2ims.service import handlers -from o2ims.domain.resource_type import ResourceTypeEnum -from o2ims.service.client.base_client import BaseClient -from o2ims.domain import ocloud -from o2ims import config +# from o2common.config import config import uuid -from o2ims.service.watcher.base import BaseWatcher, WatcherTree +from o2common.service.watcher.base import BaseWatcher, WatcherTree +from o2common.service.watcher import worker +from o2common.service.unit_of_work import AbstractUnitOfWork +from o2common.service import messagebus + +from o2ims.domain.resource_type import ResourceTypeEnum from o2ims.domain import stx_object as ocloudModel from o2ims.adapter.ocloud_repository import OcloudRepository from o2ims.domain.stx_repo import StxObjectRepository -from o2ims.service.watcher import worker -from o2ims.service.unit_of_work import AbstractUnitOfWork from o2ims.service.watcher.ocloud_watcher import OcloudWatcher -from o2ims.service import messagebus -from o2ims import bootstrap from o2ims.domain import commands +from o2common.service.client.base_client import BaseClient +from o2ims.domain import ocloud + +from o2app.service import handlers +from o2app import bootstrap + class FakeOcloudClient(BaseClient): def __init__(self): -- 2.16.6