Add swagger auto generate logic for vfc
[vfc/nfvo/lcm.git] / lcm / swagger / views.py
1 # Copyright 2016-2017 ZTE Corporation.
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 json
15 import logging
16 import os
17
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
20 from rest_framework import permissions
21 from drf_yasg.views import get_schema_view
22
23 logger = logging.getLogger(__name__)
24
25
26 SchemaView = get_schema_view(
27     validators=['ssv', 'flex'],
28     public=True,
29     permission_classes=(permissions.AllowAny,),
30 )
31
32
33 class SwaggerJsonView(APIView):
34
35     def get(self, request):
36         json_file = os.path.join(os.path.dirname(__file__), 'vfc.nslcm.swagger.json')
37         f = open(json_file)
38         json_data = json.JSONDecoder().decode(f.read())
39         f.close()
40
41         json_file = os.path.join(os.path.dirname(__file__), 'vfc.vnflcm.swagger.json')
42         f = open(json_file)
43         json_data_temp = json.JSONDecoder().decode(f.read())
44         f.close()
45
46         json_data["paths"].update(json_data_temp["paths"])
47         json_data["definitions"].update(json_data_temp["definitions"])
48
49         json_file = os.path.join(os.path.dirname(__file__), 'vfc.vllcm.swagger.json')
50         f = open(json_file)
51         json_data_temp = json.JSONDecoder().decode(f.read())
52         f.close()
53
54         json_data["paths"].update(json_data_temp["paths"])
55         json_data["definitions"].update(json_data_temp["definitions"])
56
57         json_file = os.path.join(os.path.dirname(__file__), 'vfc.sfclcm.swagger.json')
58         f = open(json_file)
59         json_data_temp = json.JSONDecoder().decode(f.read())
60         f.close()
61
62         json_data["paths"].update(json_data_temp["paths"])
63         json_data["definitions"].update(json_data_temp["definitions"])
64
65         json_file = os.path.join(os.path.dirname(__file__), 'vfc.db.swagger.json')
66         f = open(json_file)
67         json_data_temp = json.JSONDecoder().decode(f.read())
68         f.close()
69
70         json_data["paths"].update(json_data_temp["paths"])
71         json_data["definitions"].update(json_data_temp["definitions"])
72
73         json_file = os.path.join(os.path.dirname(__file__), 'vfc.others.swagger.json')
74         f = open(json_file)
75         json_data_temp = json.JSONDecoder().decode(f.read())
76         f.close()
77
78         json_data_jobtemp = json_data["paths"]["/jobs/{jobId}"]
79         json_data["paths"].update(json_data_temp["paths"])
80         json_data["paths"]["/jobs/{jobId}"].update(json_data_jobtemp)
81         json_data["definitions"].update(json_data_temp["definitions"])
82         return Response(json_data)