Merge "Fix AAI Service Delete failed issue."
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / 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 package org.openecomp.mso.bpmn.infrastructure.scripts;
21
22 import static org.apache.commons.lang3.StringUtils.*;
23 import groovy.xml.XmlUtil
24 import groovy.json.*
25
26 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
27 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
28 import org.openecomp.mso.bpmn.core.domain.ModelInfo
29 import org.openecomp.mso.bpmn.core.json.JsonUtils
30 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
31 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
33 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
34 import org.openecomp.mso.bpmn.core.RollbackData
35 import org.openecomp.mso.bpmn.core.WorkflowException
36 import org.openecomp.mso.rest.APIResponse;
37 import org.openecomp.mso.rest.RESTClient
38 import org.openecomp.mso.rest.RESTConfig
39
40 import java.util.UUID;
41
42 import org.camunda.bpm.engine.delegate.BpmnError
43 import org.camunda.bpm.engine.runtime.Execution
44 import org.json.JSONObject;
45 import org.apache.commons.lang3.*
46 import org.apache.commons.codec.binary.Base64;
47 import org.springframework.web.util.UriUtils;
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  */
74 public class DoCreateServiceInstance extends AbstractServiceTaskProcessor {
75
76         String Prefix="DCRESI_"
77         ExceptionUtil exceptionUtil = new ExceptionUtil()
78         JsonUtils jsonUtil = new JsonUtils()
79
80         public void preProcessRequest (Execution execution) {
81                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
82                 String msg = ""
83                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)
84
85                 try {
86                         String requestId = execution.getVariable("msoRequestId")
87                         execution.setVariable("prefix", Prefix)
88                         
89                         //Inputs
90                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
91                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
92
93                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
94                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
95
96                         //requestDetails.requestParameters. for SDNC assignTopology
97                         String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
98
99                         if (isBlank(globalSubscriberId)) {
100                                 msg = "Input globalSubscriberId is null"
101                                 utils.log("DEBUG", msg, isDebugEnabled)
102                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
103                         }
104                         
105                         if (isBlank(subscriptionServiceType)) {
106                                 msg = "Input subscriptionServiceType is null"
107                                 utils.log("DEBUG", msg, isDebugEnabled)
108                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109                         }
110                         
111                         if (productFamilyId == null) {
112                                 execution.setVariable("productFamilyId", "")
113                         }
114                         
115                         String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
116                         if (isBlank(sdncCallbackUrl)) {
117                                 msg = "URN_mso_workflow_sdncadapter_callback is null"
118                                 utils.log("DEBUG", msg, isDebugEnabled)
119                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
120                         }
121                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
122                         utils.log("DEBUG","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
123
124                         //requestDetails.modelInfo.for AAI PUT servieInstanceData & SDNC assignTopology
125                         String modelInvariantUuid = ""
126                         String modelVersion = ""
127                         String modelUuid = ""
128                         String modelName = ""
129                         String serviceInstanceName = "" 
130                         //Generated in parent.for AAI PUT
131                         String serviceInstanceId = ""
132                         String serviceType = ""
133                         String serviceRole = ""
134                         
135                         ServiceDecomposition serviceDecomp = (ServiceDecomposition) execution.getVariable("serviceDecomposition")
136                         if (serviceDecomp != null)
137                         {
138                                 serviceType = serviceDecomp.getServiceType()
139                                 if (serviceType == null)
140                                 {
141                                         utils.log("DEBUG", "null serviceType", isDebugEnabled)
142                                         serviceType = ""
143                                 }
144                                 else
145                                 {
146                                         utils.log("DEBUG", "serviceType:" + serviceType, isDebugEnabled)
147                                 }
148                                 serviceRole = serviceDecomp.getServiceRole()
149                                 if (serviceRole == null)
150                                 {
151                                         serviceRole = ""
152                                 }
153                                 
154                                 ServiceInstance serviceInstance = serviceDecomp.getServiceInstance()
155                                 if (serviceInstance != null)
156                                 {
157                                         serviceInstanceId = serviceInstance.getInstanceId()
158                                         serviceInstanceName = serviceInstance.getInstanceName()
159                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
160                                         execution.setVariable("serviceInstanceName", serviceInstanceName)
161                                 }
162                                 
163                                 ModelInfo modelInfo = serviceDecomp.getModelInfo()
164                                 if (modelInfo != null)
165                                 {
166                                         modelInvariantUuid = modelInfo.getModelInvariantUuid()
167                                         modelVersion = modelInfo.getModelVersion()
168                                         modelUuid = modelInfo.getModelUuid()
169                                         modelName = modelInfo.getModelName()
170                                 }
171                                 else 
172                                 {
173                                         msg = "Input serviceModelInfo is null"
174                                         utils.log("DEBUG", msg, isDebugEnabled)
175                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
176                                 }
177                         }
178                         else
179                         {
180                                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData & SDNC assignToplology
181                                 serviceInstanceName = execution.getVariable("serviceInstanceName")
182                                 serviceInstanceId = execution.getVariable("serviceInstanceId")
183                                 
184                                 String serviceModelInfo = execution.getVariable("serviceModelInfo")
185                                 if (isBlank(serviceModelInfo)) {
186                                         msg = "Input serviceModelInfo is null"
187                                         utils.log("DEBUG", msg, isDebugEnabled)
188                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
189                                 }
190                                 modelInvariantUuid = jsonUtil.getJsonValue(serviceModelInfo, "modelInvariantUuid")
191                                 modelVersion = jsonUtil.getJsonValue(serviceModelInfo, "modelVersion")
192                                 modelUuid = jsonUtil.getJsonValue(serviceModelInfo, "modelUuid")
193                                 modelName = jsonUtil.getJsonValue(serviceModelInfo, "modelName")
194                                 //modelCustomizationUuid NA for SI
195         
196                         }
197                         execution.setVariable("serviceType", serviceType)
198                         execution.setVariable("serviceRole", serviceRole)
199                         
200                         if (serviceInstanceName == null) {
201                                 execution.setVariable("serviceInstanceName", "")
202                                 serviceInstanceName = ""
203                         }
204                         if (isBlank(serviceInstanceId)){
205                                 msg = "Input serviceInstanceId is null"
206                                 utils.log("DEBUG", msg, isDebugEnabled)
207                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
208                         }
209                         
210                         if (modelInvariantUuid == null) {
211                                 modelInvariantUuid = ""
212                         }
213                         if (modelUuid == null) {
214                                 modelUuid = ""
215                         }
216                         if (modelVersion == null) {
217                                 modelVersion = ""
218                         }
219                         if (modelName == null) {
220                                 modelName = ""
221                         }
222                         
223                         execution.setVariable("modelInvariantUuid", modelInvariantUuid)
224                         execution.setVariable("modelVersion", modelVersion)
225                         execution.setVariable("modelUuid", modelUuid)
226                         execution.setVariable("modelName", modelName)
227                         
228                         StringBuilder sbParams = new StringBuilder()
229                         Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
230                         if (paramsMap != null)
231                         {
232                                 sbParams.append("<service-input-parameters>")
233                                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
234                                         String paramsXml
235                                         String paramName = entry.getKey()
236                                         String paramValue = entry.getValue()
237                                         paramsXml =
238                                                         """     <param>
239                                                         <name>${paramName}</name>
240                                                         <value>${paramValue}</value>
241                                                         </param>
242                                                         """
243                                         sbParams.append(paramsXml)
244                                 }
245                                 sbParams.append("</service-input-parameters>")
246                         }
247                         String siParamsXml = sbParams.toString()
248                         if (siParamsXml == null)
249                                 siParamsXml = ""
250                         execution.setVariable("siParamsXml", siParamsXml)
251
252                         //AAI PUT
253                         String oStatus = execution.getVariable("initialStatus") ?: ""
254                         if ("TRANSPORT".equalsIgnoreCase(serviceType))
255                         {
256                                 oStatus = "Created"
257                         }
258
259                         String statusLine = isBlank(oStatus) ? "" : "<orchestration-status>${oStatus}</orchestration-status>"
260                         String serviceTypeLine = isBlank(serviceType) ? "" : "<service-type>${serviceType}</service-type>"
261                         String serviceRoleLine = isBlank(serviceRole) ? "" : "<service-role>${serviceRole}</service-role>"
262                                 
263                         AaiUtil aaiUriUtil = new AaiUtil(this)
264                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
265                         String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
266                         String serviceInstanceData =
267                                         """<service-instance xmlns=\"${namespace}\">
268                                         <service-instance-name>${serviceInstanceName}</service-instance-name>
269                                         ${serviceTypeLine}
270                                         ${serviceRoleLine}
271                                         ${statusLine}
272                                     <model-invariant-id>${modelInvariantUuid}</model-invariant-id>
273                                     <model-version-id>${modelUuid}</model-version-id>
274                                         </service-instance>""".trim()
275
276                         execution.setVariable("serviceInstanceData", serviceInstanceData)
277                         utils.logAudit(serviceInstanceData)
278                         utils.log("DEBUG", " 'payload' to create Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
279
280                 } catch (BpmnError e) {
281                         throw e;
282                 } catch (Exception ex){
283                         msg = "Exception in preProcessRequest " + ex.getMessage()
284                         utils.log("DEBUG", msg, isDebugEnabled)
285                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
286                 }
287                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
288         }
289
290         //TODO: Will be able to replace with call to GenericGetService
291         public void getAAICustomerById (Execution execution) {
292                 // https://{aaiEP}/aai/v8/business/customers/customer/{globalCustomerId}
293                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
294                 String msg = ""
295                 try {
296
297                         String globalCustomerId = execution.getVariable("globalSubscriberId") //VID to AAI name map
298                         utils.log("DEBUG"," ***** getAAICustomerById ***** globalCustomerId:" + globalCustomerId, isDebugEnabled)
299
300                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")
301                         AaiUtil aaiUriUtil = new AaiUtil(this)
302                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
303                         if (isBlank(aai_endpoint) || isBlank(aai_uri))
304                         {
305                                 msg = "AAI URL is invalid. Endpoint:" + aai_endpoint + aai_uri
306                                 utils.log("DEBUG", msg, isDebugEnabled)
307                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
308                         }
309                         String getAAICustomerUrl = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8")
310
311                         utils.logAudit(getAAICustomerUrl)
312                         utils.log("DEBUG", "getAAICustomerById Url:" + getAAICustomerUrl, isDebugEnabled)
313                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getAAICustomerUrl)
314                         String returnCode = response.getStatusCode()
315                         String aaiResponseAsString = response.getResponseBodyAsString()
316
317                         msg = "getAAICustomerById ResponseCode:" + returnCode + " ResponseString:" + aaiResponseAsString
318                         utils.log("DEBUG",msg, isDebugEnabled)
319                         utils.logAudit(msg)
320
321                         if (returnCode=='200') {
322                                 // Customer found by ID. FLow to proceed.
323                                 utils.log("DEBUG",msg, isDebugEnabled)
324
325                                 //TODO Deferred
326                                 //we might verify that service-subscription with matching name exists
327                                 //and throw error if not. If not checked, we will get exception in subsequent step on Create call
328                                 //in 1610 we assume both customer & service subscription were pre-created
329
330                         } else {
331                                 if (returnCode=='404') {
332                                         msg = "GlobalCustomerId:" + globalCustomerId + " not found (404) in AAI"
333                                         utils.log("DEBUG", msg, isDebugEnabled)
334                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
335
336                                 } else {
337                                         if (aaiResponseAsString.contains("RESTFault")) {
338                                                 utils.log("ERROR", aaiResponseAsString)
339                                                 WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
340                                                 execution.setVariable("WorkflowException", workflowException)
341                                                 throw new BpmnError("MSOWorkflowException")
342
343                                         } else {
344                                                 // aai all errors
345                                                 msg = "Error in getAAICustomerById ResponseCode:" + returnCode
346                                                 utils.log("DEBUG", msg, isDebugEnabled)
347                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
348                                         }
349                                 }
350                         }
351
352                 } catch (BpmnError e) {
353                         throw e;
354                 } catch (Exception ex) {
355                         msg = "Exception in getAAICustomerById. " + ex.getMessage()
356                         utils.log("DEBUG", msg, isDebugEnabled)
357                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
358                 }
359                 utils.log("DEBUG"," *****Exit getAAICustomerById *****", isDebugEnabled)
360
361         }
362
363         public void postProcessAAIGET(Execution execution) {
364                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
365                 utils.log("DEBUG"," ***** postProcessAAIGET ***** ", isDebugEnabled)
366                 String msg = ""
367
368                 try {
369                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
370                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
371                         if(succInAAI != true){
372                                 utils.log("DEBUG","Error getting Service-instance from AAI", + serviceInstanceName, isDebugEnabled)
373                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
374                                 utils.logAudit("workflowException: " + workflowException)
375                                 if(workflowException != null){
376                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
377                                 }
378                                 else
379                                 {
380                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
381                                         utils.log("DEBUG", msg, isDebugEnabled)
382                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
383                                 }
384                         }
385                         else
386                         {
387                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
388                                 if(foundInAAI == true){
389                                         utils.log("DEBUG","Found Service-instance in AAI", isDebugEnabled)
390                                         msg = "ServiceInstance already exists in AAI:" + serviceInstanceName
391                                         utils.log("DEBUG", msg, isDebugEnabled)
392                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
393                                 }
394                         }
395                 } catch (BpmnError e) {
396                         throw e;
397                 } catch (Exception ex) {
398                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET. " + ex.getMessage()
399                         utils.log("DEBUG", msg, isDebugEnabled)
400                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
401                 }
402                 utils.log("DEBUG"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
403         }
404
405         public void postProcessAAIPUT(Execution execution) {
406                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
407                 utils.log("DEBUG"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
408                 String msg = ""
409                 try {
410                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
411                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
412                         if(succInAAI != true){
413                                 utils.log("DEBUG","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
414                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
415                                 utils.logAudit("workflowException: " + workflowException)
416                                 if(workflowException != null){
417                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
418                                 }
419                         }
420                         else
421                         {
422                                 //start rollback set up
423                                 RollbackData rollbackData = new RollbackData()
424                                 def disableRollback = execution.getVariable("disableRollback")
425                                 rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
426                                 rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
427                                 rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", serviceInstanceId)
428                                 rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
429                                 rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
430                                 execution.setVariable("rollbackData", rollbackData)
431                         }
432
433                 } catch (BpmnError e) {
434                         throw e;
435                 } catch (Exception ex) {
436                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
437                         utils.log("DEBUG", msg, isDebugEnabled)
438                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
439                 }
440                 utils.log("DEBUG"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
441         }
442
443         public void preProcessSDNCAssignRequest(Execution execution) {
444                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
445                 String msg = ""
446                 utils.log("DEBUG"," ***** preProcessSDNCAssignRequest *****", isDebugEnabled)
447
448                 try {
449                         def serviceInstanceId = execution.getVariable("serviceInstanceId")
450                         def serviceInstanceName = execution.getVariable("serviceInstanceName")
451                         def callbackURL = execution.getVariable("sdncCallbackUrl")
452                         def requestId = execution.getVariable("msoRequestId")
453                         def serviceId = execution.getVariable("productFamilyId")
454                         def subscriptionServiceType = execution.getVariable("subscriptionServiceType")
455                         def globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
456                         def serviceType = execution.getVariable("serviceType")
457
458                         def modelInvariantUuid = execution.getVariable("modelInvariantUuid")
459                         def modelVersion = execution.getVariable("modelVersion")
460                         def modelUuid = execution.getVariable("modelUuid")
461                         def modelName = execution.getVariable("modelName")
462                         
463                         def sdncRequestId = UUID.randomUUID().toString()
464                         
465                         def siParamsXml = execution.getVariable("siParamsXml")
466                         
467                         String sdncAssignRequest =
468                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
469                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
470                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
471                                    <sdncadapter:RequestHeader>
472                                                         <sdncadapter:RequestId>${sdncRequestId}</sdncadapter:RequestId>
473                                                         <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>
474                                                         <sdncadapter:SvcAction>assign</sdncadapter:SvcAction>
475                                                         <sdncadapter:SvcOperation>service-topology-operation</sdncadapter:SvcOperation>
476                                                         <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
477                                                         <sdncadapter:MsoAction>${serviceType}</sdncadapter:MsoAction>
478                                         </sdncadapter:RequestHeader>
479                                 <sdncadapterworkflow:SDNCRequestData>
480                                         <request-information>
481                                                 <request-id>${requestId}</request-id>
482                                                 <source>MSO</source>
483                                                 <notification-url/>
484                                                 <order-number/>
485                                                 <order-version/>
486                                                 <request-action>CreateServiceInstance</request-action>
487                                         </request-information>
488                                         <service-information>
489                                                 <service-id>${serviceId}</service-id>
490                                                 <subscription-service-type>${subscriptionServiceType}</subscription-service-type>
491                                                 <onap-model-information>
492                                                  <model-invariant-uuid>${modelInvariantUuid}</model-invariant-uuid>
493                                                  <model-uuid>${modelUuid}</model-uuid>
494                                                  <model-version>${modelVersion}</model-version>
495                                                  <model-name>${modelName}</model-name>
496                                             </onap-model-information>
497                                                 <service-instance-id>${serviceInstanceId}</service-instance-id>
498                                                 <subscriber-name/>
499                                                 <global-customer-id>${globalSubscriberId}</global-customer-id>
500                                         </service-information>
501                                         <service-request-input>
502                                                 <service-instance-name>${serviceInstanceName}</service-instance-name>
503                                                 ${siParamsXml}
504                                         </service-request-input>
505                                 </sdncadapterworkflow:SDNCRequestData>
506                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
507
508                         utils.log("DEBUG","sdncAssignRequest:\n" + sdncAssignRequest, isDebugEnabled)
509                         sdncAssignRequest = utils.formatXml(sdncAssignRequest)
510                         execution.setVariable("sdncAssignRequest", sdncAssignRequest)
511                         utils.logAudit("sdncAssignRequest:  " + sdncAssignRequest)
512
513                         def sdncRequestId2 = UUID.randomUUID().toString()
514                         String sdncDelete = sdncAssignRequest.replace(">assign<", ">delete<").replace(">CreateServiceInstance<", ">DeleteServiceInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
515                         def sdncRequestId3 = UUID.randomUUID().toString()
516                         String sdncDeactivate = sdncDelete.replace(">delete<", ">deactivate<").replace(">${sdncRequestId2}<", ">${sdncRequestId3}<")
517                         def rollbackData = execution.getVariable("rollbackData")
518                         rollbackData.put("SERVICEINSTANCE", "sdncDeactivate", sdncDeactivate)
519                         rollbackData.put("SERVICEINSTANCE", "sdncDelete", sdncDelete)
520                         execution.setVariable("rollbackData", rollbackData)
521                         
522                         utils.log("DEBUG","rollbackData:\n" + rollbackData.toString(), isDebugEnabled)
523
524                 } catch (BpmnError e) {
525                         throw e;
526                 } catch(Exception ex) {
527                         msg = "Exception in preProcessSDNCAssignRequest. " + ex.getMessage()
528                         utils.log("DEBUG", msg, isDebugEnabled)
529                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
530                 }
531                 utils.log("DEBUG"," *****Exit preProcessSDNCAssignRequest *****", isDebugEnabled)
532         }
533         
534         public void postProcessSDNCAssign (Execution execution) {
535                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
536                 utils.log("DEBUG"," ***** postProcessSDNCAssign ***** ", isDebugEnabled)
537                 try {
538                         WorkflowException workflowException = execution.getVariable("WorkflowException")
539                         utils.logAudit("workflowException: " + workflowException)
540
541                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
542
543                         String response = execution.getVariable("sdncAdapterResponse")
544                         utils.logAudit("SDNCResponse: " + response)
545
546                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
547                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
548
549                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
550                                 utils.log("DEBUG","Good response from SDNC Adapter for service-instance  topology assign: \n" + response, isDebugEnabled)
551
552                                 def rollbackData = execution.getVariable("rollbackData")
553                                 rollbackData.put("SERVICEINSTANCE", "rollbackSDNC", "true")
554                                 execution.setVariable("rollbackData", rollbackData)
555
556                         }else{
557                                 utils.log("DEBUG","Bad Response from SDNC Adapter for service-instance assign", isDebugEnabled)
558                                 throw new BpmnError("MSOWorkflowException")
559                         }
560
561                 } catch (BpmnError e) {
562                         throw e;
563                 } catch(Exception ex) {
564                         msg = "Exception in postProcessSDNCAssign. " + ex.getMessage()
565                         utils.log("DEBUG", msg, isDebugEnabled)
566                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
567                 }
568                 utils.log("DEBUG"," *** Exit postProcessSDNCAssign *** ", isDebugEnabled)
569         }
570         
571         public void postProcessAAIGET2(Execution execution) {
572                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
573                 utils.log("DEBUG"," ***** postProcessAAIGET2 ***** ", isDebugEnabled)
574                 String msg = ""
575
576                 try {
577                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
578                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
579                         if(succInAAI != true){
580                                 utils.log("DEBUG","Error getting Service-instance from AAI in postProcessAAIGET2", + serviceInstanceName, isDebugEnabled)
581                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
582                                 utils.logAudit("workflowException: " + workflowException)
583                                 if(workflowException != null){
584                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
585                                 }
586                                 else
587                                 {
588                                         msg = "Failure in postProcessAAIGET2 GENGS_SuccessIndicator:" + succInAAI
589                                         utils.log("DEBUG", msg, isDebugEnabled)
590                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
591                                 }
592                         }
593                         else
594                         {
595                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
596                                 if(foundInAAI == true){
597                                         String aaiService = execution.getVariable("GENGS_service")
598                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "service-instance-name"))) {
599                                                 execution.setVariable("serviceInstanceName",  utils.getNodeText1(aaiService, "service-instance-name"))
600                                                 utils.log("DEBUG","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
601                                         }
602                                 }
603                         }
604                 } catch (BpmnError e) {
605                         throw e;
606                 } catch (Exception ex) {
607                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET2 " + ex.getMessage()
608                         utils.log("DEBUG", msg, isDebugEnabled)
609                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
610                 }
611                 utils.log("DEBUG"," *** Exit postProcessAAIGET2 *** ", isDebugEnabled)
612         }
613
614         public void preProcessRollback (Execution execution) {
615                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
616                 utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugEnabled)
617                 try {
618                         
619                         Object workflowException = execution.getVariable("WorkflowException");
620
621                         if (workflowException instanceof WorkflowException) {
622                                 utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
623                                 execution.setVariable("prevWorkflowException", workflowException);
624                                 //execution.setVariable("WorkflowException", null);
625                         }
626                 } catch (BpmnError e) {
627                         utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugEnabled)
628                 } catch(Exception ex) {
629                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
630                         utils.log("DEBUG", msg, isDebugEnabled)
631                 }
632                 utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugEnabled)
633         }
634
635         public void postProcessRollback (Execution execution) {
636                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
637                 utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugEnabled)
638                 String msg = ""
639                 try {
640                         Object workflowException = execution.getVariable("prevWorkflowException");
641                         if (workflowException instanceof WorkflowException) {
642                                 utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugEnabled)
643                                 execution.setVariable("WorkflowException", workflowException);
644                         }
645                         execution.setVariable("rollbackData", null)
646                 } catch (BpmnError b) {
647                         utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugEnabled)
648                         throw b;
649                 } catch(Exception ex) {
650                         msg = "Exception in postProcessRollback. " + ex.getMessage()
651                         utils.log("DEBUG", msg, isDebugEnabled)
652                 }
653                 utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugEnabled)
654         }
655
656 }