Add code for parsing nsd model
[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 @api_view(http_method_names=['POST', 'GET'])
29 def nspackages_rc(request, *args, **kwargs):
30     logger.debug("Enter %s, method is %s", fun_name(), request.method)
31     ret, normal_status = None, None
32
33     if request.method == 'GET':
34         # Gets ns package list
35         ret = ns_package.ns_get_csars();
36         normal_status = status.HTTP_200_OK
37     elif request.method == 'POST':
38         # Distributes the package accroding to the given csarId
39         csar_id = ignore_case_get(request.data, "csarId")
40         logger.debug("csar_id is %s", csar_id)
41         ret = ns_package.ns_on_distribute(csar_id)
42         normal_status = status.HTTP_202_ACCEPTED
43     logger.debug("Leave %s, Return value is %s", fun_name(), ret)
44     if ret[0] != 0:
45         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
46     return Response(data=ret[1], status=normal_status)
47
48 @api_view(http_method_names=['POST', 'GET'])
49 def nfpackages_rc(request, *args, **kwargs):
50     logger.debug("Enter %s%s, method is %s", fun_name(), request.data, request.method)
51     ret, normal_status = None, None
52     if request.method == 'GET':
53         ret = nf_package.nf_get_csars()
54         normal_status = status.HTTP_200_OK
55     elif request.method == 'POST':
56         csar_id = ignore_case_get(request.data, "csarId")
57         vim_ids = ignore_case_get(request.data, "vimIds")
58         lab_vim_id = ignore_case_get(request.data, "labVimId")
59         job_id = str(uuid.uuid4())
60         nf_package.NfDistributeThread(csar_id, vim_ids, lab_vim_id, job_id).start()
61         ret = [0, {"jobId": job_id}]
62         normal_status = status.HTTP_202_ACCEPTED
63     logger.debug("Leave %s, Return value is %s", fun_name(), ret)
64     if ret[0] != 0:
65         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
66     return Response(data=ret[1], status=normal_status)
67
68 @api_view(http_method_names=['DELETE', 'GET'])
69 def ns_rd_csar(request, *args, **kwargs):
70     csar_id = ignore_case_get(kwargs, "csarId")
71     logger.info("Enter %s, method is %s, csar_id is %s", fun_name(), request.method, csar_id)
72     ret, normal_status = None, None
73     if request.method == 'GET':
74         ret = ns_package.ns_get_csar(csar_id)
75         normal_status = status.HTTP_200_OK
76     elif request.method == 'DELETE':
77         force_delete = csar_id.endswith("force")
78         if force_delete:
79             csar_id = csar_id[:-5]
80         ret = ns_package.ns_delete_csar(csar_id, force_delete)
81         normal_status = status.HTTP_202_ACCEPTED
82     logger.info("Leave %s, Return value is %s", fun_name(), str(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 @api_view(http_method_names=['DELETE', 'GET'])
88 def nf_rd_csar(request, *args, **kwargs):
89     csar_id = ignore_case_get(kwargs, "csarId")
90     logger.info("Enter %s, method is %s, csar_id is %s", fun_name(), request.method, csar_id)
91     ret, normal_status = None, None
92     if request.method == 'GET':
93         ret = nf_package.nf_get_csar(csar_id)
94         normal_status = status.HTTP_200_OK
95     elif request.method == 'DELETE':
96         force_delete = csar_id.endswith("force")
97         if force_delete:
98             csar_id = csar_id[:-5]
99         job_id = str(uuid.uuid4())
100         nf_package.NfPkgDeleteThread(csar_id, job_id, force_delete).start()
101         ret = [0, {"jobId": job_id}]
102         normal_status = status.HTTP_202_ACCEPTED
103     logger.info("Leave %s, Return value is %s", fun_name(), str(ret))
104     if ret[0] != 0:
105         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
106     return Response(data=ret[1], status=normal_status)
107
108 @api_view(http_method_names=['POST'])
109 def ns_model_parser(request, *args, **kwargs):
110     csar_id = ignore_case_get(request.data, "csarId")
111     inputs = ignore_case_get(request.data, "inputs")
112     if request.method == 'POST':
113         ret = ns_package.parser_nsdmodel(csar_id,inputs)
114         normal_status = status.HTTP_202_ACCEPTED
115
116     logger.info("Leave %s, Return value is %s", fun_name(), str(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=normal_status)
120
121 @api_view(http_method_names=['POST'])
122 def vnf_model_parser(request, *args, **kwargs):
123     csar_id = ignore_case_get(request.data, "csarId")
124     inputs = ignore_case_get(request.data, "inputs")
125     if request.method == 'POST':
126         ret = nf_package.parser_vnfdmodel(csar_id,inputs)
127         normal_status = status.HTTP_202_ACCEPTED
128
129     logger.info("Leave %s, Return value is %s", fun_name(), str(ret))
130     if ret[0] != 0:
131         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
132     return Response(data=ret[1], status=normal_status)