update get network structre
[vfc/gvnfm/vnfres.git] / res / res / resources / views / views.py
1 # Copyright 2017 ZTE Corporation.\r
2 #\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
6 #\r
7 #         http://www.apache.org/licenses/LICENSE-2.0\r
8 #\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
14 \r
15 import logging\r
16 \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
21 \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
30 \r
31 logger = logging.getLogger(__name__)\r
32 \r
33 \r
34 def query_resources(res_type, logger, resources, cvt_fun, res_serializer):\r
35     logger.debug("Enter query %s", res_type)\r
36 \r
37     resp = {\r
38         'resp_data': [cvt_fun(res) for res in resources]\r
39     }\r
40 \r
41     resp_serializer = res_serializer(data=resp)\r
42     if not resp_serializer.is_valid():\r
43         raise VNFRESException(resp_serializer.errors)\r
44 \r
45     return Response(\r
46         data=resp,\r
47         status=status.HTTP_200_OK\r
48     )\r
49 \r
50 \r
51 class getSubnets(APIView):\r
52     @swagger_auto_schema(\r
53         responses={\r
54             status.HTTP_200_OK: SubnetInfoSerializer(),\r
55             status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
56         }\r
57     )\r
58     @view_safe_call_with_log(logger=logger)\r
59     def get(self, request, vnfInstanceId):\r
60         return query_resources(\r
61             res_type="Subnets",\r
62             logger=logger,\r
63             resources=SubNetworkInstModel.objects.filter(instid=vnfInstanceId),\r
64             cvt_fun=fill_subnets_data,\r
65             res_serializer=SubnetInfoSerializer\r
66         )\r
67 \r
68 \r
69 def fill_subnets_data(subnet):\r
70     subnets_data = {\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
78         "cidr": subnet.cidr\r
79     }\r
80     return subnets_data\r
81 \r
82 \r
83 class getCps(APIView):\r
84     @swagger_auto_schema(\r
85         responses={\r
86             status.HTTP_200_OK: CpsInfoSerializer(),\r
87             status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
88         }\r
89     )\r
90     @view_safe_call_with_log(logger=logger)\r
91     def get(self, request, vnfInstanceId):\r
92         return query_resources(\r
93             res_type="Cps",\r
94             logger=logger,\r
95             resources=CPInstModel.objects.filter(ownerid=vnfInstanceId),\r
96             cvt_fun=fill_cps_data,\r
97             res_serializer=CpsInfoSerializer\r
98         )\r
99 \r
100 \r
101 def fill_cps_data(cp):\r
102     cps_data = {\r
103         "cpinstanceid": cp.cpinstanceid,\r
104         "cpdid": cp.cpdid,\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
110     }\r
111     return cps_data\r
112 \r
113 \r
114 class getVolumes(APIView):\r
115     @swagger_auto_schema(\r
116         responses={\r
117             status.HTTP_200_OK: VolumeInfoSerializer(),\r
118             status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
119         }\r
120     )\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
125             logger=logger,\r
126             resources=StorageInstModel.objects.filter(instid=vnfInstanceId),\r
127             cvt_fun=fill_volumes_data,\r
128             res_serializer=VolumeInfoSerializer\r
129         )\r
130 \r
131 \r
132 def fill_volumes_data(v):\r
133     volumes_data = {\r
134         "storageid": v.storageid,\r
135         "vimid": v.vimid,\r
136         "resouceid": v.resouceid,\r
137         "insttype": v.insttype,\r
138         "instid": v.instid,\r
139         "storagetype": v.storagetype,\r
140         "size": v.size\r
141     }\r
142     return volumes_data\r