Merge "create service instance with source as empty"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / vcpe / scripts / CreateVcpeResCustService.groovy
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.vcpe.scripts;\r
21 \r
22 import groovy.xml.XmlUtil\r
23 import groovy.json.*\r
24 \r
25 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
27 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils\r
28 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
29 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
30 import org.openecomp.mso.bpmn.core.RollbackData\r
31 import org.openecomp.mso.bpmn.core.WorkflowException\r
32 import org.openecomp.mso.bpmn.core.domain.*\r
33 \r
34 import java.util.UUID;\r
35 \r
36 import org.camunda.bpm.engine.delegate.BpmnError\r
37 import org.camunda.bpm.engine.runtime.Execution\r
38 import org.json.JSONObject;\r
39 import org.json.JSONArray;\r
40 import org.apache.commons.lang3.*\r
41 import org.apache.commons.codec.binary.Base64;\r
42 import org.springframework.web.util.UriUtils;\r
43 import static org.apache.commons.lang3.StringUtils.*\r
44 \r
45 /**\r
46  * This groovy class supports the <class>CreateVcpeResCustService.bpmn</class> process.\r
47  *\r
48  * @author ek1439\r
49  *\r
50  */\r
51 public class CreateVcpeResCustService extends AbstractServiceTaskProcessor {\r
52 \r
53         private static final String DebugFlag = "isDebugLogEnabled"\r
54 \r
55         String Prefix="CVRCS_"\r
56         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
57         JsonUtils jsonUtil = new JsonUtils()\r
58         VidUtils vidUtils = new VidUtils()\r
59         CatalogDbUtils catalogDbUtils = new CatalogDbUtils()\r
60 \r
61         /**\r
62          * This method is executed during the preProcessRequest task of the <class>CreateServiceInstance.bpmn</class> process.\r
63          * @param execution\r
64          */\r
65         public InitializeProcessVariables(Execution execution){\r
66                 /* Initialize all the process variables in this block */\r
67 \r
68                 execution.setVariable("createVcpeServiceRequest", "")\r
69                 execution.setVariable("globalSubscriberId", "")\r
70                 execution.setVariable("serviceInstanceName", "")\r
71                 execution.setVariable("msoRequestId", "")\r
72                 execution.setVariable(Prefix+"VnfsCreatedCount", 0)\r
73                 execution.setVariable("productFamilyId", "")\r
74                 execution.setVariable("brgWanMacAddress", "")\r
75 \r
76                 //TODO\r
77                 execution.setVariable("sdncVersion", "1707")\r
78         }\r
79 \r
80         // **************************************************\r
81         //     Pre or Prepare Request Section\r
82         // **************************************************\r
83         /**\r
84          * This method is executed during the preProcessRequest task of the <class>CreateServiceInstance.bpmn</class> process.\r
85          * @param execution\r
86          */\r
87         public void preProcessRequest (Execution execution) {\r
88                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
89                 execution.setVariable("prefix",Prefix)\r
90 \r
91                 utils.log("DEBUG", " ***** Inside preProcessRequest CreateVcpeResCustService Request ***** ", isDebugEnabled)\r
92 \r
93                 try {\r
94                         // initialize flow variables\r
95                         InitializeProcessVariables(execution)\r
96 \r
97                         //Config Inputs\r
98                         String aaiDistDelay = execution.getVariable('URN_mso_workflow_aai_distribution_delay')\r
99                         if (isBlank(aaiDistDelay)) {\r
100                                 msg = "URN_mso_workflow_aai_distribution_delay is null"\r
101                                 utils.log("DEBUG", msg, isDebugEnabled)\r
102                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
103                         }\r
104                         execution.setVariable("aaiDistDelay", aaiDistDelay)\r
105                         utils.log("DEBUG","AAI distribution delay: " + aaiDistDelay, isDebugEnabled)\r
106 \r
107                         // check for incoming json message/input\r
108                         String createVcpeServiceRequest = execution.getVariable("bpmnRequest")\r
109                         utils.logAudit(createVcpeServiceRequest)\r
110                         execution.setVariable("createVcpeServiceRequest", createVcpeServiceRequest);\r
111                         println 'createVcpeServiceRequest - ' + createVcpeServiceRequest\r
112 \r
113                         // extract requestId\r
114                         String requestId = execution.getVariable("mso-request-id")\r
115                         execution.setVariable("msoRequestId", requestId)\r
116 \r
117                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
118 \r
119                         if ((serviceInstanceId == null) || (serviceInstanceId.isEmpty())) {\r
120                                 serviceInstanceId = UUID.randomUUID().toString()\r
121                                 utils.log("DEBUG", " Generated new Service Instance: " + serviceInstanceId , isDebugEnabled)\r
122                         } else {\r
123                                 utils.log("DEBUG", "Using provided Service Instance ID: " + serviceInstanceId , isDebugEnabled)\r
124                         }\r
125 \r
126                         serviceInstanceId = UriUtils.encode(serviceInstanceId,"UTF-8")\r
127                         execution.setVariable("serviceInstanceId", serviceInstanceId)\r
128 \r
129                         String requestAction = execution.getVariable("requestAction")\r
130                         execution.setVariable("requestAction", requestAction)\r
131 \r
132                         setBasicDBAuthHeader(execution, isDebugEnabled)\r
133                         \r
134                         String source = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestInfo.source")\r
135                         if ((source == null) || (source.isEmpty())) {\r
136                                 source = "VID"\r
137                         }\r
138                         execution.setVariable("source", source)\r
139 \r
140                         // extract globalSubscriberId\r
141                         String globalSubscriberId = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.subscriberInfo.globalSubscriberId")\r
142 \r
143                         // verify element global-customer-id is sent from JSON input, throw exception if missing\r
144                         if ((globalSubscriberId == null) || (globalSubscriberId.isEmpty())) {\r
145                                 String dataErrorMessage = " Element 'globalSubscriberId' is missing. "\r
146                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)\r
147 \r
148                         } else {\r
149                                 execution.setVariable("globalSubscriberId", globalSubscriberId)\r
150                                 execution.setVariable("globalCustomerId", globalSubscriberId)\r
151                         }\r
152 \r
153                         // extract subscriptionServiceType\r
154                         String subscriptionServiceType = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestParameters.subscriptionServiceType")\r
155                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)\r
156                         utils.log("DEBUG", "Incoming subscriptionServiceType is: " + subscriptionServiceType, isDebugEnabled)\r
157 \r
158                         String suppressRollback = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestInfo.suppressRollback")\r
159                         execution.setVariable("disableRollback", suppressRollback)\r
160                         utils.log("DEBUG", "Incoming Suppress/Disable Rollback is: " + suppressRollback, isDebugEnabled)\r
161 \r
162                         String productFamilyId = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestInfo.productFamilyId")\r
163                         execution.setVariable("productFamilyId", productFamilyId)\r
164                         utils.log("DEBUG", "Incoming productFamilyId is: " + productFamilyId, isDebugEnabled)\r
165                         \r
166                         String subscriberInfo = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.subscriberInfo")\r
167                         execution.setVariable("subscriberInfo", subscriberInfo)\r
168                         utils.log("DEBUG", "Incoming subscriberInfo is: " + subscriberInfo, isDebugEnabled)\r
169 \r
170                   /*\r
171                   * Extracting User Parameters from incoming Request and converting into a Map\r
172                   */\r
173                   def jsonSlurper = new JsonSlurper()\r
174                   def jsonOutput = new JsonOutput()\r
175 \r
176                   Map reqMap = jsonSlurper.parseText(createVcpeServiceRequest)\r
177 \r  
178                   //InputParams\r
179                   def userParams = reqMap.requestDetails?.requestParameters?.userParams\r
180   
181                   Map<String, String> inputMap = [:]\r
182 \r
183 \r
184                   if (userParams) {\r
185                                 userParams.each {\r
186                                                                 userParam ->\r
187                                                                 if("BRG_WAN_MAC_Address".equals(userParam?.name)) {\r
188                                                                                                 execution.setVariable("brgWanMacAddress", userParam.value)\r
189                                                                                                 inputMap.put("BRG_WAN_MAC_Address", userParam.value)\r
190                                         }\r
191                                 }\r
192                   }\r
193 \r
194                   utils.log("DEBUG", "User Input Parameters map: " + userParams.toString(), isDebugEnabled)\r
195                   execution.setVariable("serviceInputParams", inputMap)\r
196 \r
197                         utils.log("DEBUG", "Incoming brgWanMacAddress is: " + execution.getVariable('brgWanMacAddress'), isDebugEnabled)\r
198 \r
199                         //For Completion Handler & Fallout Handler\r
200                         String requestInfo =\r
201                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
202                                         <request-id>${requestId}</request-id>\r
203                                         <action>CREATE</action>\r
204                                         <source>${source}</source>\r
205                                    </request-info>"""\r
206 \r
207                         execution.setVariable(Prefix+"requestInfo", requestInfo)\r
208 \r
209                         utils.log("DEBUG", " ***** Completed preProcessRequest CreateVcpeResCustService Request ***** ", isDebugEnabled)\r
210 \r
211                 } catch (BpmnError e) {\r
212                         throw e;\r
213 \r
214                 } catch (Exception ex){\r
215                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected from method preProcessRequest() - " + ex.getMessage()\r
216                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
217                 }\r
218         }\r
219 \r
220         public void sendSyncResponse(Execution execution) {\r
221                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
222 \r
223                 utils.log("DEBUG", " ***** Inside sendSyncResponse of CreateVcpeResCustService ***** ", isDebugEnabled)\r
224 \r
225                 try {\r
226                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
227                         String requestId = execution.getVariable("mso-request-id")\r
228 \r
229                         // RESTResponse (for API Handler (APIH) Reply Task)\r
230                         String syncResponse ="""{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()\r
231 \r
232                         utils.log("DEBUG", " sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse, isDebugEnabled)\r
233                         sendWorkflowResponse(execution, 202, syncResponse)\r
234 \r
235                 } catch (Exception ex) {\r
236                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected from method sendSyncResponse() - " + ex.getMessage()\r
237                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
238                 }\r
239         }\r
240 \r
241         // *******************************\r
242         //\r
243         // *******************************\r
244         public void prepareDecomposeService(Execution execution) {\r
245                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
246 \r
247                 try {\r
248                         utils.log("DEBUG", " ***** Inside prepareDecomposeService of CreateVcpeResCustService ***** ", isDebugEnabled)\r
249 \r
250                         String createVcpeServiceRequest = execution.getVariable("createVcpeServiceRequest")\r
251 \r
252                         //serviceModelInfo JSON string will be used as-is for DoCreateServiceInstance BB\r
253                         String serviceModelInfo = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.modelInfo")\r
254                         execution.setVariable("serviceModelInfo", serviceModelInfo)\r
255 \r
256                         utils.log("DEBUG", " ***** Completed prepareDecomposeService of CreateVcpeResCustService ***** ", isDebugEnabled)\r
257                 } catch (Exception ex) {\r
258                         // try error in method block\r
259                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected Error from method prepareDecomposeService() - " + ex.getMessage()\r
260                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
261                 }\r
262          }\r
263 \r
264         // *******************************\r
265         //\r
266         // *******************************\r
267         public void prepareCreateServiceInstance(Execution execution) {\r
268                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
269 \r
270                 try {\r
271                         utils.log("DEBUG", " ***** Inside prepareCreateServiceInstance of CreateVcpeResCustService ***** ", isDebugEnabled)\r
272 \r
273                         /*\r
274                          * Service modelInfo is created in earlier step. This flow can use it as-is ... or, extract from DecompositionObject\r
275                          *              ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
276                          *              ModelInfo modelInfo = serviceDecomposition.getModelInfo()\r
277                          *\r
278                          */\r
279                         String createVcpeServiceRequest = execution.getVariable("createVcpeServiceRequest")\r
280 //                      String serviceInputParams = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestParameters")\r
281 //                      execution.setVariable("serviceInputParams", serviceInputParams)\r
282 \r
283
284                         String serviceInstanceName = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestInfo.instanceName")\r
285                         execution.setVariable("serviceInstanceName", serviceInstanceName)\r
286 \r
287                         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
288                         execution.setVariable("serviceDecompositionString", serviceDecomposition.toJsonStringNoRootName())\r
289 \r
290                         utils.log("DEBUG", " ***** Completed prepareCreateServiceInstance of CreateVcpeResCustService ***** ", isDebugEnabled)\r
291                 } catch (Exception ex) {\r
292                         // try error in method block\r
293                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected Error from method prepareCreateServiceInstance() - " + ex.getMessage()\r
294                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
295                 }\r
296          }\r
297 \r
298         public void postProcessServiceInstanceCreate (Execution execution){\r
299                 def method = getClass().getSimpleName() + '.postProcessServiceInstanceCreate(' +'execution=' + execution.getId() +')'\r
300                 def isDebugLogEnabled = execution.getVariable(DebugFlag)\r
301                 logDebug('Entered ' + method, isDebugLogEnabled)\r
302 \r
303                 String requestId = execution.getVariable("mso-request-id")\r
304                 String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
305                 String serviceInstanceName = execution.getVariable("serviceInstanceName")\r
306 \r
307                 try {\r
308 \r
309                         String payload = """\r
310                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.openecomp.mso/requestsdb">\r
311                         <soapenv:Header/>\r
312                         <soapenv:Body>\r
313                         <req:updateInfraRequest>\r
314                                 <requestId>${requestId}</requestId>\r
315                                 <lastModifiedBy>BPEL</lastModifiedBy>\r
316                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>\r
317                                 <serviceInstanceName>${serviceInstanceName}</serviceInstanceName>\r
318                         </req:updateInfraRequest>\r
319                         </soapenv:Body>\r
320                         </soapenv:Envelope>\r
321                         """\r
322                         execution.setVariable(Prefix+"setUpdateDbInstancePayload", payload)\r
323                         utils.logAudit(Prefix+"setUpdateDbInstancePayload: " + payload)\r
324                         logDebug('Exited ' + method, isDebugLogEnabled)\r
325 \r
326                 } catch (BpmnError e) {\r
327                         throw e;\r
328                 } catch (Exception e) {\r
329                         logError('Caught exception in ' + method, e)\r
330                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error - Occured in" + method)\r
331                 }\r
332         }\r
333 \r
334 \r
335         public void processDecomposition (Execution execution) {\r
336                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
337 \r
338                 utils.log("DEBUG", " ***** Inside processDecomposition() of CreateVcpeResCustService ***** ", isDebugEnabled)\r
339 \r
340                 try {\r
341 \r
342                         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
343 \r
344                         // VNFs\r
345                         List<VnfResource> vnfList = serviceDecomposition.getServiceVnfs()\r
346                         execution.setVariable("vnfList", vnfList)\r
347                         execution.setVariable("vnfListString", vnfList.toString())\r
348 \r
349                         String vnfModelInfoString = ""\r
350                         if (vnfList != null && vnfList.size() > 0) {\r
351                                 execution.setVariable(Prefix+"VNFsCount", vnfList.size())\r
352                                 utils.log("DEBUG", "vnfs to create: "+ vnfList.size(), isDebugEnabled)\r
353                                 ModelInfo vnfModelInfo = vnfList[0].getModelInfo()\r
354 \r
355                                 vnfModelInfoString = vnfModelInfo.toString()\r
356                                 String vnfModelInfoWithRoot = vnfModelInfo.toString()\r
357                                 vnfModelInfoString = jsonUtil.getJsonValue(vnfModelInfoWithRoot, "modelInfo")\r
358                         } else {\r
359                                         execution.setVariable(Prefix+"VNFsCount", 0)\r
360                                         utils.log("DEBUG", "no vnfs to create based upon serviceDecomposition content", isDebugEnabled)\r
361                         }\r
362 \r
363                         execution.setVariable("vnfModelInfo", vnfModelInfoString)\r
364                         execution.setVariable("vnfModelInfoString", vnfModelInfoString)\r
365                         utils.log("DEBUG", " vnfModelInfoString :" + vnfModelInfoString, isDebugEnabled)\r
366 \r
367                         utils.log("DEBUG", " ***** Completed processDecomposition() of CreateVcpeResCustService ***** ", isDebugEnabled)\r
368                 } catch (Exception ex) {\r
369                         sendSyncError(execution)\r
370                    String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. processDecomposition() - " + ex.getMessage()\r
371                    utils.log("DEBUG", exceptionMessage, isDebugEnabled)\r
372                    exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
373                 }\r
374         }\r
375 \r
376 \r
377         public void prepareCreateAllottedResourceTXC(Execution execution) {\r
378                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
379 \r
380                 try {\r
381                         utils.log("DEBUG", " ***** Inside prepareCreateAllottedResourceTXC of CreateVcpeResCustService ***** ", isDebugEnabled)\r
382 \r
383                         /*\r
384                          * Service modelInfo is created in earlier step. This flow can use it as-is ... or, extract from DecompositionObject\r
385                          *              ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
386                          *              ModelInfo modelInfo = serviceDecomposition.getModelInfo()\r
387                          *\r
388                          */\r
389                         String createVcpeServiceRequest = execution.getVariable("createVcpeServiceRequest")\r
390                         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
391 \r
392                         //allottedResourceModelInfo\r
393                         //allottedResourceRole\r
394                         //The model Info parameters are a JSON structure as defined in the Service Instantiation API.\r
395                         //It would be sufficient to only include the service model UUID (i.e. the modelVersionId), since this BB will query the full model from the Catalog DB.\r
396                         List<AllottedResource> allottedResources = serviceDecomposition.getServiceAllottedResources()\r
397                         if (allottedResources != null) {\r
398                                 Iterator iter = allottedResources.iterator();\r
399                                 while (iter.hasNext()){\r
400                                         AllottedResource allottedResource = (AllottedResource)iter.next();\r
401 \r
402                                         utils.log("DEBUG", " getting model info for AllottedResource # :" + allottedResource.toJsonStringNoRootName(), isDebugEnabled)\r
403                                         utils.log("DEBUG", " allottedResource.getAllottedResourceType() :" + allottedResource.getAllottedResourceType(), isDebugEnabled)\r
404                                         if("TunnelXConn".equalsIgnoreCase(allottedResource.getAllottedResourceType())){\r
405                                                 //set create flag to true\r
406                                                 execution.setVariable("createTXCAR", true)\r
407                                                 ModelInfo allottedResourceModelInfo = allottedResource.getModelInfo()\r
408                                                 execution.setVariable("allottedResourceModelInfoTXC", allottedResourceModelInfo.toJsonStringNoRootName())\r
409                                                 execution.setVariable("allottedResourceRoleTXC", allottedResource.getAllottedResourceRole())\r
410                                                 execution.setVariable("allottedResourceTypeTXC", allottedResource.getAllottedResourceType())\r
411                                                 //After decomposition and homing BBs, there should be an allotted resource object in the decomposition that represents the TXC,\r
412                                                 //and in its homingSolution section should be found the infraServiceInstanceId (i.e. infraServiceInstanceId in TXC Allotted Resource structure) (which the Homing BB would have populated).\r
413                                                 execution.setVariable("parentServiceInstanceIdTXC", allottedResource.getHomingSolution().getServiceInstanceId())\r
414                                         }
415                                 }\r
416                         }\r
417 \r
418                         //unit test only\r
419                         String allottedResourceId = execution.getVariable("allottedResourceId")\r
420                         execution.setVariable("allottedResourceIdTXC", allottedResourceId)\r
421                         utils.log("DEBUG", "setting allottedResourceId CreateVcpeResCustService "+allottedResourceId, isDebugEnabled)\r
422                         \r
423                         utils.log("DEBUG", " ***** Completed prepareCreateAllottedResourceTXC of CreateVcpeResCustService ***** ", isDebugEnabled)\r
424                 } catch (Exception ex) {\r
425                         // try error in method block\r
426                         String exceptionMessage = "Bpmn error encountered in prepareCreateAllottedResourceTXC flow. Unexpected Error from method prepareCreateServiceInstance() - " + ex.getMessage()\r
427                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
428                 }\r
429          }\r
430         public void prepareCreateAllottedResourceBRG(Execution execution) {\r
431                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
432 \r
433                 try {\r
434                         utils.log("DEBUG", " ***** Inside prepareCreateAllottedResourceBRG of CreateVcpeResCustService ***** ", isDebugEnabled)\r
435 \r
436                         /*\r
437                          * Service modelInfo is created in earlier step. This flow can use it as-is ... or, extract from DecompositionObject\r
438                          *              ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
439                          *              ModelInfo modelInfo = serviceDecomposition.getModelInfo()\r
440                          *\r
441                          */\r
442                         String createVcpeServiceRequest = execution.getVariable("createVcpeServiceRequest")\r
443                         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")\r
444 \r
445                         //allottedResourceModelInfo\r
446                         //allottedResourceRole\r
447                         //The model Info parameters are a JSON structure as defined in the Service Instantiation API.\r
448                         //It would be sufficient to only include the service model UUID (i.e. the modelVersionId), since this BB will query the full model from the Catalog DB.\r
449                         List<AllottedResource> allottedResources = serviceDecomposition.getServiceAllottedResources()\r
450                         if (allottedResources != null) {\r
451                                 Iterator iter = allottedResources.iterator();\r
452                                 while (iter.hasNext()){\r
453                                         AllottedResource allottedResource = (AllottedResource)iter.next();\r
454 \r
455                                         utils.log("DEBUG", " getting model info for AllottedResource # :" + allottedResource.toJsonStringNoRootName(), isDebugEnabled)\r
456                                         utils.log("DEBUG", " allottedResource.getAllottedResourceType() :" + allottedResource.getAllottedResourceType(), isDebugEnabled)\r
457                                         if("BRG".equalsIgnoreCase(allottedResource.getAllottedResourceType())){\r
458                                                 //set create flag to true\r
459                                                 execution.setVariable("createBRGAR", true)\r
460                                                 ModelInfo allottedResourceModelInfo = allottedResource.getModelInfo()\r
461                                                 execution.setVariable("allottedResourceModelInfoBRG", allottedResourceModelInfo.toJsonStringNoRootName())\r
462                                                 execution.setVariable("allottedResourceRoleBRG", allottedResource.getAllottedResourceRole())\r
463                                                 execution.setVariable("allottedResourceTypeBRG", allottedResource.getAllottedResourceType())\r
464                                                 //After decomposition and homing BBs, there should be an allotted resource object in the decomposition that represents the BRG,\r
465                                                 //and in its homingSolution section should be found the infraServiceInstanceId (i.e. infraServiceInstanceId in BRG Allotted Resource structure) (which the Homing BB would have populated).\r
466                                                 execution.setVariable("parentServiceInstanceIdBRG", allottedResource.getHomingSolution().getServiceInstanceId())\r
467                                         }\r
468                                 }\r
469                         }\r
470 \r
471                         //unit test only\r
472                         String allottedResourceId = execution.getVariable("allottedResourceId")\r
473                         execution.setVariable("allottedResourceIdBRG", allottedResourceId)\r
474                         utils.log("DEBUG", "setting allottedResourceId CreateVcpeResCustService "+allottedResourceId, isDebugEnabled)\r
475                         \r
476                         utils.log("DEBUG", " ***** Completed prepareCreateAllottedResourceBRG of CreateVcpeResCustService ***** ", isDebugEnabled)\r
477                 } catch (Exception ex) {\r
478                         // try error in method block\r
479                         String exceptionMessage = "Bpmn error encountered in prepareCreateAllottedResourceBRG flow. Unexpected Error from method prepareCreateServiceInstance() - " + ex.getMessage()\r
480                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
481                 }\r
482          }\r
483 \r
484
485
486         // *******************************\r
487         //     Generate Network request Section\r
488         // *******************************\r
489         public void prepareVnfAndModulesCreate (Execution execution) {\r
490                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
491 \r
492                 try {\r
493                         utils.log("DEBUG", " ***** Inside prepareVnfAndModulesCreate of CreateVcpeResCustService ***** ", isDebugEnabled)\r
494 \r
495                         //                      String disableRollback = execution.getVariable("disableRollback")\r
496                         //                      def backoutOnFailure = ""\r
497                         //                      if(disableRollback != null){\r
498                         //                              if ( disableRollback == true) {\r
499                         //                                      backoutOnFailure = "false"\r
500                         //                              } else if ( disableRollback == false) {\r
501                         //                                      backoutOnFailure = "true"\r
502                         //                              }\r
503                         //                      }\r
504                                                 //failIfExists - optional\r
505 \r
506                         String createVcpeServiceRequest = execution.getVariable("createVcpeServiceRequest")\r
507                         String productFamilyId = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.requestInfo.productFamilyId")\r
508                         execution.setVariable("productFamilyId", productFamilyId)\r
509                         utils.log("DEBUG","productFamilyId: "+ productFamilyId, isDebugEnabled)\r
510 \r
511                         List<VnfResource> vnfList = execution.getVariable("vnfList")\r
512 \r
513                         Integer vnfsCreatedCount = execution.getVariable(Prefix+"VnfsCreatedCount")\r
514                         String vnfModelInfoString = null;\r
515 \r
516                         if (vnfList != null && vnfList.size() > 0 ) {\r
517                                 utils.log("DEBUG", "getting model info for vnf # " + vnfsCreatedCount, isDebugEnabled)\r
518                                 ModelInfo vnfModelInfo1 = vnfList[0].getModelInfo()\r
519                                 utils.log("DEBUG", "got 0 ", isDebugEnabled)\r
520                                 ModelInfo vnfModelInfo = vnfList[vnfsCreatedCount.intValue()].getModelInfo()\r
521                                 vnfModelInfoString = vnfModelInfo.toString()\r
522                         } else {\r
523                                 //TODO: vnfList does not contain data. Need to investigate why ... . Fro VCPE use model stored\r
524                                 vnfModelInfoString = execution.getVariable("vnfModelInfo")\r
525                         }\r
526 \r
527                         utils.log("DEBUG", " vnfModelInfoString :" + vnfModelInfoString, isDebugEnabled)\r
528 \r
529                         // extract cloud configuration\r
530                         String lcpCloudRegionId = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.cloudConfiguration.lcpCloudRegionId")\r
531                         execution.setVariable("lcpCloudRegionId", lcpCloudRegionId)\r
532                         utils.log("DEBUG","lcpCloudRegionId: "+ lcpCloudRegionId, isDebugEnabled)\r
533                         String tenantId = jsonUtil.getJsonValue(createVcpeServiceRequest, "requestDetails.cloudConfiguration.tenantId")\r
534                         execution.setVariable("tenantId", tenantId)\r
535                         utils.log("DEBUG","tenantId: "+ tenantId, isDebugEnabled)\r
536 \r
537                         String sdncVersion = execution.getVariable("sdncVersion")\r
538                         utils.log("DEBUG","sdncVersion: "+ sdncVersion, isDebugEnabled)\r
539 \r
540                         utils.log("DEBUG", " ***** Completed prepareVnfAndModulesCreate of CreateVcpeResCustService ***** ", isDebugEnabled)\r
541                 } catch (Exception ex) {\r
542                         // try error in method block\r
543                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected Error from method prepareVnfAndModulesCreate() - " + ex.getMessage()\r
544                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
545                 }\r
546          }\r
547 \r
548         // *******************************\r
549         //     Validate Vnf request Section -> increment count\r
550         // *******************************\r
551         public void validateVnfCreate (Execution execution) {\r
552                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
553 \r
554                 try {\r
555                         utils.log("DEBUG", " ***** Inside validateVnfCreate of CreateVcpeResCustService ***** ", isDebugEnabled)\r
556 \r
557                         Integer vnfsCreatedCount = execution.getVariable(Prefix+"VnfsCreatedCount")\r
558                         vnfsCreatedCount++\r
559 \r
560                         execution.setVariable(Prefix+"VnfsCreatedCount", vnfsCreatedCount)\r
561 \r
562                         utils.log("DEBUG", " ***** Completed validateVnfCreate of CreateVcpeResCustService ***** "+" vnf # "+vnfsCreatedCount, isDebugEnabled)\r
563                 } catch (Exception ex) {\r
564                         // try error in method block\r
565                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected Error from method validateVnfCreate() - " + ex.getMessage()\r
566                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
567                 }\r
568          }\r
569 \r
570         // *****************************************\r
571         //     Prepare Completion request Section\r
572         // *****************************************\r
573         public void postProcessResponse (Execution execution) {\r
574                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
575 \r
576                 utils.log("DEBUG", " ***** Inside postProcessResponse of CreateVcpeResCustService ***** ", isDebugEnabled)\r
577 \r
578                 try {\r
579                         String source = execution.getVariable("source")\r
580                         String requestId = execution.getVariable("mso-request-id")\r
581                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
582 \r
583                         String msoCompletionRequest =\r
584                                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
585                                                                         xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
586                                                         <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
587                                                                 <request-id>${requestId}</request-id>\r
588                                                                 <action>CREATE</action>\r
589                                                                 <source>${source}</source>\r
590                                                         </request-info>\r
591                                                         <status-message>Service Instance has been created successfully via macro orchestration</status-message>\r
592                                                         <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>\r
593                                                         <mso-bpel-name>BPMN macro create</mso-bpel-name>\r
594                                                 </aetgt:MsoCompletionRequest>"""\r
595 \r
596                         // Format Response\r
597                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)\r
598 \r
599                         utils.logAudit(xmlMsoCompletionRequest)\r
600                         execution.setVariable(Prefix+"Success", true)\r
601                         execution.setVariable(Prefix+"CompleteMsoProcessRequest", xmlMsoCompletionRequest)\r
602                         utils.log("DEBUG", " SUCCESS flow, going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)\r
603                 } catch (BpmnError e) {\r
604                         throw e;\r
605                 } catch (Exception ex) {\r
606                         // try error in method block\r
607                         String exceptionMessage = "Bpmn error encountered in CreateVcpeResCustService flow. Unexpected Error from method postProcessResponse() - " + ex.getMessage()\r
608                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
609                 }\r
610         }\r
611 \r
612         public void preProcessRollback (Execution execution) {\r
613                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
614                 utils.log("DEBUG"," ***** preProcessRollback of CreateVcpeResCustService ***** ", isDebugEnabled)\r
615                 try {\r
616 \r
617                         Object workflowException = execution.getVariable("WorkflowException");\r
618 \r
619                         if (workflowException instanceof WorkflowException) {\r
620                                 utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)\r
621                                 execution.setVariable("prevWorkflowException", workflowException);\r
622                                 //execution.setVariable("WorkflowException", null);\r
623                         }\r
624                 } catch (BpmnError e) {\r
625                         utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugEnabled)\r
626                 } catch(Exception ex) {\r
627                         String msg = "Exception in preProcessRollback. " + ex.getMessage()\r
628                         utils.log("DEBUG", msg, isDebugEnabled)\r
629                 }\r
630                 utils.log("DEBUG"," *** Exit preProcessRollback of CreateVcpeResCustService *** ", isDebugEnabled)\r
631         }\r
632 \r
633         public void postProcessRollback (Execution execution) {\r
634                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
635                 utils.log("DEBUG"," ***** postProcessRollback of CreateVcpeResCustService ***** ", isDebugEnabled)\r
636                 String msg = ""\r
637                 try {\r
638                         Object workflowException = execution.getVariable("prevWorkflowException");\r
639                         if (workflowException instanceof WorkflowException) {\r
640                                 utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugEnabled)\r
641                                 execution.setVariable("WorkflowException", workflowException);\r
642                         }\r
643                 } catch (BpmnError b) {\r
644                         utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugEnabled)\r
645                         throw b;\r
646                 } catch(Exception ex) {\r
647                         msg = "Exception in postProcessRollback. " + ex.getMessage()\r
648                         utils.log("DEBUG", msg, isDebugEnabled)\r
649                 }\r
650                 utils.log("DEBUG"," *** Exit postProcessRollback of CreateVcpeResCustService *** ", isDebugEnabled)\r
651         }\r
652 \r
653         public void prepareFalloutRequest(Execution execution){\r
654                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
655 \r
656                 utils.log("DEBUG", " *** STARTED CreateVcpeResCustService prepareFalloutRequest Process *** ", isDebugEnabled)\r
657 \r
658                 try {\r
659                         WorkflowException wfex = execution.getVariable("WorkflowException")\r
660                         utils.log("DEBUG", " Incoming Workflow Exception: " + wfex.toString(), isDebugEnabled)\r
661                         String requestInfo = execution.getVariable(Prefix+"requestInfo")\r
662                         utils.log("DEBUG", " Incoming Request Info: " + requestInfo, isDebugEnabled)\r
663 \r
664                         //TODO. hmmm. there is no way to UPDATE error message.\r
665 //                      String errorMessage = wfex.getErrorMessage()\r
666 //                      boolean successIndicator = execution.getVariable("DCRESI_rolledBack")\r
667 //                      if (successIndicator){\r
668 //                              errorMessage = errorMessage + ". Rollback successful."\r
669 //                      } else {\r
670 //                              errorMessage = errorMessage + ". Rollback not completed."\r
671 //                      }\r
672 \r
673                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)\r
674 \r
675                         execution.setVariable(Prefix+"falloutRequest", falloutRequest)\r
676 \r
677                 } catch (Exception ex) {\r
678                         utils.log("DEBUG", "Error Occured in CreateVcpeResCustService prepareFalloutRequest Process " + ex.getMessage(), isDebugEnabled)\r
679                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVcpeResCustService prepareFalloutRequest Process")\r
680                 }\r
681                 utils.log("DEBUG", "*** COMPLETED CreateVcpeResCustService prepareFalloutRequest Process ***", isDebugEnabled)\r
682         }\r
683 \r
684 \r
685         public void sendSyncError (Execution execution) {\r
686                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
687                 execution.setVariable("prefix", Prefix)\r
688 \r
689                 utils.log("DEBUG", " ***** Inside sendSyncError() of CreateVcpeResCustService ***** ", isDebugEnabled)\r
690 \r
691                 try {\r
692                         String errorMessage = ""\r
693                         def wfe = execution.getVariable("WorkflowException")\r
694                         if (wfe instanceof WorkflowException) {\r
695                                 errorMessage = wfe.getErrorMessage()\r
696                         } else {\r
697                                 errorMessage = "Sending Sync Error."\r
698                         }\r
699 \r
700                         String buildworkflowException =\r
701                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
702                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>\r
703                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
704                                    </aetgt:WorkflowException>"""\r
705 \r
706                         utils.logAudit(buildworkflowException)\r
707                         sendWorkflowResponse(execution, 500, buildworkflowException)\r
708                 } catch (Exception ex) {\r
709                         utils.log("DEBUG", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)\r
710                 }\r
711         }\r
712 \r
713         public void processJavaException(Execution execution){\r
714                 def isDebugEnabled=execution.getVariable(DebugFlag)\r
715                 execution.setVariable("prefix",Prefix)\r
716                 try{\r
717                         utils.log("DEBUG", "Caught a Java Exception", isDebugEnabled)\r
718                         utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)\r
719                         utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)\r
720                         execution.setVariable(Prefix+"unexpectedError", "Caught a Java Lang Exception")  // Adding this line temporarily until this flows error handling gets updated\r
721                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Caught a Java Lang Exception")\r
722                 }catch(BpmnError b){\r
723                         utils.log("ERROR", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
724                         throw b\r
725                 }catch(Exception e){\r
726                         utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)\r
727                         execution.setVariable(Prefix+"unexpectedError", "Exception in processJavaException method")  // Adding this line temporarily until this flows error handling gets updated\r
728                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Exception in processJavaException method")\r
729                 }\r
730                 utils.log("DEBUG", "Completed processJavaException Method", isDebugEnabled)\r
731         }\r
732 }\r