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
5 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
22 logger = logging.getLogger(__name__)
24 # TODO: Move to a constant file.
25 REGISTRY_URI = "registry"
27 IDENTITY_URI = "identity/v3"
28 IDENTITY_AUTH_URI = "identity/v3/auth/tokens"
31 class V0_Controller(object):
35 return syscomm.getVIMTypes()
37 @pecan.expose('json', route="swagger.json")
38 def swagger_json(self):
39 return utils.get_swagger_json_data()
41 def _filter_illegal_uri(self, uri, method):
43 Filter unsupported actions, so they can be stopped at begginning.
46 if uri == REGISTRY_URI and method != "POST":
49 if uri == UNREGISTRY_URI and method != "DELETE":
52 if (uri in (IDENTITY_URI, IDENTITY_AUTH_URI) and
53 method not in ("POST", "GET")):
57 def _route(self, remainder, request):
58 uri = "/".join(remainder[1:])
59 method = request.method
60 self._filter_illegal_uri(uri, method)
62 return self.forwarder, remainder
65 def forwarder(self, *remainder, **kwargs):
66 """ Forward any requests that don't have a specific match """
68 # TODO(xiaohhui): Add validator for vim_id.
70 request = pecan.request
72 vim_url = syscomm.getMultivimDriver(vim_id,
73 full_path=request.path)
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))
85 # Execptions are handled within req_by_msb
86 logger.error("Status code is %s, detail is %s.",
88 response = pecan.Response(body=content, status=status_code)
90 for k in syscomm.getHeadersKeys(resp):
91 response.headers[k] = resp[k]