Update model of resourcetype and set empty for the gLocationId of the resourcepool...
[pti/o2.git] / o2ims / service / auditor / pserver_port_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 o2common.service.unit_of_work import AbstractUnitOfWork
23 from o2ims.domain.resource_type import MismatchedModel
24 from o2ims.domain.ocloud import Resource, ResourceType
25 from o2ims.domain.subscription_obj import NotificationEventEnum
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_port(
36     cmd: commands.UpdatePserverIfPort,
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             resourcetype_id = str(uuid.uuid4())
55             uow.resource_types.add(ResourceType(
56                 resourcetype_id,
57                 'pserver_if_port', stxobj.type,
58                 resourcepool.oCloudId))
59             res_type_name = 'pserver_if_port'
60             resourcetype_id = str(uuid.uuid3(
61                 uuid.NAMESPACE_URL, res_type_name))
62             uow.resource_types.add(ResourceType(
63                 resourcetype_id,
64                 res_type_name, stxobj.type,
65                 resourcepool.oCloudId,
66                 description='A Port resource type of Physical Server'))
67         else:
68             resourcetype_id = first['resourceTypeId']
69
70         resource = uow.resources.get(stxobj.id)
71         if not resource:
72             logger.info("add the port of pserver interface:" + 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 port of pserver interface: " + stxobj.id
80                         + ", name: " + stxobj.name)
81         else:
82             localmodel = resource
83             if is_outdated(localmodel, stxobj):
84                 logger.info("update port of pserver interface:" + 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 port of pserver interface: " + stxobj.id
92                         + ", name: " + stxobj.name)
93         uow.commit()
94
95
96 def is_outdated(resource: Resource, stxobj: StxGenericModel):
97     return True if resource.hash != stxobj.hash else False
98
99
100 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
101         -> Resource:
102     # content = json.loads(stxobj.content)
103     resourcetype_id = resourcetype_id
104     resourcepool_id = parent.resourcePoolId
105     parent_id = parent.resourceId
106     gAssetId = ''  # TODO: global ID
107     description = "%s : A port resource of the interface"\
108         % stxobj.name
109     resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
110                         stxobj.name, parent_id, gAssetId, stxobj.content,
111                         description)
112     resource.createtime = stxobj.createtime
113     resource.updatetime = stxobj.updatetime
114     resource.hash = stxobj.hash
115
116     return resource
117
118
119 def update_by(target: Resource, stxobj: StxGenericModel,
120               parentid: str) -> None:
121     if target.resourceId != stxobj.id:
122         raise MismatchedModel("Mismatched Id")
123     target.createtime = stxobj.createtime
124     target.updatetime = stxobj.updatetime
125     target.hash = stxobj.hash
126     target.version_number = target.version_number + 1
127     target.events.append(events.ResourceChanged(
128         id=stxobj.id,
129         resourcePoolId=target.resourcePoolId,
130         notificationEventType=NotificationEventEnum.MODIFY,
131         updatetime=stxobj.updatetime
132     ))