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