1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
"""A&AI Complex module."""
# Copyright 2022 Orange, Deutsche Telekom AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, Iterator
from urllib.parse import urlencode
from onapsdk.utils.jinja import jinja_env
from ..aai_element import AaiResource
class Complex(AaiResource): # pylint: disable=too-many-instance-attributes
"""Complex class.
Collection of physical locations that can house cloud-regions.
"""
def __init__(self, # pylint: disable=too-many-locals
physical_location_id: str,
*,
name: str = "",
data_center_code: str = "",
identity_url: str = "",
resource_version: str = "",
physical_location_type: str = "",
street1: str = "",
street2: str = "",
city: str = "",
state: str = "",
postal_code: str = "",
country: str = "",
region: str = "",
latitude: str = "",
longitude: str = "",
elevation: str = "",
lata: str = "",
timezone: str = "",
data_owner: str = "",
data_source: str = "",
data_source_version: str = "") -> None:
"""Complex object initialization.
Args:
name (str): complex name
physical_location_id (str): complex ID
data_center_code (str, optional): complex data center code. Defaults to "".
identity_url (str, optional): complex identity url. Defaults to "".
resource_version (str, optional): complex resource version. Defaults to "".
physical_location_type (str, optional): complex physical location type. Defaults to "".
street1 (str, optional): complex address street part one. Defaults to "".
street2 (str, optional): complex address street part two. Defaults to "".
city (str, optional): complex address city. Defaults to "".
state (str, optional): complex address state. Defaults to "".
postal_code (str, optional): complex address postal code. Defaults to "".
country (str, optional): complex address country. Defaults to "".
region (str, optional): complex address region. Defaults to "".
latitude (str, optional): complex geographical location latitude. Defaults to "".
longitude (str, optional): complex geographical location longitude. Defaults to "".
elevation (str, optional): complex elevation. Defaults to "".
lata (str, optional): complex lata. Defaults to "".
timezone (str, optional): the time zone where the complex is located. Defaults to "".
data_owner (str, optional): Identifies the entity that is responsible managing this
inventory object. Defaults to "".
data_source (str, optional): Identifies the upstream source of the data. Defaults to "".
data_source_version (str, optional): Identifies the version of the upstream source.
Defaults to "".
"""
super().__init__()
self.name: str = name
self.physical_location_id: str = physical_location_id
self.data_center_code: str = data_center_code
self.identity_url: str = identity_url
self.resource_version: str = resource_version
self.physical_location_type: str = physical_location_type
self.street1: str = street1
self.street2: str = street2
self.city: str = city
self.state: str = state
self.postal_code: str = postal_code
self.country: str = country
self.region: str = region
self.latitude: str = latitude
self.longitude: str = longitude
self.elevation: str = elevation
self.lata: str = lata
self.timezone: str = timezone
self.data_owner: str = data_owner
self.data_source: str = data_source
self.data_source_version: str = data_source_version
def __repr__(self) -> str:
"""Complex object description.
Returns:
str: Complex object description
"""
return (f"Complex(name={self.name}, "
f"physical_location_id={self.physical_location_id}, "
f"resource_version={self.resource_version})")
@property
def url(self) -> str:
"""Complex url.
Returns:
str: Complex url
"""
return (f"{self.base_url}{self.api_version}/cloud-infrastructure/complexes/complex/"
f"{self.physical_location_id}")
@classmethod
def create(cls, # pylint: disable=too-many-locals
physical_location_id: str,
*,
name: str = "",
data_center_code: str = "",
identity_url: str = "",
resource_version: str = "",
physical_location_type: str = "",
street1: str = "",
street2: str = "",
city: str = "",
state: str = "",
postal_code: str = "",
country: str = "",
region: str = "",
latitude: str = "",
longitude: str = "",
elevation: str = "",
lata: str = "",
timezone: str = "",
data_owner: str = "",
data_source: str = "",
data_source_version: str = "") -> "Complex":
"""Create complex.
Create complex object by calling A&AI API.
If API request doesn't fail it returns Complex object.
Returns:
Complex: Created complex object
"""
complex_object: Complex = Complex(
name=name,
physical_location_id=physical_location_id,
data_center_code=data_center_code,
identity_url=identity_url,
resource_version=resource_version,
physical_location_type=physical_location_type,
street1=street1,
street2=street2,
city=city,
state=state,
postal_code=postal_code,
country=country,
region=region,
latitude=latitude,
longitude=longitude,
elevation=elevation,
lata=lata,
timezone=timezone,
data_owner=data_owner,
data_source=data_source,
data_source_version=data_source_version
)
payload: str = jinja_env().get_template("complex_create.json.j2").render(
complex=complex_object)
url: str = (
f"{cls.base_url}{cls.api_version}/cloud-infrastructure/complexes/complex/"
f"{complex_object.physical_location_id}"
)
cls.send_message("PUT", "create complex", url, data=payload)
return complex_object
@classmethod
def get_all_url(cls) -> str: # pylint: disable=arguments-differ
"""Return an url to get all complexes.
Returns:
str: URL to get all complexes
"""
return f"{cls.base_url}{cls.api_version}/cloud-infrastructure/complexes"
@classmethod
def get_all(cls,
physical_location_id: str = None,
data_center_code: str = None,
complex_name: str = None,
identity_url: str = None) -> Iterator["Complex"]:
"""Get all complexes from A&AI.
Call A&AI API to get all complex objects.
Args:
physical_location_id (str, optional): Unique identifier for physical location,
e.g., CLLI. Defaults to None.
data_center_code (str, optional): Data center code which can be an alternate way
to identify a complex. Defaults to None.
complex_name (str, optional): Gamma complex name for LCP instance. Defaults to None.
identity_url (str, optional): URL of the keystone identity service. Defaults to None.
Yields:
Complex -- Complex object. Can not yield anything if any complex with given filter
parameters doesn't exist
"""
filter_parameters: dict = cls.filter_none_key_values(
{
"physical-location-id": physical_location_id,
"data-center-code": data_center_code,
"complex-name": complex_name,
"identity-url": identity_url,
}
)
url: str = (f"{cls.get_all_url()}?{urlencode(filter_parameters)}")
for complex_json in cls.send_message_json("GET",
"get cloud regions",
url).get("complex", []):
yield cls.create_from_api_response(complex_json)
@classmethod
def get_by_physical_location_id(cls, physical_location_id: str) -> "Complex":
"""Get complex by physical location id.
Args:
physical_location_id (str): Physical location id of Complex
Returns:
Complex: Complex object
Raises:
ResourceNotFound: Complex with given physical location id not found
"""
response = cls.send_message_json("GET",
"Get complex with physical location id: "
f"{physical_location_id}",
f"{cls.base_url}{cls.api_version}/cloud-infrastructure/"
f"complexes/complex/{physical_location_id}")
return cls.create_from_api_response(response)
@classmethod
def create_from_api_response(cls,
api_response: Dict[str, Any]) -> "Complex":
"""Create complex object using given A&AI API response JSON.
Args:
api_response (Dict[str, Any]): Complex A&AI API response
Returns:
Complex: Complex object created from given response
"""
return cls(
name=api_response.get("complex-name"),
physical_location_id=api_response["physical-location-id"],
data_center_code=api_response.get("data-center-code"),
identity_url=api_response.get("identity-url"),
resource_version=api_response.get("resource-version"),
physical_location_type=api_response.get("physical-location-type"),
street1=api_response.get("street1"),
street2=api_response.get("street2"),
city=api_response.get("city"),
state=api_response.get("state"),
postal_code=api_response.get("postal-code"),
country=api_response.get("country"),
region=api_response.get("region"),
latitude=api_response.get("latitude"),
longitude=api_response.get("longitude"),
elevation=api_response.get("elevation"),
lata=api_response.get("lata"),
timezone=api_response.get("time-zone"),
data_owner=api_response.get("data-owner"),
data_source=api_response.get("data-source"),
data_source_version=api_response.get("data-source-version")
)
def delete(self) -> None:
"""Delete complex."""
self.send_message(
"DELETE",
f"Delete {self.physical_location_id} complex",
f"{self.url}?resource-version={self.resource_version}"
)
|