Add o2dms api endpoint
[pti/o2.git] / o2ims / service / auditor / dms_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 o2ims.service.unit_of_work import AbstractUnitOfWork
24 from o2ims.domain.resource_type import MismatchedModel
25 from o2ims.domain.ocloud import DeploymentManager
26 from o2ims import config
27 # if TYPE_CHECKING:
28 #     from . import unit_of_work
29
30 from o2common.helper import o2logging
31 logger = o2logging.get_logger(__name__)
32
33
34 class InvalidResourceType(Exception):
35     pass
36
37
38 def update_dms(
39     cmd: commands.UpdateDms,
40     uow: AbstractUnitOfWork
41 ):
42     stxobj = cmd.data
43     with uow:
44         dms = uow.deployment_managers.get(stxobj.id)
45         if not dms:
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)
53
54             logger.info("Add a dms: " + stxobj.id
55                         + ", name: " + stxobj.name)
56         else:
57             localmodel = dms
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)
65
66             logger.info("Update a dms: " + stxobj.id
67                         + ", name: " + stxobj.name)
68         uow.commit()
69
70
71 def is_outdated(ocloud: DeploymentManager, stxobj: StxGenericModel):
72     # if stxobj.updatetime:
73     #     return True if Ocloud.updatetime < stxobj.updatetime else False
74     # else:
75     return True if ocloud.hash != stxobj.hash else False
76
77
78 def create_by(stxobj: StxGenericModel, parentid: str) -> DeploymentManager:
79     dmsendpoint = config.get_api_url() +\
80                   config.get_o2dms_api_base() + "/" + stxobj.id
81     description = "A DMS"
82     ocloudid = parentid
83     supportedLocations = ''
84     capabilities = ''
85     capacity = ''
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
92
93     return localmodel
94
95
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