Merge "Add test_get_res_from_aai"
[multicloud/framework.git] / multivimbroker / multivimbroker / api_v2 / api_router / v0_controller.py
1 #    Licensed under the Apache License, Version 2.0 (the "License");
2 #    you may not use this file except in compliance with the License.
3 #    You may obtain a copy of the License at
4 #
5 #        http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS,
9 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 #    See the License for the specific language governing permissions and
11 #    limitations under the License.
12
13 import logging
14 import pecan
15
16 from multivimbroker.pub import exceptions
17 from multivimbroker.pub.utils import restcall
18 from multivimbroker.pub.utils import syscomm
19 from multivimbroker.swagger import utils
20
21
22 logger = logging.getLogger(__name__)
23
24 # TODO: Move to a constant file.
25 REGISTRY_URI = "registry"
26 UNREGISTRY_URI = ""
27 IDENTITY_URI = "identity/v3"
28 IDENTITY_AUTH_URI = "identity/v3/auth/tokens"
29
30
31 class V0_Controller(object):
32
33     @pecan.expose('json')
34     def vim_types(self):
35         return syscomm.getVIMTypes()
36
37     @pecan.expose('json', route="swagger.json")
38     def swagger_json(self):
39         return utils.get_swagger_json_data()
40
41     def _filter_illegal_uri(self, uri, method):
42         """
43         Filter unsupported actions, so they can be stopped at begginning.
44         """
45
46         if uri == REGISTRY_URI and method != "POST":
47             pecan.abort(405)
48
49         if uri == UNREGISTRY_URI and method != "DELETE":
50             pecan.abort(405)
51
52         if (uri in (IDENTITY_URI, IDENTITY_AUTH_URI) and
53                 method not in ("POST", "GET")):
54             pecan.abort(405)
55
56     @pecan.expose()
57     def _route(self, remainder, request):
58         uri = "/".join(remainder[1:])
59         method = request.method
60         self._filter_illegal_uri(uri, method)
61
62         return self.forwarder, remainder
63
64     @pecan.expose('json')
65     def forwarder(self, *remainder, **kwargs):
66         """ Forward any requests that don't have a specific match """
67
68         # TODO(xiaohhui): Add validator for vim_id.
69         vim_id = remainder[0]
70         request = pecan.request
71         try:
72             vim_url = syscomm.getMultivimDriver(vim_id,
73                                                 full_path=request.path)
74
75             # NOTE: Not sure headers should be set here. According to original
76             # code, headers are discarded.
77             retcode, content, status_code, resp = restcall.req_by_msb(
78                 vim_url, request.method, content=request.body)
79         except exceptions.NotFound as e:
80             pecan.abort(404, detail=str(e))
81         except Exception as e:
82             pecan.abort(500, detail=str(e))
83
84         if retcode:
85             # Execptions are handled within req_by_msb
86             logger.error("Status code is %s, detail is %s.",
87                          status_code, content)
88         response = pecan.Response(body=content, status=status_code)
89
90         for k in syscomm.getHeadersKeys(resp):
91             response.headers[k] = resp[k]
92
93         return response