3dffa58bd173a4194b5dde3d24ecc22e4d8d446b
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / msapi / aai.py
1 # Copyright 2016 ZTE Corporation.
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 import json
16 import logging
17 import uuid
18
19 from lcm.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWD
20 from lcm.pub.utils import restcall
21 logger = logging.getLogger(__name__)
22
23
24 def call_aai(resource, method, content=''):
25     additional_headers = {
26         'X-FromAppId': 'VFC-CATALOG',
27         'X-TransactionId': str(uuid.uuid1())
28     }
29     return restcall.call_req(AAI_BASE_URL,
30                              AAI_USER,
31                              AAI_PASSWD,
32                              restcall.rest_no_auth,
33                              resource,
34                              method,
35                              content,
36                              additional_headers)
37
38
39 def split_vim_to_owner_region(vim_id):
40     split_vim = vim_id.split('_')
41     cloud_owner = split_vim[0]
42     cloud_region = "".join(split_vim[1:])
43     return cloud_owner, cloud_region
44
45
46 def get_flavor_info(vim_id):
47     cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
48     resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/flavors?depth=all" % \
49                (cloud_owner, cloud_region)
50
51     ret = call_aai(resource, "GET")
52     if ret[0] != 0:
53         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
54
55     return json.JSONDecoder().decode(ret[1]) if ret[1] else ret[1]