Add create vl of build-in workflow
[vfc/nfvo/lcm.git] / lcm / workflows / build_in.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 import logging
15 import traceback
16
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
21
22 logger = logging.getLogger(__name__)
23
24 RESULT_OK, RESULT_NG = "0", "1"
25
26 """
27 format of input_data
28 {
29     "jobId": uuid of job, 
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
38 }
39 """
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")
51     
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)
55
56     update_job(job_id, 30, "0", "Start to create VNF")
57     for i in range(vnf_count):
58         create_vnf()
59     wait_until_job_done()
60
61     update_job(job_id, 70, "0", "Start to create SFC")
62     for i in range(sfc_count):
63         create_sfc()
64     wait_until_job_done()
65
66     update_job(job_id, 90, "0", "Start to post deal")
67     post_deal()
68
69     update_job(job_id, 100, "0", "Create NS successfully.")
70
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,
75         "vlIndex": vl_index,
76         "context": nsd,
77         "additionalParamForNs": ns_param
78     })
79
80     ret = restcall.req_by_msb(uri, "POST", data)
81     if ret[0] != 0:
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)
84
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)
91
92     logger.debug("Create VL(%s) successfully.", vl_id)
93
94 def create_vnf():
95     # TODO:
96     pass
97
98 def create_sfc():
99     # TODO:
100     pass
101
102 def post_deal():
103     # TODO:
104     pass    
105
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,
110         "errcode": errcode,
111         "desc": desc
112     })
113     restcall.req_by_msb(uri, "POST", data)  
114
115 def wait_until_job_done():
116     # TODO:
117     pass