update version of lcm
[vfc/nfvo/lcm.git] / lcm / ns / sfcs / detail_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 import logging
15
16 from rest_framework import status
17 from rest_framework.response import Response
18 from rest_framework.views import APIView
19 from drf_yasg.utils import swagger_auto_schema
20
21 from lcm.ns.sfcs.delete_sfcs import DeleteSfcs
22 from lcm.ns.sfcs.get_sfcs import GetSfcs
23 from lcm.ns.sfcs.serializers import GetSfcRespSerializer
24 from lcm.ns.sfcs.serializers import DeleteSfcRespSerializer
25
26 logger = logging.getLogger(__name__)
27
28
29 class SfcDetailView(APIView):
30     @swagger_auto_schema(
31         request_body=None,
32         responses={
33             status.HTTP_200_OK: GetSfcRespSerializer(),
34             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error",
35             status.HTTP_404_NOT_FOUND: "SFC not found"
36         }
37     )
38     def get(self, request, sfc_inst_id):
39         logger.debug("SfcCreateView--get::> %s", sfc_inst_id)
40         sfc_inst_info = GetSfcs(sfc_inst_id).do()
41         if not sfc_inst_info:
42             return Response(status=status.HTTP_404_NOT_FOUND)
43
44         resp_data = {'sfcInstId': sfc_inst_id,
45                      'sfcName': "xxx",
46                      'sfcStatus': sfc_inst_info[0].status}
47
48         resp_serializer = GetSfcRespSerializer(data=resp_data)
49         if not resp_serializer.is_valid():
50             logger.error(resp_serializer.errors)
51             return Response(data={'error': resp_serializer.errors},
52                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
53
54         return Response(status=status.HTTP_200_OK, data=resp_serializer.data)
55
56     @swagger_auto_schema(
57         request_body=None,
58         responses={
59             status.HTTP_202_ACCEPTED: DeleteSfcRespSerializer()
60         }
61     )
62     def delete(self, request_paras, sfc_inst_id):
63         resp = DeleteSfcs(sfc_inst_id).do()
64
65         resp_serializer = DeleteSfcRespSerializer(data=resp)
66         if not resp_serializer.is_valid():
67             logger.error(resp_serializer.errors)
68             return Response(data={"result": 1, "detail": resp_serializer.errors},
69                             status=status.HTTP_202_ACCEPTED)
70
71         return Response(data=resp, status=status.HTTP_202_ACCEPTED)