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
21 from o2ims.domain.stx_object import StxGenericModel
22 # from dataclasses import asdict
23 # from typing import List, Dict, Callable, Type
25 from o2ims.domain import commands
26 from o2common.service.unit_of_work import AbstractUnitOfWork
27 from o2ims.domain.resource_type import MismatchedModel
28 from o2ims.domain.ocloud import DeploymentManager
29 from o2common.config import config
31 # from . import unit_of_work
33 from o2common.helper import o2logging
34 logger = o2logging.get_logger(__name__)
37 class InvalidResourceType(Exception):
42 cmd: commands.UpdateDms,
43 uow: AbstractUnitOfWork
47 dms = uow.deployment_managers.get(stxobj.id)
49 logger.info("add dms: " + stxobj.name
50 + " update_at: " + str(stxobj.updatetime)
51 + " id: " + str(stxobj.id)
52 + " hash: " + str(stxobj.hash))
53 # ocloud = uow.oclouds.get(cmd.parent.oCloudId)
54 localmodel = create_by(stxobj, cmd.parentid)
55 uow.deployment_managers.add(localmodel)
57 logger.info("Add a dms: " + stxobj.id
58 + ", name: " + stxobj.name)
61 if is_outdated(localmodel, stxobj):
62 logger.info("update a dms: " + stxobj.name
63 + " update_at: " + str(stxobj.updatetime)
64 + " id: " + str(stxobj.id)
65 + " hash: " + str(stxobj.hash))
66 update_by(localmodel, stxobj, cmd.parentid)
67 uow.deployment_managers.update(localmodel)
69 logger.info("Update a dms: " + stxobj.id
70 + ", name: " + stxobj.name)
74 def is_outdated(ocloud: DeploymentManager, stxobj: StxGenericModel):
75 # if stxobj.updatetime:
76 # return True if Ocloud.updatetime < stxobj.updatetime else False
78 return True if ocloud.hash != stxobj.hash else False
81 def create_by(stxobj: StxGenericModel, parentid: str) -> DeploymentManager:
82 dmsendpoint = config.get_api_url() +\
83 config.get_o2dms_api_base() + "/" + stxobj.id
86 supportedLocations = ''
89 profile = _convert_content(stxobj.content)
90 localmodel = DeploymentManager(
91 stxobj.id, stxobj.name, ocloudid, dmsendpoint, description,
92 supportedLocations, capabilities, capacity, profile)
93 localmodel.createtime = stxobj.createtime
94 localmodel.updatetime = stxobj.updatetime
95 localmodel.hash = stxobj.hash
100 def update_by(target: DeploymentManager, stxobj: StxGenericModel,
101 parentid: str) -> None:
102 if target.deploymentManagerId != stxobj.id:
103 raise MismatchedModel("Mismatched Id")
104 target.name = stxobj.name
105 target.createtime = stxobj.createtime
106 target.updatetime = stxobj.updatetime
107 target.hash = stxobj.hash
108 target.oCloudId = parentid
109 target.version_number = target.version_number + 1
110 target.profile = _convert_content(stxobj.content)
114 def _convert_content(stxobj_content: str):
115 # Convert api retrun content to dict object
116 content = json.loads(stxobj_content)
117 admin_user = content["admin_user"]
118 cluster_api_endpoint = content["cluster_api_endpoint"]
119 cluster_ca_cert = _b64_encode_str(content["cluster_ca_cert"])
120 admin_client_cert = _b64_encode_str(content["admin_client_cert"])
121 admin_client_key = _b64_encode_str(content["admin_client_key"])
122 # admin_client_cert = base64.b64encode(
123 # bytes(content["admin_client_cert"], "utf-8"))
124 # admin_client_key = base64.b64encode(
125 # bytes(content["admin_client_key"], "utf-8"))
127 "admin_user": admin_user,
128 "cluster_api_endpoint": cluster_api_endpoint,
129 "cluster_ca_cert": cluster_ca_cert,
130 "admin_client_cert": admin_client_cert,
131 "admin_client_key": admin_client_key
134 return json.dumps(profile)
137 def _b64_encode_str(msg: str, encode: str = 'utf-8') -> str:
138 msg_bytes = msg.encode('utf-8')
139 base64_bytes = base64.b64encode(msg_bytes)
140 base64_msg = base64_bytes.decode('utf-8')