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