Add get detail of a resource to API; remove the dependency of the domain in the view...
[pti/o2.git] / o2ims / service / auditor / ocloud_handler.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # pylint: disable=unused-argument
16 from __future__ import annotations
17
18 from o2ims.domain.stx_object import StxGenericModel
19 # from dataclasses import asdict
20 # from typing import List, Dict, Callable, Type
21 # TYPE_CHECKING
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
28 # if TYPE_CHECKING:
29 #     from . import unit_of_work
30
31 from o2common.helper import o2logging
32 logger = o2logging.get_logger(__name__)
33
34
35 class InvalidResourceType(Exception):
36     pass
37
38
39 def update_ocloud(
40     cmd: commands.UpdateOCloud,
41     uow: AbstractUnitOfWork
42 ):
43     stxobj = cmd.data
44     with uow:
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)
55
56             logger.info("Add the ocloud: " + stxobj.id
57                         + ", name: " + stxobj.name)
58         else:
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)
67
68             logger.info("Update the ocloud: " + stxobj.id
69                         + ", name: " + stxobj.name)
70         uow.commit()
71
72
73 def is_outdated(ocloud: Ocloud, stxobj: StxGenericModel):
74     # if stxobj.updatetime:
75     #     return True if Ocloud.updatetime < stxobj.updatetime else False
76     # else:
77     return True if ocloud.hash != stxobj.hash else False
78
79
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
89
90     return ocloud
91
92
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
102     ocloud.events = []