Fix INF-352 Add aggregate resource types
[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", "oCloudId", "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             uow.resource_types.add(ResourceType(
58                 resourcetype_id,
59                 res_type_name, stxobj.type,
60                 resourcepool.oCloudId,
61                 description='The network Aggregate resource type'))
62         else:
63             resourcetype_id = first['resourceTypeId']
64
65         resource = uow.resources.get(stxobj.id)
66         if not resource:
67             logger.info("Add network aggregate:" + stxobj.name
68                         + " update_at: " + str(stxobj.updatetime)
69                         + " id: " + str(stxobj.id)
70                         + " hash: " + str(stxobj.hash))
71             localmodel = create_by(stxobj, cmd.parentid, resourcetype_id)
72             uow.resources.add(localmodel)
73
74             logger.info("Add the network aggregate: " + stxobj.id
75                         + ", name: " + stxobj.name)
76         else:
77             localmodel = resource
78             if is_outdated(localmodel, stxobj):
79                 logger.info("update network aggregate:" + stxobj.name
80                             + " update_at: " + str(stxobj.updatetime)
81                             + " id: " + str(stxobj.id)
82                             + " hash: " + str(stxobj.hash))
83                 update_by(localmodel, stxobj, cmd.parentid)
84                 uow.resources.update(localmodel)
85
86             logger.info("Update the network aggregate: " + 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, parentid: str, resourcetype_id: str) \
96         -> Resource:
97     # content = json.loads(stxobj.content)
98     resourcetype_id = resourcetype_id
99     resourcepool_id = parentid
100     parent_id = None  # the root of the resource has no parent id
101     gAssetId = ''  # TODO: global ID
102     description = "%s : A Network Aggregate server resource" % stxobj.name
103     resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
104                         stxobj.name, parent_id, gAssetId, stxobj.content,
105                         description)
106     resource.createtime = stxobj.createtime
107     resource.updatetime = stxobj.updatetime
108     resource.hash = stxobj.hash
109
110     return resource
111
112
113 def update_by(target: Resource, stxobj: StxGenericModel,
114               parentid: str) -> None:
115     if target.resourceId != stxobj.id:
116         raise MismatchedModel("Mismatched Id")
117     target.createtime = stxobj.createtime
118     target.updatetime = stxobj.updatetime
119     target.hash = stxobj.hash
120     target.version_number = target.version_number + 1
121     target.events.append(events.ResourceChanged(
122         id=stxobj.id,
123         resourcePoolId=target.resourcePoolId,
124         notificationEventType=NotificationEventEnum.MODIFY,
125         updatetime=stxobj.updatetime
126     ))