Fix INF-328 and INF-373 the resource change and update issue
[pti/o2.git] / o2ims / service / auditor / pserver_cpu_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
19 from o2common.service.unit_of_work import AbstractUnitOfWork
20 from o2ims.domain import commands, events
21 from o2ims.domain.ocloud import ResourceType
22 from o2ims.domain.subscription_obj import NotificationEventEnum
23 from o2ims.service.auditor.pserver_res_handler import is_outdated, \
24     create_by, update_by
25
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
28
29
30 def update_pserver_cpu(
31     cmd: commands.UpdatePserverCpu,
32     uow: AbstractUnitOfWork
33 ):
34     stxobj = cmd.data
35     with uow:
36         p_resource = uow.resources.get(cmd.parentid)
37         # resourcepool = uow.resource_pools.get(p_resource.resourcePoolId)
38
39         res = uow.session.execute(
40             '''
41             SELECT "resourceTypeId", "name"
42             FROM "resourceType"
43             WHERE "resourceTypeEnum" = :resource_type_enum
44             ''',
45             dict(resource_type_enum=stxobj.type.name)
46         )
47         first = res.first()
48         if first is None:
49             res_type_name = 'pserver_cpu'
50             resourcetype_id = str(uuid.uuid3(
51                 uuid.NAMESPACE_URL, res_type_name))
52             res_type = ResourceType(
53                 resourcetype_id,
54                 res_type_name, stxobj.type,
55                 description='A CPU resource type of the Physical Server')
56             dict_id = str(uuid.uuid3(
57                 uuid.NAMESPACE_URL,
58                 str(f"{res_type_name}_alarmdictionary")))
59             alarm_dictionary = uow.alarm_dictionaries.get(dict_id)
60             if alarm_dictionary:
61                 res_type.alarmDictionary = alarm_dictionary
62             res_type.events.append(events.ResourceTypeChanged(
63                 id=res_type.resourceTypeId,
64                 notificationEventType=NotificationEventEnum.CREATE,
65                 updatetime=stxobj.updatetime))
66             uow.resource_types.add(res_type)
67         else:
68             resourcetype_id = first['resourceTypeId']
69
70         resource = uow.resources.get(stxobj.id)
71         if not resource:
72             logger.info("add the cpu of pserver:" + stxobj.name
73                         + " update_at: " + str(stxobj.updatetime)
74                         + " id: " + str(stxobj.id)
75                         + " hash: " + str(stxobj.hash))
76             localmodel = create_by(stxobj, p_resource, resourcetype_id)
77             uow.resources.add(localmodel)
78
79             logger.info("Add the cpu of pserver: " + stxobj.id
80                         + ", name: " + stxobj.name)
81         else:
82             localmodel = resource
83             if is_outdated(localmodel, stxobj):
84                 logger.info("update cpu of pserver:" + stxobj.name
85                             + " update_at: " + str(stxobj.updatetime)
86                             + " id: " + str(stxobj.id)
87                             + " hash: " + str(stxobj.hash))
88                 update_by(localmodel, stxobj, p_resource)
89                 uow.resources.update(localmodel)
90
91             logger.info("Update the cpu of pserver: " + stxobj.id
92                         + ", name: " + stxobj.name)
93         uow.commit()