1 # Copyright 2016 ZTE Corporation.
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.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from requests import RequestException
21 from lcm.pub.nfvi.vim.lib.syscomm import fun_name
22 from lcm.pub.nfvi.vim.lib.vimexception import VimException
24 logger = logging.getLogger(__name__)
28 def __init__(self, connectInfo):
29 logger.info("[VimAdaptor]connectInfo=%s" % connectInfo)
30 self.apiImpl, self.authInfo = None, [1, "No auth info"]
31 self.create_api(connectInfo)
32 self.force_login(connectInfo)
34 def create_api(self, connectInfo):
35 vimtype = connectInfo['vimtype'] if 'vimtype' in connectInfo else None
36 logger.info("call %s, vimtype=%s" % (fun_name(), vimtype))
37 from lcm.pub.nfvi.vim.api.multivim.api import MultiVimApi
38 self.apiImpl = MultiVimApi()
40 def api_call(self, funname, fun, *args):
41 logger.info("call %s%s" % (funname, str(args)))
44 ret = fun(self.authInfo[1], *args) if self.authInfo[0] == 0 else self.authInfo
45 except VimException as e:
47 except RequestException as e:
48 logger.error("request=%s, url=%s" % (e.request.headers._store, e.request.url))
49 logger.error(traceback.format_exc())
50 ret = [1, e.message if e.message else str(sys.exc_info())]
51 except Exception as ex:
52 logger.error(traceback.format_exc())
53 ret = [1, ex.message if ex.message else str(sys.exc_info())]
55 logger.error(traceback.format_exc())
56 ret = [1, str(sys.exc_info())]
57 logger.info("[%s]ret=%s" % (funname, ret))
60 def force_login(self, connectInfo):
62 logger.info("call %s(%s)" % (fun_name(), connectInfo))
64 self.authInfo = self.apiImpl.login(connectInfo)
65 except VimException as e:
66 self.authInfo = [1, e.args[0]]
67 except Exception as e:
68 logger.error(traceback.format_exc())
69 logger.error(str(sys.exc_info()))
70 self.authInfo = [1, e.args[0] if e.args[0] else str(sys.exc_info())]
72 logger.error(traceback.format_exc())
73 self.authInfo = [1, str(sys.exc_info())]
74 logger.info("self.authInfo=%s" % self.authInfo)
76 def query_net(self, net_id):
77 return self.api_call(fun_name(), self.apiImpl.query_net, net_id)
80 return self.api_call(fun_name(), self.apiImpl.query_nets)
82 def query_subnet(self, subnet_id):
83 return self.api_call(fun_name(), self.apiImpl.query_subnet, subnet_id)
85 def query_port(self, port_id):
86 return self.api_call(fun_name(), self.apiImpl.query_port, port_id)
88 def create_image(self, data):
89 return self.api_call(fun_name(), self.apiImpl.create_image, data)
91 def get_image(self, image_id):
92 return self.api_call(fun_name(), self.apiImpl.get_image, image_id)
95 return self.api_call(fun_name(), self.apiImpl.get_images)
97 def delete_image(self, image_id):
98 return self.api_call(fun_name(), self.apiImpl.delete_image, image_id)
100 def create_network(self, data):
101 return self.api_call(fun_name(), self.apiImpl.create_network, data)
103 def delete_network(self, network_id):
104 return self.api_call(fun_name(), self.apiImpl.delete_network, network_id)
106 def delete_subnet(self, subnet_id):
107 return self.api_call(fun_name(), self.apiImpl.delete_subnet, subnet_id)
109 def create_port(self, data):
110 return self.api_call(fun_name(), self.apiImpl.create_port, data)
112 def delete_port(self, port_id):
113 return self.api_call(fun_name(), self.apiImpl.delete_port, port_id)