65039ae080507c6e4abca80dd7169142f5e1a5f9
[multicloud/framework.git] / multivimbroker / multivimbroker / pub / utils / syscomm.py
1 # Copyright (c) 2017 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 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
12 import inspect
13 import json
14 import os
15 import re
16
17 import multivimbroker.pub.exceptions as exceptions
18 from multivimbroker.pub.msapi.extsys import get_vim_by_id
19
20
21 def fun_name():
22     return inspect.stack()[1][3]
23
24
25 # Which headers are hop-by-hop headers by default
26 HOP_BY_HOP = ['connection', 'keep-alive', 'proxy-authenticate',
27               'proxy-authorization', 'te', 'trailers',
28               'transfer-encoding', 'upgrade']
29
30
31 def getHeadersKeys(response):
32     hopbyhop = HOP_BY_HOP
33     hopbyhop.extend([x.strip()
34                      for x in response.get('connection', '').split(',')])
35     return [header for header in response.keys() if header not in hopbyhop]
36
37
38 def findMultivimDriver(vim=None):
39     json_file = os.path.join(os.path.dirname(__file__),
40                              '../config/provider-plugin.json')
41     with open(json_file, "r") as f:
42         plugins = json.load(f)
43     if not vim or vim.get("type") not in plugins.keys():
44         raise exceptions.NotFound("Not support VIM type")
45     plugin = plugins[vim["type"]]
46     if vim.get("version") in plugin["versions"].keys():
47         return plugin["versions"][vim["version"]]["provider_plugin"]
48     return plugin["provider_plugin"]
49
50
51 def getMultivimDriver(vimid, full_path=""):
52     multcloud = "multicloud"
53     vim = get_vim_by_id(vimid)
54     multclouddriver = findMultivimDriver(vim=vim)
55     return re.sub(multcloud, multclouddriver, full_path)