summaryrefslogtreecommitdiffstats
path: root/vio/pub/vim/vimadaptor.py
blob: 1af3e05433db32a21b79450a97aa9d56dc0a377c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright 2016 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import sys
import traceback

from requests import RequestException

from vio.pub.utils.syscomm import fun_name
from vio.pub.vim import const
from vio.pub.exceptions import VimDriverVioException

logger = logging.getLogger(__name__)


class VimAdaptor:
    def __init__(self, connectInfo):
        logger.info("[VimAdaptor]connectInfo=%s" % connectInfo)
        self.apiImpl, self.authInfo = None, [1, "No auth info"]
        self.create_api(connectInfo)
        self.force_login(connectInfo)

    def create_api(self, connectInfo):
        vimtype = connectInfo['vimtype'] if 'vimtype' in connectInfo else None
        logger.info("call %s, vimtype=%s" % (fun_name(), vimtype))
        if vimtype == const.VIM_OPENSTACK:
            from vio.pub.nfvi.vim.api.openstack.api import OpenstackApi
            self.apiImpl = OpenstackApi()
        else:
            self.authInfo = [1, "Unsupported vimtype(%s)" % vimtype]

    def api_call(self, funname, fun, *args):
        logger.info("call %s%s" % (funname, str(args)))
        ret = None
        try:
            ret = fun(self.authInfo[1], *args) if self.authInfo[0] == 0 else self.authInfo
        except VimException as e:
            ret = [1, e.message]
        except RequestException as e:
            logger.error("request=%s, url=%s" % (e.request.headers._store, e.request.url))
            logger.error(traceback.format_exc())
            ret = [1, e.message if e.message else str(sys.exc_info())]
        except Exception as ex:
            logger.error(traceback.format_exc())
            ret = [1, ex.message if ex.message else str(sys.exc_info())]
        except:
            logger.error(traceback.format_exc())
            ret = [1, str(sys.exc_info())]
        logger.info("[%s]ret=%s" % (funname, ret))
        return ret

    def force_login(self, connectInfo):
        if self.apiImpl:
            logger.info("call %s(%s)" % (fun_name(), connectInfo))
            try:
                self.authInfo = self.apiImpl.login(connectInfo)
            except VimException as e:
                self.authInfo = [1, e.message]
            except Exception as ex:
                logger.error(traceback.format_exc())
                logger.error(str(sys.exc_info()))
                self.authInfo = [1, ex.message if ex.message else str(sys.exc_info())]
            except:
                logger.error(traceback.format_exc())
                self.authInfo = [1, str(sys.exc_info())]
            logger.info("self.authInfo=%s" % self.authInfo)

    def query_net(self, net_id):
        return self.api_call(fun_name(), self.apiImpl.query_net, net_id)

    def query_nets(self):
        return self.api_call(fun_name(), self.apiImpl.query_nets)

    def query_subnet(self, subnet_id):
        return self.api_call(fun_name(), self.apiImpl.query_subnet, subnet_id)

    def query_port(self, port_id):
        return self.api_call(fun_name(), self.apiImpl.query_port, port_id)

    def create_image(self, data):
        return self.api_call(fun_name(), self.apiImpl.create_image, data)

    def get_image(self, image_id):
        return self.api_call(fun_name(), self.apiImpl.get_image, image_id)

    def get_images(self):
        return self.api_call(fun_name(), self.apiImpl.get_images)

    def delete_image(self, image_id):
        return self.api_call(fun_name(), self.apiImpl.delete_image, image_id)

    def create_network(self, data):
        return self.api_call(fun_name(), self.apiImpl.create_network, data)

    def delete_network(self, network_id):
        return self.api_call(fun_name(), self.apiImpl.delete_network, network_id)

    def delete_subnet(self, subnet_id):
        return self.api_call(fun_name(), self.apiImpl.delete_subnet, subnet_id)

    def create_port(self, data):
        return self.api_call(fun_name(), self.apiImpl.create_port, data)

    def delete_port(self, port_id):
        return self.api_call(fun_name(), self.apiImpl.delete_port, port_id)