Merge "Add build-in workflow to ns instant"
[vfc/nfvo/lcm.git] / lcm / ns / ns_instant.py
1 # Copyright 2016-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 import traceback
18 import time
19 import uuid
20
21 from rest_framework import status
22
23 from lcm.pub.database.models import DefPkgMappingModel, ServiceBaseInfoModel, InputParamMappingModel
24 from lcm.pub.database.models import NSInstModel, NfPackageModel, VNFFGInstModel
25 from lcm.pub.msapi.catalog import get_process_id, get_download_url_from_catalog
26 from lcm.pub.msapi.catalog import query_rawdata_from_catalog, get_servicetemplate_id, get_servicetemplate
27 from lcm.pub.msapi.wso2bpel import workflow_run
28 from lcm.pub.msapi.extsys import select_vnfm
29 from lcm.pub.utils.jobutil import JobUtil
30 from lcm.pub.utils import toscautil
31 from lcm.pub.utils.values import ignore_case_get
32 from lcm.pub.exceptions import NSLCMException
33 from lcm.pub.config.config import WORKFLOW_OPTION
34 from lcm.workflows import build_in
35
36 logger = logging.getLogger(__name__)
37
38
39 class InstantNSService(object):
40     def __init__(self, ns_inst_id, plan_content):
41         self.ns_inst_id = ns_inst_id
42         self.req_data = plan_content
43
44     def do_biz(self):
45         try:
46             job_id = JobUtil.create_job("NS", "NS_INST", self.ns_inst_id)
47             logger.debug('ns-instant(%s) workflow starting...' % self.ns_inst_id)
48             logger.debug('req_data=%s' % self.req_data)
49             ns_inst = NSInstModel.objects.get(id=self.ns_inst_id)
50
51             input_parameters = []
52             for key, val in self.req_data['additionalParamForNs'].items():
53                 input_parameters.append({"key": key, "value": val})
54
55             vim_id = ''
56             if 'location' in self.req_data['additionalParamForNs']:
57                 vim_id = self.req_data['additionalParamForNs']['location']
58             location_constraints = []
59             if 'locationConstraints' in self.req_data:
60                 location_constraints = self.req_data['locationConstraints']
61             
62             JobUtil.add_job_status(job_id, 5, 'Start query nsd(%s)' % ns_inst.nspackage_id)
63             src_plan = query_rawdata_from_catalog(ns_inst.nspackage_id, input_parameters)
64             dst_plan = toscautil.convert_nsd_model(src_plan["rawData"])
65             logger.debug('tosca plan dest:%s' % dst_plan)
66             NSInstModel.objects.filter(id=self.ns_inst_id).update(nsd_model=dst_plan)
67
68             params_json = json.JSONEncoder().encode(self.req_data["additionalParamForNs"])
69             # start
70             params_vnf = []
71             plan_dict = json.JSONDecoder().decode(dst_plan)
72             for vnf in ignore_case_get(plan_dict, "vnfs"):
73                 vnfd_id = vnf['properties']['id']
74                 vnfd = NfPackageModel.objects.get(vnfdid=vnfd_id)
75                 vnfd_model = json.JSONDecoder().decode(vnfd.vnfdmodel)
76                 vnfm_type = vnfd_model["metadata"].get("vnfmType", "ztevmanagerdriver")
77                 vimid = self.get_vnf_vim_id(vim_id, location_constraints, vnfd_id)
78                 vnfm_info = select_vnfm(vnfm_type=vnfm_type, vim_id=vimid)
79                 params_vnf.append({
80                     "vnfProfileId": vnf["vnf_id"],
81                     "additionalParam": {
82                         "vimId": vimid,
83                         "vnfmInstanceId": vnfm_info["vnfmId"],
84                         "vnfmType": vnfm_type,
85                         "inputs": params_json
86                     }
87                 })
88             # end
89             
90             self.set_vl_vim_id(vim_id, location_constraints, plan_dict)
91             dst_plan = json.JSONEncoder().encode(plan_dict)
92             logger.debug('tosca plan dest add vimid:%s' % dst_plan)
93             NSInstModel.objects.filter(id=self.ns_inst_id).update(nsd_model=dst_plan)
94             
95             vnf_params_json = json.JSONEncoder().encode(params_vnf)
96             plan_input = {'jobId': job_id, 
97                 'nsInstanceId': self.req_data["nsInstanceId"],
98                 'object_context': dst_plan,
99                 'object_additionalParamForNs': params_json,
100                 'object_additionalParamForVnf': vnf_params_json}
101             plan_input.update(**self.get_model_count(dst_plan))
102             plan_input["sdnControllerId"] = ignore_case_get(
103                 self.req_data['additionalParamForNs'], "sdncontroller")
104
105             ServiceBaseInfoModel(service_id=self.ns_inst_id,
106                                  service_name=ns_inst.name,
107                                  service_type='NFVO',
108                                  description=ns_inst.description,
109                                  active_status='--',
110                                  status=ns_inst.status,
111                                  creator='--',
112                                  create_time=int(time.time()*1000)).save()
113
114             service_tpl = get_servicetemplate(ns_inst.nsd_id)
115             DefPkgMappingModel(service_id=self.ns_inst_id,
116                                service_def_id=service_tpl['csarId'],
117                                template_name=service_tpl['templateName'],
118                                template_id=service_tpl['serviceTemplateId']).save()
119
120             for key, val in self.req_data['additionalParamForNs'].items():
121                 InputParamMappingModel(service_id=self.ns_inst_id,
122                     input_key=key, input_value=val).save()
123
124             for vnffg in ignore_case_get(plan_dict, "vnffgs"):
125                 VNFFGInstModel(vnffgdid=vnffg["vnffg_id"],
126                     vnffginstid=str(uuid.uuid4()),
127                     nsinstid=self.ns_inst_id,
128                     endpointnumber=0).save()
129
130             if WORKFLOW_OPTION == "wso2":
131                 return self.start_wso2_workflow(job_id, ns_inst, plan_input)
132             elif WORKFLOW_OPTION == "activiti":
133                 return self.start_activiti_workflow()
134             else:
135                 return self.start_buildin_workflow(job_id, plan_input)
136
137         except Exception as e:
138             logger.error(traceback.format_exc())
139             logger.error("ns-instant(%s) workflow error:%s" % (self.ns_inst_id, e.message))
140             JobUtil.add_job_status(job_id, 255, 'NS instantiation failed: %s' % e.message)
141             return dict(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
142     
143     def start_wso2_workflow(self, job_id, ns_inst, plan_input):
144         servicetemplate_id = get_servicetemplate_id(ns_inst.nsd_id)
145         process_id = get_process_id('init', servicetemplate_id)
146         data = {"processId": process_id, "params": {"planInput": plan_input}}
147         logger.debug('ns-instant(%s) workflow data:%s' % (self.ns_inst_id, data))
148
149         ret = workflow_run(data)
150         logger.info("ns-instant(%s) workflow result:%s" % (self.ns_inst_id, ret))
151         JobUtil.add_job_status(job_id, 10, 'NS inst(%s) workflow started: %s' % (
152             self.ns_inst_id, ret.get('status')))
153         if ret.get('status') == 1:
154             return dict(data={'jobId': job_id}, status=status.HTTP_200_OK)
155         return dict(data={'error': ret['message']}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
156
157     def start_activiti_workflow(self):
158         pass
159
160     def start_buildin_workflow(self, job_id, plan_input):
161         build_in.run_ns_instantiate(plan_input)
162         return dict(data={'jobId': job_id}, status=status.HTTP_200_OK)
163
164     def get_vnf_vim_id(self, vim_id, location_constraints, vnfdid):
165         for location in location_constraints:
166             if "vnfProfileId" in location and vnfdid == location["vnfProfileId"]:
167                 return location["locationConstraints"]["vimId"]
168         if vim_id:
169             return vim_id
170         raise NSLCMException("No Vim info is found for vnf(%s)." % vnfdid)
171         
172     def set_vl_vim_id(self, vim_id, location_constraints, plan_dict):
173         if "vls" not in plan_dict:
174             logger.debug("No vl is found in nsd.")
175             return
176         vl_vnf = {}
177         for vnf in ignore_case_get(plan_dict, "vnfs"):
178             if "dependencies" in vnf:
179                 for depend in vnf["dependencies"]:
180                     vl_vnf[depend["vl_id"]] = vnf['properties']['id']
181         vnf_vim = {}
182         for location in location_constraints:
183             if "vnfProfileId" in location:
184                 vnfd_id = location["vnfProfileId"]
185                 vnf_vim[vnfd_id] = location["locationConstraints"]["vimId"]
186         for vl in plan_dict["vls"]:
187             vnfdid = ignore_case_get(vl_vnf, vl["vl_id"])
188             vimid = ignore_case_get(vnf_vim, vnfdid)
189             if not vimid:
190                 vimid = vim_id
191             if not vimid:
192                 raise NSLCMException("No Vim info for vl(%s) of vnf(%s)." % (vl["vl_id"], vnfdid))
193             if "location_info" not in vl["properties"]:
194                 vl["properties"]["location_info"] = {}
195             vl["properties"]["location_info"]["vimid"] = vimid
196        
197     @staticmethod
198     def get_model_count(context):
199         data = json.JSONDecoder().decode(context)
200         vls = len(data.get('vls', []))
201         sfcs = len(data.get('fps', []))
202         vnfs = len(data.get('vnfs', []))
203         return {'vlCount': str(vls), 'sfcCount': str(sfcs), 'vnfCount': str(vnfs)}