Merge "Bug fix for getInstnaceId"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.bpmn.infrastructure.scripts;
22
23 import static org.apache.commons.lang3.StringUtils.*;
24
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.OwningEntity
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
30 import org.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.common.scripts.MsoUtils
33 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
34 import org.onap.so.bpmn.core.RollbackData
35 import org.onap.so.bpmn.core.UrnPropertiesReader
36 import org.onap.so.bpmn.core.WorkflowException
37 import org.onap.so.bpmn.core.domain.ModelInfo
38 import org.onap.so.bpmn.core.domain.ServiceDecomposition
39 import org.onap.so.bpmn.core.domain.ServiceInstance
40 import org.onap.so.bpmn.core.json.JsonUtils
41 import org.onap.so.bpmn.infrastructure.aai.groovyflows.AAICreateResources
42 import org.onap.so.client.aai.AAIObjectType
43 import org.onap.so.client.aai.AAIResourcesClient
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri
45 import org.onap.so.client.aai.entities.uri.AAIUri
46 import org.onap.so.client.aai.entities.uri.AAIUriFactory
47 import org.onap.so.logger.MsoLogger
48
49 /**
50  * This groovy class supports the <class>DoCreateServiceInstance.bpmn</class> process.
51  *
52  * Inputs:
53  * @param - msoRequestId
54  * @param - globalSubscriberId
55  * @param - subscriptionServiceType
56  * @param - serviceInstanceId
57  * @param - serviceInstanceName - O
58  * @param - serviceModelInfo
59  * @param - productFamilyId
60  * @param - disableRollback
61  * @param - failExists - TODO
62  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
63  * @param - sdncVersion ("1610")
64  * @param - serviceDecomposition - Decomposition for R1710
65  * (if macro provides serviceDecompsition then serviceModelInfo, serviceInstanceId & serviceInstanceName will be ignored)
66  *
67  * Outputs:
68  * @param - rollbackData (localRB->null)
69  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
70  * @param - WorkflowException
71  * @param - serviceInstanceName - (GET from AAI if null in input)
72  *
73  * This BB processes Macros(except TRANSPORT all sent to sdnc) and Alacartes(sdncSvcs && nonSdncSvcs)
74  */
75 public class DoCreateServiceInstance extends AbstractServiceTaskProcessor {
76
77         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateServiceInstance.class);
78         String Prefix="DCRESI_"
79         ExceptionUtil exceptionUtil = new ExceptionUtil()
80         JsonUtils jsonUtil = new JsonUtils()
81         CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create()
82
83         public void preProcessRequest (DelegateExecution execution) {
84                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
85                 String msg = ""
86                 msoLogger.trace("preProcessRequest")
87
88                 try {
89                         String requestId = execution.getVariable("msoRequestId")
90                         execution.setVariable("prefix", Prefix)
91                         
92                         def rollbackData = execution.getVariable("rollbackData")
93                         if (rollbackData == null) {
94                                 rollbackData = new RollbackData()
95                         }
96                         execution.setVariable("rollbackData", rollbackData)
97
98                         setBasicDBAuthHeader(execution, isDebugEnabled)
99                         //Inputs
100                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
101                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
102
103                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
104                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
105
106                         //requestDetails.requestParameters. for SDNC assignTopology
107                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
108
109                         if (isBlank(globalSubscriberId)) {
110                                 msg = "Input globalSubscriberId is null"
111                                 msoLogger.debug(msg)
112                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
113                         }
114
115                         if (isBlank(subscriptionServiceType)) {
116                                 msg = "Input subscriptionServiceType is null"
117                                 msoLogger.debug(msg)
118                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
119                         }
120
121                         if (productFamilyId == null) {
122                                 execution.setVariable("productFamilyId", "")
123                         }
124
125                         String sdncCallbackUrl = UrnPropertiesReader.getVariable("mso.workflow.sdncadapter.callback",execution)
126                         if (isBlank(sdncCallbackUrl)) {
127                                 msg = "mso.workflow.sdncadapter.callback is null"
128                                 msoLogger.debug(msg)
129                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
130                         }
131                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
132                         msoLogger.debug("SDNC Callback URL: " + sdncCallbackUrl)
133
134                         //requestDetails.modelInfo.for AAI PUT servieInstanceData & SDNC assignTopology
135                         String modelInvariantUuid = ""
136                         String modelVersion = ""
137                         String modelUuid = ""
138                         String modelName = ""
139                         String serviceInstanceName = ""
140                         //Generated in parent.for AAI PUT
141                         String serviceInstanceId = ""
142                         String serviceType = ""
143                         String serviceRole = ""
144
145                         ServiceDecomposition serviceDecomp = (ServiceDecomposition) execution.getVariable("serviceDecomposition")
146                         if (serviceDecomp != null)
147                         {
148                                 serviceType = serviceDecomp.getServiceType() ?: ""
149                                 msoLogger.debug("serviceType:" + serviceType)
150                                 serviceRole = serviceDecomp.getServiceRole() ?: ""
151
152                                 ServiceInstance serviceInstance = serviceDecomp.getServiceInstance()
153                                 if (serviceInstance != null)
154                                 {
155                                         serviceInstanceId = serviceInstance.getInstanceId() ?: ""
156                                         serviceInstanceName = serviceInstance.getInstanceName() ?: ""
157                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
158                                         execution.setVariable("serviceInstanceName", serviceInstanceName)
159                                 }
160
161                                 ModelInfo modelInfo = serviceDecomp.getModelInfo()
162                                 if (modelInfo != null)
163                                 {
164                                         modelInvariantUuid = modelInfo.getModelInvariantUuid() ?: ""
165                                         modelVersion = modelInfo.getModelVersion() ?: ""
166                                         modelUuid = modelInfo.getModelUuid() ?: ""
167                                         modelName = modelInfo.getModelName() ?: ""
168                                 }
169                                 else
170                                 {
171                                         msg = "Input serviceModelInfo is null"
172                                         msoLogger.debug(msg)
173                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
174                                 }
175                         }
176                         else
177                         {
178                                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData & SDNC assignToplology
179                                 serviceInstanceName = execution.getVariable("serviceInstanceName") ?: ""
180                                 serviceInstanceId = execution.getVariable("serviceInstanceId") ?: ""
181
182                                 String serviceModelInfo = execution.getVariable("serviceModelInfo")
183                                 if (isBlank(serviceModelInfo)) {
184                                         msg = "Input serviceModelInfo is null"
185                                         msoLogger.debug(msg)
186                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
187                                 }
188                                 modelInvariantUuid = jsonUtil.getJsonValue(serviceModelInfo, "modelInvariantUuid") ?: ""
189                                 modelVersion = jsonUtil.getJsonValue(serviceModelInfo, "modelVersion") ?: ""
190                                 modelUuid = jsonUtil.getJsonValue(serviceModelInfo, "modelUuid") ?: ""
191                                 modelName = jsonUtil.getJsonValue(serviceModelInfo, "modelName") ?: ""
192                                 //modelCustomizationUuid NA for SI
193
194                         }
195
196                         execution.setVariable("serviceType", serviceType)
197                         execution.setVariable("serviceRole", serviceRole)
198                         execution.setVariable("serviceInstanceName", serviceInstanceName)
199
200                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)
201                         execution.setVariable("modelVersion", modelVersion)
202                         execution.setVariable("modelUuid", modelUuid)
203                         execution.setVariable("modelName", modelName)
204
205                         //alacarte SIs are NOT sent to sdnc. exceptions are listed in config variable
206                         String svcTypes = UrnPropertiesReader.getVariable("sdnc.si.svc.types",execution) ?: ""
207                         msoLogger.debug("SDNC SI serviceTypes:" + svcTypes)
208                         List<String> svcList = Arrays.asList(svcTypes.split("\\s*,\\s*"));
209                         boolean isSdncService= false
210                         for (String listEntry : svcList){
211                                 if (listEntry.equalsIgnoreCase(serviceType)){
212                                         isSdncService = true
213                                         break;
214                                 }
215                         }
216
217                         //All Macros are sent to SDNC, TRANSPORT(Macro) is sent to SDNW
218                         //Alacartes are sent to SDNC if they are listed in config variable above
219                         execution.setVariable("sendToSDNC", true)
220                         if(execution.getVariable("sdncVersion").equals("1610")) //alacarte
221                         {
222                                 if(!isSdncService){
223                                         execution.setVariable("sendToSDNC", false)
224                                         //alacarte non-sdnc svcs must provide name (sdnc provides name for rest)
225                                         if (isBlank(execution.getVariable("serviceInstanceName" )))
226                                         {
227                                                 msg = "Input serviceInstanceName must be provided for alacarte"
228                                                 msoLogger.debug(msg)
229                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
230                                         }
231                                 }
232                         }
233
234                         msoLogger.debug("isSdncService: " + isSdncService)
235                         msoLogger.debug("Send To SDNC: " + execution.getVariable("sendToSDNC"))
236                         msoLogger.debug("Service Type: " + execution.getVariable("serviceType"))
237
238                         //macro may provide name and alacarte-portm may provide name
239                         execution.setVariable("checkAAI", false)
240                         if (!isBlank(execution.getVariable("serviceInstanceName" )))
241                         {
242                                 execution.setVariable("checkAAI", true)
243                         }
244
245                         if (isBlank(serviceInstanceId)){
246                                 msg = "Input serviceInstanceId is null"
247                                 msoLogger.debug(msg)
248                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
249                         }
250
251
252                         StringBuilder sbParams = new StringBuilder()
253                         Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
254                         if (paramsMap != null)
255                         {
256                                 sbParams.append("<service-input-parameters>")
257                                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
258                                         String paramsXml
259                                         String paramName = entry.getKey()
260                                         String paramValue = entry.getValue()
261                                         paramsXml =
262                                                         """     <param>
263                                                         <name>${MsoUtils.xmlEscape(paramName)}</name>
264                                                         <value>${MsoUtils.xmlEscape(paramValue)}</value>
265                                                         </param>
266                                                         """
267                                         sbParams.append(paramsXml)
268                                 }
269                                 sbParams.append("</service-input-parameters>")
270                         }
271                         String siParamsXml = sbParams.toString()
272                         if (siParamsXml == null)
273                                 siParamsXml = ""
274                         execution.setVariable("siParamsXml", siParamsXml)
275
276                 } catch (BpmnError e) {
277                         throw e;
278                 } catch (Exception ex){
279                         msg = "Exception in preProcessRequest " + ex.getMessage()
280                         msoLogger.debug(msg)
281                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
282                 }
283                 msoLogger.trace("Exit preProcessRequest")
284         }
285
286         public void getAAICustomerById (DelegateExecution execution) {
287                 // https://{aaiEP}/aai/v8/business/customers/customer/{globalCustomerId}
288                 try {
289
290                         String globalCustomerId = execution.getVariable("globalSubscriberId") //VID to AAI name map
291                         msoLogger.debug(" ***** getAAICustomerById ***** globalCustomerId:" + globalCustomerId)
292
293                         AAIUri uri = AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalCustomerId)
294                         if(!getAAIClient().exists(uri)){
295                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "GlobalCustomerId:" + globalCustomerId + " not found (404) in AAI")
296                         }
297                 } catch (BpmnError e) {
298                         throw e;
299                 } catch (Exception ex) {
300                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in getAAICustomerById. " + ex.getMessage())
301                 }
302                 msoLogger.trace("Exit getAAICustomerById")
303
304         }
305
306         public void putServiceInstance(DelegateExecution execution) {
307                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
308                 msoLogger.trace("putServiceInstance")
309                 String msg = ""
310                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
311                 try {
312
313                         String serviceType = execution.getVariable("serviceType")
314                         //AAI PUT
315                         String oStatus = execution.getVariable("initialStatus") ?: "Active"
316                         if ("TRANSPORT".equalsIgnoreCase(serviceType))
317                         {
318                                 oStatus = "Created"
319                         }
320
321                         //QUERY CATALOG DB AND GET WORKLOAD / ENVIRONMENT CONTEXT
322                         String environmentContext = ""
323                         String workloadContext =""
324                         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
325
326                         try{
327                                  String json = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuidString(execution,modelInvariantUuid )
328
329                                  msoLogger.debug("JSON IS: "+json)
330
331                                  environmentContext = jsonUtil.getJsonValue(json, "serviceResources.environmentContext") ?: ""
332                                  workloadContext = jsonUtil.getJsonValue(json, "serviceResources.workloadContext") ?: ""
333                                  msoLogger.debug("Env Context is: "+ environmentContext)
334                                  msoLogger.debug("Workload Context is: "+ workloadContext)
335                         }catch(BpmnError e){
336                                 throw e
337                         } catch (Exception ex){
338                                 msg = "Exception in preProcessRequest " + ex.getMessage()
339                                 msoLogger.debug(msg)
340                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
341                         }
342
343                         org.onap.aai.domain.yang.ServiceInstance si = new org.onap.aai.domain.yang.ServiceInstance()
344                         si.setServiceInstanceName(execution.getVariable("serviceInstanceName"))
345                         si.setServiceType(serviceType)
346                         si.setServiceRole(execution.getVariable("serviceRole"))
347                         si.setOrchestrationStatus(oStatus)
348                         si.setModelInvariantId(modelInvariantUuid)
349                         si.setModelVersionId(execution.getVariable("modelUuid"))
350                         si.setEnvironmentContext(environmentContext)
351                         si.setWorkloadContext(workloadContext)
352
353                         AAIResourcesClient client = new AAIResourcesClient()
354                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), serviceInstanceId)
355                         client.create(uri, si)
356
357                 } catch (BpmnError e) {
358                         throw e;
359                 } catch (Exception ex) {
360                         //start rollback set up
361                         def rollbackData = execution.getVariable("rollbackData")
362                         if (rollbackData == null) {
363                                 rollbackData = new RollbackData()
364                         }                       
365                         def disableRollback = execution.getVariable("disableRollback")
366                         rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
367                         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
368                         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
369                         rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
370                         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
371                         execution.setVariable("rollbackData", rollbackData)
372
373                         msg = "Exception in DoCreateServiceInstance.putServiceInstance. " + ex.getMessage()
374                         msoLogger.debug(msg)
375                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
376                 }
377                 msoLogger.trace("Exit putServiceInstance")
378         }
379
380         public void preProcessSDNCAssignRequest(DelegateExecution execution) {
381                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
382                 String msg = ""
383                 msoLogger.trace("preProcessSDNCAssignRequest")
384
385                 try {
386                         def serviceInstanceId = execution.getVariable("serviceInstanceId")
387                         def serviceInstanceName = execution.getVariable("serviceInstanceName")
388                         def callbackURL = execution.getVariable("sdncCallbackUrl")
389                         def requestId = execution.getVariable("msoRequestId")
390                         def serviceId = execution.getVariable("productFamilyId")
391                         def subscriptionServiceType = execution.getVariable("subscriptionServiceType")
392                         def globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
393                         def msoAction = ""
394
395                         def modelInvariantUuid = execution.getVariable("modelInvariantUuid")
396                         def modelVersion = execution.getVariable("modelVersion")
397                         def modelUuid = execution.getVariable("modelUuid")
398                         def modelName = execution.getVariable("modelName")
399
400                         def sdncRequestId = UUID.randomUUID().toString()
401
402                         def siParamsXml = execution.getVariable("siParamsXml")
403
404                         // special URL for SDNW, msoAction helps set diff url in SDNCA
405                         if("TRANSPORT".equalsIgnoreCase(execution.getVariable("serviceType")))
406                         {
407                                 msoAction = "TRANSPORT"
408                         }
409
410                         String sdncAssignRequest =
411                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
412                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
413                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
414                                    <sdncadapter:RequestHeader>
415                                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(sdncRequestId)}</sdncadapter:RequestId>
416                                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
417                                                         <sdncadapter:SvcAction>assign</sdncadapter:SvcAction>
418                                                         <sdncadapter:SvcOperation>service-topology-operation</sdncadapter:SvcOperation>
419                                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
420                                                         <sdncadapter:MsoAction>${MsoUtils.xmlEscape(msoAction)}</sdncadapter:MsoAction>
421                                         </sdncadapter:RequestHeader>
422                                 <sdncadapterworkflow:SDNCRequestData>
423                                         <request-information>
424                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
425                                                 <source>MSO</source>
426                                                 <notification-url/>
427                                                 <order-number/>
428                                                 <order-version/>
429                                                 <request-action>CreateServiceInstance</request-action>
430                                         </request-information>
431                                         <service-information>
432                                                 <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
433                                                 <subscription-service-type>${MsoUtils.xmlEscape(subscriptionServiceType)}</subscription-service-type>
434                                                 <onap-model-information>
435                                                  <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-uuid>
436                                                  <model-uuid>${MsoUtils.xmlEscape(modelUuid)}</model-uuid>
437                                                  <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
438                                                  <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
439                                             </onap-model-information>
440                                                 <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
441                                                 <subscriber-name/>
442                                                 <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
443                                         </service-information>
444                                         <service-request-input>
445                                                 <service-instance-name>${MsoUtils.xmlEscape(serviceInstanceName)}</service-instance-name>
446                                                 ${siParamsXml}
447                                         </service-request-input>
448                                 </sdncadapterworkflow:SDNCRequestData>
449                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
450
451                         msoLogger.debug("sdncAssignRequest:\n" + sdncAssignRequest)
452                         sdncAssignRequest = utils.formatXml(sdncAssignRequest)
453                         execution.setVariable("sdncAssignRequest", sdncAssignRequest)
454                         msoLogger.debug("sdncAssignRequest:  " + sdncAssignRequest)
455
456                         def sdncRequestId2 = UUID.randomUUID().toString()
457                         String sdncDelete = sdncAssignRequest.replace(">assign<", ">delete<").replace(">CreateServiceInstance<", ">DeleteServiceInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
458                         def sdncRequestId3 = UUID.randomUUID().toString()
459                         String sdncDeactivate = sdncDelete.replace(">delete<", ">deactivate<").replace(">${sdncRequestId2}<", ">${sdncRequestId3}<")
460                         def rollbackData = execution.getVariable("rollbackData")
461                         if (rollbackData != null) {
462                                 rollbackData.put("SERVICEINSTANCE", "sdncDeactivate", sdncDeactivate)
463                                 rollbackData.put("SERVICEINSTANCE", "sdncDelete", sdncDelete)
464                                 execution.setVariable("rollbackData", rollbackData)             
465
466                                 msoLogger.debug("rollbackData:\n" + rollbackData.toString())
467                         }
468
469                 } catch (BpmnError e) {
470                         throw e;
471                 } catch(Exception ex) {
472                         msg = "Exception in preProcessSDNCAssignRequest. " + ex.getMessage()
473                         msoLogger.debug(msg)
474                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
475                 }
476                 msoLogger.trace("Exit preProcessSDNCAssignRequest")
477         }
478
479         public void postProcessSDNCAssign (DelegateExecution execution) {
480                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
481                 msoLogger.trace("postProcessSDNCAssign")
482                 try {
483                         WorkflowException workflowException = execution.getVariable("WorkflowException")
484                         msoLogger.debug("workflowException: " + workflowException)
485
486                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
487
488                         String response = execution.getVariable("sdncAdapterResponse")
489                         msoLogger.debug("SDNCResponse: " + response)
490
491                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
492                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
493
494                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
495                                 msoLogger.debug("Good response from SDNC Adapter for service-instance  topology assign: \n" + response)
496
497                                 def rollbackData = execution.getVariable("rollbackData")
498                                 if (rollbackData != null) {
499                                         rollbackData.put("SERVICEINSTANCE", "rollbackSDNC", "true")
500                                         execution.setVariable("rollbackData", rollbackData)
501                                 }
502
503                         }else{
504                                 msoLogger.debug("Bad Response from SDNC Adapter for service-instance assign")
505                                 throw new BpmnError("MSOWorkflowException")
506                         }
507
508                 } catch (BpmnError e) {
509                         throw e;
510                 } catch(Exception ex) {
511                         msg = "Exception in postProcessSDNCAssign. " + ex.getMessage()
512                         msoLogger.debug(msg)
513                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
514                 }
515                 msoLogger.trace("Exit postProcessSDNCAssign")
516         }
517
518         public void postProcessAAIGET2(DelegateExecution execution) {
519                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
520                 msoLogger.trace("postProcessAAIGET2")
521                 String msg = ""
522
523                 try {
524                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
525                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
526                         if(!succInAAI){
527                                 msoLogger.debug("Error getting Service-instance from AAI in postProcessAAIGET2", + serviceInstanceName)
528                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
529                                 msoLogger.debug("workflowException: " + workflowException)
530                                 if(workflowException != null){
531                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
532                                 }
533                                 else
534                                 {
535                                         msg = "Failure in postProcessAAIGET2 GENGS_SuccessIndicator:" + succInAAI
536                                         msoLogger.debug(msg)
537                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
538                                 }
539                         }
540                         else
541                         {
542                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
543                                 if(foundInAAI){
544                                         String aaiService = execution.getVariable("GENGS_service")
545                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "service-instance-name"))) {
546                                                 execution.setVariable("serviceInstanceName",  utils.getNodeText(aaiService, "service-instance-name"))
547                                                 msoLogger.debug("Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"))
548                                         }
549                                 }
550                         }
551                 } catch (BpmnError e) {
552                         throw e;
553                 } catch (Exception ex) {
554                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET2 " + ex.getMessage()
555                         msoLogger.debug(msg)
556                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
557                 }
558                 msoLogger.trace("Exit postProcessAAIGET2")
559         }
560
561         public void preProcessRollback (DelegateExecution execution) {
562                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
563                 msoLogger.trace("preProcessRollback")
564                 try {
565
566                         Object workflowException = execution.getVariable("WorkflowException");
567
568                         if (workflowException instanceof WorkflowException) {
569                                 msoLogger.debug("Prev workflowException: " + workflowException.getErrorMessage())
570                                 execution.setVariable("prevWorkflowException", workflowException);
571                                 //execution.setVariable("WorkflowException", null);
572                         }
573                 } catch (BpmnError e) {
574                         msoLogger.debug("BPMN Error during preProcessRollback")
575                 } catch(Exception ex) {
576                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
577                         msoLogger.debug(msg)
578                 }
579                 msoLogger.trace("Exit preProcessRollback")
580         }
581
582         public void postProcessRollback (DelegateExecution execution) {
583                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
584                 msoLogger.trace("postProcessRollback")
585                 String msg = ""
586                 try {
587                         Object workflowException = execution.getVariable("prevWorkflowException");
588                         if (workflowException instanceof WorkflowException) {
589                                 msoLogger.debug("Setting prevException to WorkflowException: ")
590                                 execution.setVariable("WorkflowException", workflowException);
591                         }
592                         execution.setVariable("rollbackData", null)
593                 } catch (BpmnError b) {
594                         msoLogger.debug("BPMN Error during postProcessRollback")
595                         throw b;
596                 } catch(Exception ex) {
597                         msg = "Exception in postProcessRollback. " + ex.getMessage()
598                         msoLogger.debug(msg)
599                 }
600                 msoLogger.trace("Exit postProcessRollback")
601         }
602
603         public void createProject(DelegateExecution execution) {
604                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
605                 msoLogger.trace("createProject")
606
607                 String bpmnRequest = execution.getVariable("requestJson")
608                 String projectName = jsonUtil.getJsonValue(bpmnRequest, "requestDetails.project.projectName")
609                 String serviceInstance = execution.getVariable("serviceInstanceId")
610
611                 msoLogger.debug("BPMN REQUEST IS: "+ bpmnRequest)
612                 msoLogger.debug("PROJECT NAME: " + projectName)
613                 msoLogger.debug("Service Instance: " + serviceInstance)
614
615                 if(projectName == null||projectName.equals("")){
616                         msoLogger.debug("Project Name was not found in input. Skipping task...")
617                 }else{
618                         try{
619                                 AAICreateResources aaiCR = new AAICreateResources()
620                                 aaiCR.createAAIProject(projectName, serviceInstance)
621                         }catch(Exception ex){
622                                 String msg = "Exception in createProject. " + ex.getMessage();
623                                 msoLogger.debug(msg)
624                                 msoLogger.error(ex);
625                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
626                         }
627                 }
628                 msoLogger.trace("Exit createProject")
629         }
630
631         public void createOwningEntity(DelegateExecution execution) {
632                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
633                 msoLogger.trace("createOwningEntity")
634                 String msg = "";
635                 String bpmnRequest = execution.getVariable("requestJson")
636                 String owningEntityId = jsonUtil.getJsonValue(bpmnRequest, "requestDetails.owningEntity.owningEntityId")
637                 String owningEntityName = jsonUtil.getJsonValue(bpmnRequest,"requestDetails.owningEntity.owningEntityName");
638                 String serviceInstance = execution.getVariable("serviceInstanceId")
639
640                 msoLogger.debug("owningEntity: " + owningEntityId)
641                 msoLogger.debug("OwningEntityName: "+ owningEntityName)
642                 msoLogger.debug("Service Instance: " + serviceInstance)
643
644                 try{
645                         AAICreateResources aaiCR = new AAICreateResources()
646                         if(owningEntityId==null||owningEntityId.equals("")){
647                                 msg = "Exception in createOwningEntity. OwningEntityId is null in input.";
648                                 throw new IllegalStateException();
649                         }else{
650                                 if(aaiCR.existsOwningEntity(owningEntityId)){
651                                         aaiCR.connectOwningEntityandServiceInstance(owningEntityId,serviceInstance)
652                                 }else{
653                                         if(owningEntityName==null||owningEntityName.equals("")){
654                                                 msg = "Exception in createOwningEntity. Can't create an owningEntity without an owningEntityName in input.";
655                                                 throw new IllegalStateException();
656                                         }else{
657                                                 Optional<OwningEntity> owningEntity = aaiCR.getOwningEntityNames(owningEntityName);
658                                                 if(owningEntity.isPresent()){
659                                                         msg = "Exception in createOwningEntity. Can't create OwningEntity as name already exists in AAI associated with a different owning-entity-id (name must be unique).";
660                                                         throw new IllegalStateException();
661                                                 } else {
662                                                         aaiCR.createAAIOwningEntity(owningEntityId, owningEntityName, serviceInstance)
663                                                 }
664                                         }
665                                 }
666                         }
667                 }catch(Exception ex){
668                         msoLogger.debug(msg)
669                         msoLogger.error(ex);
670                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
671                 }
672                 msoLogger.trace("Exit createOwningEntity")
673         }
674
675         // *******************************
676         //     Build Error Section
677         // *******************************
678
679         public void processJavaException(DelegateExecution execution){
680                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
681
682                 try{
683                         msoLogger.debug("Caught a Java Exception in DoCreateServiceInstance")
684                         msoLogger.debug("Started processJavaException Method")
685                         msoLogger.debug("Variables List: " + execution.getVariables())
686                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception in DoCreateServiceInstance")  // Adding this line temporarily until this flows error handling gets updated
687                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception in DoCreateServiceInstance")
688
689                 }catch(Exception e){
690                         msoLogger.debug("Caught Exception during processJavaException Method: " + e)
691                         execution.setVariable("UnexpectedError", "Exception in processJavaException")  // Adding this line temporarily until this flows error handling gets updated
692                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method")
693                 }
694                 msoLogger.trace("Completed processJavaException Method in DoCreateServiceInstance")
695         }
696
697 }