1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from rest_framework import status
16 from rest_framework.response import Response
17 from rest_framework.views import APIView
18 from drf_yasg.utils import swagger_auto_schema
20 from lcm.ns.vls.create_vls import CreateVls
21 from lcm.ns.vls.delete_vls import DeleteVls
22 from lcm.ns.vls.get_vls import GetVls
23 from lcm.ns.vls.serializers import CreateVlReqSerializer, CreateVlRespSerializer
24 from lcm.ns.vls.serializers import GetVlRespSerializer
25 from lcm.ns.vls.serializers import DeleteVlRespSerializer
29 logger = logging.getLogger(__name__)
32 class VlView(APIView):
34 request_body=CreateVlReqSerializer(),
36 status.HTTP_201_CREATED: CreateVlRespSerializer()
39 def post(self, request):
40 logger.debug("VlsCreateView--post::> %s" % request.data)
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)
48 resp = CreateVls(request.data).do()
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)
56 return Response(data=resp, status=status.HTTP_201_CREATED)
59 class VlDetailView(APIView):
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"
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()
72 return Response(status=status.HTTP_404_NOT_FOUND)
74 resp_serializer = GetVlRespSerializer(data={
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)
83 return Response(status=status.HTTP_200_OK, data=resp_serializer.data)
88 status.HTTP_202_ACCEPTED: DeleteVlRespSerializer()
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()
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)
101 return Response(data=resp, status=status.HTTP_202_ACCEPTED)