1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # pylint: disable=unused-argument
16 from __future__ import annotations
19 from typing import Callable
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
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
32 class InvalidResourceType(Exception):
37 cmd: commands.UpdatePserver,
38 uow: AbstractUnitOfWork,
43 # resourcepool = uow.resource_pools.get(cmd.parentid)
45 # res = uow.session.execute(select(resourcetype).where(
46 # resourcetype.c.resourceTypeEnum == stxobj.type))
47 res = uow.session.execute(
49 SELECT "resourceTypeId", "name"
51 WHERE "resourceTypeEnum" = :resource_type_enum
53 dict(resource_type_enum=stxobj.type.name)
57 res_type_name = 'pserver'
58 resourcetype_id = str(uuid.uuid3(
59 uuid.NAMESPACE_URL, res_type_name))
60 res_type = ResourceType(
62 res_type_name, stxobj.type,
63 description='The Physical Server resource type')
64 dict_id = str(uuid.uuid3(
66 str(f"{res_type_name}_alarmdictionary")))
67 alarm_dictionary = uow.alarm_dictionaries.get(dict_id)
69 res_type.alarmDictionary = alarm_dictionary
70 res_type.events.append(events.ResourceTypeChanged(
71 id=res_type.resourceTypeId,
72 notificationEventType=NotificationEventEnum.CREATE,
73 updatetime=stxobj.updatetime))
74 uow.resource_types.add(res_type)
76 resourcetype_id = first['resourceTypeId']
78 resource = uow.resources.get(stxobj.id)
80 logger.info("add pserver:" + stxobj.name
81 + " update_at: " + str(stxobj.updatetime)
82 + " id: " + str(stxobj.id)
83 + " hash: " + str(stxobj.hash))
84 localmodel = create_by(stxobj, cmd.parentid, resourcetype_id)
85 uow.resources.add(localmodel)
87 logger.info("Add the pserver: " + stxobj.id
88 + ", name: " + stxobj.name)
91 if is_outdated(localmodel, stxobj):
92 logger.info("update pserver:" + stxobj.name
93 + " update_at: " + str(stxobj.updatetime)
94 + " id: " + str(stxobj.id)
95 + " hash: " + str(stxobj.hash))
96 update_by(localmodel, stxobj, cmd.parentid)
97 uow.resources.update(localmodel)
99 logger.info("Update the pserver: " + stxobj.id
100 + ", name: " + stxobj.name)
104 def is_outdated(resource: Resource, stxobj: StxGenericModel):
105 return True if resource.hash != stxobj.hash else False
108 def create_by(stxobj: StxGenericModel, parentid: str, resourcetype_id: str) \
110 # content = json.loads(stxobj.content)
111 resourcetype_id = resourcetype_id
112 resourcepool_id = parentid
113 parent_id = None # the root of the resource has no parent id
114 gAssetId = '' # TODO: global ID
115 # description = "%s : A physical server resource" % stxobj.name
116 content = json.loads(stxobj.content)
118 "hostname", "personality", "id", "mgmt_ip", "mgmt_mac",
119 "software_load", "capabilities",
120 "operational", "availability", "administrative",
121 "boot_device", "rootfs_device", "install_state", "subfunctions",
122 "clock_synchronization", "max_cpu_mhz_allowed"
125 filter(lambda item: item[0] in selected_keys, content.items()))
126 extensions = json.dumps(filtered)
127 description = ";".join([f"{k}:{v}" for k, v in filtered.items()])
128 resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
129 parent_id, gAssetId, stxobj.content, description,
131 resource.createtime = stxobj.createtime
132 resource.updatetime = stxobj.updatetime
133 resource.hash = stxobj.hash
135 resource.events.append(events.ResourceChanged(
137 resourcePoolId=resource.resourcePoolId,
138 notificationEventType=NotificationEventEnum.CREATE,
139 updatetime=stxobj.updatetime
145 def update_by(target: Resource, stxobj: StxGenericModel,
146 parentid: str) -> None:
147 if target.resourceId != stxobj.id:
148 raise MismatchedModel("Mismatched Id")
149 target.createtime = stxobj.createtime
150 target.updatetime = stxobj.updatetime
151 target.hash = stxobj.hash
152 target.version_number = target.version_number + 1
153 target.events.append(events.ResourceChanged(
155 resourcePoolId=target.resourcePoolId,
156 notificationEventType=NotificationEventEnum.MODIFY,
157 updatetime=stxobj.updatetime