Remove call nslcm from vnf pkg api
[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         ret = ns_package.ns_delete_csar(csar_id)
81         normal_status = status.HTTP_202_ACCEPTED
82     logger.info("Leave %s, Return value is %s", fun_name(), ret)
83     if ret[0] != 0:
84         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
85     return Response(data=ret[1], status=normal_status)
86
87
88 @api_view(http_method_names=['DELETE', 'GET'])
89 def nf_rd_csar(request, *args, **kwargs):
90     csar_id = ignore_case_get(kwargs, "csarId")
91     logger.info("Enter %s, method is %s, csar_id is %s", fun_name(), request.method, csar_id)
92     ret, normal_status = None, None
93     if request.method == 'GET':
94         ret = nf_package.nf_get_csar(csar_id)
95         normal_status = status.HTTP_200_OK
96     elif request.method == 'DELETE':
97         force_delete = csar_id.endswith("force")
98         if force_delete:
99             csar_id = csar_id[:-5]
100         job_id = str(uuid.uuid4())
101         nf_package.NfPkgDeleteThread(csar_id, job_id, force_delete).start()
102         ret = [0, {"jobId": job_id}]
103         normal_status = status.HTTP_202_ACCEPTED
104     logger.info("Leave %s, Return value is %s", fun_name(), ret)
105     if ret[0] != 0:
106         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
107     return Response(data=ret[1], status=normal_status)
108
109
110 @api_view(http_method_names=['POST'])
111 def ns_model_parser(request, *args, **kwargs):
112     csar_id = ignore_case_get(request.data, "csarId")
113     inputs = ignore_case_get(request.data, "inputs")
114     logger.debug("Enter %s, csar_id=%s, inputs=%s", fun_name(), csar_id, inputs)
115     ret = ns_package.parse_nsd(csar_id, inputs)
116     logger.info("Leave %s, Return value is %s", fun_name(), ret)
117     if ret[0] != 0:
118         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
119     return Response(data=ret[1], status=status.HTTP_202_ACCEPTED)
120
121
122 @api_view(http_method_names=['POST'])
123 def vnf_model_parser(request, *args, **kwargs):
124     csar_id = ignore_case_get(request.data, "csarId")
125     inputs = ignore_case_get(request.data, "inputs")
126     logger.debug("Enter %s, csar_id=%s, inputs=%s", fun_name(), csar_id, inputs)
127     nf_package.parse_vnfd(csar_id, inputs)
128     logger.info("Leave %s, Return value is %s", fun_name(), ret)
129     if ret[0] != 0:
130         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
131     return Response(data=ret[1], status=status.HTTP_202_ACCEPTED)