1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from lcm.pub.utils.syscomm import fun_name
18 from lcm.pub.utils.values import ignore_case_get
19 from lcm.pub.utils import restcall
20 from lcm.pub.exceptions import NSLCMException
22 logger = logging.getLogger(__name__)
24 RESULT_OK, RESULT_NG = "0", "1"
30 "nsInstanceId": id of ns instance,
31 "object_context": json format of nsd,
32 "object_additionalParamForNs": json format of additional parameters for ns,
33 "object_additionalParamForVnf": json format of additional parameters for vnf,
34 "vlCount": int type of VL count,
35 "vnfCount: int type of VNF count,
36 "sfcCount": int type of SFC count,
37 "sdnControllerId": uuid of SDN controller
40 def run_ns_instantiate(input_data):
41 logger.debug("Enter %s, input_data is %s", fun_name(), input_data)
42 job_id = ignore_case_get(input_data, "jobId")
43 ns_inst_id = ignore_case_get(input_data, "nsInstanceId")
44 nsd_json = ignore_case_get(input_data, "object_context")
45 ns_param_json = ignore_case_get(input_data, "object_additionalParamForNs")
46 vnf_param_json = ignore_case_get(input_data, "object_additionalParamForVnf")
47 vl_count = ignore_case_get(input_data, "vlCount")
48 vnf_count = ignore_case_get(input_data, "vnfCount")
49 sfc_count = ignore_case_get(input_data, "sfcCount")
50 sdnc_id = ignore_case_get(input_data, "sdnControllerId")
52 update_job(job_id, 10, "0", "Start to create VL")
53 for i in range(vl_count):
54 create_vl(ns_inst_id, i + 1, nsd_json, ns_param_json)
56 update_job(job_id, 30, "0", "Start to create VNF")
57 for i in range(vnf_count):
61 update_job(job_id, 70, "0", "Start to create SFC")
62 for i in range(sfc_count):
66 update_job(job_id, 90, "0", "Start to post deal")
69 update_job(job_id, 100, "0", "Create NS successfully.")
71 def create_vl(ns_inst_id, vl_index, nsd, ns_param):
72 uri = "api/nslcm/v1/ns/vls"
73 data = json.JSONEncoder().encode({
74 "nsInstanceId": ns_inst_id,
77 "additionalParamForNs": ns_param
80 ret = restcall.req_by_msb(uri, "POST", data)
82 logger.error("Failed to call create_vl(%s): %s", vl_index, ret[1])
83 raise NSLCMException("Failed to call create_vl(index is %s)" % vl_index)
85 result = str(ret[1]["result"])
86 detail = ret[1]["detail"]
87 vl_id = ret[1]["vlId"]
88 if result != RESULT_OK:
89 logger.error("Failed to create VL(%s): %s", vl_id, detail)
90 raise NSLCMException("Failed to create VL(%s)" % vl_id)
92 logger.debug("Create VL(%s) successfully.", vl_id)
106 def update_job(job_id, progress, errcode, desc):
107 uri = "api/nslcm/v1/jobs/{jobId}".format(jobId=job_id)
108 data = json.JSONEncoder().encode({
109 "progress": progress,
113 restcall.req_by_msb(uri, "POST", data)
115 def wait_until_job_done():