Add auditor for resource pool, pserver and cpu/memory/port/interface for pserver...
[pti/o2.git] / o2ims / service / auditor / resourcepool_handler.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # pylint: disable=unused-argument
16 from __future__ import annotations
17 import json
18
19 from o2ims.domain.stx_object import StxGenericModel
20 # from dataclasses import asdict
21 # from typing import List, Dict, Callable, Type
22 # TYPE_CHECKING
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
28 # if TYPE_CHECKING:
29 #     from . import unit_of_work
30
31 from o2common.helper import o2logging
32 logger = o2logging.get_logger(__name__)
33
34
35 class InvalidResourceType(Exception):
36     pass
37
38
39 def update_resourcepool(
40     cmd: commands.UpdateResourcePool,
41     uow: AbstractUnitOfWork
42 ):
43     stxobj = cmd.data
44     with uow:
45         resource_pool = uow.resource_pools.get(stxobj.id)
46         if not resource_pool:
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)
53
54             logger.info("Add the resource pool: " + stxobj.id
55                         + ", name: " + stxobj.name)
56         else:
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)
65
66             logger.info("Update the resource pool: " + stxobj.id
67                         + ", name: " + stxobj.name)
68         uow.commit()
69
70
71 def is_outdated(resourcepool: ResourcePool, stxobj: StxGenericModel):
72     return True if resourcepool.hash != stxobj.hash else False
73
74
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,
80                                 content['location'],
81                                 parentid, globalcloudId, description)
82     resourcepool.createtime = stxobj.createtime
83     resourcepool.updatetime = stxobj.updatetime
84     resourcepool.hash = stxobj.hash
85
86     return resourcepool
87
88
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
101     target.events = []