SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns_vls / views / views.py
1 # Copyright 2016 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
15 import logging
16
17 from drf_yasg.utils import swagger_auto_schema
18 from lcm.ns_vls.biz.delete_vls import DeleteVls
19 from lcm.ns_vls.biz.get_vls import GetVls
20 from lcm.ns_vls.serializers.serializers import CreateVlReqSerializer, CreateVlRespSerializer
21 from lcm.ns_vls.serializers.serializers import DeleteVlRespSerializer
22 from lcm.ns_vls.serializers.serializers import GetVlRespSerializer
23 from rest_framework import status
24 from rest_framework.response import Response
25 from rest_framework.views import APIView
26
27 from lcm.ns_vls.biz.create_vls import CreateVls
28
29 logger = logging.getLogger(__name__)
30
31
32 class VlView(APIView):
33     @swagger_auto_schema(
34         request_body=CreateVlReqSerializer(),
35         responses={
36             status.HTTP_201_CREATED: CreateVlRespSerializer()
37         }
38     )
39     def post(self, request):
40         logger.debug("VlsCreateView--post::> %s" % request.data)
41
42         req_serializer = CreateVlReqSerializer(data=request.data)
43         if not req_serializer.is_valid():
44             logger.error(req_serializer.errors)
45             resp = {"result": 1, "detail": req_serializer.errors, "vlId": ""}
46             return Response(data=resp, status=status.HTTP_201_CREATED)
47
48         resp = CreateVls(request.data).do()
49
50         resp_serializer = CreateVlRespSerializer(data=resp)
51         if not resp_serializer.is_valid():
52             logger.error(resp_serializer.errors)
53             resp = {"result": 1, "detail": resp_serializer.errors, "vlId": ""}
54             return Response(data=resp, status=status.HTTP_201_CREATED)
55
56         return Response(data=resp, status=status.HTTP_201_CREATED)
57
58
59 class VlDetailView(APIView):
60     @swagger_auto_schema(
61         request_body=None,
62         responses={
63             status.HTTP_200_OK: GetVlRespSerializer(),
64             status.HTTP_404_NOT_FOUND: "VL instance is not found",
65             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
66         }
67     )
68     def get(self, request, vl_inst_id):
69         logger.debug("VlDetailView--get::> %s" % vl_inst_id)
70         vl_inst_info = GetVls(vl_inst_id).do()
71         if not vl_inst_info:
72             return Response(status=status.HTTP_404_NOT_FOUND)
73
74         resp_serializer = GetVlRespSerializer(data={
75             'vlId': vl_inst_id,
76             'vlName': vl_inst_info[0].vlinstancename,
77             'vlStatus': "active"})
78         if not resp_serializer.is_valid():
79             logger.error(resp_serializer.errors)
80             return Response(data={'error': resp_serializer.errors},
81                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
82
83         return Response(status=status.HTTP_200_OK, data=resp_serializer.data)
84
85     @swagger_auto_schema(
86         request_body=None,
87         responses={
88             status.HTTP_202_ACCEPTED: DeleteVlRespSerializer()
89         }
90     )
91     def delete(self, request_paras, vl_inst_id):
92         logger.debug("VlDetailView--delete::> %s" % vl_inst_id)
93         resp = DeleteVls(vl_inst_id).do()
94
95         resp_serializer = DeleteVlRespSerializer(data=resp)
96         if not resp_serializer.is_valid():
97             logger.error(resp_serializer.errors)
98             resp = {"result": 0, "detail": resp_serializer.errors}
99             return Response(data=resp, status=status.HTTP_202_ACCEPTED)
100
101         return Response(data=resp, status=status.HTTP_202_ACCEPTED)