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 InvalidOcloudState
25 from o2ims.domain.resource_type import MismatchedModel
26 from o2ims.domain.ocloud import Ocloud
27 from o2common.config import config
29 # from . import unit_of_work
31 from o2common.helper import o2logging
32 logger = o2logging.get_logger(__name__)
35 class InvalidResourceType(Exception):
40 cmd: commands.UpdateOCloud,
41 uow: AbstractUnitOfWork
45 oclouds = uow.oclouds.list()
46 if oclouds and oclouds.count() > 1:
47 raise InvalidOcloudState("More than 1 ocloud is found")
48 elif not oclouds or oclouds.count() == 0:
49 logger.info("add ocloud:" + stxobj.name
50 + " update_at: " + str(stxobj.updatetime)
51 + " id: " + str(stxobj.id)
52 + " hash: " + str(stxobj.hash))
53 entry = create_by(stxobj)
54 uow.oclouds.add(entry)
56 logger.info("Add the ocloud: " + stxobj.id
57 + ", name: " + stxobj.name)
59 localmodel = oclouds.first()
60 if is_outdated(localmodel, stxobj):
61 logger.info("update ocloud:" + stxobj.name
62 + " update_at: " + str(stxobj.updatetime)
63 + " id: " + str(stxobj.id)
64 + " hash: " + str(stxobj.hash))
65 update_by(localmodel, stxobj)
66 uow.oclouds.update(localmodel)
68 logger.info("Update the ocloud: " + stxobj.id
69 + ", name: " + stxobj.name)
73 def is_outdated(ocloud: Ocloud, stxobj: StxGenericModel):
74 # if stxobj.updatetime:
75 # return True if Ocloud.updatetime < stxobj.updatetime else False
77 return True if ocloud.hash != stxobj.hash else False
80 def create_by(stxobj: StxGenericModel) -> Ocloud:
81 imsendpoint = config.get_api_url() + config.get_o2ims_api_base()
82 globalcloudId = stxobj.id # to be updated
83 description = "An ocloud"
84 ocloud = Ocloud(stxobj.id, stxobj.name, imsendpoint,
85 globalcloudId, description, 1)
86 ocloud.createtime = stxobj.createtime
87 ocloud.updatetime = stxobj.updatetime
88 ocloud.hash = stxobj.hash
93 def update_by(ocloud: Ocloud, stxobj: StxGenericModel) -> None:
94 if ocloud.oCloudId != stxobj.id:
95 raise MismatchedModel("More than 1 ocloud found")
96 ocloud.name = stxobj.name
97 ocloud.createtime = stxobj.createtime
98 ocloud.updatetime = stxobj.updatetime
99 # ocloud.content = stxobj.content
100 ocloud.hash = stxobj.hash
101 ocloud.version_number = ocloud.version_number + 1