Add capabilities of the DMS query
[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         if 'capabilities' in d and d['capabilities'] != '':
67             d['capabilities'] = json.loads(d['capabilities'])
68         return d
69
70
71 class ResourcePool(AgRoot, Serializer):
72     def __init__(self, id: str, name: str, location: str,
73                  ocloudid: str, gLocationId: str = '',
74                  description: str = '') -> None:
75         super().__init__()
76         self.resourcePoolId = id
77         self.globalLocationId = gLocationId
78         self.name = name
79         self.description = description
80         self.oCloudId = ocloudid
81         self.location = location
82         self.resources = ''
83         self.extensions = []
84
85         self.version_number = 0
86
87
88 class ResourceType(AgRoot, Serializer):
89     def __init__(self, typeid: str, name: str, typeEnum: ResourceTypeEnum,
90                  vendor: str = '', model: str = '',
91                  version: str = '',
92                  description: str = '') -> None:
93         super().__init__()
94         self.resourceTypeId = typeid
95         self.resourceTypeEnum = typeEnum
96         self.name = name
97         self.description = description
98         self.vendor = vendor
99         self.model = model
100         self.version = version
101         self.alarmDictionary = None
102         self.resourceKind = ResourceKindEnum.UNDEFINED
103         self.resourceClass = ResourceTypeEnum.UNDEFINED
104         self.extensions = []
105
106         self.version_number = 0
107
108     def serialize(self):
109         d = Serializer.serialize(self)
110         if 'alarmDictionary' in d and \
111                 type(d['alarmDictionary']) is AlarmDictionary:
112             d['alarmDictionary'] = d['alarmDictionary'].serialize()
113         return d
114
115
116 class Resource(AgRoot, Serializer):
117     def __init__(self, resourceId: str, resourceTypeId: str,
118                  resourcePoolId: str, parentId: str = '',
119                  gAssetId: str = '', elements: str = '',
120                  description: str = '', extensions: str = '') -> None:
121         super().__init__()
122         self.resourceId = resourceId
123         self.description = description
124         self.resourceTypeId = resourceTypeId
125         self.globalAssetId = gAssetId
126         self.resourcePoolId = resourcePoolId
127         self.elements = elements
128         self.extensions = extensions
129
130         self.parentId = parentId
131         self.children = []
132
133         self.version_number = 0
134
135     def set_children(self, children: list):
136         self.children = children
137
138     def set_resource_type_name(self, resource_type_name: str):
139         self.resourceTypeName = resource_type_name
140
141     def serialize(self):
142         d = Serializer.serialize(self)
143
144         if 'elements' in d and d['elements'] != '':
145             d['elements'] = json.loads(d['elements'])
146
147         if not hasattr(self, 'children') or len(self.children) == 0:
148             return d
149         else:
150             d['children'] = []
151
152         for child in self.children:
153             d['children'].append(child.serialize())
154         return d
155
156
157 class Ocloud(AgRoot, Serializer):
158     def __init__(self, ocloudid: str, name: str, imsendpoint: str,
159                  globalcloudId: str = '',
160                  description: str = '', version_number: int = 0) -> None:
161         super().__init__()
162         self.oCloudId = ocloudid
163         self.globalCloudId = globalcloudId
164         self.name = name
165         self.description = description
166         self.serviceUri = imsendpoint
167         self.resourceTypes = []
168         self.resourcePools = []
169         self.deploymentManagers = []
170         self.smoRegistrationService = ''
171         self.extensions = []
172
173         self.version_number = version_number
174
175     # def addDeploymentManager(self,
176     #                          deploymentManager: DeploymentManager):
177
178     #     deploymentManager.oCloudId = self.oCloudId
179     #     old = filter(
180     #         lambda x: x.deploymentManagerId ==
181     #         deploymentManager.deploymentManagerId,
182     #         self.deploymentManagers)
183     #     for o in old or []:
184     #         self.deploymentManagers.remove(o)
185     #     self.deploymentManagers.append(deploymentManager)