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 NetworkInstModel
\r
25 from res.pub.database.models import SubNetworkInstModel
\r
26 from res.pub.database.models import CPInstModel
\r
27 from res.resources.serializers import VolumeInfoSerializer
\r
28 from res.resources.serializers import CpsInfoSerializer
\r
29 from res.resources.serializers import SubnetInfoSerializer
\r
30 from res.resources.serializers import NetworkInfoSerializer
\r
31 from res.resources.views.base_view import view_safe_call_with_log
\r
33 logger = logging.getLogger(__name__)
\r
36 def query_resources(res_type, logger, resources, cvt_fun, res_serializer):
\r
37 logger.debug("Enter query %s", res_type)
\r
40 'resp_data': [cvt_fun(res) for res in resources]
\r
43 resp_serializer = res_serializer(data=resp)
\r
44 if not resp_serializer.is_valid():
\r
45 raise VNFRESException(resp_serializer.errors)
\r
49 status=status.HTTP_200_OK
\r
53 class getNetworks(APIView):
\r
54 @swagger_auto_schema(
\r
56 status.HTTP_200_OK: NetworkInfoSerializer(),
\r
57 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
\r
60 @view_safe_call_with_log(logger=logger)
\r
61 def get(self, request, vnfInstanceId):
\r
62 return query_resources(
\r
63 res_type="Networks",
\r
65 resources=NetworkInstModel.objects.filter(instid=vnfInstanceId),
\r
66 cvt_fun=fill_networks_data,
\r
67 res_serializer=NetworkInfoSerializer
\r
71 def fill_networks_data(network):
\r
73 "networkid": network.networkid,
\r
74 "vimid": network.vimid,
\r
75 "resouceid": network.resouceid,
\r
76 "insttype": network.insttype,
\r
77 "instid": network.instid,
\r
78 "name": network.name
\r
80 return networks_data
\r
83 class getSubnets(APIView):
\r
84 @swagger_auto_schema(
\r
86 status.HTTP_200_OK: SubnetInfoSerializer(),
\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=SubNetworkInstModel.objects.filter(instid=vnfInstanceId),
\r
96 cvt_fun=fill_subnets_data,
\r
97 res_serializer=SubnetInfoSerializer
\r
101 def fill_subnets_data(subnet):
\r
103 "subnetworkid": subnet.subnetworkid,
\r
104 "vimid": subnet.vimid,
\r
105 "resouceid": subnet.resouceid,
\r
106 "networkid": subnet.networkid,
\r
107 "insttype": subnet.insttype,
\r
108 "instid": subnet.instid,
\r
109 "name": subnet.name,
\r
110 "cidr": subnet.cidr
\r
112 return subnets_data
\r
115 class getCps(APIView):
\r
116 @swagger_auto_schema(
\r
118 status.HTTP_200_OK: CpsInfoSerializer(),
\r
119 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
\r
122 @view_safe_call_with_log(logger=logger)
\r
123 def get(self, request, vnfInstanceId):
\r
124 return query_resources(
\r
127 resources=CPInstModel.objects.filter(ownerid=vnfInstanceId),
\r
128 cvt_fun=fill_cps_data,
\r
129 res_serializer=CpsInfoSerializer
\r
133 def fill_cps_data(cp):
\r
135 "cpinstanceid": cp.cpinstanceid,
\r
137 "cpinstancename": cp.cpinstancename,
\r
138 "vlinstanceid": cp.vlinstanceid,
\r
139 "ownertype": cp.ownertype,
\r
140 "ownerid": cp.ownerid,
\r
141 "relatedtype": cp.relatedtype
\r
146 class getVolumes(APIView):
\r
147 @swagger_auto_schema(
\r
149 status.HTTP_200_OK: VolumeInfoSerializer(),
\r
150 status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'
\r
153 @view_safe_call_with_log(logger=logger)
\r
154 def get(self, request, vnfInstanceId):
\r
155 return query_resources(
\r
156 res_type="Volumes",
\r
158 resources=StorageInstModel.objects.filter(instid=vnfInstanceId),
\r
159 cvt_fun=fill_volumes_data,
\r
160 res_serializer=VolumeInfoSerializer
\r
164 def fill_volumes_data(v):
\r
166 "storageid": v.storageid,
\r
168 "resouceid": v.resouceid,
\r
169 "insttype": v.insttype,
\r
170 "instid": v.instid,
\r
171 "storagetype": v.storagetype,
\r
174 return volumes_data
\r