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
20 from o2ims.domain import commands, events
21 from o2ims.domain.stx_object import StxGenericModel
22 from o2ims.domain.subscription_obj import NotificationEventEnum
23 from o2common.service.unit_of_work import AbstractUnitOfWork
24 from o2ims.domain.resource_type import MismatchedModel
25 from o2ims.domain.ocloud import DeploymentManager
26 from o2common.config import config
28 # from . import unit_of_work
30 from o2common.helper import o2logging
31 logger = o2logging.get_logger(__name__)
34 class InvalidResourceType(Exception):
39 cmd: commands.UpdateDms,
40 uow: AbstractUnitOfWork
44 dms = uow.deployment_managers.get(stxobj.id)
46 logger.info("add dms: " + stxobj.name
47 + " update_at: " + str(stxobj.updatetime)
48 + " id: " + str(stxobj.id)
49 + " hash: " + str(stxobj.hash))
50 # ocloud = uow.oclouds.get(cmd.parent.oCloudId)
51 localmodel = create_by(stxobj, cmd.parentid)
52 uow.deployment_managers.add(localmodel)
54 logger.info("Add a dms: " + stxobj.id
55 + ", name: " + stxobj.name)
58 if is_outdated(localmodel, stxobj):
59 logger.info("update a dms: " + stxobj.name
60 + " update_at: " + str(stxobj.updatetime)
61 + " id: " + str(stxobj.id)
62 + " hash: " + str(stxobj.hash))
63 update_by(localmodel, stxobj, cmd.parentid)
64 uow.deployment_managers.update(localmodel)
66 logger.info("Update a dms: " + stxobj.id
67 + ", name: " + stxobj.name)
71 def is_outdated(ocloud: DeploymentManager, stxobj: StxGenericModel):
72 # if stxobj.updatetime:
73 # return True if Ocloud.updatetime < stxobj.updatetime else False
75 return True if ocloud.hash != stxobj.hash else False
78 def create_by(stxobj: StxGenericModel, parentid: str) -> DeploymentManager:
79 dmsendpoint = config.get_api_url() +\
80 config.get_o2dms_api_base() + "/" + stxobj.id
83 supportedLocations = ''
86 profile = _convert_content(stxobj.content)
87 localmodel = DeploymentManager(
88 stxobj.id, stxobj.name, ocloudid, dmsendpoint, description,
89 supportedLocations, capabilities, capacity, profile)
90 localmodel.createtime = stxobj.createtime
91 localmodel.updatetime = stxobj.updatetime
92 localmodel.hash = stxobj.hash
94 localmodel.events.append(events.DmsChanged(
96 notificationEventType=NotificationEventEnum.CREATE,
97 updatetime=stxobj.updatetime
103 def update_by(target: DeploymentManager, stxobj: StxGenericModel,
104 parentid: str) -> None:
105 if target.deploymentManagerId != stxobj.id:
106 raise MismatchedModel("Mismatched Id")
107 target.name = stxobj.name
108 target.createtime = stxobj.createtime
109 target.updatetime = stxobj.updatetime
110 target.hash = stxobj.hash
111 target.oCloudId = parentid
112 target.version_number = target.version_number + 1
113 target.profile = _convert_content(stxobj.content)
115 target.events.append(events.DmsChanged(
117 notificationEventType=NotificationEventEnum.MODIFY,
118 updatetime=stxobj.updatetime
122 def _convert_content(stxobj_content: str):
123 # Convert api retrun content to dict object
124 content = json.loads(stxobj_content)
125 admin_user = content["admin_user"]
126 cluster_api_endpoint = content["cluster_api_endpoint"]
127 cluster_ca_cert = _b64_encode_str(content["cluster_ca_cert"])
128 admin_client_cert = _b64_encode_str(content["admin_client_cert"])
129 admin_client_key = _b64_encode_str(content["admin_client_key"])
130 # admin_client_cert = base64.b64encode(
131 # bytes(content["admin_client_cert"], "utf-8"))
132 # admin_client_key = base64.b64encode(
133 # bytes(content["admin_client_key"], "utf-8"))
135 "admin_user": admin_user,
136 "cluster_api_endpoint": cluster_api_endpoint,
137 "cluster_ca_cert": cluster_ca_cert,
138 "admin_client_cert": admin_client_cert,
139 "admin_client_key": admin_client_key
142 return json.dumps(profile)
145 def _b64_encode_str(msg: str, encode: str = 'utf-8') -> str:
146 msg_bytes = msg.encode('utf-8')
147 base64_bytes = base64.b64encode(msg_bytes)
148 base64_msg = base64_bytes.decode('utf-8')