From: Zhang Rong(Jon) Date: Fri, 3 Dec 2021 03:47:51 +0000 (+0800) Subject: Add auditor for resource pool, pserver and cpu/memory/port/interface for pserver... X-Git-Tag: 1.0.0~14 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=f73c8e3b01b8f5b7438ba544870e06d8f30cdea0;p=pti%2Fo2.git Add auditor for resource pool, pserver and cpu/memory/port/interface for pserver of INF 1. Add auditor for resource pool, pserver and cpu/memory/port/interface of pserver 2. CUpdate resource type column, auto create resource type before create cpu/memory/port/interface resource 3. Give the git clone parameters to make download size lesser Issue-ID: INF-236 Issue-ID: INF-235 Issue-ID: INF-234 Signed-off-by: Zhang Rong(Jon) Change-Id: I7ddc7d8c18b7f3c17b22cdd35bdf68864f32f780 --- diff --git a/Dockerfile b/Dockerfile index 3cbe00a..db2258d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,12 +5,12 @@ RUN apt-get update; apt-get install -y git gcc # in case git repo is not accessable # RUN mkdir -p /cgtsclient # COPY temp/config /cgtsclient/ -RUN git clone https://opendev.org/starlingx/config.git /cgtsclient +RUN git clone --depth 1 --branch master https://opendev.org/starlingx/config.git /cgtsclient RUN pip install -e /cgtsclient/sysinv/cgts-client/cgts-client/ # RUN mkdir -p /distcloud-client # COPY temp/distcloud-client /distcloud-client/ -RUN git clone https://opendev.org/starlingx/distcloud-client.git /distcloud-client/ +RUN git clone --depth 1 --branch master https://opendev.org/starlingx/distcloud-client.git /distcloud-client/ RUN pip install -e /distcloud-client/distributedcloud-client # in case git repo is not accessable diff --git a/README-o2imsbuilder.md b/README-o2imsbuilder.md index 4ab644f..6e00564 100644 --- a/README-o2imsbuilder.md +++ b/README-o2imsbuilder.md @@ -36,8 +36,8 @@ cd o2 mkdir -p temp cd temp -git clone https://opendev.org/starlingx/config.git -git clone https://opendev.org/starlingx/distcloud-client.git +git clone --depth 1 --branch master https://opendev.org/starlingx/config.git +git clone --depth 1 --branch master https://opendev.org/starlingx/distcloud-client.git cd - docker-compose build diff --git a/README.md b/README.md index 1612e8e..0782690 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ cloned into temp before docker building ```sh mkdir -p temp cd temp -git clone https://opendev.org/starlingx/config.git -git clone https://opendev.org/starlingx/distcloud-client.git +git clone --depth 1 --branch master https://opendev.org/starlingx/config.git +git clone --depth 1 --branch master https://opendev.org/starlingx/distcloud-client.git cd - ``` diff --git a/o2app/entrypoints/resource_watcher.py b/o2app/entrypoints/resource_watcher.py index 8a9b0cc..b7ad799 100644 --- a/o2app/entrypoints/resource_watcher.py +++ b/o2app/entrypoints/resource_watcher.py @@ -31,6 +31,15 @@ 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.pserver_mem_watcher import PServerMemWatcher +from o2ims.adapter.clients.ocloud_sa_client import StxMemClient + +from o2ims.service.watcher.pserver_if_watcher import PServerIfWatcher +from o2ims.adapter.clients.ocloud_sa_client import StxIfClient + +from o2ims.service.watcher.pserver_port_watcher import PServerPortWatcher +from o2ims.adapter.clients.ocloud_sa_client import StxPortClient + from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -58,6 +67,12 @@ class WatcherService(cotyledon.Service): PServerWatcher(StxPserverClient(), self.bus)) child_pserver.addchild( PServerCpuWatcher(StxCpuClient(), self.bus)) + child_pserver.addchild( + PServerMemWatcher(StxMemClient(), self.bus)) + child_pserver.addchild( + PServerIfWatcher(StxIfClient(), self.bus)) + child_pserver.addchild( + PServerPortWatcher(StxPortClient(), self.bus)) self.worker.add_watcher(root) diff --git a/o2app/service/handlers.py b/o2app/service/handlers.py index 830e1ff..f3b3d6d 100644 --- a/o2app/service/handlers.py +++ b/o2app/service/handlers.py @@ -16,10 +16,11 @@ 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 +from o2ims.service.auditor import ocloud_handler, dms_handler, \ + resourcepool_handler, pserver_handler, pserver_cpu_handler, \ + pserver_mem_handler, pserver_port_handler, pserver_if_handler # if TYPE_CHECKING: # from . import unit_of_work @@ -35,5 +36,11 @@ EVENT_HANDLERS = { COMMAND_HANDLERS = { commands.UpdateOCloud: ocloud_handler.update_ocloud, - commands.UpdateDms: dms_handler.update_dms + commands.UpdateDms: dms_handler.update_dms, + commands.UpdateResourcePool: resourcepool_handler.update_resourcepool, + commands.UpdatePserver: pserver_handler.update_pserver, + commands.UpdatePserverCpu: pserver_cpu_handler.update_pserver_cpu, + commands.UpdatePserverMem: pserver_mem_handler.update_pserver_mem, + commands.UpdatePserverIf: pserver_if_handler.update_pserver_if, + commands.UpdatePserverPort: pserver_port_handler.update_pserver_port, } # type: Dict[Type[commands.Command], Callable] diff --git a/o2common/domain/base.py b/o2common/domain/base.py index 8a673da..dfd4b25 100644 --- a/o2common/domain/base.py +++ b/o2common/domain/base.py @@ -20,7 +20,7 @@ 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] + # self.id = "" diff --git a/o2ims/adapter/clients/ocloud_sa_client.py b/o2ims/adapter/clients/ocloud_sa_client.py index e8f1685..31c9069 100644 --- a/o2ims/adapter/clients/ocloud_sa_client.py +++ b/o2ims/adapter/clients/ocloud_sa_client.py @@ -89,9 +89,47 @@ class StxCpuClient(BaseClient): def _list(self, **filters) -> List[ocloudModel.StxGenericModel]: return self.driver.getCpuList(**filters) -# internal driver which implement client call to Stx Standalone instance + +class StxMemClient(BaseClient): + def __init__(self): + super().__init__() + # self._pserver_id = pserver_id + self.driver = StxSaClientImp() + + def _get(self, id) -> ocloudModel.StxGenericModel: + return self.driver.getMem(id) + + def _list(self, **filters) -> List[ocloudModel.StxGenericModel]: + return self.driver.getMemList(**filters) + + +class StxPortClient(BaseClient): + def __init__(self): + super().__init__() + # self._pserver_id = pserver_id + self.driver = StxSaClientImp() + + def _get(self, id) -> ocloudModel.StxGenericModel: + return self.driver.getPort(id) + + def _list(self, **filters) -> List[ocloudModel.StxGenericModel]: + return self.driver.getPortList(**filters) + + +class StxIfClient(BaseClient): + def __init__(self): + super().__init__() + # self._pserver_id = pserver_id + self.driver = StxSaClientImp() + + def _get(self, id) -> ocloudModel.StxGenericModel: + return self.driver.getIf(id) + + def _list(self, **filters) -> List[ocloudModel.StxGenericModel]: + return self.driver.getIfList(**filters) +# internal driver which implement client call to Stx Standalone instance class StxSaClientImp(object): def __init__(self, stx_client=None): super().__init__() @@ -114,7 +152,7 @@ class StxSaClientImp(object): logger.debug("host 1:" + str(hosts[0].to_dict())) return [ocloudModel.StxGenericModel( ResourceTypeEnum.PSERVER, self._hostconverter(host)) - for host in hosts if host] + for host in hosts if host] def getPserver(self, id) -> ocloudModel.StxGenericModel: host = self.stxclient.ihost.get(id) @@ -158,6 +196,45 @@ class StxSaClientImp(object): return ocloudModel.StxGenericModel( ResourceTypeEnum.PSERVER_CPU, self._cpuconverter(cpuinfo)) + def getMemList(self, **filters) -> List[ocloudModel.StxGenericModel]: + hostid = filters.get("hostid", None) + assert (hostid is not None), "missing hostid to query imem list" + memlist = self.stxclient.imemory.list(hostid) + return [ocloudModel.StxGenericModel( + ResourceTypeEnum.PSERVER_RAM, + self._memconverter(memories)) for memories in memlist if memories] + + def getMem(self, id) -> ocloudModel.StxGenericModel: + meminfo = self.stxclient.imemory.get(id) + return ocloudModel.StxGenericModel( + ResourceTypeEnum.PSERVER_RAM, self._memconverter(meminfo)) + + def getPortList(self, **filters) -> List[ocloudModel.StxGenericModel]: + hostid = filters.get("hostid", None) + assert (hostid is not None), "missing hostid to query port list" + portlist = self.stxclient.port.list(hostid) + return [ocloudModel.StxGenericModel( + ResourceTypeEnum.PSERVER_PORT, + port) for port in portlist if port] + + def getPort(self, id) -> ocloudModel.StxGenericModel: + portinfo = self.stxclient.port.get(id) + return ocloudModel.StxGenericModel( + ResourceTypeEnum.PSERVER_PORT, portinfo) + + def getIfList(self, **filters) -> List[ocloudModel.StxGenericModel]: + hostid = filters.get("hostid", None) + assert (hostid is not None), "missing hostid to query iinterface list" + iflist = self.stxclient.iinterface.list(hostid) + return [ocloudModel.StxGenericModel( + ResourceTypeEnum.PSERVER_IF, + self._ifconverter(ifs)) for ifs in iflist if ifs] + + def getIf(self, id) -> ocloudModel.StxGenericModel: + ifinfo = self.stxclient.iinterface.get(id) + return ocloudModel.StxGenericModel( + ResourceTypeEnum.PSERVER_IF, self._ifconverter(ifinfo)) + def _getIsystems(self): return self.stxclient.isystem.list() @@ -171,28 +248,41 @@ class StxSaClientImp(object): 'more than one system exists in the account.') return isystems[0] - @staticmethod + @ staticmethod def _hostconverter(host): setattr(host, "name", host.hostname) return host - @staticmethod + @ staticmethod def _cpuconverter(cpu): - setattr(cpu, "name", "core-"+str(cpu.core)) + setattr(cpu, "name", "cpu-"+str(cpu.cpu)) return cpu - @staticmethod + @ staticmethod + def _memconverter(mem): + setattr(mem, "name", "mem-node-"+str(mem.numa_node)) + return mem + + @ staticmethod + def _ifconverter(ifs): + setattr(ifs, "name", ifs.ifname) + setattr(ifs, 'updated_at', None) + setattr(ifs, 'created_at', None) + return ifs + + @ staticmethod def _k8sconverter(cluster): setattr(cluster, "name", cluster.cluster_name) setattr(cluster, "uuid", uuid.uuid3(uuid.NAMESPACE_URL, cluster.cluster_name)) setattr(cluster, 'updated_at', None) setattr(cluster, 'created_at', None) + setattr(cluster, 'events', []) logger.debug("k8s cluster name/uuid:" + cluster.name + "/" + str(cluster.uuid)) return cluster - @staticmethod + @ staticmethod def _k8shasher(cluster): return str(hash((cluster.cluster_name, cluster.cluster_api_endpoint, cluster.admin_user))) diff --git a/o2ims/adapter/ocloud_repository.py b/o2ims/adapter/ocloud_repository.py index 0826cf0..bc3557d 100644 --- a/o2ims/adapter/ocloud_repository.py +++ b/o2ims/adapter/ocloud_repository.py @@ -18,6 +18,8 @@ from o2ims.domain import ocloud from o2ims.domain.ocloud_repo import OcloudRepository, ResourceTypeRepository,\ ResourcePoolRepository, ResourceRepository, DeploymentManagerRepository,\ SubscriptionRepository +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) class OcloudSqlAlchemyRepository(OcloudRepository): diff --git a/o2ims/adapter/orm.py b/o2ims/adapter/orm.py index 7aafde5..05a8dc7 100644 --- a/o2ims/adapter/orm.py +++ b/o2ims/adapter/orm.py @@ -12,12 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +# from typing_extensions import Required from sqlalchemy import ( Table, MetaData, Column, Integer, String, + Text, + Enum, # Date, DateTime, ForeignKey, @@ -29,6 +32,7 @@ from sqlalchemy.orm import mapper, relationship # from sqlalchemy.sql.sqltypes import Integer from o2ims.domain import ocloud as ocloudModel +from o2ims.domain.resource_type import ResourceTypeEnum from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -59,6 +63,7 @@ resourcetype = Table( Column("hash", String(255)), Column("resourceTypeId", String(255), primary_key=True), + Column("resourceTypeEnum", Enum(ResourceTypeEnum), nullable=False), Column("oCloudId", ForeignKey("ocloud.oCloudId")), Column("name", String(255)), Column("vendor", String(255)), @@ -98,10 +103,9 @@ resource = Table( Column("resourceTypeId", ForeignKey("resourcetype.resourceTypeId")), Column("resourcePoolId", ForeignKey("resourcepool.resourcePoolId")), # Column("globalAssetId", String(255)), - # Column("oCloudId", ForeignKey("ocloud.oCloudId")), Column("parentId", String(255)), Column("description", String(255)), - # Column("elements", String(255)) + Column("elements", Text()) # Column("extensions", String(1024)) ) diff --git a/o2ims/domain/commands.py b/o2ims/domain/commands.py index 1a31f0e..8cd8243 100644 --- a/o2ims/domain/commands.py +++ b/o2ims/domain/commands.py @@ -42,16 +42,36 @@ class UpdateResourcePool(UpdateStxObject): parentid: str +@dataclass +class UpdateResourceType(UpdateStxObject): + parentid: str + + @dataclass class UpdateResource(UpdateStxObject): parentid: str +@dataclass +class UpdatePserver(UpdateResource): + pass + + @dataclass class UpdatePserverCpu(UpdateResource): pass @dataclass -class UpdatePserver(UpdateResource): +class UpdatePserverMem(UpdateResource): + pass + + +@dataclass +class UpdatePserverIf(UpdateResource): + pass + + +@dataclass +class UpdatePserverPort(UpdateResource): pass diff --git a/o2ims/domain/events.py b/o2ims/domain/events.py index c97c688..7ab04a0 100644 --- a/o2ims/domain/events.py +++ b/o2ims/domain/events.py @@ -20,3 +20,8 @@ from o2common.domain.events import Event @dataclass class OcloudUpdated(Event): oCloudId: str + + +@dataclass +class ResourceTypeUpdated(Event): + oCloudId: str diff --git a/o2ims/domain/ocloud.py b/o2ims/domain/ocloud.py index 0829fb9..ec41c0e 100644 --- a/o2ims/domain/ocloud.py +++ b/o2ims/domain/ocloud.py @@ -73,8 +73,9 @@ class ResourceType(AgRoot): description: str = '') -> None: super().__init__() self.resourceTypeId = typeid + self.version_number = 0 self.oCloudId = ocloudid - self.resourceTypeEnum = typeEnum.value + self.resourceTypeEnum = typeEnum self.name = name self.vender = vender self.model = model @@ -85,17 +86,16 @@ class ResourceType(AgRoot): class Resource(AgRoot): def __init__(self, resourceId: str, resourceTypeId: str, - resourcePoolId: str, oCloudId: str = '', - parentId: str = '', elements: list = [], + resourcePoolId: str, parentId: str = '', + gAssetId: str = '', elements: str = '', description: str = '') -> None: super().__init__() self.resourceId = resourceId self.version_number = 0 - self.oCloudId = oCloudId self.resourceTypeId = resourceTypeId self.resourcePoolId = resourcePoolId + self.globalAssetId = gAssetId self.parentId = parentId - self.path = str() self.elements = elements self.description = description self.extensions = [] @@ -116,7 +116,6 @@ class Ocloud(AgRoot): self.deploymentManagers = [] self.resourceTypes = [] self.extensions = [] - # self.events = [] # def addDeploymentManager(self, # deploymentManager: DeploymentManager): diff --git a/o2ims/domain/resource_type.py b/o2ims/domain/resource_type.py index 79b36fd..f94e688 100644 --- a/o2ims/domain/resource_type.py +++ b/o2ims/domain/resource_type.py @@ -2,12 +2,14 @@ from enum import Enum class ResourceTypeEnum(Enum): - OCLOUD = 1, - RESOURCE_POOL = 2, - DMS = 3, + OCLOUD = 1 + RESOURCE_POOL = 2 + DMS = 3 PSERVER = 11 PSERVER_CPU = 12 PSERVER_RAM = 13 + PSERVER_PORT = 14 + PSERVER_IF = 15 class InvalidOcloudState(Exception): diff --git a/o2ims/domain/stx_object.py b/o2ims/domain/stx_object.py index 5e4d660..be668b6 100644 --- a/o2ims/domain/stx_object.py +++ b/o2ims/domain/stx_object.py @@ -25,6 +25,7 @@ logger = o2logging.get_logger(__name__) class StxGenericModel(AgRoot): def __init__(self, type: ResourceTypeEnum, api_response: dict = None, content_hash=None) -> None: + super().__init__() if api_response: self.id = str(api_response.uuid) self.type = type diff --git a/o2ims/service/auditor/dms_handler.py b/o2ims/service/auditor/dms_handler.py index c3d4a77..9ef0a77 100644 --- a/o2ims/service/auditor/dms_handler.py +++ b/o2ims/service/auditor/dms_handler.py @@ -77,7 +77,7 @@ def is_outdated(ocloud: DeploymentManager, stxobj: StxGenericModel): def create_by(stxobj: StxGenericModel, parentid: str) -> DeploymentManager: dmsendpoint = config.get_api_url() +\ - config.get_o2dms_api_base() + "/" + stxobj.id + config.get_o2dms_api_base() + "/" + stxobj.id description = "A DMS" ocloudid = parentid supportedLocations = '' @@ -104,3 +104,4 @@ def update_by(target: DeploymentManager, stxobj: StxGenericModel, target.hash = stxobj.hash target.oCloudId = parentid target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/auditor/ocloud_handler.py b/o2ims/service/auditor/ocloud_handler.py index 554b2e1..c8c1e1b 100644 --- a/o2ims/service/auditor/ocloud_handler.py +++ b/o2ims/service/auditor/ocloud_handler.py @@ -99,3 +99,4 @@ def update_by(ocloud: Ocloud, stxobj: StxGenericModel) -> None: # ocloud.content = stxobj.content ocloud.hash = stxobj.hash ocloud.version_number = ocloud.version_number + 1 + ocloud.events = [] diff --git a/o2ims/service/auditor/pserver_cpu_handler.py b/o2ims/service/auditor/pserver_cpu_handler.py new file mode 100644 index 0000000..d447057 --- /dev/null +++ b/o2ims/service/auditor/pserver_cpu_handler.py @@ -0,0 +1,116 @@ +# 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 +import uuid +# import json + +from o2ims.domain import commands +from o2ims.domain.stx_object import StxGenericModel +from o2common.service.unit_of_work import AbstractUnitOfWork +from o2ims.domain.resource_type import MismatchedModel +from o2ims.domain.ocloud import Resource, ResourceType + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class InvalidResourceType(Exception): + pass + + +def update_pserver_cpu( + cmd: commands.UpdatePserverCpu, + uow: AbstractUnitOfWork +): + stxobj = cmd.data + with uow: + p_resource = uow.resources.get(cmd.parentid) + resourcepool = uow.resource_pools.get(p_resource.resourcePoolId) + + res = uow.session.execute( + ''' + SELECT "resourceTypeId", "oCloudId", "name" + FROM resourcetype + WHERE "resourceTypeEnum" = :resource_type_enum + ''', + dict(resource_type_enum=stxobj.type.name) + ) + first = res.first() + if first is None: + resourcetype_id = str(uuid.uuid4()) + uow.resource_types.add(ResourceType( + resourcetype_id, + 'pserver_cpu', stxobj.type, + resourcepool.oCloudId)) + else: + resourcetype_id = first['resourceTypeId'] + + resource = uow.resources.get(stxobj.id) + if not resource: + logger.info("add the cpu of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + localmodel = create_by(stxobj, p_resource, resourcetype_id) + uow.resources.add(localmodel) + + logger.info("Add the cpu of pserver: " + stxobj.id + + ", name: " + stxobj.name) + else: + localmodel = resource + if is_outdated(localmodel, stxobj): + logger.info("update cpu of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + update_by(localmodel, stxobj, p_resource) + uow.resources.update(localmodel) + + logger.info("Update the cpu of pserver: " + stxobj.id + + ", name: " + stxobj.name) + uow.commit() + + +def is_outdated(resource: Resource, stxobj: StxGenericModel): + return True if resource.hash != stxobj.hash else False + + +def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\ + -> Resource: + # content = json.loads(stxobj.content) + resourcetype_id = resourcetype_id + resourcepool_id = parent.resourcePoolId + parent_id = parent.resourceId + gAssetId = '' # TODO: global ID + description = "A CPU resource of the physical server" + resource = Resource(stxobj.id, resourcetype_id, resourcepool_id, + parent_id, gAssetId, stxobj.content, description) + resource.createtime = stxobj.createtime + resource.updatetime = stxobj.updatetime + resource.hash = stxobj.hash + + return resource + + +def update_by(target: Resource, stxobj: StxGenericModel, + parentid: str) -> None: + if target.resourceId != stxobj.id: + raise MismatchedModel("Mismatched Id") + target.createtime = stxobj.createtime + target.updatetime = stxobj.updatetime + target.hash = stxobj.hash + target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/auditor/pserver_handler.py b/o2ims/service/auditor/pserver_handler.py new file mode 100644 index 0000000..f5f526a --- /dev/null +++ b/o2ims/service/auditor/pserver_handler.py @@ -0,0 +1,117 @@ +# 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 +import uuid +# import json + +from o2ims.domain import commands +from o2ims.domain.stx_object import StxGenericModel +from o2common.service.unit_of_work import AbstractUnitOfWork +from o2ims.domain.resource_type import MismatchedModel +from o2ims.domain.ocloud import Resource, ResourceType + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class InvalidResourceType(Exception): + pass + + +def update_pserver( + cmd: commands.UpdatePserver, + uow: AbstractUnitOfWork +): + stxobj = cmd.data + with uow: + resourcepool = uow.resource_pools.get(cmd.parentid) + + # res = uow.session.execute(select(resourcetype).where( + # resourcetype.c.resourceTypeEnum == stxobj.type)) + res = uow.session.execute( + ''' + SELECT "resourceTypeId", "oCloudId", "name" + FROM resourcetype + WHERE "resourceTypeEnum" = :resource_type_enum + ''', + dict(resource_type_enum=stxobj.type.name) + ) + first = res.first() + if first is None: + resourcetype_id = str(uuid.uuid4()) + uow.resource_types.add(ResourceType( + resourcetype_id, + 'pserver', stxobj.type, + resourcepool.oCloudId)) + else: + resourcetype_id = first['resourceTypeId'] + + resource = uow.resources.get(stxobj.id) + if not resource: + logger.info("add pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + localmodel = create_by(stxobj, cmd.parentid, resourcetype_id) + uow.resources.add(localmodel) + + logger.info("Add the pserver: " + stxobj.id + + ", name: " + stxobj.name) + else: + localmodel = resource + if is_outdated(localmodel, stxobj): + logger.info("update pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + update_by(localmodel, stxobj, cmd.parentid) + uow.resources.update(localmodel) + + logger.info("Update the pserver: " + stxobj.id + + ", name: " + stxobj.name) + uow.commit() + + +def is_outdated(resource: Resource, stxobj: StxGenericModel): + return True if resource.hash != stxobj.hash else False + + +def create_by(stxobj: StxGenericModel, parentid: str, resourcetype_id: str) \ + -> Resource: + # content = json.loads(stxobj.content) + resourcetype_id = resourcetype_id + resourcepool_id = parentid + parent_id = parentid + gAssetId = '' # TODO: global ID + description = "A physical server resource" + resource = Resource(stxobj.id, resourcetype_id, resourcepool_id, + parent_id, gAssetId, stxobj.content, description) + resource.createtime = stxobj.createtime + resource.updatetime = stxobj.updatetime + resource.hash = stxobj.hash + + return resource + + +def update_by(target: Resource, stxobj: StxGenericModel, + parentid: str) -> None: + if target.resourceId != stxobj.id: + raise MismatchedModel("Mismatched Id") + target.createtime = stxobj.createtime + target.updatetime = stxobj.updatetime + target.hash = stxobj.hash + target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/auditor/pserver_if_handler.py b/o2ims/service/auditor/pserver_if_handler.py new file mode 100644 index 0000000..174f709 --- /dev/null +++ b/o2ims/service/auditor/pserver_if_handler.py @@ -0,0 +1,116 @@ +# 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 +import uuid +# import json + +from o2ims.domain import commands +from o2ims.domain.stx_object import StxGenericModel +from o2common.service.unit_of_work import AbstractUnitOfWork +from o2ims.domain.resource_type import MismatchedModel +from o2ims.domain.ocloud import Resource, ResourceType + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class InvalidResourceType(Exception): + pass + + +def update_pserver_if( + cmd: commands.UpdatePserverIf, + uow: AbstractUnitOfWork +): + stxobj = cmd.data + with uow: + p_resource = uow.resources.get(cmd.parentid) + resourcepool = uow.resource_pools.get(p_resource.resourcePoolId) + + res = uow.session.execute( + ''' + SELECT "resourceTypeId", "oCloudId", "name" + FROM resourcetype + WHERE "resourceTypeEnum" = :resource_type_enum + ''', + dict(resource_type_enum=stxobj.type.name) + ) + first = res.first() + if first is None: + resourcetype_id = str(uuid.uuid4()) + uow.resource_types.add(ResourceType( + resourcetype_id, + 'pserver_if', stxobj.type, + resourcepool.oCloudId)) + else: + resourcetype_id = first['resourceTypeId'] + + resource = uow.resources.get(stxobj.id) + if not resource: + logger.info("add the interface of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + localmodel = create_by(stxobj, p_resource, resourcetype_id) + uow.resources.add(localmodel) + + logger.info("Add the interface of pserver: " + stxobj.id + + ", name: " + stxobj.name) + else: + localmodel = resource + if is_outdated(localmodel, stxobj): + logger.info("update interface of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + update_by(localmodel, stxobj, p_resource) + uow.resources.update(localmodel) + + logger.info("Update the interface of pserver: " + stxobj.id + + ", name: " + stxobj.name) + uow.commit() + + +def is_outdated(resource: Resource, stxobj: StxGenericModel): + return True if resource.hash != stxobj.hash else False + + +def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\ + -> Resource: + # content = json.loads(stxobj.content) + resourcetype_id = resourcetype_id + resourcepool_id = parent.resourcePoolId + parent_id = parent.resourceId + gAssetId = '' # TODO: global ID + description = "A interface resource of the physical server" + resource = Resource(stxobj.id, resourcetype_id, resourcepool_id, + parent_id, gAssetId, stxobj.content, description) + resource.createtime = stxobj.createtime + resource.updatetime = stxobj.updatetime + resource.hash = stxobj.hash + + return resource + + +def update_by(target: Resource, stxobj: StxGenericModel, + parentid: str) -> None: + if target.resourceId != stxobj.id: + raise MismatchedModel("Mismatched Id") + target.createtime = stxobj.createtime + target.updatetime = stxobj.updatetime + target.hash = stxobj.hash + target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/auditor/pserver_mem_handler.py b/o2ims/service/auditor/pserver_mem_handler.py new file mode 100644 index 0000000..42bccb7 --- /dev/null +++ b/o2ims/service/auditor/pserver_mem_handler.py @@ -0,0 +1,116 @@ +# 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 +import uuid +# import json + +from o2ims.domain import commands +from o2ims.domain.stx_object import StxGenericModel +from o2common.service.unit_of_work import AbstractUnitOfWork +from o2ims.domain.resource_type import MismatchedModel +from o2ims.domain.ocloud import Resource, ResourceType + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class InvalidResourceType(Exception): + pass + + +def update_pserver_mem( + cmd: commands.UpdatePserverMem, + uow: AbstractUnitOfWork +): + stxobj = cmd.data + with uow: + p_resource = uow.resources.get(cmd.parentid) + resourcepool = uow.resource_pools.get(p_resource.resourcePoolId) + + res = uow.session.execute( + ''' + SELECT "resourceTypeId", "oCloudId", "name" + FROM resourcetype + WHERE "resourceTypeEnum" = :resource_type_enum + ''', + dict(resource_type_enum=stxobj.type.name) + ) + first = res.first() + if first is None: + resourcetype_id = str(uuid.uuid4()) + uow.resource_types.add(ResourceType( + resourcetype_id, + 'pserver_mem', stxobj.type, + resourcepool.oCloudId)) + else: + resourcetype_id = first['resourceTypeId'] + + resource = uow.resources.get(stxobj.id) + if not resource: + logger.info("add the memory of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + localmodel = create_by(stxobj, p_resource, resourcetype_id) + uow.resources.add(localmodel) + + logger.info("Add the memory of pserver: " + stxobj.id + + ", name: " + stxobj.name) + else: + localmodel = resource + if is_outdated(localmodel, stxobj): + logger.info("update memory of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + update_by(localmodel, stxobj, p_resource) + uow.resources.update(localmodel) + + logger.info("Update the memory of pserver: " + stxobj.id + + ", name: " + stxobj.name) + uow.commit() + + +def is_outdated(resource: Resource, stxobj: StxGenericModel): + return True if resource.hash != stxobj.hash else False + + +def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\ + -> Resource: + # content = json.loads(stxobj.content) + resourcetype_id = resourcetype_id + resourcepool_id = parent.resourcePoolId + parent_id = parent.resourceId + gAssetId = '' # TODO: global ID + description = "A memory resource of the physical server" + resource = Resource(stxobj.id, resourcetype_id, resourcepool_id, + parent_id, gAssetId, stxobj.content, description) + resource.createtime = stxobj.createtime + resource.updatetime = stxobj.updatetime + resource.hash = stxobj.hash + + return resource + + +def update_by(target: Resource, stxobj: StxGenericModel, + parentid: str) -> None: + if target.resourceId != stxobj.id: + raise MismatchedModel("Mismatched Id") + target.createtime = stxobj.createtime + target.updatetime = stxobj.updatetime + target.hash = stxobj.hash + target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/auditor/pserver_port_handler.py b/o2ims/service/auditor/pserver_port_handler.py new file mode 100644 index 0000000..df62f14 --- /dev/null +++ b/o2ims/service/auditor/pserver_port_handler.py @@ -0,0 +1,116 @@ +# 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 +import uuid +# import json + +from o2ims.domain import commands +from o2ims.domain.stx_object import StxGenericModel +from o2common.service.unit_of_work import AbstractUnitOfWork +from o2ims.domain.resource_type import MismatchedModel +from o2ims.domain.ocloud import Resource, ResourceType + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class InvalidResourceType(Exception): + pass + + +def update_pserver_port( + cmd: commands.UpdatePserverPort, + uow: AbstractUnitOfWork +): + stxobj = cmd.data + with uow: + p_resource = uow.resources.get(cmd.parentid) + resourcepool = uow.resource_pools.get(p_resource.resourcePoolId) + + res = uow.session.execute( + ''' + SELECT "resourceTypeId", "oCloudId", "name" + FROM resourcetype + WHERE "resourceTypeEnum" = :resource_type_enum + ''', + dict(resource_type_enum=stxobj.type.name) + ) + first = res.first() + if first is None: + resourcetype_id = str(uuid.uuid4()) + uow.resource_types.add(ResourceType( + resourcetype_id, + 'pserver_port', stxobj.type, + resourcepool.oCloudId)) + else: + resourcetype_id = first['resourceTypeId'] + + resource = uow.resources.get(stxobj.id) + if not resource: + logger.info("add the port of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + localmodel = create_by(stxobj, p_resource, resourcetype_id) + uow.resources.add(localmodel) + + logger.info("Add the port of pserver: " + stxobj.id + + ", name: " + stxobj.name) + else: + localmodel = resource + if is_outdated(localmodel, stxobj): + logger.info("update port of pserver:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + update_by(localmodel, stxobj, p_resource) + uow.resources.update(localmodel) + + logger.info("Update the port of pserver: " + stxobj.id + + ", name: " + stxobj.name) + uow.commit() + + +def is_outdated(resource: Resource, stxobj: StxGenericModel): + return True if resource.hash != stxobj.hash else False + + +def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\ + -> Resource: + # content = json.loads(stxobj.content) + resourcetype_id = resourcetype_id + resourcepool_id = parent.resourcePoolId + parent_id = parent.resourceId + gAssetId = '' # TODO: global ID + description = "A port resource of the physical server" + resource = Resource(stxobj.id, resourcetype_id, resourcepool_id, + parent_id, gAssetId, stxobj.content, description) + resource.createtime = stxobj.createtime + resource.updatetime = stxobj.updatetime + resource.hash = stxobj.hash + + return resource + + +def update_by(target: Resource, stxobj: StxGenericModel, + parentid: str) -> None: + if target.resourceId != stxobj.id: + raise MismatchedModel("Mismatched Id") + target.createtime = stxobj.createtime + target.updatetime = stxobj.updatetime + target.hash = stxobj.hash + target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/auditor/resourcepool_handler.py b/o2ims/service/auditor/resourcepool_handler.py new file mode 100644 index 0000000..88cf182 --- /dev/null +++ b/o2ims/service/auditor/resourcepool_handler.py @@ -0,0 +1,101 @@ +# 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 +import json + +from o2ims.domain.stx_object import StxGenericModel +# from dataclasses import asdict +# from typing import List, Dict, Callable, Type +# TYPE_CHECKING +from o2ims.domain import commands +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 ResourcePool +# if TYPE_CHECKING: +# from . import unit_of_work + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class InvalidResourceType(Exception): + pass + + +def update_resourcepool( + cmd: commands.UpdateResourcePool, + uow: AbstractUnitOfWork +): + stxobj = cmd.data + with uow: + resource_pool = uow.resource_pools.get(stxobj.id) + if not resource_pool: + logger.info("add resource pool:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + localmodel = create_by(stxobj, cmd.parentid) + uow.resource_pools.add(localmodel) + + logger.info("Add the resource pool: " + stxobj.id + + ", name: " + stxobj.name) + else: + localmodel = resource_pool + if is_outdated(localmodel, stxobj): + logger.info("update resource pool:" + stxobj.name + + " update_at: " + str(stxobj.updatetime) + + " id: " + str(stxobj.id) + + " hash: " + str(stxobj.hash)) + update_by(localmodel, stxobj, cmd.parentid) + uow.resource_pools.update(localmodel) + + logger.info("Update the resource pool: " + stxobj.id + + ", name: " + stxobj.name) + uow.commit() + + +def is_outdated(resourcepool: ResourcePool, stxobj: StxGenericModel): + return True if resourcepool.hash != stxobj.hash else False + + +def create_by(stxobj: StxGenericModel, parentid: str) -> ResourcePool: + content = json.loads(stxobj.content) + globalcloudId = stxobj.id # to be updated + description = "A Resource Pool" + resourcepool = ResourcePool(stxobj.id, stxobj.name, + content['location'], + parentid, globalcloudId, description) + resourcepool.createtime = stxobj.createtime + resourcepool.updatetime = stxobj.updatetime + resourcepool.hash = stxobj.hash + + return resourcepool + + +def update_by(target: ResourcePool, stxobj: StxGenericModel, + parentid: str) -> None: + if target.resourcePoolId != stxobj.id: + raise MismatchedModel("Mismatched Id") + content = json.loads(stxobj.content) + target.name = stxobj.name + target.location = content['location'] + target.createtime = stxobj.createtime + target.updatetime = stxobj.updatetime + target.hash = stxobj.hash + target.oCloudId = parentid + target.version_number = target.version_number + 1 + target.events = [] diff --git a/o2ims/service/watcher/pserver_cpu_watcher.py b/o2ims/service/watcher/pserver_cpu_watcher.py index cf471a4..51a5a76 100644 --- a/o2ims/service/watcher/pserver_cpu_watcher.py +++ b/o2ims/service/watcher/pserver_cpu_watcher.py @@ -34,8 +34,5 @@ class PServerCpuWatcher(ResourceWatcher): def _probe(self, parent: StxGenericModel): hostid = parent.id newmodels = self._client.list(hostid=hostid) - # for newmodel in newmodels: - # super()._compare_and_update(newmodel) - # return newmodels return [commands.UpdatePserverCpu(data=m, parentid=hostid) for m in newmodels] diff --git a/o2ims/service/watcher/pserver_if_watcher.py b/o2ims/service/watcher/pserver_if_watcher.py new file mode 100644 index 0000000..8bdde89 --- /dev/null +++ b/o2ims/service/watcher/pserver_if_watcher.py @@ -0,0 +1,38 @@ +# 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 o2ims.domain.stx_object import StxGenericModel +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 o2common.service.messagebus import MessageBus + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class PServerIfWatcher(ResourceWatcher): + def __init__(self, client: BaseClient, + bus: MessageBus) -> None: + super().__init__(client, bus) + + def _targetname(self): + return "pserver_if" + + def _probe(self, parent: StxGenericModel): + hostid = parent.id + newmodels = self._client.list(hostid=hostid) + return [commands.UpdatePserverIf(data=m, parentid=hostid) + for m in newmodels] diff --git a/o2ims/service/watcher/pserver_mem_watcher.py b/o2ims/service/watcher/pserver_mem_watcher.py new file mode 100644 index 0000000..ddeb3ef --- /dev/null +++ b/o2ims/service/watcher/pserver_mem_watcher.py @@ -0,0 +1,38 @@ +# 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 o2ims.domain.stx_object import StxGenericModel +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 o2common.service.messagebus import MessageBus + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class PServerMemWatcher(ResourceWatcher): + def __init__(self, client: BaseClient, + bus: MessageBus) -> None: + super().__init__(client, bus) + + def _targetname(self): + return "pserver_mem" + + def _probe(self, parent: StxGenericModel): + hostid = parent.id + newmodels = self._client.list(hostid=hostid) + return [commands.UpdatePserverMem(data=m, parentid=hostid) + for m in newmodels] diff --git a/o2ims/service/watcher/pserver_port_watcher.py b/o2ims/service/watcher/pserver_port_watcher.py new file mode 100644 index 0000000..9ab6c7d --- /dev/null +++ b/o2ims/service/watcher/pserver_port_watcher.py @@ -0,0 +1,38 @@ +# 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 o2ims.domain.stx_object import StxGenericModel +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 o2common.service.messagebus import MessageBus + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class PServerPortWatcher(ResourceWatcher): + def __init__(self, client: BaseClient, + bus: MessageBus) -> None: + super().__init__(client, bus) + + def _targetname(self): + return "pserver_port" + + def _probe(self, parent: StxGenericModel): + hostid = parent.id + newmodels = self._client.list(hostid=hostid) + return [commands.UpdatePserverPort(data=m, parentid=hostid) + for m in newmodels] diff --git a/o2ims/service/watcher/pserver_watcher.py b/o2ims/service/watcher/pserver_watcher.py index ca3e898..8222cc3 100644 --- a/o2ims/service/watcher/pserver_watcher.py +++ b/o2ims/service/watcher/pserver_watcher.py @@ -34,5 +34,5 @@ class PServerWatcher(ResourceWatcher): def _probe(self, parent: StxGenericModel): resourcepoolid = parent.id newmodels = self._client.list(resourcepoolid=resourcepoolid) - return [commands.UpdatePserverCpu(data=m, parentid=resourcepoolid) + return [commands.UpdatePserver(data=m, parentid=resourcepoolid) for m in newmodels] diff --git a/o2ims/views/ocloud_route.py b/o2ims/views/ocloud_route.py index bb4e63f..6d5e8e3 100644 --- a/o2ims/views/ocloud_route.py +++ b/o2ims/views/ocloud_route.py @@ -12,12 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import uuid from flask_restx import Resource from o2ims.views import ocloud_view, api_ims_inventory_v1 from o2common.config import config -from o2ims.domain.ocloud import Subscription from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\ ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO @@ -180,12 +178,8 @@ class SubscriptionsListRouter(Resource): @api_ims_inventory_v1.marshal_with(post_resp, code=201) def post(self): data = api_ims_inventory_v1.payload - sub_uuid = str(uuid.uuid4()) - subscription = Subscription( - sub_uuid, data['callback'], data['consumerSubscriptionId'], - data['filter']) - ocloud_view.subscription_create(subscription, bus.uow) - return {"subscriptionId": sub_uuid}, 201 + result = ocloud_view.subscription_create(data, bus.uow) + return result, 201 @api_ims_inventory_v1.route("/subscriptions/") @@ -208,10 +202,8 @@ class SubscriptionGetDelRouter(Resource): @api_ims_inventory_v1.doc('Delete subscription by ID') @api_ims_inventory_v1.response(204, 'Subscription deleted') def delete(self, subscriptionID): - with bus.uow: - bus.uow.subscriptions.delete(subscriptionID) - bus.uow.commit() - return '', 204 + result = ocloud_view.subscription_delete(subscriptionID, bus.uow) + return result, 204 def configure_namespace(app, bus_new): diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index 9a1a595..9404d9a 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -12,11 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +import uuid from sqlalchemy import select from o2ims.adapter.orm import ocloud, resource, resourcetype, \ resourcepool, deploymentmanager, subscription from o2common.service import unit_of_work +from o2ims.views.ocloud_dto import SubscriptionDTO from o2ims.domain.ocloud import Subscription @@ -115,11 +117,18 @@ def subscription_one(subscriptionId: str, return None if first is None else dict(first) -def subscription_create(subscription: Subscription, +def subscription_create(subscriptionDto: SubscriptionDTO.subscription, uow: unit_of_work.AbstractUnitOfWork): + + sub_uuid = str(uuid.uuid4()) + subscription = Subscription( + sub_uuid, subscriptionDto['callback'], + subscriptionDto['consumerSubscriptionId'], + subscriptionDto['filter']) with uow: uow.subscriptions.add(subscription) uow.commit() + return {"subscriptionId": sub_uuid} def subscription_delete(subscriptionId: str, diff --git a/tests/integration-ocloud/test_clientdriver_stx_sa.py b/tests/integration-ocloud/test_clientdriver_stx_sa.py index 7ece56b..5423953 100644 --- a/tests/integration-ocloud/test_clientdriver_stx_sa.py +++ b/tests/integration-ocloud/test_clientdriver_stx_sa.py @@ -12,15 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys +# import sys +# import logging import pytest from o2common.config import config from o2ims.adapter.clients.ocloud_sa_client import StxSaClientImp from cgtsclient.client import get_client -import logging - @pytest.fixture def real_stx_aio_client(): @@ -32,7 +31,7 @@ def real_stx_aio_client(): def test_get_instanceinfo(real_stx_aio_client): - logger = logging.getLogger(__name__) + # logger = logging.getLogger(__name__) stxclientimp = StxSaClientImp(real_stx_aio_client) assert stxclientimp is not None systeminfo = stxclientimp.getInstanceInfo() @@ -61,6 +60,7 @@ def test_get_pserver(real_stx_aio_client): assert host1 != host2 assert host1.id == host2.id + def test_get_k8s_list(real_stx_aio_client): stxSaClientImp = StxSaClientImp(real_stx_aio_client) assert stxSaClientImp is not None @@ -73,6 +73,7 @@ def test_get_k8s_list(real_stx_aio_client): assert k8s1.name == k8s2.name assert k8s1.id == k8s2.id + def test_get_cpu_list(real_stx_aio_client): stxSaClientImp = StxSaClientImp(real_stx_aio_client) assert stxSaClientImp is not None @@ -85,3 +86,45 @@ def test_get_cpu_list(real_stx_aio_client): cpu2 = stxSaClientImp.getCpu(cpu1.id) assert cpu1 != cpu2 assert cpu1.id == cpu2.id + + +def test_get_mem_list(real_stx_aio_client): + stxSaClientImp = StxSaClientImp(real_stx_aio_client) + assert stxSaClientImp is not None + hostlist = stxSaClientImp.getPserverList() + assert len(hostlist) > 0 + + memlist = stxSaClientImp.getMemList(hostid=hostlist[0].id) + assert len(memlist) > 0 + mem1 = memlist[0] + mem2 = stxSaClientImp.getMem(mem1.id) + assert mem1 != mem2 + assert mem1.id == mem2.id + + +def test_get_if_list(real_stx_aio_client): + stxSaClientImp = StxSaClientImp(real_stx_aio_client) + assert stxSaClientImp is not None + hostlist = stxSaClientImp.getPserverList() + assert len(hostlist) > 0 + + iflist = stxSaClientImp.getIfList(hostid=hostlist[0].id) + assert len(iflist) > 0 + if1 = iflist[0] + if2 = stxSaClientImp.getIf(if1.id) + assert if1 != if2 + assert if1.id == if2.id + + +def test_get_port_list(real_stx_aio_client): + stxSaClientImp = StxSaClientImp(real_stx_aio_client) + assert stxSaClientImp is not None + hostlist = stxSaClientImp.getPserverList() + assert len(hostlist) > 0 + + portlist = stxSaClientImp.getPortList(hostid=hostlist[0].id) + assert len(portlist) > 0 + port1 = portlist[0] + port2 = stxSaClientImp.getPort(port1.id) + assert port1 != port2 + assert port1.id == port2.id diff --git a/tests/unit/test_watcher.py b/tests/unit/test_watcher.py index 8c1afac..c4b134d 100644 --- a/tests/unit/test_watcher.py +++ b/tests/unit/test_watcher.py @@ -77,7 +77,6 @@ class FakeOcloudRepo(OcloudRepository): ocloud1.update_by(ocloud) - class FakeStxObjRepo(StxObjectRepository): def __init__(self): super().__init__() @@ -129,8 +128,8 @@ class FakeUnitOfWork(AbstractUnitOfWork): def create_fake_bus(uow): def update_ocloud( - cmd: commands.UpdateOCloud, - uow: AbstractUnitOfWork): + cmd: commands.UpdateOCloud, + uow: AbstractUnitOfWork): return fakeuow = FakeUnitOfWork() @@ -169,7 +168,7 @@ def test_watchers_worker(): def _targetname(self): return "fakeocloudwatcher" - def _probe(self, parent: object=None): + def _probe(self, parent: object = None): # import pdb; pdb.set_trace() self.fakeOcloudWatcherCounter += 1 # hacking to stop the blocking sched task @@ -177,7 +176,6 @@ def test_watchers_worker(): testedworker.stop() return [] - # fakeRepo = FakeOcloudRepo() fakeuow = FakeUnitOfWork() bus = create_fake_bus(fakeuow)