Add auditor for resource pool, pserver and cpu/memory/port/interface for pserver...
[pti/o2.git] / o2ims / service / auditor / pserver_mem_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 import uuid
18 # import json
19
20 from o2ims.domain import commands
21 from o2ims.domain.stx_object import StxGenericModel
22 from o2common.service.unit_of_work import AbstractUnitOfWork
23 from o2ims.domain.resource_type import MismatchedModel
24 from o2ims.domain.ocloud import Resource, ResourceType
25
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
28
29
30 class InvalidResourceType(Exception):
31     pass
32
33
34 def update_pserver_mem(
35     cmd: commands.UpdatePserverMem,
36     uow: AbstractUnitOfWork
37 ):
38     stxobj = cmd.data
39     with uow:
40         p_resource = uow.resources.get(cmd.parentid)
41         resourcepool = uow.resource_pools.get(p_resource.resourcePoolId)
42
43         res = uow.session.execute(
44             '''
45             SELECT "resourceTypeId", "oCloudId", "name"
46             FROM resourcetype
47             WHERE "resourceTypeEnum" = :resource_type_enum
48             ''',
49             dict(resource_type_enum=stxobj.type.name)
50         )
51         first = res.first()
52         if first is None:
53             resourcetype_id = str(uuid.uuid4())
54             uow.resource_types.add(ResourceType(
55                 resourcetype_id,
56                 'pserver_mem', stxobj.type,
57                 resourcepool.oCloudId))
58         else:
59             resourcetype_id = first['resourceTypeId']
60
61         resource = uow.resources.get(stxobj.id)
62         if not resource:
63             logger.info("add the memory of pserver:" + stxobj.name
64                         + " update_at: " + str(stxobj.updatetime)
65                         + " id: " + str(stxobj.id)
66                         + " hash: " + str(stxobj.hash))
67             localmodel = create_by(stxobj, p_resource, resourcetype_id)
68             uow.resources.add(localmodel)
69
70             logger.info("Add the memory of pserver: " + stxobj.id
71                         + ", name: " + stxobj.name)
72         else:
73             localmodel = resource
74             if is_outdated(localmodel, stxobj):
75                 logger.info("update memory of pserver:" + stxobj.name
76                             + " update_at: " + str(stxobj.updatetime)
77                             + " id: " + str(stxobj.id)
78                             + " hash: " + str(stxobj.hash))
79                 update_by(localmodel, stxobj, p_resource)
80                 uow.resources.update(localmodel)
81
82             logger.info("Update the memory of pserver: " + stxobj.id
83                         + ", name: " + stxobj.name)
84         uow.commit()
85
86
87 def is_outdated(resource: Resource, stxobj: StxGenericModel):
88     return True if resource.hash != stxobj.hash else False
89
90
91 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
92         -> Resource:
93     # content = json.loads(stxobj.content)
94     resourcetype_id = resourcetype_id
95     resourcepool_id = parent.resourcePoolId
96     parent_id = parent.resourceId
97     gAssetId = ''  # TODO: global ID
98     description = "A memory resource of the physical server"
99     resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
100                         parent_id, gAssetId, stxobj.content, description)
101     resource.createtime = stxobj.createtime
102     resource.updatetime = stxobj.updatetime
103     resource.hash = stxobj.hash
104
105     return resource
106
107
108 def update_by(target: Resource, stxobj: StxGenericModel,
109               parentid: str) -> None:
110     if target.resourceId != stxobj.id:
111         raise MismatchedModel("Mismatched Id")
112     target.createtime = stxobj.createtime
113     target.updatetime = stxobj.updatetime
114     target.hash = stxobj.hash
115     target.version_number = target.version_number + 1
116     target.events = []