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