Fix INF-364 alarmEventRecord does not comply with spec
[pti/o2.git] / o2ims / domain / ocloud.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 from __future__ import annotations
16 import json
17
18 from o2common.config import config
19 from o2common.domain.base import AgRoot, Serializer
20 # from dataclasses import dataclass
21 # from datetime import date
22 # from typing import Optional, List, Set
23 from .resource_type import ResourceKindEnum, ResourceTypeEnum
24 from .alarm_obj import AlarmDictionary
25
26
27 DeploymentManagerProfileDefault = 'native_k8sapi'
28 DeploymentManagerProfileSOL018 = 'sol018'
29 DeploymentManagerProfileSOL018HelmCLI = 'sol018_helmcli'
30
31
32 class DeploymentManager(AgRoot, Serializer):
33     def __init__(self, id: str, name: str, ocloudid: str,
34                  dmsendpoint: str, description: str = '',
35                  supportedLocations: str = '', capabilities: str = '',
36                  capacity: str = '', profile: str = '') -> None:
37         super().__init__()
38         self.deploymentManagerId = id
39         self.name = name
40         self.description = description
41         self.oCloudId = ocloudid
42         self.serviceUri = dmsendpoint
43         self.supportedLocations = supportedLocations
44         self.capabilities = capabilities
45         self.capacity = capacity
46         self.profile = profile
47         self.extensions = []
48
49         self.version_number = 0
50
51     def serialize(self):
52         d = Serializer.serialize(self)
53
54         if 'profile' in d and d['profile'] != '':
55             d['profile'] = json.loads(d['profile'])
56         d['profileSupportList'] = [
57             DeploymentManagerProfileDefault,
58         ]
59         profiles = config.get_dms_support_profiles()
60         for profile in profiles:
61             if profile == DeploymentManagerProfileSOL018:
62                 d['profileSupportList'].append(profile)
63             elif profile == DeploymentManagerProfileSOL018HelmCLI:
64                 d['profileSupportList'].append(profile)
65
66         return d
67
68
69 class ResourcePool(AgRoot, Serializer):
70     def __init__(self, id: str, name: str, location: str,
71                  ocloudid: str, gLocationId: str = '',
72                  description: str = '') -> None:
73         super().__init__()
74         self.resourcePoolId = id
75         self.globalLocationId = gLocationId
76         self.name = name
77         self.description = description
78         self.oCloudId = ocloudid
79         self.location = location
80         self.resources = ''
81         self.extensions = []
82
83         self.version_number = 0
84
85
86 class ResourceType(AgRoot, Serializer):
87     def __init__(self, typeid: str, name: str, typeEnum: ResourceTypeEnum,
88                  vendor: str = '', model: str = '',
89                  version: str = '',
90                  description: str = '') -> None:
91         super().__init__()
92         self.resourceTypeId = typeid
93         self.resourceTypeEnum = typeEnum
94         self.name = name
95         self.description = description
96         self.vendor = vendor
97         self.model = model
98         self.version = version
99         self.alarmDictionary = None
100         self.resourceKind = ResourceKindEnum.UNDEFINED
101         self.resourceClass = ResourceTypeEnum.UNDEFINED
102         self.extensions = []
103
104         self.version_number = 0
105
106     def serialize(self):
107         d = Serializer.serialize(self)
108         if 'alarmDictionary' in d and \
109                 type(d['alarmDictionary']) is AlarmDictionary:
110             d['alarmDictionary'] = d['alarmDictionary'].serialize()
111         return d
112
113
114 class Resource(AgRoot, Serializer):
115     def __init__(self, resourceId: str, resourceTypeId: str,
116                  resourcePoolId: str, name: str, parentId: str = '',
117                  gAssetId: str = '', elements: str = '',
118                  description: str = '', extensions: str = '') -> None:
119         super().__init__()
120         self.resourceId = resourceId
121         self.description = description
122         self.resourceTypeId = resourceTypeId
123         self.globalAssetId = gAssetId
124         self.resourcePoolId = resourcePoolId
125         self.elements = elements
126         self.extensions = extensions
127
128         self.name = name
129         self.parentId = parentId
130         self.children = []
131
132         self.version_number = 0
133
134     def set_children(self, children: list):
135         self.children = children
136
137     def set_resource_type_name(self, resource_type_name: str):
138         self.resourceTypeName = resource_type_name
139
140     def serialize(self):
141         d = Serializer.serialize(self)
142
143         if 'elements' in d and d['elements'] != '':
144             d['elements'] = json.loads(d['elements'])
145
146         if not hasattr(self, 'children') or len(self.children) == 0:
147             return d
148         else:
149             d['children'] = []
150
151         for child in self.children:
152             d['children'].append(child.serialize())
153         return d
154
155
156 class Ocloud(AgRoot, Serializer):
157     def __init__(self, ocloudid: str, name: str, imsendpoint: str,
158                  globalcloudId: str = '',
159                  description: str = '', version_number: int = 0) -> None:
160         super().__init__()
161         self.oCloudId = ocloudid
162         self.globalCloudId = globalcloudId
163         self.name = name
164         self.description = description
165         self.serviceUri = imsendpoint
166         self.resourceTypes = []
167         self.resourcePools = []
168         self.deploymentManagers = []
169         self.smoRegistrationService = ''
170         self.extensions = []
171
172         self.version_number = version_number
173
174     # def addDeploymentManager(self,
175     #                          deploymentManager: DeploymentManager):
176
177     #     deploymentManager.oCloudId = self.oCloudId
178     #     old = filter(
179     #         lambda x: x.deploymentManagerId ==
180     #         deploymentManager.deploymentManagerId,
181     #         self.deploymentManagers)
182     #     for o in old or []:
183     #         self.deploymentManagers.remove(o)
184     #     self.deploymentManagers.append(deploymentManager)