1 # Copyright (c) 2018 Amdocs
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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 from multicloud_azure.pub.config.config import AAI_SCHEMA_VERSION
22 from multicloud_azure.pub.config.config import AAI_SERVICE_URL
23 from multicloud_azure.pub.config.config import AAI_USERNAME
24 from multicloud_azure.pub.config.config import AAI_PASSWORD
25 from multicloud_azure.pub.config.config import MSB_SERVICE_IP, MSB_SERVICE_PORT
27 from multicloud_azure.pub.exceptions import VimDriverAzureException
29 rest_no_auth, rest_oneway_auth, rest_bothway_auth = 0, 1, 2
30 HTTP_200_OK, HTTP_201_CREATED = '200', '201'
31 HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED = '204', '202'
32 status_ok_list = [HTTP_200_OK, HTTP_201_CREATED,
33 HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED]
34 HTTP_404_NOTFOUND, HTTP_403_FORBIDDEN = '404', '403'
35 HTTP_401_UNAUTHORIZED, HTTP_400_BADREQUEST = '401', '400'
37 logger = logging.getLogger(__name__)
40 def call_req(base_url, user, passwd, auth_type, resource, method, content='',
42 callid = str(uuid.uuid1())
43 # logger.debug("[%s]call_req('%s','%s','%s',%s,'%s','%s','%s')" % (
44 # callid, base_url, user, passwd, auth_type, resource, method, content))
51 full_url = combine_url(base_url, resource)
54 headers['content-type'] = 'application/json'
57 headers['Authorization'] = 'Basic ' + \
58 ('%s:%s' % (user, passwd)).encode("base64")
60 for retry_times in range(3):
63 disable_ssl_certificate_validation=(
64 auth_type == rest_no_auth))
65 http.follow_all_redirects = True
67 logger.debug("request=%s" % full_url)
68 resp, resp_content = http.request(
69 full_url, method=method.upper(), body=content,
71 resp_status = resp['status']
72 resp_body = resp_content.decode('UTF-8')
74 if resp_status in status_ok_list:
75 ret = [0, resp_body, resp_status, resp]
77 ret = [1, resp_body, resp_status, resp]
79 except Exception as ex:
80 if 'httplib.ResponseNotReady' in str(sys.exc_info()):
81 logger.error(traceback.format_exc())
82 ret = [1, "Unable to connect to %s" % full_url,
86 except urllib2.URLError as err:
87 ret = [2, str(err), resp_status, resp]
88 except Exception as ex:
89 logger.error(traceback.format_exc())
90 logger.error("[%s]ret=%s" % (callid, str(sys.exc_info())))
91 res_info = str(sys.exc_info())
92 if 'httplib.ResponseNotReady' in res_info:
93 res_info = ("The URL[%s] request failed or is not responding." %
95 ret = [3, res_info, resp_status, resp]
96 # logger.debug("[%s]ret=%s" % (callid, str(ret)))
100 def req_by_msb(resource, method, content=''):
101 base_url = "http://%s:%s/" % (MSB_SERVICE_IP, MSB_SERVICE_PORT)
102 return call_req(base_url, "", "", rest_no_auth, resource, method, content)
105 def combine_url(base_url, resource):
107 if base_url.endswith('/') and resource.startswith('/'):
108 full_url = base_url[:-1] + resource
109 elif base_url.endswith('/') and not resource.startswith('/'):
110 full_url = base_url + resource
111 elif not base_url.endswith('/') and resource.startswith('/'):
112 full_url = base_url + resource
114 full_url = base_url + '/' + resource
118 def get_res_from_aai(resource, content=''):
120 'X-FromAppId': 'MultiCloud',
121 'X-TransactionId': '9001',
122 'content-type': 'application/json',
123 'accept': 'application/json'
125 base_url = "%s/%s" % (AAI_SERVICE_URL, AAI_SCHEMA_VERSION)
126 return call_req(base_url, AAI_USERNAME, AAI_PASSWORD, rest_no_auth,
127 resource, "GET", content, headers)
130 class AAIClient(object):
131 def __init__(self, cloud_owner, cloud_region):
132 self.base_url = "%s/%s" % (AAI_SERVICE_URL, AAI_SCHEMA_VERSION)
133 self.username = AAI_USERNAME
134 self.password = AAI_PASSWORD
135 self.default_headers = {
136 'X-FromAppId': 'multicloud-azure',
137 'X-TransactionId': '9004',
138 'content-type': 'application/json',
139 'accept': 'application/json'
141 self.cloud_owner = cloud_owner
142 self.cloud_region = cloud_region
143 self._vim_info = None
145 def get_vim(self, get_all=False):
146 resource = ("/cloud-infrastructure/cloud-regions/cloud-region"
147 "/%s/%s" % (self.cloud_owner, self.cloud_region))
149 resource = "%s?depth=all" % resource
150 resp = call_req(self.base_url, self.username, self.password,
151 rest_no_auth, resource, "GET",
152 headers=self.default_headers)
154 raise VimDriverAzureException(
156 content="Failed to query VIM with id (%s_%s) from extsys." % (
157 self.cloud_owner, self.cloud_region))
158 return json.loads(resp[1])
160 def delete_vim(self):
161 resp = self.get_vim(get_all=True)
162 logger.debug('Delete cloud region')
163 resource = ("/cloud-infrastructure/cloud-regions/cloud-region"
164 "/%s/%s?resource-version=%s" %
165 (self.cloud_owner, self.cloud_region,
166 resp['resource-version']))
167 resp = call_req(self.base_url, self.username, self.password,
168 rest_no_auth, resource, "DELETE",
169 headers=self.default_headers)
171 raise VimDriverAzureException(
173 content="Failed to delete cloud %s_%s: %s." % (
174 self.cloud_owner, self.cloud_region, resp[1]))
176 def update_vim(self, content):
177 self.add_flavors(content)
179 def update_identity_url(self):
181 vim['identity-url'] = ("http://%s/api/multicloud/v0/%s_%s/identity/"
182 "v3" % (MSB_SERVICE_IP, self.cloud_owner,
184 resource = ("/cloud-infrastructure/cloud-regions/cloud-region"
185 "/%s/%s" % (self.cloud_owner, self.cloud_region))
186 logger.debug("Updating identity url %s" % vim)
187 call_req(self.base_url, self.username, self.password,
188 rest_no_auth, resource, "PUT",
189 content=json.dumps(vim),
190 headers=self.default_headers)
192 def add_flavors(self, content):
193 for flavor in content['flavors']:
194 resource = ("/cloud-infrastructure/cloud-regions/cloud-region/"
195 "%s/%s/flavors/flavor/%s" % (
196 self.cloud_owner, self.cloud_region,
199 'flavor-name': flavor['name'],
200 'flavor-vcpus': flavor['vcpus'],
201 'flavor-ram': flavor['ram'],
202 'flavor-disk': flavor['disk'],
203 'flavor-selflink': ""
206 if flavor['name'].startswith("onap."):
207 hpa_capabilities = self._get_hpa_capabilities(
209 body['hpa-capabilities'] = {
210 'hpa-capability': hpa_capabilities}
212 logger.debug("Adding flavors to cloud region")
213 call_req(self.base_url, self.username, self.password,
214 rest_no_auth, resource, "PUT",
215 content=json.dumps(body),
216 headers=self.default_headers)
218 def _get_hpa_capabilities(self, flavor):
222 caps_dict = self._get_hpa_basic_capabilities(flavor)
223 if len(caps_dict) > 0:
224 logger.debug("basic_capabilities_info: %s" % caps_dict)
225 hpa_caps.append(caps_dict)
227 # storage capabilities
228 caps_dict = self._get_storage_capabilities(flavor)
229 if len(caps_dict) > 0:
230 logger.debug("storage_capabilities_info: %s" % caps_dict)
231 hpa_caps.append(caps_dict)
233 # CPU instruction set extension capabilities
234 caps_dict = self._get_instruction_set_capabilities(
235 flavor['extra_specs'])
236 if len(caps_dict) > 0:
237 logger.debug("instruction_set_capabilities_info: %s" % caps_dict)
238 hpa_caps.append(caps_dict)
240 # ovsdpdk capabilities
241 caps_dict = self._get_ovsdpdk_capabilities()
242 if len(caps_dict) > 0:
243 logger.debug("ovsdpdk_capabilities_info: %s" % caps_dict)
244 hpa_caps.append(caps_dict)
248 def _get_hpa_basic_capabilities(self, flavor):
249 basic_capability = {}
250 feature_uuid = uuid.uuid4()
252 basic_capability['hpa-capability-id'] = str(feature_uuid)
253 basic_capability['hpa-feature'] = 'basicCapabilities'
254 basic_capability['architecture'] = 'generic'
255 basic_capability['hpa-version'] = 'v1'
257 basic_capability['hpa-feature-attributes'] = []
258 basic_capability['hpa-feature-attributes'].append({
259 'hpa-attribute-key': 'numVirtualCpu',
260 'hpa-attribute-value': json.dumps(
261 {'value': str(flavor['vcpus'])})})
262 basic_capability['hpa-feature-attributes'].append({
263 'hpa-attribute-key': 'virtualMemSize',
264 'hpa-attribute-value': json.dumps({'value': str(
265 flavor['ram']), 'unit': 'GB'})})
267 return basic_capability
269 def _get_storage_capabilities(self, flavor):
270 storage_capability = {}
271 feature_uuid = uuid.uuid4()
273 storage_capability['hpa-capability-id'] = str(feature_uuid)
274 storage_capability['hpa-feature'] = 'localStorage'
275 storage_capability['architecture'] = 'generic'
276 storage_capability['hpa-version'] = 'v1'
278 storage_capability['hpa-feature-attributes'] = []
279 storage_capability['hpa-feature-attributes'].append({
280 'hpa-attribute-key': 'diskSize',
281 'hpa-attribute-value': json.dumps({'value': str(
282 flavor['disk']), 'unit': 'MB'})
284 storage_capability['hpa-feature-attributes'].append({
285 'hpa-attribute-key': 'swapMemSize',
286 'hpa-attribute-value': json.dumps({'value': str(
287 flavor.get('swap', 0)), 'unit': 'MB'})
289 storage_capability['hpa-feature-attributes'].append({
290 'hpa-attribute-key': 'ephemeralDiskSize',
291 'hpa-attribute-value': json.dumps({'value': str(
292 flavor.get('OS-FLV-EXT-DATA:ephemeral', 0)), 'unit': 'GB'})
294 return storage_capability
296 def _get_instruction_set_capabilities(self, extra_specs):
297 instruction_capability = {}
298 feature_uuid = uuid.uuid4()
300 if extra_specs.get('hw:capabilities:cpu_info:features'):
301 instruction_capability['hpa-capability-id'] = str(feature_uuid)
302 instruction_capability['hpa-feature'] = 'instructionSetExtensions'
303 instruction_capability['architecture'] = 'Intel64'
304 instruction_capability['hpa-version'] = 'v1'
306 instruction_capability['hpa-feature-attributes'] = []
307 instruction_capability['hpa-feature-attributes'].append({
308 'hpa-attribute-key': 'instructionSetExtensions',
309 'hpa-attribute-value': json.dumps(
310 {'value': extra_specs[
311 'hw:capabilities:cpu_info:features']})
313 return instruction_capability
315 def _get_ovsdpdk_capabilities(self):
316 ovsdpdk_capability = {}
317 feature_uuid = uuid.uuid4()
319 if not self._vim_info:
320 self._vim_info = self.get_vim(get_all=True)
321 cloud_extra_info_str = self._vim_info.get('cloud-extra-info')
322 if not isinstance(cloud_extra_info_str, dict):
324 cloud_extra_info_str = json.loads(cloud_extra_info_str)
325 except Exception as ex:
326 logger.error("Can not convert cloud extra info %s %s" % (
327 str(ex), cloud_extra_info_str))
329 if cloud_extra_info_str:
330 cloud_dpdk_info = cloud_extra_info_str.get("ovsDpdk")
332 ovsdpdk_capability['hpa-capability-id'] = str(feature_uuid)
333 ovsdpdk_capability['hpa-feature'] = 'ovsDpdk'
334 ovsdpdk_capability['architecture'] = 'Intel64'
335 ovsdpdk_capability['hpa-version'] = 'v1'
337 ovsdpdk_capability['hpa-feature-attributes'] = []
338 ovsdpdk_capability['hpa-feature-attributes'].append({
339 'hpa-attribute-key': str(cloud_dpdk_info.get("libname")),
340 'hpa-attribute-value': json.dumps(
341 {'value': cloud_dpdk_info.get("libversion")})
343 return ovsdpdk_capability