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
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
29 # from . import unit_of_work
31 from o2common.helper import o2logging
32 logger = o2logging.get_logger(__name__)
35 class InvalidResourceType(Exception):
39 def update_resourcepool(
40 cmd: commands.UpdateResourcePool,
41 uow: AbstractUnitOfWork
45 resource_pool = uow.resource_pools.get(stxobj.id)
47 logger.info("add resource pool:" + stxobj.name
48 + " update_at: " + str(stxobj.updatetime)
49 + " id: " + str(stxobj.id)
50 + " hash: " + str(stxobj.hash))
51 localmodel = create_by(stxobj, cmd.parentid)
52 uow.resource_pools.add(localmodel)
54 logger.info("Add the resource pool: " + stxobj.id
55 + ", name: " + stxobj.name)
57 localmodel = resource_pool
58 if is_outdated(localmodel, stxobj):
59 logger.info("update resource pool:" + stxobj.name
60 + " update_at: " + str(stxobj.updatetime)
61 + " id: " + str(stxobj.id)
62 + " hash: " + str(stxobj.hash))
63 update_by(localmodel, stxobj, cmd.parentid)
64 uow.resource_pools.update(localmodel)
66 logger.info("Update the resource pool: " + stxobj.id
67 + ", name: " + stxobj.name)
71 def is_outdated(resourcepool: ResourcePool, stxobj: StxGenericModel):
72 return True if resourcepool.hash != stxobj.hash else False
75 def create_by(stxobj: StxGenericModel, parentid: str) -> ResourcePool:
76 content = json.loads(stxobj.content)
77 globalcloudId = stxobj.id # to be updated
78 description = "A Resource Pool"
79 resourcepool = ResourcePool(stxobj.id, stxobj.name,
81 parentid, globalcloudId, description)
82 resourcepool.createtime = stxobj.createtime
83 resourcepool.updatetime = stxobj.updatetime
84 resourcepool.hash = stxobj.hash
89 def update_by(target: ResourcePool, stxobj: StxGenericModel,
90 parentid: str) -> None:
91 if target.resourcePoolId != stxobj.id:
92 raise MismatchedModel("Mismatched Id")
93 content = json.loads(stxobj.content)
94 target.name = stxobj.name
95 target.location = content['location']
96 target.createtime = stxobj.createtime
97 target.updatetime = stxobj.updatetime
98 target.hash = stxobj.hash
99 target.oCloudId = parentid
100 target.version_number = target.version_number + 1