366b77fe68e5f6e9768eeaa680d638ae4e9c095c
[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, 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
29 # if TYPE_CHECKING:
30 #     from . import unit_of_work
31
32 from o2common.helper import o2logging
33 logger = o2logging.get_logger(__name__)
34
35
36 class InvalidResourceType(Exception):
37     pass
38
39
40 def update_resourcepool(
41     cmd: commands.UpdateResourcePool,
42     uow: AbstractUnitOfWork
43 ):
44     stxobj = cmd.data
45     with uow:
46         resource_pool = uow.resource_pools.get(stxobj.id)
47         if not resource_pool:
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)
54
55             logger.info("Add the resource pool: " + stxobj.id
56                         + ", name: " + stxobj.name)
57         else:
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)
66
67             logger.info("Update the resource pool: " + stxobj.id
68                         + ", name: " + stxobj.name)
69         uow.commit()
70
71
72 def is_outdated(resourcepool: ResourcePool, stxobj: StxGenericModel):
73     return True if resourcepool.hash != stxobj.hash else False
74
75
76 def create_by(stxobj: StxGenericModel, parentid: str) -> ResourcePool:
77     content = json.loads(stxobj.content)
78     globalLocationId = ''  # to be updated
79     description = "A Resource Pool"
80     location = content['location'] if content['location'] is not None else ''
81     resourcepool = ResourcePool(stxobj.id, stxobj.name,
82                                 location,
83                                 parentid, globalLocationId, description)
84     resourcepool.createtime = stxobj.createtime
85     resourcepool.updatetime = stxobj.updatetime
86     resourcepool.hash = stxobj.hash
87     resourcepool.events.append(events.ResourcePoolChanged(
88         id=stxobj.id,
89         notificationEventType=NotificationEventEnum.CREATE,
90         updatetime=stxobj.updatetime
91     ))
92     return resourcepool
93
94
95 def update_by(target: ResourcePool, stxobj: StxGenericModel,
96               parentid: str) -> None:
97     if target.resourcePoolId != stxobj.id:
98         raise MismatchedModel("Mismatched Id")
99     content = json.loads(stxobj.content)
100     target.name = stxobj.name
101     target.location = content['location'] if content['location'] is not None \
102         else ''
103     target.createtime = stxobj.createtime
104     target.updatetime = stxobj.updatetime
105     target.hash = stxobj.hash
106     target.oCloudId = parentid
107     target.version_number = target.version_number + 1
108     target.events.append(events.ResourcePoolChanged(
109         id=stxobj.id,
110         notificationEventType=NotificationEventEnum.MODIFY,
111         updatetime=stxobj.updatetime
112     ))