Add Accelerator resource watcher; fix bug of the alarm dictionary
[pti/o2.git] / o2ims / service / auditor / pserver_acc_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 uuid
18 # import json
19
20 from o2ims.domain import commands, events
21 from o2ims.domain.stx_object import StxGenericModel
22 from o2ims.domain.subscription_obj import NotificationEventEnum
23 from o2common.service.unit_of_work import AbstractUnitOfWork
24 from o2ims.domain.resource_type import MismatchedModel
25 from o2ims.domain.ocloud import Resource, ResourceType
26
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
29
30
31 class InvalidResourceType(Exception):
32     pass
33
34
35 def update_pserver_acc(
36     cmd: commands.UpdatePserverAcc,
37     uow: AbstractUnitOfWork
38 ):
39     stxobj = cmd.data
40     with uow:
41         p_resource = uow.resources.get(cmd.parentid)
42         resourcepool = uow.resource_pools.get(p_resource.resourcePoolId)
43
44         res = uow.session.execute(
45             '''
46             SELECT "resourceTypeId", "oCloudId", "name"
47             FROM "resourceType"
48             WHERE "resourceTypeEnum" = :resource_type_enum
49             ''',
50             dict(resource_type_enum=stxobj.type.name)
51         )
52         first = res.first()
53         if first is None:
54             res_type_name = 'pserver_acc'
55             resourcetype_id = str(uuid.uuid3(
56                 uuid.NAMESPACE_URL, res_type_name))
57             uow.resource_types.add(ResourceType(
58                 resourcetype_id,
59                 res_type_name, stxobj.type,
60                 resourcepool.oCloudId,
61                 description='An Accelerator resource type of Physical Server'))
62         else:
63             resourcetype_id = first['resourceTypeId']
64
65         resource = uow.resources.get(stxobj.id)
66         if not resource:
67             logger.info("add the accelerator  of pserver:" + stxobj.name
68                         + " update_at: " + str(stxobj.updatetime)
69                         + " id: " + str(stxobj.id)
70                         + " hash: " + str(stxobj.hash))
71             localmodel = create_by(stxobj, p_resource, resourcetype_id)
72             uow.resources.add(localmodel)
73
74             logger.info("Add the accelerator of pserver: " + stxobj.id
75                         + ", name: " + stxobj.name)
76         else:
77             localmodel = resource
78             if is_outdated(localmodel, stxobj):
79                 logger.info("update accelerator of pserver:" + stxobj.name
80                             + " update_at: " + str(stxobj.updatetime)
81                             + " id: " + str(stxobj.id)
82                             + " hash: " + str(stxobj.hash))
83                 update_by(localmodel, stxobj, p_resource)
84                 uow.resources.update(localmodel)
85
86             logger.info("Update the accelerator of pserver: " + stxobj.id
87                         + ", name: " + stxobj.name)
88         uow.commit()
89
90
91 def is_outdated(resource: Resource, stxobj: StxGenericModel):
92     return True if resource.hash != stxobj.hash else False
93
94
95 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
96         -> Resource:
97     # content = json.loads(stxobj.content)
98     resourcetype_id = resourcetype_id
99     resourcepool_id = parent.resourcePoolId
100     parent_id = parent.resourceId
101     gAssetId = ''  # TODO: global ID
102     description = "%s : An Accelerator resource of the physical server"\
103         % stxobj.name
104     resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
105                         stxobj.name, parent_id, gAssetId, stxobj.content,
106                         description)
107     resource.createtime = stxobj.createtime
108     resource.updatetime = stxobj.updatetime
109     resource.hash = stxobj.hash
110
111     return resource
112
113
114 def update_by(target: Resource, stxobj: StxGenericModel,
115               parentid: str) -> None:
116     if target.resourceId != stxobj.id:
117         raise MismatchedModel("Mismatched Id")
118     target.createtime = stxobj.createtime
119     target.updatetime = stxobj.updatetime
120     target.hash = stxobj.hash
121     target.version_number = target.version_number + 1
122     target.events.append(events.ResourceChanged(
123         id=stxobj.id,
124         resourcePoolId=target.resourcePoolId,
125         notificationEventType=NotificationEventEnum.MODIFY,
126         updatetime=stxobj.updatetime
127     ))