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
18 from o2ims.domain.stx_object import StxGenericModel
19 # from dataclasses import asdict
20 # from typing import List, Dict, Callable, Type
22 from o2ims.domain import commands
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 localmodel = DeploymentManager(
87 stxobj.id, stxobj.name, ocloudid, dmsendpoint, description,
88 supportedLocations, capabilities, capacity)
89 localmodel.createtime = stxobj.createtime
90 localmodel.updatetime = stxobj.updatetime
91 localmodel.hash = stxobj.hash
96 def update_by(target: DeploymentManager, stxobj: StxGenericModel,
97 parentid: str) -> None:
98 if target.deploymentManagerId != stxobj.id:
99 raise MismatchedModel("Mismatched Id")
100 target.name = stxobj.name
101 target.createtime = stxobj.createtime
102 target.updatetime = stxobj.updatetime
103 # ocloud.content = stxobj.content
104 target.hash = stxobj.hash
105 target.oCloudId = parentid
106 target.version_number = target.version_number + 1