Fix INF-378 inventory subscription filter not take effect as expected
[pti/o2.git] / o2ims / service / auditor / agg_network_handler.py
1 # Copyright (C) 2022 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_network_aggregate(
37     cmd: commands.UpdateNetworkAgg,
38     uow: AbstractUnitOfWork,
39     publish: Callable
40 ):
41     stxobj = cmd.data
42     with uow:
43         res = uow.session.execute(
44             '''
45             SELECT "resourceTypeId", "name"
46             FROM "resourceType"
47             WHERE "resourceTypeEnum" = :resource_type_enum
48             ''',
49             dict(resource_type_enum=stxobj.type.name)
50         )
51         first = res.first()
52         if first is None:
53             # resourcepool = uow.resource_pools.get(cmd.parentid)
54             res_type_name = 'network_aggregate'
55             resourcetype_id = str(uuid.uuid3(
56                 uuid.NAMESPACE_URL, res_type_name))
57             res_type = ResourceType(
58                 resourcetype_id,
59                 res_type_name, stxobj.type,
60                 description='The network Aggregate resource type')
61             dict_id = str(uuid.uuid3(
62                 uuid.NAMESPACE_URL,
63                 str(f"{res_type_name}_alarmdictionary")))
64             alarm_dictionary = uow.alarm_dictionaries.get(dict_id)
65             if alarm_dictionary:
66                 res_type.alarmDictionary = alarm_dictionary
67             uow.resource_types.add(res_type)
68         else:
69             resourcetype_id = first['resourceTypeId']
70
71         resource = uow.resources.get(stxobj.id)
72         if not resource:
73             logger.info("Add network aggregate:" + stxobj.name
74                         + " update_at: " + str(stxobj.updatetime)
75                         + " id: " + str(stxobj.id)
76                         + " hash: " + str(stxobj.hash))
77             localmodel = create_by(stxobj, cmd.parentid, resourcetype_id)
78             uow.resources.add(localmodel)
79
80             logger.info("Add the network aggregate: " + stxobj.id
81                         + ", name: " + stxobj.name)
82         else:
83             localmodel = resource
84             if is_outdated(localmodel, stxobj):
85                 logger.info("update network aggregate:" + stxobj.name
86                             + " update_at: " + str(stxobj.updatetime)
87                             + " id: " + str(stxobj.id)
88                             + " hash: " + str(stxobj.hash))
89                 update_by(localmodel, stxobj, cmd.parentid)
90                 uow.resources.update(localmodel)
91
92             logger.info("Update the network aggregate: " + stxobj.id
93                         + ", name: " + stxobj.name)
94         uow.commit()
95
96
97 def is_outdated(resource: Resource, stxobj: StxGenericModel):
98     return True if resource.hash != stxobj.hash else False
99
100
101 def create_by(stxobj: StxGenericModel, parentid: str, resourcetype_id: str) \
102         -> Resource:
103     # content = json.loads(stxobj.content)
104     resourcetype_id = resourcetype_id
105     resourcepool_id = parentid
106     parent_id = None  # the root of the resource has no parent id
107     gAssetId = ''  # TODO: global ID
108     description = "%s : A Network Aggregate server resource" % stxobj.name
109     resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
110                         parent_id, gAssetId, stxobj.content, description)
111     resource.createtime = stxobj.createtime
112     resource.updatetime = stxobj.updatetime
113     resource.hash = stxobj.hash
114
115     return resource
116
117
118 def update_by(target: Resource, stxobj: StxGenericModel,
119               parentid: str) -> None:
120     if target.resourceId != stxobj.id:
121         raise MismatchedModel("Mismatched Id")
122     target.createtime = stxobj.createtime
123     target.updatetime = stxobj.updatetime
124     target.hash = stxobj.hash
125     target.version_number = target.version_number + 1
126     target.events.append(events.ResourceChanged(
127         id=stxobj.id,
128         resourcePoolId=target.resourcePoolId,
129         notificationEventType=NotificationEventEnum.MODIFY,
130         updatetime=stxobj.updatetime
131     ))