Implements NBIs of multivimbroker
[multicloud/framework.git] / multivimbroker / multivimbroker / forwarder / views.py
1 # Copyright 2017 Wind River Systems, 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 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import logging
15 import re
16
17 from django.http import HttpResponse
18 from django.views.decorators.csrf import csrf_exempt
19
20 from rest_framework import status
21
22 from multivimbroker.pub.utils.restcall import req_by_msb
23 from multivimbroker.pub.msapi.extsys import get_vim_by_id
24
25 logger = logging.getLogger(__name__)
26
27
28 @csrf_exempt
29 def route(request, vimid=''):
30     """ get vim info from vimid from local cache first
31         and then to ESR if cache miss
32     """
33     content = ""
34     status_code = status.HTTP_200_OK
35     try:
36         vim = get_vim_by_id(vimid)
37
38         # if vim type is openstack, use latest "newton" version as default
39         if vim["type"] == "openstack":
40             vim["type"] = "multivim-newton"
41
42         route_uri = re.sub('multivim', vim["type"], request.get_full_path())
43         retcode, content, status_code = \
44             req_by_msb(route_uri, request.method, request.body)
45         if retcode != 0:
46             # Execptions are handled within req_by_msb
47             logger.error("Status code is %s, detail is %s.",
48                          status_code, content)
49     except Exception as e:
50         content = e
51         status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
52     return HttpResponse(content, status_code)