80e6e758c013111d3ab316c00863cdb343d3568a
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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 org.json.JSONObject
24 import org.json.XML
25
26 import static org.apache.commons.lang3.StringUtils.*
27 import groovy.xml.XmlUtil
28 import groovy.json.*
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
32 import org.onap.so.bpmn.common.scripts.AaiUtil
33 import org.onap.so.bpmn.common.scripts.MsoUtils
34 import org.onap.so.bpmn.common.recipe.ResourceInput
35 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
36 import org.onap.so.bpmn.core.WorkflowException
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
39 import org.onap.so.rest.APIResponse
40 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
41 import org.onap.so.bpmn.infrastructure.workflow.service.ServicePluginFactory
42 import java.util.UUID
43 import org.onap.so.logger.MsoLogger
44
45 import org.camunda.bpm.engine.runtime.Execution
46 import org.camunda.bpm.engine.delegate.BpmnError
47 import org.camunda.bpm.engine.delegate.DelegateExecution
48 import org.apache.commons.lang3.*
49 import org.apache.commons.codec.binary.Base64
50 import org.springframework.web.util.UriUtils
51 import org.onap.so.rest.RESTClient
52 import org.onap.so.rest.RESTConfig
53
54 /**
55  * This groovy class supports the <class>Create3rdONAPE2EServiceInstance.bpmn</class> process.
56  * flow for Create E2EServiceInstance in 3rdONAP 
57  */
58 public class Create3rdONAPE2EServiceInstance extends AbstractServiceTaskProcessor {
59
60         String Prefix = "CRE3rdONAPESI_"
61
62         ExceptionUtil exceptionUtil = new ExceptionUtil()
63
64         JsonUtils jsonUtil = new JsonUtils()
65         
66         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, Create3rdONAPE2EServiceInstance.class)
67
68         public void checkSPPartnerInfo (DelegateExecution execution) {
69                 msoLogger.info(" ***** Started checkSPPartnerInfo *****")
70                 try {
71                         //get bpmn inputs from resource request.
72                         String requestId = execution.getVariable("mso-request-id")
73                         String requestAction = execution.getVariable("requestAction")
74                         msoLogger.info("The requestAction is: " + requestAction)
75                         String recipeParamsFromRequest = execution.getVariable("recipeParams")
76                         msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
77                         String resourceInput = execution.getVariable("resourceInput")
78                         msoLogger.info("The resourceInput is: " + resourceInput)
79                         //Get ResourceInput Object
80                         ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)                       
81                         String resourceInputPrameters = resourceInputObj.getResourceParameters()
82                         String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
83                         JSONObject inputParameters = new JSONObject(customizeResourceParam(inputParametersJson))
84                         
85                         // set local resourceInput
86                         execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
87                         
88                         boolean is3rdONAPExist = false
89
90                         if(inputParameters.has("id"))
91                         {
92                                 String sppartnerId = inputParameters.get("id")
93                         }
94                         if(inputParameters.has("url"))
95                         {
96                                 String sppartnerUrl = inputParameters.get("url")
97                                 if(!isBlank(sppartnerUrl)) {
98                                         execution.setVariable(Prefix + "SppartnerUrl", sppartnerUrl)
99                                         is3rdONAPExist = true
100                                 }
101                                 else {
102                                         is3rdONAPExist = false
103                                         String msg = "sppartner Url is blank."
104                                         msoLogger.debug(msg)
105                                 }
106                         }
107                         if(inputParameters.has("providingServiceInvarianteUuid"))
108                         {
109                                 String sppartnerInvarianteUUID = inputParameters.get("providingServiceInvarianteUuid")
110                                 execution.setVariable(Prefix + "SppartnerInvarianteUUID", sppartnerInvarianteUUID)
111                                 is3rdONAPExist = true
112                         }
113                         else {
114                                 is3rdONAPExist = false
115                                 String msg = "sppartner providingServiceInvarianteUuid is blank."
116                                 msoLogger.debug(msg)
117                         }
118                         if(inputParameters.has("providingServiceUuid"))
119                         {
120                                 String sppartnerUUID = inputParameters.get("providingServiceUuid")
121                                 execution.setVariable(Prefix + "SppartnerUUID", sppartnerUUID)
122                                 is3rdONAPExist = true
123                         }
124                         else {
125                                 is3rdONAPExist = false
126                                 String msg = "sppartner providingServiceUuid is blank."
127                                 msoLogger.debug(msg)
128                         }
129                         
130                         if(inputParameters.has("handoverMode"))
131                         {
132                                 String handoverMode = inputParameters.get("handoverMode")
133                                 execution.setVariable(Prefix + "HandoverMode", handoverMode)
134                             is3rdONAPExist = true
135                         }
136                         else {
137                                 is3rdONAPExist = false
138                                 String msg = "sppartner handoverMode is blank."
139                                 msoLogger.debug(msg)
140                         }
141                         
142                         execution.setVariable("Is3rdONAPExist", is3rdONAPExist)
143                         execution.setVariable(Prefix + "ServiceInstanceId", resourceInputObj.getServiceInstanceId())
144                         execution.setVariable("mso-request-id", requestId)
145                         execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
146
147                 } catch (BpmnError e) {
148                         throw e
149                 } catch (Exception ex){
150                         String msg = "Exception in checkSPPartnerInfo " + ex.getMessage()
151                         msoLogger.debug(msg)
152                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
153                 }
154         }
155
156         public void checkLocallCall (DelegateExecution execution) {
157                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
158                 msoLogger.info(" ***** Started checkLocallCall *****")
159                 try {
160                                         
161                         //Get ResourceInput Object
162                         ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")                        
163
164                         //uuiRequest
165                         String incomingRequest = resourceInputObj.getRequestsInputs()
166                         String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
167                         JSONObject inputParameters = new JSONObject(customizeResourceParam(serviceParameters))
168                         execution.setVariable(Prefix + "ServiceParameters", inputParameters)
169                         
170                         // CallSource is added only when ONAP SO calling 3rdONAP(External API) SO(Remote call)
171                         boolean isLocalCall = true
172                         String callSource = "UUI"
173                         if(inputParameters.has("CallSource"))
174                         {
175                                 callSource = inputParameters.get("CallSource")
176                                 if("ExternalAPI".equalsIgnoreCase(callSource)) {
177                                         isLocalCall = false
178                                 }                                                       
179                         }
180                         execution.setVariable(Prefix + "CallSource", callSource)
181                         msoLogger.debug("callSource is: " + callSource )
182                         
183                         execution.setVariable("IsLocalCall", isLocalCall)
184
185                 } catch (Exception ex){
186                         String msg = "Exception in checkLocallCall " + ex.getMessage()
187                         msoLogger.debug(msg)
188                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
189                 }
190         }
191
192         public void preProcessRequest(DelegateExecution execution){
193                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
194                 msoLogger.info(" ***** Started preProcessRequest *****")
195                 try {
196                         ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
197                         String msg = ""
198
199                         String globalSubscriberId = resourceInputObj.getGlobalSubscriberId()
200                         if (isBlank(globalSubscriberId)) {
201                                 msg = "Input globalSubscriberId is null"
202                                 msoLogger.info(msg)
203                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
204                         }
205                         //set local variable
206                         execution.setVariable("globalSubscriberId", globalSubscriberId)
207                         msoLogger.info("globalSubscriberId:" + globalSubscriberId)
208
209                         String serviceType = resourceInputObj.getServiceType()
210                         if (isBlank(serviceType)) {
211                                 msg = "Input serviceType is null"
212                                 msoLogger.info(msg)
213                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
214                         }
215                         execution.setVariable("serviceType", serviceType)
216                         msoLogger.info("serviceType:" + serviceType)
217                         
218                         String resourceName = resourceInputObj.getResourceInstanceName()
219                         if (isBlank(resourceName)) {
220                                 msg = "Input resourceName is null"
221                                 msoLogger.info(msg)
222                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
223                         }
224                         execution.setVariable("resourceName", resourceName)
225                         msoLogger.info("resourceName:" + resourceName)
226                         
227                         int beginIndex = resourceName.indexOf("_") + 1
228                         String serviceInstanceName = resourceName.substring(beginIndex)
229                         execution.setVariable("serviceInstanceName", serviceInstanceName)
230                         
231                         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
232                         if (isBlank(serviceInstanceId)) {
233                                 msg = "Input serviceInstanceId is null"
234                                 msoLogger.info(msg)
235                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
236                         }
237                         execution.setVariable("serviceInstanceId", serviceInstanceId)
238                         msoLogger.info("serviceInstanceId:" + serviceInstanceId)
239
240                 } catch (BpmnError e) {
241                         throw e
242                 } catch (Exception ex){
243                         String msg = "Exception in preProcessRequest " + ex.getMessage()
244                         msoLogger.debug(msg)
245                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
246                 }
247         }
248
249         public void prepareUpdateProgress(DelegateExecution execution) {
250                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
251                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
252                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
253                 String operType = resourceInputObj.getOperationType()
254                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
255                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
256                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
257                 String operationId = resourceInputObj.getOperationId()
258                 String progress = execution.getVariable("progress")
259                 String status = execution.getVariable("status")
260                 String statusDescription = execution.getVariable("statusDescription")
261
262                 String body = """
263                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
264                         xmlns:ns="http://org.openecomp.mso/requestsdb">
265                         <soapenv:Header/>
266                 <soapenv:Body>
267                     <ns:updateResourceOperationStatus>
268                                <operType>${operType}</operType>
269                                <operationId>${operationId}</operationId>
270                                <progress>${progress}</progress>
271                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
272                                <serviceId>${ServiceInstanceId}</serviceId>
273                                <status>${status}</status>
274                                <statusDescription>${statusDescription}</statusDescription>
275                     </ns:updateResourceOperationStatus>
276                 </soapenv:Body>
277                 </soapenv:Envelope>"""
278
279                 setProgressUpdateVariables(execution, body)
280                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
281         }
282
283         public void allocateCrossONAPResource(DelegateExecution execution) {
284                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
285                 msoLogger.info(" ***** Started allocateCrossONAPResource *****")
286                 
287                 //get TP links from AAI for SOTN handoverMode only
288                 String handoverMode = execution.getVariable(Prefix + "HandoverMode")
289                 if("SOTN".equalsIgnoreCase(handoverMode)) {
290                         //to do get tp link in AAI
291                         
292                         
293                         // Put TP Link info into serviceParameters
294                         String accessProviderId = ""
295                         String accessClientId = ""
296                         String accessTopologyId = ""
297                         String accessNodeId = ""
298                         String accessLtpId = ""
299                         JSONObject inputParameters = execution.getVariable(Prefix + "ServiceParameters")                        
300                         inputParameters.put("access-provider-id", accessProviderId)
301                         inputParameters.put("access-client-id", accessClientId)
302                         inputParameters.put("access-topology-id", accessTopologyId)
303                         inputParameters.put("access-node-id", accessNodeId)
304                         inputParameters.put("access-ltp-id", accessLtpId)
305                         execution.setVariable(Prefix + "ServiceParameters", inputParameters)
306                 }
307                 
308                 msoLogger.info("Exit " + allocateCrossONAPResource)
309         }
310
311         public void prepare3rdONAPRequest(DelegateExecution execution) {
312                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
313                 msoLogger.info(" ***** Started prepare3rdONAPRequest *****")
314                 
315                 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
316                 String extAPIPath = sppartnerUrl + 'serviceOrder'
317                 execution.setVariable("ExternalAPIURL", extAPIPath)
318                 
319                 // ExternalAPI message format
320                 String externalId = execution.getVariable("resourceName")
321                 String category = "E2E Service"
322                 String description = "Service Order from SPPartner"
323                 String requestedStartDate = utils.generateCurrentTimeInUtc()
324                 String requestedCompletionDate = utils.generateCurrentTimeInUtc()
325                 String priority = "1" // 0-4 0:highest
326                 String subscriberId = execution.getVariable("globalSubscriberId")
327                 String customerRole = ""
328                 String subscriberName = ""
329                 String referredType = "Consumer"
330                 String orderItemId = "1"
331                 String action = "add" //for create
332                 String serviceState = "active"
333                 String serviceName = execution.getVariable("serviceInstanceName")
334                 String serviceType = execution.getVariable("serviceType")
335                 String serviceId = execution.getVariable("serviceInstanceId")
336                 
337                 Map<String, String> valueMap = new HashMap<>()
338                 valueMap.put("externalId", '"' + externalId + '"')
339                 valueMap.put("category", '"' + category + '"')
340                 valueMap.put("description", '"' + description + '"')
341                 valueMap.put("requestedStartDate", '"' + requestedStartDate + '"')
342                 valueMap.put("requestedCompletionDate", '"' + requestedCompletionDate + '"')
343                 valueMap.put("priority", '"'+ priority + '"')
344                 valueMap.put("subscriberId", '"' + subscriberId + '"')
345                 valueMap.put("customerRole", '"' + customerRole + '"')
346                 valueMap.put("subscriberName", '"' + subscriberName + '"')
347                 valueMap.put("referredType", '"' + referredType + '"')
348                 valueMap.put("orderItemId", '"' + orderItemId + '"')
349                 valueMap.put("action", '"' + action + '"')
350                 valueMap.put("serviceState", '"' + serviceState + '"')
351                 valueMap.put("serviceName", '"' + serviceName + '"')
352                 valueMap.put("serviceType", '"' + serviceType + '"')
353                 valueMap.put("serviceId", '"' + serviceId + '"')
354                 
355                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil(this)
356                 
357                 // insert CallSource='ExternalAPI' to uuiRequest                
358                 Map<String, String> callSourceMap = new HashMap<>()
359                 callSourceMap.put("inputName", "CallSource")
360                 callSourceMap.put("inputValue", "ExternalAPI")
361                 String _requestInputs_ = externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, callSourceMap)
362                 
363                 // Transfer all uuiRequest incomeParameters to ExternalAPI format
364                 JSONObject inputParameters = execution.getVariable(Prefix + "ServiceParameters")
365                 for(String key : inputParameters.keySet()) {                    
366                         String inputName = key
367                         String inputValue = inputParameters.opt(key)
368                         Map<String, String> requestInputsMap = new HashMap<>()
369                         requestInputsMap.put("inputName", '"' + inputName+ '"')
370                         requestInputsMap.put("inputValue", '"' + inputValue + '"')
371                         _requestInputs_ += ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
372                 }                
373                 valueMap.put("_requestInputs_",  _requestInputs_)
374                 
375                 String payload = externalAPIUtil.setTemplate(ExternalAPIUtil.PostServiceOrderRequestsTemplate, valueMap)
376                 execution.setVariable(Prefix + "Payload", payload)
377                 msoLogger.info("Exit " + prepare3rdONAPRequest)
378         }
379
380         public void doCreateE2ESIin3rdONAP(DelegateExecution execution) {
381                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
382                 msoLogger.info(" ***** Started doCreateE2ESIin3rdONAP *****")
383                 
384                 String extAPIPath = execution.getVariable("ExternalAPIURL")
385                 String payload = execution.getVariable(Prefix + "Payload")
386                 
387                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil(this)
388
389                 APIResponse response = externalAPIUtil.executeExternalAPIPostCall(execution, extAPIPath, payload)
390
391                 int responseCode = response.getStatusCode()
392                 execution.setVariable(Prefix + "PostServiceOrderResponseCode", responseCode)
393                 msoLogger.debug("Post ServiceOrder response code is: " + responseCode)
394
395                 String extApiResponse = response.getResponseBodyAsString()
396                 JSONObject responseObj = new JSONObject(extApiResponse)
397                 execution.setVariable(Prefix + "PostServiceOrderResponse", extApiResponse)
398                 //Process Response
399                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
400                         //200 OK 201 CREATED 202 ACCEPTED
401                 {
402                         msoLogger.debug("Post ServiceOrder Received a Good Response")
403                         String serviceOrderId = responseObj.get("ServiceOrderId")
404                         execution.setVariable(Prefix + "SuccessIndicator", true)
405                         execution.setVariable("serviceOrderId", serviceOrderId)
406                 }
407                 else{
408                         msoLogger.debug("Post ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
409                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Post ServiceOrder Received a bad response from 3rdONAP External API")
410                 }
411                 
412                 msoLogger.info("Exit " + doCreateE2ESIin3rdONAP)
413         }
414         
415
416         public void getE2ESIProgressin3rdONAP(DelegateExecution execution) {
417                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
418                 msoLogger.info(" ***** Started getE2ESIProgressin3rdONAP *****")
419                 
420                 String extAPIPath = execution.getVariable("ExternalAPIURL")
421                 extAPIPath += "/" + execution.getVariable("ServiceOrderId")
422                 
423                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil(this)
424
425                 APIResponse response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
426
427                 int responseCode = response.getStatusCode()
428                 execution.setVariable(Prefix + "GetServiceOrderResponseCode", responseCode)
429                 msoLogger.debug("Get ServiceOrder response code is: " + responseCode)
430
431                 String extApiResponse = response.getResponseBodyAsString()
432                 JSONObject responseObj = new JSONObject(extApiResponse)
433                 execution.setVariable(Prefix + "GetServiceOrderResponse", extApiResponse)
434                 
435                 //Process Response //200 OK 201 CREATED 202 ACCEPTED
436                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )                  
437                 {
438                         msoLogger.debug("Get ServiceOrder Received a Good Response")
439                         String serviceOrderState = responseObj.get("State")
440                         execution.setVariable(Prefix + "SuccessIndicator", true)
441                         execution.setVariable("serviceOrderState", serviceOrderState)                   
442                         
443                         // Get serviceOrder State and process progress
444                         if("ACKNOWLEDGED".equalsIgnoreCase(serviceOrderState)) {
445                                 execution.setVariable("progress", 15)
446                                 execution.setVariable("status", "processing")                           
447                         }
448                         if("INPROGRESS".equalsIgnoreCase(serviceOrderState)) {
449                                 execution.setVariable("progress", 40)
450                                 execution.setVariable("status", "processing")
451                         }
452                         if("COMPLETED".equalsIgnoreCase(serviceOrderState)) {
453                                 execution.setVariable("progress", 100)
454                                 execution.setVariable("status", "finished")
455                         }
456                         if("FAILED".equalsIgnoreCase(serviceOrderState)) {
457                                 execution.setVariable("progress", 100)
458                                 execution.setVariable("status", "error")
459                         }
460                         else {
461                                 execution.setVariable("progress", 100)
462                                 execution.setVariable("status", "error")
463                                 execution.setVariable("statusDescription", "Create Service Order Status is unknown")
464                         }
465                         execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
466                 }
467                 else{                   
468                         msoLogger.debug("Get ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
469                         execution.setVariable("progress", 100)
470                         execution.setVariable("status", "error")
471                         execution.setVariable("statusDescription", "Get ServiceOrder Received a bad response")
472                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get ServiceOrder Received a bad response from 3rdONAP External API")
473                 }               
474                 
475                 msoLogger.info("Exit " + getE2ESIProgressin3rdONAP)
476         }
477         
478         /**
479          * delay 5 sec
480          */
481         public void timeDelay(DelegateExecution execution) {
482                 def isDebugEnabled= execution.getVariable("isDebugLogEnabled")
483                 try {
484                         Thread.sleep(5000)
485                 } catch(InterruptedException e) {
486                         utils.log("ERROR", "Time Delay exception" + e )
487                 }
488         }
489
490         public void saveSPPartnerInAAI(DelegateExecution execution) {
491                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
492                 msoLogger.info(" ***** Started postCreateE2ESIin3rdONAP *****") 
493                 
494                 String sppartnerId = UUID.randomUUID().toString()
495                 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
496                 String callSource = execution.getVariable(Prefix + "CallSource")
497                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
498                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
499                 String serviceType = execution.getVariable("serviceType")
500                 
501                 AaiUtil aaiUriUtil = new AaiUtil(this)
502                 String aai_uri = aaiUriUtil.getBusinessSPPartnerUri(execution)
503                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
504                 
505                 String payload =
506                                 """<sp-partner xmlns=\"${namespace}\">
507                                 <id>${sppartnerId}</id>
508                                 <url>${sppartnerUrl}</url>
509                                 <callsource>${callSource}</callsource>
510                                 <relationship-list>
511                                   <relationship>
512                                     <related-to>service-instance</related-to>
513                                     <related-link>/aai/v14/business/customers/customer/${globalSubscriberId}/service-subscriptions/service-subscription/${serviceType}/service-instances/service-instance/${serviceInstanceId}</related-link>
514                                     <relationship-data>
515                                         <relationship-key>service-instance.service-instance-id</relationship-key>
516                                         <relationship-value>${serviceInstanceId}</relationship-value>
517                                     </relationship-data> 
518                                   </relationship>
519                                 </relationship-list>
520                                 </sp-partner>""".trim()
521                 utils.logAudit(payload)
522                 
523                 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
524                 String serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(sppartnerId,"UTF-8")
525                 
526                 APIResponse response = aaiUriUtil.executeAAIPutCall(execution, serviceAaiPath, payload)
527                 int responseCode = response.getStatusCode()
528                 execution.setVariable(Prefix + "PutSppartnerResponseCode", responseCode)
529                 msoLogger.debug("  Put sppartner response code is: " + responseCode)
530
531                 String aaiResponse = response.getResponseBodyAsString()
532                 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
533                 execution.setVariable(Prefix + "PutSppartnerResponse", aaiResponse)
534
535                 //Process Response
536                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
537                         //200 OK 201 CREATED 202 ACCEPTED
538                 {
539                         msoLogger.debug("PUT sppartner Received a Good Response")
540                         execution.setVariable(Prefix + "SuccessIndicator", true)
541                 }
542                 else
543                 {
544                         msoLogger.debug("Put sppartner Received a Bad Response Code. Response Code is: " + responseCode)
545                         exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
546                         throw new BpmnError("MSOWorkflowException")
547                 }
548                 
549                 msoLogger.info("Exit " + saveSPPartnerInAAI)
550         }
551
552         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
553                 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
554                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
555                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
556         }       
557
558         public void postProcess(DelegateExecution execution){
559                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
560                 msoLogger.info(" ***** Started postProcess *****")
561                 String responseCode = execution.getVariable(Prefix + "PutSppartnerResponseCode")
562                 String responseObj = execution.getVariable(Prefix + "PutSppartnerResponse")
563
564                 msoLogger.info("response from AAI for put sppartner, response code :" + responseCode + "  response object :" + responseObj)
565                 msoLogger.info(" ***** Exit postProcess *****")
566         }
567
568         public void sendSyncResponse (DelegateExecution execution) {
569                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
570                 msoLogger.debug(" *** sendSyncResponse *** ")
571
572                 try {
573                         String operationStatus = "finished"
574                         // RESTResponse for main flow
575                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
576                         msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
577                         sendWorkflowResponse(execution, 202, resourceOperationResp)
578                         execution.setVariable("sentSyncResponse", true)
579
580                 } catch (Exception ex) {
581                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
582                         msoLogger.debug(msg)
583                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
584                 }
585                 msoLogger.debug(" ***** Exit sendSyncResopnse *****")
586         }
587         
588         String customizeResourceParam(String inputParametersJson) {
589                 List<Map<String, Object>> paramList = new ArrayList()
590                 JSONObject jsonObject = new JSONObject(inputParametersJson)
591                 Iterator iterator = jsonObject.keys()
592                 while (iterator.hasNext()) {
593                         String key = iterator.next()
594                         HashMap<String, String> hashMap = new HashMap()
595                         hashMap.put("name", key)
596                         hashMap.put("value", jsonObject.get(key))
597                         paramList.add(hashMap)
598                 }
599                 Map<String, List<Map<String, Object>>> paramMap = new HashMap()
600                 paramMap.put("param", paramList)
601
602                 return  new JSONObject(paramMap).toString()
603         }
604 }