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