Merge "SO License issue fix take 3"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateE2EServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved. 
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.openecomp.mso.bpmn.infrastructure.scripts;
22
23 import static org.apache.commons.lang3.StringUtils.*;
24 import org.apache.http.HttpResponse
25 import groovy.xml.XmlUtil
26 import groovy.json.*
27
28 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
29 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
30 import org.openecomp.mso.bpmn.core.domain.ModelInfo
31 import org.openecomp.mso.bpmn.core.domain.Resource
32 import org.openecomp.mso.bpmn.core.domain.AllottedResource
33 import org.openecomp.mso.bpmn.core.domain.NetworkResource
34 import org.openecomp.mso.bpmn.core.domain.VnfResource
35 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
36 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder;
37 import org.openecomp.mso.bpmn.common.recipe.BpmnRestClient
38 import org.openecomp.mso.bpmn.core.json.JsonUtils
39 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
40 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
41 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
42 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
43 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils;
44 import org.openecomp.mso.bpmn.core.RollbackData
45 import org.openecomp.mso.bpmn.core.WorkflowException
46 import org.openecomp.mso.rest.APIResponse;
47 import org.openecomp.mso.rest.RESTClient
48 import org.openecomp.mso.rest.RESTConfig
49
50 import java.util.List;
51 import java.util.UUID;
52
53 import org.camunda.bpm.engine.delegate.BpmnError
54 import org.camunda.bpm.engine.delegate.DelegateExecution
55 import org.json.JSONObject;
56 import org.json.JSONArray;
57 import org.apache.commons.lang3.*
58 import org.apache.commons.codec.binary.Base64;
59 import org.springframework.web.util.UriUtils;
60
61 /**
62  * This groovy class supports the <class>DoCreateServiceInstance.bpmn</class> process.
63  *
64  * Inputs:
65  * @param - msoRequestId
66  * @param - globalSubscriberId
67  * @param - subscriptionServiceType
68  * @param - serviceInstanceId
69  * @param - serviceInstanceName - O
70  * @param - serviceModelInfo
71  * @param - productFamilyId
72  * @param - disableRollback
73  * @param - failExists - TODO
74  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
75  * @param - sdncVersion ("1610")
76  * @param - serviceDecomposition - Decomposition for R1710 
77  * (if macro provides serviceDecompsition then serviceModelInfo, serviceInstanceId & serviceInstanceName will be ignored)
78  *
79  * Outputs:
80  * @param - rollbackData (localRB->null)
81  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
82  * @param - WorkflowException
83  * @param - serviceInstanceName - (GET from AAI if null in input)
84  *
85  */
86 public class DoCreateE2EServiceInstance extends AbstractServiceTaskProcessor {
87
88         String Prefix="DCRESI_"
89         ExceptionUtil exceptionUtil = new ExceptionUtil()
90         JsonUtils jsonUtil = new JsonUtils()
91         CatalogDbUtils cutils = new CatalogDbUtils()
92
93         public void preProcessRequest (DelegateExecution execution) {
94                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
95                 String msg = ""
96                 utils.log("INFO"," ***** preProcessRequest *****",  isDebugEnabled)
97
98                 try {
99                         execution.setVariable("prefix", Prefix)
100                         //Inputs
101                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
102                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
103                         utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId,  isDebugEnabled)
104                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
105                         String serviceType = execution.getVariable("serviceType")
106                         utils.log("INFO"," ***** serviceType *****" + serviceType,  isDebugEnabled)
107                         //requestDetails.requestParameters. for SDNC assignTopology
108                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
109
110                         if (isBlank(globalSubscriberId)) {
111                                 msg = "Input globalSubscriberId is null"
112                                 utils.log("INFO", msg, isDebugEnabled)
113                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
114                         }
115                         
116                         if (isBlank(serviceType)) {
117                                 msg = "Input serviceType is null"
118                                 utils.log("INFO", msg, isDebugEnabled)
119                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
120                         }
121                         
122                         if (productFamilyId == null) {
123                                 execution.setVariable("productFamilyId", "")
124                         }
125                         
126                         String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
127                         if (isBlank(sdncCallbackUrl)) {
128                                 msg = "URN_mso_workflow_sdncadapter_callback is null"
129                                 utils.log("INFO", msg, isDebugEnabled)
130                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
131                         }
132                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
133                         utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
134
135                         //requestDetails.modelInfo.for AAI PUT servieInstanceData                       
136                         //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData 
137                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
138                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
139                         String uuiRequest = execution.getVariable("uuiRequest")
140                         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceDefId")
141                         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.templateId")
142                         String serviceModelName = jsonUtil.getJsonValue(uuiRequest, "service.parameters.templateName")
143                         execution.setVariable("serviceModelName", serviceModelName)
144                         //aai serviceType and Role can be setted as fixed value now.
145                         String aaiServiceType = "E2E Service"
146                         String aaiServiceRole = "E2E Service"
147                         
148                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)
149                         execution.setVariable("modelUuid", modelUuid)
150
151                         //AAI PUT
152                         String oStatus = execution.getVariable("initialStatus") ?: ""
153                         if ("TRANSPORT".equalsIgnoreCase(serviceType))
154                         {
155                                 oStatus = "Created"
156                         }
157
158                         String statusLine = isBlank(oStatus) ? "" : "<orchestration-status>${oStatus}</orchestration-status>"
159                                 
160                         AaiUtil aaiUriUtil = new AaiUtil(this)
161                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
162                         String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
163                         String serviceInstanceData =
164                                         """<service-instance xmlns=\"${namespace}\">
165                                 <service-instance-id>${serviceInstanceId}</service-instance-id>
166                                 <service-instance-name>${serviceInstanceName}</service-instance-name>
167                                         <service-type>${aaiServiceType}</service-type>
168                                         <service-role>${aaiServiceRole}</service-role>
169                                         ${statusLine}
170                                     <model-invariant-id>${modelInvariantUuid}</model-invariant-id>
171                                     <model-version-id>${modelUuid}</model-version-id>
172                                         </service-instance>""".trim()                                   
173                         execution.setVariable("serviceInstanceData", serviceInstanceData)
174                         utils.logAudit(serviceInstanceData)
175                         utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
176                         utils.log("INFO", " 'payload' to create Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
177
178                 } catch (BpmnError e) {
179                         throw e;
180                 } catch (Exception ex){
181                         msg = "Exception in preProcessRequest " + ex.getMessage()
182                         utils.log("INFO", msg, isDebugEnabled)
183                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
184                 }
185                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
186         }
187         
188    public void prepareDecomposeService(DelegateExecution execution) {
189         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
190
191         try {
192             utils.log("DEBUG", " ***** Inside prepareDecomposeService of create generic e2e service ***** ", isDebugEnabled)
193             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
194             String modelUuid = execution.getVariable("modelUuid")
195             //here modelVersion is not set, we use modelUuid to decompose the service.
196             String serviceModelInfo = """{
197             "modelInvariantUuid":"${modelInvariantUuid}",
198             "modelUuid":"${modelUuid}",
199             "modelVersion":""
200              }"""
201             execution.setVariable("serviceModelInfo", serviceModelInfo)
202
203             utils.log("DEBUG", " ***** Completed prepareDecomposeService of  create generic e2e service ***** ", isDebugEnabled)
204         } catch (Exception ex) {
205             // try error in method block
206             String exceptionMessage = "Bpmn error encountered in  create generic e2e service flow. Unexpected Error from method prepareDecomposeService() - " + ex.getMessage()
207             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
208         }
209      }
210
211     public void processDecomposition(DelegateExecution execution) {
212         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
213     
214         utils.log("DEBUG", " ***** Inside processDecomposition() of  create generic e2e service flow ***** ", isDebugEnabled)    
215         try {
216             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
217         } catch (Exception ex) {
218             String exceptionMessage = "Bpmn error encountered in  create generic e2e service flow. processDecomposition() - " + ex.getMessage()
219             utils.log("DEBUG", exceptionMessage, isDebugEnabled)
220             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
221         }
222     }
223     
224     public void doServiceHoming(DelegateExecution execution) {
225         //Now Homing is not clear. So to be implemented.
226     }
227     
228         public void postProcessAAIGET(DelegateExecution execution) {
229                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
230                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
231                 String msg = ""
232
233                 try {
234                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
235                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
236                         if(!succInAAI){
237                                 utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceName, isDebugEnabled)
238                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
239                                 utils.logAudit("workflowException: " + workflowException)
240                                 if(workflowException != null){
241                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
242                                 }
243                                 else
244                                 {
245                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
246                                         utils.log("INFO", msg, isDebugEnabled)
247                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
248                                 }
249                         }
250                         else
251                         {
252                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
253                                 if(foundInAAI){
254                                         utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
255                                         msg = "ServiceInstance already exists in AAI:" + serviceInstanceName
256                                         utils.log("INFO", msg, isDebugEnabled)
257                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
258                                 }
259                         }
260                 } catch (BpmnError e) {
261                         throw e;
262                 } catch (Exception ex) {
263                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET. " + ex.getMessage()
264                         utils.log("INFO", msg, isDebugEnabled)
265                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
266                 }
267                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
268         }
269
270         public void postProcessAAIPUT(DelegateExecution execution) {
271                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
272                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
273                 String msg = ""
274                 try {
275                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
276                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
277                         if(!succInAAI){
278                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
279                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
280                                 utils.logAudit("workflowException: " + workflowException)
281                                 if(workflowException != null){
282                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
283                                 }
284                         }
285                         else
286                         {
287                                 //start rollback set up
288                                 RollbackData rollbackData = new RollbackData()
289                                 def disableRollback = execution.getVariable("disableRollback")
290                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
291                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
292                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
293                                 rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
294                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
295                                 execution.setVariable("rollbackData", rollbackData)
296                         }
297
298                 } catch (BpmnError e) {
299                         throw e;
300                 } catch (Exception ex) {
301                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
302                         utils.log("INFO", msg, isDebugEnabled)
303                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
304                 }
305                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
306         }
307         
308         public void postProcessAAIGET2(DelegateExecution execution) {
309                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
310                 utils.log("INFO"," ***** postProcessAAIGET2 ***** ", isDebugEnabled)
311                 String msg = ""
312
313                 try {
314                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
315                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
316                         if(!succInAAI){
317                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET2", + serviceInstanceName, isDebugEnabled)
318                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
319                                 utils.logAudit("workflowException: " + workflowException)
320                                 if(workflowException != null){
321                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
322                                 }
323                                 else
324                                 {
325                                         msg = "Failure in postProcessAAIGET2 GENGS_SuccessIndicator:" + succInAAI
326                                         utils.log("INFO", msg, isDebugEnabled)
327                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
328                                 }
329                         }
330                         else
331                         {
332                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
333                                 if(foundInAAI){
334                                         String aaiService = execution.getVariable("GENGS_service")
335                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "service-instance-name"))) {
336                                                 execution.setVariable("serviceInstanceName",  utils.getNodeText1(aaiService, "service-instance-name"))
337                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
338                                         }
339                                 }
340                         }
341                 } catch (BpmnError e) {
342                         throw e;
343                 } catch (Exception ex) {
344                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET2 " + ex.getMessage()
345                         utils.log("INFO", msg, isDebugEnabled)
346                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
347                 }
348                 utils.log("INFO"," *** Exit postProcessAAIGET2 *** ", isDebugEnabled)
349         }
350
351         public void preProcessRollback (DelegateExecution execution) {
352                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
353                 utils.log("INFO"," ***** preProcessRollback ***** ", isDebugEnabled)
354                 try {
355                         
356                         Object workflowException = execution.getVariable("WorkflowException");
357
358                         if (workflowException instanceof WorkflowException) {
359                                 utils.log("INFO", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
360                                 execution.setVariable("prevWorkflowException", workflowException);
361                                 //execution.setVariable("WorkflowException", null);
362                         }
363                 } catch (BpmnError e) {
364                         utils.log("INFO", "BPMN Error during preProcessRollback", isDebugEnabled)
365                 } catch(Exception ex) {
366                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
367                         utils.log("INFO", msg, isDebugEnabled)
368                 }
369                 utils.log("INFO"," *** Exit preProcessRollback *** ", isDebugEnabled)
370         }
371
372         public void postProcessRollback (DelegateExecution execution) {
373                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
374                 utils.log("INFO"," ***** postProcessRollback ***** ", isDebugEnabled)
375                 String msg = ""
376                 try {
377                         Object workflowException = execution.getVariable("prevWorkflowException");
378                         if (workflowException instanceof WorkflowException) {
379                                 utils.log("INFO", "Setting prevException to WorkflowException: ", isDebugEnabled)
380                                 execution.setVariable("WorkflowException", workflowException);
381                         }
382                         execution.setVariable("rollbackData", null)
383                 } catch (BpmnError b) {
384                         utils.log("INFO", "BPMN Error during postProcessRollback", isDebugEnabled)
385                         throw b;
386                 } catch(Exception ex) {
387                         msg = "Exception in postProcessRollback. " + ex.getMessage()
388                         utils.log("INFO", msg, isDebugEnabled)
389                 }
390                 utils.log("INFO"," *** Exit postProcessRollback *** ", isDebugEnabled)
391         }
392
393         public void preInitResourcesOperStatus(DelegateExecution execution){
394         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
395
396         utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
397         try{
398             String serviceId = execution.getVariable("serviceInstanceId")
399             String operationId = execution.getVariable("operationId")
400             String operationType = execution.getVariable("operationType")
401             String resourceTemplateUUIDs = ""
402             String result = "processing"
403             String progress = "0"
404             String reason = ""
405             String operationContent = "Prepare service creation"
406             utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
407             serviceId = UriUtils.encode(serviceId,"UTF-8")
408             execution.setVariable("serviceInstanceId", serviceId)
409             execution.setVariable("operationId", operationId)
410             execution.setVariable("operationType", operationType)
411             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
412             List<Resource>  resourceList = serviceDecomposition.getServiceResources()
413             
414             for(String resource : resourceList){
415                     resourceTemplateUUIDs  = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"                            
416             }           
417
418             def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
419             execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
420             utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
421
422             String payload =
423                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
424                         xmlns:ns="http://org.openecomp.mso/requestsdb">
425                         <soapenv:Header/>
426                         <soapenv:Body>
427                             <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
428                             <serviceId>${serviceId}</serviceId>
429                             <operationId>${operationId}</operationId>
430                             <operationType>${operationType}</operationType>
431                             <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>
432                         </ns:initResourceOperationStatus>
433                     </soapenv:Body>
434                 </soapenv:Envelope>"""
435
436             payload = utils.formatXml(payload)
437             execution.setVariable("CVFMI_initResOperStatusRequest", payload)
438             utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
439             utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
440
441         }catch(Exception e){
442             utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
443             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
444         }
445         utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)  
446         }
447         
448
449         /**
450          * sequence resource. we should analyze resource sequence from service template
451          * Here we make VF first, and then network for E2E service.
452          */
453         public void sequenceResoure(DelegateExecution execution){
454             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
455         utils.log("INFO", "======== Start sequenceResoure Process ======== ", isDebugEnabled)  
456             String serviceModelUUID = execution.getVariable("modelUuid")
457         JSONArray networks = cutils.getAllNetworksByServiceModelUuid(execution, serviceModelUUID)
458         utils.log("DEBUG", "obtained Network list: " + networks, isDebugEnabled)            
459         if (networks == null) {
460             utils.log("INFO", "No matching networks in Catalog DB for serviceModelUUID=" + serviceModelUUID, isDebugEnabled)
461         }
462         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
463                 
464         //we use VF to define a network service
465         List<VnfResource>  vnfResourceList= serviceDecomposition.getServiceVnfs()
466         
467         //here wan is defined as a network resource        
468         List<NetworkResource> networkResourceList = serviceDecomposition.getServiceNetworks()
469
470         //allotted resource
471         List<AllottedResource> arResourceList= serviceDecomposition.getServiceAllottedResources()
472         //define sequenced resource list, we deploy vf first and then network and then ar
473         //this is defaule sequence
474         List<Resource>  sequencedResourceList = new ArrayList<Resource>();
475         if(null != vnfResourceList){
476             sequencedResourceList.addAll(vnfResourceList)
477         }
478         if(null != networkResourceList){
479             sequencedResourceList.addAll(networkResourceList)
480         }
481         if(null != arResourceList){
482             sequencedResourceList.addAll(arResourceList)
483         }
484
485         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
486         execution.setVariable("isContainsWanResource", isContainsWanResource)
487         execution.setVariable("currentResourceIndex", 0)
488         execution.setVariable("sequencedResourceList", sequencedResourceList)
489         utils.log("INFO", "sequencedResourceList: " + sequencedResourceList, isDebugEnabled)  
490         utils.log("INFO", "======== COMPLETED sequenceResoure Process ======== ", isDebugEnabled)  
491         }
492         
493         public void getCurrentResoure(DelegateExecution execution){
494             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
495         utils.log("INFO", "======== Start getCurrentResoure Process ======== ", isDebugEnabled)    
496             def currentIndex = execution.getVariable("currentResourceIndex")
497             List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
498             Resource currentResource = sequencedResourceList.get(currentIndex)
499             utils.log("INFO", "Now we deal with resouce:" + currentResource.getModelInfo().getModelName(), isDebugEnabled)  
500         utils.log("INFO", "======== COMPLETED getCurrentResoure Process ======== ", isDebugEnabled)  
501     }
502
503            /**
504      * sequence resource
505      */
506     public void parseNextResource(DelegateExecution execution){
507         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
508         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)    
509         def currentIndex = execution.getVariable("currentResourceIndex")
510         def nextIndex =  currentIndex + 1
511         execution.setVariable("currentResourceIndex", nextIndex)
512         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")    
513         if(nextIndex >= sequencedResourceList.size()){
514             execution.setVariable("allResourceFinished", "true")
515         }else{
516             execution.setVariable("allResourceFinished", "false")
517         }
518         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)       
519     }
520     
521       /**
522       * post config request.
523       */
524      public void postConfigRequest(DelegateExecution execution){
525          //now do noting
526      } 
527      
528      public void prepareResourceRecipeRequest(DelegateExecution execution){
529          def isDebugEnabled=execution.getVariable("isDebugLogEnabled")                 
530          utils.log("INFO", "======== Start prepareResourceRecipeRequest Process ======== ", isDebugEnabled) 
531          ResourceInput resourceInput = new ResourceInput()         
532          String serviceInstanceName = execution.getVariable("serviceInstanceName")
533          String resourceInstanceName = resourceType + "_" + serviceInstanceName
534          resourceInput.setResourceInstanceName(resourceInstanceName)
535          utils.log("INFO", "Prepare Resource Request resourceInstanceName:" + resourceInstanceName, isDebugEnabled)
536          String globalSubscriberId = execution.getVariable("globalSubscriberId")
537          String serviceType = execution.getVariable("serviceType")
538          String serviceInstanceId = execution.getVariable("serviceInstanceId")
539          String operationId = execution.getVariable("operationId")
540          String operationType = execution.getVariable("operationType")
541          resourceInput.setGlobalSubscriberId(globalSubscriberId)
542          resourceInput.setServiceType(serviceType)
543          resourceInput.setServiceInstanceId(serviceInstanceId)
544          resourceInput.setOperationId(operationId)
545          resourceInput.setOperationType(operationType);
546          def currentIndex = execution.getVariable("currentResourceIndex")
547          List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
548          Resource currentResource = sequencedResourceList.get(currentIndex)
549          resourceInput.setResourceModelInfo(currentResource.getModelInfo());
550          ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
551          resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
552          
553          String incomingRequest = execution.getVariable("uuiRequest")
554          //set the requestInputs from tempalte  To Be Done
555          String serviceModelUuid = execution.getVariable("modelUuid")
556          String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
557          String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
558          resourceInput.setResourceParameters(resourceParameters)
559          execution.setVariable("resourceInput", resourceInput)
560          utils.log("INFO", "======== COMPLETED prepareResourceRecipeRequest Process ======== ", isDebugEnabled)      
561      }
562      
563      public void executeResourceRecipe(DelegateExecution execution){
564          def isDebugEnabled=execution.getVariable("isDebugLogEnabled")                 
565          utils.log("INFO", "======== Start executeResourceRecipe Process ======== ", isDebugEnabled) 
566          String requestId = execution.getVariable("msoRequestId")
567          String serviceInstanceId = execution.getVariable("serviceInstanceId")
568          String serviceType = execution.getVariable("serviceType")
569          ResourceInput resourceInput = execution.getVariable("resourceInput")
570          String requestAction = resourceInput.getOperationType()
571          JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceUuid(), requestAction)
572          String recipeUri = resourceRecipe.getString("orchestrationUri")
573          String recipeTimeOut = resourceRecipe.getString("recipeTimeout")
574          String recipeParamXsd = resourceRecipe.get("paramXSD")
575          HttpResponse resp = BpmnRestClient.post(recipeUri, requestId, recipeTimeout, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
576          
577      }
578 }