fix creat pnf error
[vfc/nfvo/lcm.git] / lcm / pub / msapi / sdc_run_catalog.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 json
16 import logging
17
18 from lcm.pub.utils.restcall import req_by_msb
19 from lcm.pub.exceptions import NSLCMException
20
21 logger = logging.getLogger(__name__)
22
23
24 def parse_nsd(csar_id, input_parameters=[]):
25     req_param = json.JSONEncoder().encode({"csarId": csar_id, "inputs": input_parameters})
26     ret = req_by_msb("/api/catalog/v1/parsernsd", "POST", req_param)
27     if ret[0] != 0:
28         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
29         raise NSLCMException("Failed to parsernsd of CSAR(%s) from catalog." % csar_id)
30     ns_model = json.JSONDecoder().decode(ret[1])
31     return ns_model.get("model")
32
33
34 def parse_vnfd(csar_id, input_parameters=[]):
35     req_param = json.JSONEncoder().encode({"csarId": csar_id, "inputs": input_parameters})
36     ret = req_by_msb("/api/catalog/v1/parservnfd", "POST", req_param)
37     if ret[0] != 0:
38         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
39         raise NSLCMException("Failed to parse_vnfd of CSAR(%s) from catalog." % csar_id)
40     vnf_model = json.JSONDecoder().decode(ret[1])
41     return vnf_model.get("model")
42
43
44 def query_nspackage_by_id(csar_id):
45     ret = req_by_msb("/api/catalog/v1/nspackages/%s" % csar_id, "GET")
46     if ret[0] != 0:
47         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
48         raise NSLCMException("Failed to query ns CSAR(%s) from catalog." % csar_id)
49     return json.JSONDecoder().decode(ret[1])
50
51
52 def query_vnfpackage_by_id(csar_id):
53     ret = req_by_msb("/api/catalog/v1/vnfpackages/%s" % csar_id, "GET")
54     if ret[0] != 0:
55         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
56         raise NSLCMException("Failed to query vnf CSAR(%s) from catalog." % csar_id)
57     return json.JSONDecoder().decode(ret[1])
58
59
60 def query_pnf_descriptor(filter=None):
61     if filter:
62         pnfdId = filter.get("pnfdId")
63         ret = req_by_msb("/api/nsd/v1/pnf_descriptors?pnfdId=%s" % pnfdId, "GET")
64     else:
65         ret = req_by_msb("/api/nsd/v1/pnf_descriptors", "GET")
66     if ret[0] != 0:
67         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
68         raise NSLCMException("Failed to query pnf descriptor(%s) from catalog." % pnfdId)
69     return json.JSONDecoder().decode(ret[1])