Fix decode vim id bug
[multicloud/framework.git] / multivimbroker / multivimbroker / pub / msapi / extsys.py
1 # Copyright (c) 2017 Wind River Systems, Inc.
2 # Copyright (c) 2017-2018 VMware, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
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
13 import json
14 import logging
15 import re
16
17 from multivimbroker.pub.exceptions import VimBrokerException
18 from multivimbroker.pub.utils import restcall
19
20 logger = logging.getLogger(__name__)
21
22
23 def encode_vim_id(cloud_owner, cloud_region_id):
24     '''
25     compose vim_id by cloud_owner and cloud_region, make sure the vimid can be
26     converted back when talking to AAI,etc.
27     This is a backward compatibility design to reuse the existing
28     implementation code
29     :param cloud_owner:
30     :param cloud_region:
31     :return:
32     '''
33
34     # since the {cloud_owner}/{cloud_region_id"} is globally unique, the
35     # concatenated one as below will be unique as well.
36
37     vim_id = cloud_owner + "_" + cloud_region_id
38
39     # other options:
40     # 1, store it into cache so the decode and just look up the cache for
41     # decoding
42     # 2, use other delimiter in case that '_' was used by
43     # cloud owner/cloud region id,
44     # e.g. '.', '#', hence the decode need to try more than one time
45
46     return vim_id
47
48
49 def decode_vim_id(vim_id):
50     m = re.search(r'^([0-9a-zA-Z-]+)_([0-9a-zA-Z_-]+)$', vim_id)
51     cloud_owner, cloud_region_id = m.group(1), m.group(2)
52     return cloud_owner, cloud_region_id
53
54
55 def split_vim_to_owner_region(vim_id):
56     split_vim = vim_id.split('_')
57     cloud_owner = split_vim[0]
58     cloud_region = "".join(split_vim[1:])
59     return cloud_owner, cloud_region
60
61
62 def get_vim_by_id(vim_id):
63     if vim_id == "vmware_fake":
64         return {
65             "type": "vmware",
66             "version": "4.0",
67             "vimId": vim_id
68         }
69     cloud_owner, cloud_region = decode_vim_id(vim_id)
70     ret = restcall.get_res_from_aai("/cloud-infrastructure/cloud-regions/"
71                                     "cloud-region/%s/%s" % (
72                                         cloud_owner, cloud_region))
73     if ret[0] != 0:
74         logger.error("Status code is %s, detail is %s." % (ret[2], ret[1]))
75         raise VimBrokerException(
76             status_code=404,
77             content="Failed to query VIM with id (%s) from extsys." % vim_id)
78     ret = json.JSONDecoder().decode(ret[1])
79     ret['type'] = ret['cloud-type']
80     ret['version'] = ret['cloud-region-version']
81     ret['vimId'] = vim_id
82     return ret