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