512bb6a51ba7771c12e656393bceda5c4e5043b9
[multicloud/framework.git] / multivimbroker / multivimbroker / forwarder / base.py
1 # Copyright (c) 2017-2018 VMware, 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 #
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 logging
14
15 from django.http import HttpResponse
16 from rest_framework import status
17
18 import multivimbroker.pub.exceptions as exceptions
19 from multivimbroker.pub.utils.syscomm import getHeadersKeys
20 from multivimbroker.pub.utils.syscomm import getMultivimDriver
21 from multivimbroker.pub.utils.restcall import req_by_msb
22 from multivimbroker.pub.utils.restcall import req_by_msb_multipart
23
24
25 logger = logging.getLogger(__name__)
26
27
28 class BaseHandler(object):
29
30     def _request(self, route_uri, method, body="", headers=None):
31
32         try:
33             if "multipart" in route_uri:
34                 return self._multipart_req(route_uri, method, body, headers)
35             retcode, content, status_code, resp = \
36                 req_by_msb(route_uri, method, body, headers)
37             if retcode != 0:
38                 # Execptions are handled within req_by_msb
39                 logger.error("Status code is %s, detail is %s.",
40                              status_code, content)
41
42         except exceptions.NotFound as e:
43             return HttpResponse(str(e), status=status.HTTP_404_NOT_FOUND)
44
45         except Exception as e:
46             content = e
47             status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
48             logger.exception("exception: %s" % e)
49
50         response = HttpResponse(content, status=status_code)
51         for k in getHeadersKeys(resp):
52             response[k] = resp[k]
53         return response
54
55     def _multipart_req(self, route_uri, method, body, headers=None):
56
57         try:
58             retcode, content, status_code, resp = \
59                 req_by_msb_multipart(route_uri, method, body, headers)
60             if retcode != 0:
61                 # Execptions are handled within req_by_msb
62                 logger.error("Status code is %s, detail is %s.",
63                              status_code, content)
64
65         except exceptions.NotFound as e:
66             return HttpResponse(str(e), status=status.HTTP_404_NOT_FOUND)
67
68         except Exception as e:
69             content = e
70             status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
71             logger.exception("exception: %s" % e)
72
73         response = HttpResponse(content, status=status_code)
74         return response
75
76     def send(self, vimid, full_path, body, method, headers=None):
77
78         try:
79             url = getMultivimDriver(vimid, full_path=full_path)
80         except exceptions.VimBrokerException as e:
81             logging.exception("vimbroker exception: %s" % e)
82             return HttpResponse(e.content, status=e.status_code)
83         except Exception as e:
84             logging.exception("unkown exception: %s" % e)
85             return HttpResponse(str(e),
86                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
87
88         return self._request(url, method, body=body, headers=headers)