1 # Copyright 2017 ZTE Corporation.
\r
3 # Licensed under the Apache License, Version 2.0 (the "License");
\r
4 # you may not use this file except in compliance with the License.
\r
5 # You may obtain a copy of the License at
\r
7 # http://www.apache.org/licenses/LICENSE-2.0
\r
9 # Unless required by applicable law or agreed to in writing, software
\r
10 # distributed under the License is distributed on an "AS IS" BASIS,
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 # See the License for the specific language governing permissions and
\r
13 # limitations under the License.
\r
17 from drf_yasg.utils import swagger_auto_schema
\r
18 from rest_framework import status
\r
19 from rest_framework.response import Response
\r
20 from rest_framework.views import APIView
\r
22 from res.pub.exceptions import VNFRESException
\r
23 from res.pub.database.models import StorageInstModel
\r
24 from res.pub.database.models import SubNetworkInstModel
\r
25 from res.pub.database.models import CPInstModel
\r
26 from res.resources.serializers import VolumeInfoSerializer
\r
27 from res.resources.serializers import CpsInfoSerializer
\r
28 from res.resources.serializers import SubnetInfoSerializer
\r
29 from res.resources.views.base_view import view_safe_call_with_log
\r
31 logger = logging.getLogger(__name__)
\r
34 def query_resources(res_type, logger, resources, cvt_fun, res_serializer):
\r
35 logger.debug("Enter query %s", res_type)
\r
38 'resp_data': [cvt_fun(res) for res in resources]
\r
41 resp_serializer = res_serializer(data=resp)
\r
42 if not resp_serializer.is_valid():
\r
43 raise VNFRESException(resp_serializer.errors)
\r
47 status=status.HTTP_200_OK
\r
51 class getSubnets(APIView):
\r
52 @swagger_auto_schema(
\r
54 status.HTTP_200_OK: SubnetInfoSerializer(),
\r
55 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
\r
58 @view_safe_call_with_log(logger=logger)
\r
59 def get(self, request, vnfInstanceId):
\r
60 return query_resources(
\r
63 resources=SubNetworkInstModel.objects.filter(instid=vnfInstanceId),
\r
64 cvt_fun=fill_subnets_data,
\r
65 res_serializer=SubnetInfoSerializer
\r
69 def fill_subnets_data(subnet):
\r
71 "subnetworkid": subnet.subnetworkid,
\r
72 "vimid": subnet.vimid,
\r
73 "resouceid": subnet.resouceid,
\r
74 "networkid": subnet.networkid,
\r
75 "insttype": subnet.insttype,
\r
76 "instid": subnet.instid,
\r
77 "name": subnet.name,
\r
83 class getCps(APIView):
\r
84 @swagger_auto_schema(
\r
86 status.HTTP_200_OK: CpsInfoSerializer(),
\r
87 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
\r
90 @view_safe_call_with_log(logger=logger)
\r
91 def get(self, request, vnfInstanceId):
\r
92 return query_resources(
\r
95 resources=CPInstModel.objects.filter(ownerid=vnfInstanceId),
\r
96 cvt_fun=fill_cps_data,
\r
97 res_serializer=CpsInfoSerializer
\r
101 def fill_cps_data(cp):
\r
103 "cpinstanceid": cp.cpinstanceid,
\r
105 "cpinstancename": cp.cpinstancename,
\r
106 "vlinstanceid": cp.vlinstanceid,
\r
107 "ownertype": cp.ownertype,
\r
108 "ownerid": cp.ownerid,
\r
109 "relatedtype": cp.relatedtype
\r
114 class getVolumes(APIView):
\r
115 @swagger_auto_schema(
\r
117 status.HTTP_200_OK: VolumeInfoSerializer(),
\r
118 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
\r
121 @view_safe_call_with_log(logger=logger)
\r
122 def get(self, request, vnfInstanceId):
\r
123 return query_resources(
\r
124 res_type="Volumes",
\r
126 resources=StorageInstModel.objects.filter(instid=vnfInstanceId),
\r
127 cvt_fun=fill_volumes_data,
\r
128 res_serializer=VolumeInfoSerializer
\r
132 def fill_volumes_data(v):
\r
134 "storageid": v.storageid,
\r
136 "resouceid": v.resouceid,
\r
137 "insttype": v.insttype,
\r
138 "instid": v.instid,
\r
139 "storagetype": v.storagetype,
\r
142 return volumes_data
\r