1 # Copyright (C) 2021 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # pylint: disable=unused-argument
16 from __future__ import annotations
19 from o2ims.domain.stx_object import StxGenericModel
20 # from dataclasses import asdict
21 # from typing import List, Dict, Callable, Type
23 from o2ims.domain import commands, events
24 from o2common.service.unit_of_work import AbstractUnitOfWork
25 # from o2ims.domain.resource_type import InvalidOcloudState
26 from o2ims.domain.resource_type import MismatchedModel
27 from o2ims.domain.ocloud import ResourcePool
28 from o2ims.domain.subscription_obj import NotificationEventEnum
30 # from . import unit_of_work
32 from o2common.helper import o2logging
33 logger = o2logging.get_logger(__name__)
36 class InvalidResourceType(Exception):
40 def update_resourcepool(
41 cmd: commands.UpdateResourcePool,
42 uow: AbstractUnitOfWork
46 resource_pool = uow.resource_pools.get(stxobj.id)
48 logger.info("add resource pool:" + stxobj.name
49 + " update_at: " + str(stxobj.updatetime)
50 + " id: " + str(stxobj.id)
51 + " hash: " + str(stxobj.hash))
52 localmodel = create_by(stxobj, cmd.parentid)
53 uow.resource_pools.add(localmodel)
55 logger.info("Add the resource pool: " + stxobj.id
56 + ", name: " + stxobj.name)
58 localmodel = resource_pool
59 if is_outdated(localmodel, stxobj):
60 logger.info("update resource pool:" + stxobj.name
61 + " update_at: " + str(stxobj.updatetime)
62 + " id: " + str(stxobj.id)
63 + " hash: " + str(stxobj.hash))
64 update_by(localmodel, stxobj, cmd.parentid)
65 uow.resource_pools.update(localmodel)
67 logger.info("Update the resource pool: " + stxobj.id
68 + ", name: " + stxobj.name)
72 def is_outdated(resourcepool: ResourcePool, stxobj: StxGenericModel):
73 return True if resourcepool.hash != stxobj.hash else False
76 def create_by(stxobj: StxGenericModel, parentid: str) -> ResourcePool:
77 content = json.loads(stxobj.content)
78 globalcloudId = stxobj.id # to be updated
79 description = "A Resource Pool"
80 resourcepool = ResourcePool(stxobj.id, stxobj.name,
82 parentid, globalcloudId, description)
83 resourcepool.createtime = stxobj.createtime
84 resourcepool.updatetime = stxobj.updatetime
85 resourcepool.hash = stxobj.hash
90 def update_by(target: ResourcePool, stxobj: StxGenericModel,
91 parentid: str) -> None:
92 if target.resourcePoolId != stxobj.id:
93 raise MismatchedModel("Mismatched Id")
94 content = json.loads(stxobj.content)
95 target.name = stxobj.name
96 target.location = content['location']
97 target.createtime = stxobj.createtime
98 target.updatetime = stxobj.updatetime
99 target.hash = stxobj.hash
100 target.oCloudId = parentid
101 target.version_number = target.version_number + 1
102 target.events.append(events.ResourcePoolChanged(
104 notificationEventType=NotificationEventEnum.MODIFY,
105 updatetime=stxobj.updatetime