Fix some bugs in vfc catalog
[vfc/nfvo/catalog.git] / catalog / packages / views.py
1 # Copyright 2017 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
15 import logging
16 import uuid
17 from catalog.pub.utils.syscomm import fun_name
18 from rest_framework.response import Response
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from catalog.pub.utils.values import ignore_case_get
22 from catalog.packages import nf_package
23 from catalog.packages import ns_package
24
25
26 logger = logging.getLogger(__name__)
27
28
29 @api_view(http_method_names=['POST', 'GET'])
30 def nspackages_rc(request, *args, **kwargs):
31     logger.debug("Enter %s, method is %s", fun_name(), request.method)
32     ret, normal_status = None, None
33
34     if request.method == 'GET':
35         # Gets ns package list
36         ret = ns_package.ns_get_csars()
37         normal_status = status.HTTP_200_OK
38     elif request.method == 'POST':
39         # Distributes the package accroding to the given csarId
40         csar_id = ignore_case_get(request.data, "csarId")
41         logger.debug("csar_id is %s", csar_id)
42         ret = ns_package.ns_on_distribute(csar_id)
43         normal_status = status.HTTP_202_ACCEPTED
44     logger.debug("Leave %s, Return value is %s", fun_name(), ret)
45     if ret[0] != 0:
46         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
47     return Response(data=ret[1], status=normal_status)
48
49
50 @api_view(http_method_names=['POST', 'GET'])
51 def nfpackages_rc(request, *args, **kwargs):
52     logger.debug("Enter %s%s, method is %s", fun_name(), request.data, request.method)
53     ret, normal_status = None, None
54     if request.method == 'GET':
55         ret = nf_package.nf_get_csars()
56         normal_status = status.HTTP_200_OK
57     elif request.method == 'POST':
58         csar_id = ignore_case_get(request.data, "csarId")
59         vim_ids = ignore_case_get(request.data, "vimIds")
60         lab_vim_id = ignore_case_get(request.data, "labVimId")
61         job_id = str(uuid.uuid4())
62         nf_package.NfDistributeThread(csar_id, vim_ids, lab_vim_id, job_id).start()
63         ret = [0, {"jobId": job_id}]
64         normal_status = status.HTTP_202_ACCEPTED
65     logger.debug("Leave %s, Return value is %s", fun_name(), ret)
66     if ret[0] != 0:
67         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
68     return Response(data=ret[1], status=normal_status)
69
70
71 @api_view(http_method_names=['DELETE', 'GET'])
72 def ns_rd_csar(request, *args, **kwargs):
73     csar_id = ignore_case_get(kwargs, "csarId")
74     logger.info("Enter %s, method is %s, csar_id is %s", fun_name(), request.method, csar_id)
75     ret, normal_status = None, None
76     if request.method == 'GET':
77         ret = ns_package.ns_get_csar(csar_id)
78         normal_status = status.HTTP_200_OK
79     elif request.method == 'DELETE':
80         force_delete = csar_id.endswith("force")
81         if force_delete:
82             csar_id = csar_id[:-5]
83         ret = ns_package.ns_delete_csar(csar_id, force_delete)
84         normal_status = status.HTTP_202_ACCEPTED
85     logger.info("Leave %s, Return value is %s", fun_name(), ret)
86     if ret[0] != 0:
87         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
88     return Response(data=ret[1], status=normal_status)
89
90
91 @api_view(http_method_names=['DELETE', 'GET'])
92 def nf_rd_csar(request, *args, **kwargs):
93     csar_id = ignore_case_get(kwargs, "csarId")
94     logger.info("Enter %s, method is %s, csar_id is %s", fun_name(), request.method, csar_id)
95     ret, normal_status = None, None
96     if request.method == 'GET':
97         ret = nf_package.nf_get_csar(csar_id)
98         normal_status = status.HTTP_200_OK
99     elif request.method == 'DELETE':
100         force_delete = csar_id.endswith("force")
101         if force_delete:
102             csar_id = csar_id[:-5]
103         job_id = str(uuid.uuid4())
104         nf_package.NfPkgDeleteThread(csar_id, job_id, force_delete).start()
105         ret = [0, {"jobId": job_id}]
106         normal_status = status.HTTP_202_ACCEPTED
107     logger.info("Leave %s, Return value is %s", fun_name(), ret)
108     if ret[0] != 0:
109         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
110     return Response(data=ret[1], status=normal_status)
111
112
113 @api_view(http_method_names=['POST'])
114 def ns_model_parser(request, *args, **kwargs):
115     csar_id = ignore_case_get(request.data, "csarId")
116     inputs = ignore_case_get(request.data, "inputs")
117     if request.method == 'POST':
118         ret = ns_package.parser_nsdmodel(csar_id,inputs)
119         normal_status = status.HTTP_202_ACCEPTED
120     else:
121         ret = [1, "Request is not allowed"]
122         normal_status = ""
123
124     logger.info("Leave %s, Return value is %s", fun_name(), ret)
125     if ret[0] != 0:
126         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
127     return Response(data=ret[1], status=normal_status)
128
129
130 @api_view(http_method_names=['POST'])
131 def vnf_model_parser(request, *args, **kwargs):
132     csar_id = ignore_case_get(request.data, "csarId")
133     inputs = ignore_case_get(request.data, "inputs")
134     if request.method == 'POST':
135         ret = nf_package.parser_vnfdmodel(csar_id,inputs)
136         normal_status = status.HTTP_202_ACCEPTED
137     else:
138         ret = [1, "Request is not allowed"]
139         normal_status = ""
140
141     logger.info("Leave %s, Return value is %s", fun_name(), ret)
142     if ret[0] != 0:
143         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
144     return Response(data=ret[1], status=normal_status)