afd67c37f5b1481617f6a2d17a6a465a252203c0
[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.JSONArray
24 import org.json.JSONObject
25 import org.json.XML
26
27 import static org.apache.commons.lang3.StringUtils.*
28 import groovy.xml.XmlUtil
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.MsoUtils
33 import org.onap.aai.domain.yang.SpPartner
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.core.UrnPropertiesReader
39 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
40 import org.onap.so.client.aai.AAIObjectType
41 import org.onap.so.client.aai.AAIResourcesClient
42 import org.onap.so.client.aai.entities.uri.AAIResourceUri
43 import org.onap.so.client.aai.entities.uri.AAIUriFactory
44 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
45 import org.onap.so.bpmn.infrastructure.workflow.service.ServicePluginFactory
46 import java.util.Map
47 import java.util.UUID
48 import javax.ws.rs.core.Response
49 import org.onap.so.logger.MsoLogger
50
51 import org.camunda.bpm.engine.runtime.Execution
52 import org.camunda.bpm.engine.delegate.BpmnError
53 import org.camunda.bpm.engine.delegate.DelegateExecution
54 import org.apache.commons.lang3.*
55 import org.apache.commons.codec.binary.Base64
56
57
58 /**
59  * This groovy class supports the <class>Create3rdONAPE2EServiceInstance.bpmn</class> process.
60  * flow for Create E2EServiceInstance in 3rdONAP
61  */
62 public class Create3rdONAPE2EServiceInstance extends AbstractServiceTaskProcessor {
63
64         String Prefix = "CRE3rdONAPESI_"
65
66         ExceptionUtil exceptionUtil = new ExceptionUtil()
67
68         JsonUtils jsonUtil = new JsonUtils()
69
70         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, Create3rdONAPE2EServiceInstance.class)
71
72         public void checkSPPartnerInfo (DelegateExecution execution) {
73                 msoLogger.info(" ***** Started checkSPPartnerInfo *****")
74                 try {
75                         //get bpmn inputs from resource request.
76                         String requestId = execution.getVariable("mso-request-id")
77                         String requestAction = execution.getVariable("requestAction")
78                         msoLogger.info("The requestAction is: " + requestAction)
79                         String recipeParamsFromRequest = execution.getVariable("recipeParams")
80                         msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
81                         String resourceInput = execution.getVariable("resourceInput")
82                         msoLogger.info("The resourceInput is: " + resourceInput)
83                         //Get ResourceInput Object
84                         ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
85                         String resourceInputPrameters = resourceInputObj.getResourceParameters()
86                         String inputParametersJson = JsonUtils.getJsonValue(resourceInputPrameters, "requestInputs")
87                         JSONObject inputParameters = new JSONObject(inputParametersJson)
88
89                         // set local resourceInput
90                         execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
91
92                         boolean is3rdONAPExist = false
93
94                         if(inputParameters.has("sppartner_url"))
95                         {
96                                 String sppartnerUrl = inputParameters.get("sppartner_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("sppartner_providingServiceUuid"))
108                         {
109                                 String sppartnerUUID= inputParameters.get("sppartner_providingServiceUuid")
110                                 execution.setVariable(Prefix + "SppartnerUUID", sppartnerUUID)
111                                 is3rdONAPExist = true
112                         }
113                         else {
114                                 is3rdONAPExist = false
115                                 String msg = "sppartner providingServiceUuid is blank."
116                                 msoLogger.debug(msg)
117                         }
118                         if(inputParameters.has("sppartner_providingServiceInvariantUuid"))
119                         {
120                                 String sppartnerInvarianteUUID  = inputParameters.get("sppartner_providingServiceInvariantUuid")
121                                 execution.setVariable(Prefix + "SppartnerInvarianteUUID", sppartnerInvarianteUUID)
122                                 is3rdONAPExist = true
123                         }
124                         else {
125                                 is3rdONAPExist = false
126                                 String msg = "sppartner providingServiceInvarianteUuid is blank."
127                                 msoLogger.debug(msg)
128                         }
129
130                         if(inputParameters.has("sppartner_handoverMode"))
131                         {
132                                 String handoverMode = inputParameters.get("sppartner_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 (Exception ex){
148                         String msg = "Exception in checkSPPartnerInfo " + ex.getMessage()
149                         msoLogger.debug(msg)
150 //                      exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
151                 }
152         }
153
154         public void checkLocallCall (DelegateExecution execution) {
155                 msoLogger.info(" ***** Started checkLocallCall *****")
156                 try {
157
158                         //Get ResourceInput Object
159                         ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
160
161                         //uuiRequest
162                         String incomingRequest = resourceInputObj.getRequestsInputs()
163                         String serviceParameters = JsonUtils.getJsonValue(incomingRequest, "service.parameters")
164                         String requestInputs = JsonUtils.getJsonValue(serviceParameters, "requestInputs")
165                         JSONObject inputParameters = new JSONObject(requestInputs)
166                         execution.setVariable(Prefix + "ServiceParameters", inputParameters)
167
168                         // CallSource is added only when ONAP SO calling 3rdONAP(External API) SO(Remote call)
169                         boolean isLocalCall = true
170                         String callSource = "UUI"
171                         if(inputParameters.has("CallSource"))
172                         {
173                                 callSource = inputParameters.get("CallSource")
174                                 if("ExternalAPI".equalsIgnoreCase(callSource)) {
175                                         String sppartnerId = inputParameters.get("SppartnerServiceId")
176                                         execution.setVariable(Prefix + "SppartnerServiceId", sppartnerId)
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                 msoLogger.info(" ***** Started preProcessRequest *****")
194                 String msg = ""
195
196                 try {
197                         ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
198
199                         String globalSubscriberId = resourceInputObj.getGlobalSubscriberId()
200                         if (isBlank(globalSubscriberId)) {
201                                 msg = "Input globalSubscriberId is null"
202                                 msoLogger.error(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.error(msg)
212                         }
213                         execution.setVariable("serviceType", serviceType)
214                         msoLogger.info("serviceType:" + serviceType)
215
216                         String resourceName = resourceInputObj.getResourceInstanceName()
217                         if (isBlank(resourceName)) {
218                                 msg = "Input resourceName is null"
219                                 msoLogger.error(msg)
220                         }
221                         execution.setVariable("resourceName", resourceName)
222                         msoLogger.info("resourceName:" + resourceName)
223
224                         int beginIndex = resourceName.indexOf("_") + 1
225                         String serviceInstanceName = resourceName.substring(beginIndex)
226                         execution.setVariable("serviceInstanceName", serviceInstanceName)
227
228                         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
229                         if (isBlank(serviceInstanceId)) {
230                                 msg = "Input serviceInstanceId is null"
231                                 msoLogger.error(msg)
232                         }
233                         execution.setVariable(Prefix + "ServiceInstanceId", serviceInstanceId)
234                         msoLogger.info("serviceInstanceId:" + serviceInstanceId)
235
236                         String resourceModelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid()
237                         if (isBlank(resourceModelInvariantUuid)) {
238                                 msg = "Input resourceModelInvariantUuid is null"
239                                 msoLogger.error(msg)
240                         }
241                         execution.setVariable(Prefix + "ResourceModelInvariantUuid", resourceModelInvariantUuid)
242                         msoLogger.info("resourceModelInvariantUuid:" + resourceModelInvariantUuid)
243
244                         String resourceModelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
245                         if (isBlank(resourceModelUuid)) {
246                                 msg = "Input resourceModelUuid is null"
247                                 msoLogger.error(msg)
248                         }
249                         execution.setVariable(Prefix + "ResourceModelUuid", resourceModelUuid)
250                         msoLogger.info("resourceModelUuid:" + resourceModelUuid)
251
252                         String resourceModelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
253                         if (isBlank(resourceModelCustomizationUuid)) {
254                                 msg = "Input resourceModelCustomizationUuid is null"
255                                 msoLogger.error(msg)
256                         }
257                         execution.setVariable(Prefix + "ResourceModelCustomizationUuid", resourceModelCustomizationUuid)
258                         msoLogger.info("resourceModelCustomizationUuid:" + resourceModelCustomizationUuid)
259
260                 } catch (Exception ex){
261                         msg = "Exception in preProcessRequest " + ex.getMessage()
262                         msoLogger.debug(msg)
263 //                      exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
264                 }
265         }
266
267         public void prepareUpdateProgress(DelegateExecution execution) {
268                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
269                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
270                 String operType = resourceInputObj.getOperationType()
271                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
272                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
273                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
274                 String operationId = resourceInputObj.getOperationId()
275                 String progress = execution.getVariable("progress")
276                 String status = execution.getVariable("status")
277                 String statusDescription = execution.getVariable("statusDescription")
278
279                 String body = """
280                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
281                         xmlns:ns="http://org.openecomp.mso/requestsdb">
282                         <soapenv:Header/>
283                 <soapenv:Body>
284                     <ns:updateResourceOperationStatus>
285                                <operType>${operType}</operType>
286                                <operationId>${operationId}</operationId>
287                                <progress>${progress}</progress>
288                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
289                                <serviceId>${ServiceInstanceId}</serviceId>
290                                <status>${status}</status>
291                                <statusDescription>${statusDescription}</statusDescription>
292                     </ns:updateResourceOperationStatus>
293                 </soapenv:Body>
294                 </soapenv:Envelope>"""
295
296                 setProgressUpdateVariables(execution, body)
297                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
298         }
299
300         public void allocateCrossONAPResource(DelegateExecution execution) {
301                 msoLogger.info(" ***** Started allocateCrossONAPResource *****")
302
303                 //get TP links from AAI for SOTN handoverMode only
304                 String handoverMode = execution.getVariable(Prefix + "HandoverMode")
305                 if("SOTN".equalsIgnoreCase(handoverMode)) {
306                         // Put TP Link info into serviceParameters
307                         JSONObject inputParameters = execution.getVariable(Prefix + "ServiceParameters")
308                         if(inputParameters.has("remote-access-provider-id")) {
309                                 Map<String, Object> crossTPs = new HashMap<String, Object>();
310                                 crossTPs.put("local-access-provider-id", inputParameters.get("remote-access-provider-id"));
311                                 crossTPs.put("local-access-client-id", inputParameters.get("remote-access-client-id"));
312                                 crossTPs.put("local-access-topology-id", inputParameters.get("remote-access-topology-id"));
313                                 crossTPs.put("local-access-node-id", inputParameters.get("remote-access-node-id"));
314                                 crossTPs.put("local-access-ltp-id", inputParameters.get("remote-access-ltp-id"));
315                                 crossTPs.put("remote-access-provider-id", inputParameters.get("local-access-provider-id"));
316                                 crossTPs.put("remote-access-client-id", inputParameters.get("local-access-client-id"));
317                                 crossTPs.put("remote-access-topology-id", inputParameters.get("local-access-topology-id"));
318                                 crossTPs.put("remote-access-node-id", inputParameters.get("local-access-node-id"));
319                                 crossTPs.put("remote-access-ltp-id", inputParameters.get("local-access-ltp-id"));
320
321                                 inputParameters.put("local-access-provider-id", crossTPs.get("local-access-provider-id"));
322                                 inputParameters.put("local-access-client-id", crossTPs.get("local-access-client-id"));
323                                 inputParameters.put("local-access-topology-id", crossTPs.get("local-access-topology-id"));
324                                 inputParameters.put("local-access-node-id", crossTPs.get("local-access-node-id"));
325                                 inputParameters.put("local-access-ltp-id", crossTPs.get("local-access-ltp-id"));
326                                 inputParameters.put("remote-access-provider-id", crossTPs.get("remote-access-provider-id"));
327                                 inputParameters.put("remote-access-client-id", crossTPs.get("remote-access-client-id"));
328                                 inputParameters.put("remote-access-topology-id", crossTPs.get("remote-access-topology-id"));
329                                 inputParameters.put("remote-access-node-id", crossTPs.get("remote-access-node-id"));
330                                 inputParameters.put("remote-access-ltp-id", crossTPs.get("remote-access-ltp-id"));
331
332                                 execution.setVariable(Prefix + "ServiceParameters", inputParameters)
333                         }
334                         else {
335                                         msoLogger.error("No allocated CrossONAPResource found in ServiceParameters")
336                         }
337                 }
338
339                 msoLogger.info("Exit " + allocateCrossONAPResource)
340         }
341
342         public void prepare3rdONAPRequest(DelegateExecution execution) {
343                 msoLogger.info(" ***** Started prepare3rdONAPRequest *****")
344
345                 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
346                 String extAPIPath = sppartnerUrl + '/serviceOrder'
347                 execution.setVariable("ExternalAPIURL", extAPIPath)
348
349                 // ExternalAPI message format
350                 String externalId = execution.getVariable("resourceName")
351                 String category = "E2E Service"
352                 String description = "Service Order from SPPartner"
353                 String requestedStartDate = utils.generateCurrentTimeInUtc()
354                 String requestedCompletionDate = utils.generateCurrentTimeInUtc()
355                 String priority = "1" // 0-4 0:highest
356                 String subscriberId = execution.getVariable("globalSubscriberId")
357                 String customerRole = "ONAPcustomer"
358                 String subscriberName = subscriberId
359                 String referredType = "Consumer"
360                 String orderItemId = "1"
361                 String action = "add" //for create
362                 String serviceState = "active"
363                 String serviceName = execution.getVariable("serviceInstanceName")
364                 String serviceUuId = execution.getVariable(Prefix + "SppartnerUUID")
365
366                 Map<String, String> valueMap = new HashMap<>()
367                 valueMap.put("externalId", '"' + externalId + '"')
368                 valueMap.put("category", '"' + category + '"')
369                 valueMap.put("description", '"' + description + '"')
370                 valueMap.put("requestedStartDate", '"' + requestedStartDate + '"')
371                 valueMap.put("requestedCompletionDate", '"' + requestedCompletionDate + '"')
372                 valueMap.put("priority", '"'+ priority + '"')
373                 valueMap.put("subscriberId", '"' + subscriberId + '"')
374                 valueMap.put("customerRole", '"' + customerRole + '"')
375                 valueMap.put("subscriberName", '"' + subscriberName + '"')
376                 valueMap.put("referredType", '"' + referredType + '"')
377                 valueMap.put("orderItemId", '"' + orderItemId + '"')
378                 valueMap.put("action", '"' + action + '"')
379                 valueMap.put("serviceState", '"' + serviceState + '"')
380                 valueMap.put("serviceId", "null") //null for add
381                 valueMap.put("serviceName", '"' + serviceName + '"')
382                 valueMap.put("serviceUuId", '"' + serviceUuId + '"')
383
384                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
385
386                 // insert CallSource='ExternalAPI' to uuiRequest
387                 Map<String, String> requestInputsMap = new HashMap<>()
388                 requestInputsMap.put("inputName",  '"CallSource"')
389                 requestInputsMap.put("inputValue", '"ExternalAPI"')
390                 String _requestInputs_ = externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
391
392                 requestInputsMap.clear()
393                 String serviceInstanceId = execution.getVariable(Prefix + "ServiceInstanceId")
394                 requestInputsMap.put("inputName", '"SppartnerServiceId"')
395                 requestInputsMap.put("inputValue", '"' + serviceInstanceId + '"')
396                 _requestInputs_ +=  ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
397
398                 requestInputsMap.clear()
399                 String serviceType = execution.getVariable("serviceType")
400                 requestInputsMap.put("inputName", '"serviceType"')
401                 requestInputsMap.put("inputValue", '"' + serviceType + '"')
402                 _requestInputs_ +=  ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
403
404                 // Transfer all uuiRequest incomeParameters to ExternalAPI format
405                 JSONObject inputParameters = execution.getVariable(Prefix + "ServiceParameters")
406                 for(String key : inputParameters.keySet()) {
407                         String inputName = key
408                         String inputValue = inputParameters.opt(key)
409                         requestInputsMap.clear()
410                         requestInputsMap.put("inputName", '"' + inputName+ '"')
411                         requestInputsMap.put("inputValue", '"' + inputValue + '"')
412                         _requestInputs_ += ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
413                 }
414                 valueMap.put("_requestInputs_",  _requestInputs_)
415
416                 String payload = externalAPIUtil.setTemplate(ExternalAPIUtil.PostServiceOrderRequestsTemplate, valueMap)
417                 execution.setVariable(Prefix + "Payload", payload)
418                 msoLogger.info("Exit " + prepare3rdONAPRequest)
419         }
420
421         public void doCreateE2ESIin3rdONAP(DelegateExecution execution) {
422                 msoLogger.info(" ***** Started doCreateE2ESIin3rdONAP *****")
423         try {
424                 String extAPIPath = execution.getVariable("ExternalAPIURL")
425                 String payload = execution.getVariable(Prefix + "Payload")
426                 msoLogger.debug("doCreateE2ESIin3rdONAP externalAPIURL is: " + extAPIPath)
427                 msoLogger.debug("doCreateE2ESIin3rdONAP payload is: " + payload)
428
429                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
430                 execution.setVariable("ServiceOrderId", "")
431
432                 Response response = externalAPIUtil.executeExternalAPIPostCall(execution, extAPIPath, payload)
433
434                 int responseCode = response.getStatus()
435                 execution.setVariable(Prefix + "PostServiceOrderResponseCode", responseCode)
436                 msoLogger.debug("Post ServiceOrder response code is: " + responseCode)
437
438                 String extApiResponse = response.readEntity(String.class)
439                 JSONObject responseObj = new JSONObject(extApiResponse)
440                 execution.setVariable(Prefix + "PostServiceOrderResponse", extApiResponse)
441
442                 msoLogger.debug("doCreateE2ESIin3rdONAP response body is: " + extApiResponse)
443
444                 //Process Response
445                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
446                 //200 OK 201 CREATED 202 ACCEPTED
447                 {
448                         msoLogger.debug("Post ServiceOrder Received a Good Response")
449                         String serviceOrderId = responseObj.get("id")
450                         execution.setVariable(Prefix + "SuccessIndicator", true)
451                         execution.setVariable("ServiceOrderId", serviceOrderId)
452                         msoLogger.info("Post ServiceOrderid is: " + serviceOrderId)
453                 }
454                 else{
455                         msoLogger.error("Post ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
456 //                      exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Post ServiceOrder Received a bad response from 3rdONAP External API")
457                 }
458         }catch(Exception e){
459             msoLogger.error("doCreateE2ESIin3rdONAP exception:" + e.getMessage())
460         }
461
462                 msoLogger.info("Exit " + doCreateE2ESIin3rdONAP)
463         }
464
465
466         public void getE2ESIProgressin3rdONAP(DelegateExecution execution) {
467                 msoLogger.info(" ***** Started getE2ESIProgressin3rdONAP *****")
468         try {
469
470                 String extAPIPath = execution.getVariable("ExternalAPIURL")
471                 extAPIPath += "/" + execution.getVariable("ServiceOrderId")
472                 msoLogger.debug("getE2ESIProgressin3rdONAP create externalAPIURL is: " + extAPIPath)
473
474                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
475
476                 Response response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
477
478                 int responseCode = response.getStatus()
479                 execution.setVariable(Prefix + "GetServiceOrderResponseCode", responseCode)
480                 msoLogger.debug("Get ServiceOrder response code is: " + responseCode)
481
482                 String extApiResponse = response.readEntity(String.class)
483                 JSONObject responseObj = new JSONObject(extApiResponse)
484                 execution.setVariable(Prefix + "GetServiceOrderResponse", extApiResponse)
485
486                 msoLogger.debug("getE2ESIProgressin3rdONAP create response body is: " + extApiResponse)
487
488                 //Process Response //200 OK 201 CREATED 202 ACCEPTED
489                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
490                 {
491                         msoLogger.debug("Get Create ServiceOrder Received a Good Response")
492
493                         String orderState = responseObj.get("state")
494                         if("REJECTED".equalsIgnoreCase(orderState)) {
495                                 execution.setVariable("progress", 100)
496                                 execution.setVariable("status", "error")
497                                 execution.setVariable("statusDescription", "Create Service Order Status is REJECTED")
498                                 return
499                         }
500
501                         JSONArray items = responseObj.getJSONArray("orderItem")
502                         JSONObject item = items[0]
503                         JSONObject service = item.get("service")
504                         String sppartnerServiceId = service.get("id")
505                         if(sppartnerServiceId == null || sppartnerServiceId.equals("null")) {
506                                 execution.setVariable("progress", 100)
507                                 execution.setVariable("status", "error")
508                                 execution.setVariable("statusDescription", "Create Service Order Status get null sppartnerServiceId")
509                                 msoLogger.error("null sppartnerServiceId while getting progress from externalAPI")
510                                 return
511                         }
512
513                         execution.setVariable(Prefix + "SppartnerServiceId", sppartnerServiceId)
514
515                         String serviceOrderState = item.get("state")
516                         execution.setVariable(Prefix + "SuccessIndicator", true)
517                         execution.setVariable("ServiceOrderState", serviceOrderState)
518
519                         // Get serviceOrder State and process progress
520                         if("ACKNOWLEDGED".equalsIgnoreCase(serviceOrderState)) {
521                                 execution.setVariable("progress", 15)
522                                 execution.setVariable("status", "processing")
523                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
524                         }
525                         else if("INPROGRESS".equalsIgnoreCase(serviceOrderState)) {
526                                 execution.setVariable("progress", 40)
527                                 execution.setVariable("status", "processing")
528                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
529                         }
530                         else if("COMPLETED".equalsIgnoreCase(serviceOrderState)) {
531                                 execution.setVariable("progress", 100)
532                                 execution.setVariable("status", "finished")
533                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
534                         }
535                         else if("FAILED".equalsIgnoreCase(serviceOrderState)) {
536                                 execution.setVariable("progress", 100)
537                                 execution.setVariable("status", "error")
538                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
539                         }
540                         else {
541                                 execution.setVariable("progress", 100)
542                                 execution.setVariable("status", "error")
543                                 execution.setVariable("statusDescription", "Create Service Order Status is unknown")
544                         }
545                 }
546                 else{
547                         msoLogger.debug("Get ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
548                         execution.setVariable("progress", 100)
549                         execution.setVariable("status", "error")
550                         execution.setVariable("statusDescription", "Get Create ServiceOrder Received a bad response")
551 //                      exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get Create ServiceOrder Received a bad response from 3rdONAP External API")
552                 }
553
554         }catch(Exception e){            
555             execution.setVariable("progress", 100)
556             execution.setVariable("status", "error")
557             execution.setVariable("statusDescription", "Get Create ServiceOrder Exception")
558             msoLogger.error("getE2ESIProgressin3rdONAP exception:" + e.getMessage())
559         }
560                 msoLogger.info("Exit " + getE2ESIProgressin3rdONAP)
561         }
562
563         /**
564          * delay 5 sec
565          */
566         public void timeDelay(DelegateExecution execution) {
567                 try {
568                         Thread.sleep(5000)
569                 } catch(InterruptedException e) {
570                         msoLogger.error("Time Delay exception" + e)
571                 }
572         }
573
574         public void saveSPPartnerInAAI(DelegateExecution execution) {
575                 msoLogger.info(" ***** Started saveSPPartnerInAAI *****")
576         try {
577                 String sppartnerId = execution.getVariable(Prefix + "SppartnerServiceId")
578                 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
579                 String callSource = execution.getVariable(Prefix + "CallSource")
580                 String serviceInstanceId = execution.getVariable(Prefix + "ServiceInstanceId")
581                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
582                 String serviceType = execution.getVariable("serviceType")
583                 String resourceModelInvariantUuid = execution.getVariable(Prefix + "ResourceModelInvariantUuid")
584                 String resourceModelUuid = execution.getVariable(Prefix + "ResourceModelUuid")
585                 String resourceModelCustomizationUuid = execution.getVariable(Prefix + "ResourceModelCustomizationUuid")
586                 
587                 SpPartner partner = new SpPartner()
588                 partner.setSpPartnerId(sppartnerId)
589                 partner.setUrl(sppartnerUrl)
590                 partner.setCallsource(callSource)
591                 partner.setModelInvariantId(resourceModelInvariantUuid)
592                 partner.setModelVersionId(resourceModelUuid)
593                 partner.setModelCustomizationId(resourceModelCustomizationUuid)
594
595                 AAIResourcesClient client = new AAIResourcesClient()
596                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, sppartnerId)
597                 client.create(uri, partner)
598
599                 AAIResourceUri siUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceInstanceId)
600                 client.connect(uri, siUri)
601         } catch (Exception ex) {
602             String msg = "Exception in Create3rdONAPE2EServiceInstance.saveSPPartnerInAAI. " + ex.getMessage()
603             msoLogger.info(msg)
604 //            throw new BpmnError("MSOWorkflowException")
605         }
606                 msoLogger.info("Exit " + saveSPPartnerInAAI)
607         }
608
609         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
610                 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
611                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
612                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
613         }
614
615         public void postProcess(DelegateExecution execution){
616                 msoLogger.info(" ***** Started postProcess *****")
617                 String responseCode = execution.getVariable(Prefix + "PutSppartnerResponseCode")
618                 String responseObj = execution.getVariable(Prefix + "PutSppartnerResponse")
619
620                 msoLogger.info("response from AAI for put sppartner, response code :" + responseCode + "  response object :" + responseObj)
621                 msoLogger.info(" ***** Exit postProcess *****")
622         }
623
624         public void sendSyncResponse (DelegateExecution execution) {
625                 msoLogger.debug(" *** sendSyncResponse *** ")
626
627                 try {
628                         String operationStatus = "finished"
629                         // RESTResponse for main flow
630                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
631                         msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
632                         sendWorkflowResponse(execution, 202, resourceOperationResp)
633                         execution.setVariable("sentSyncResponse", true)
634
635                 } catch (Exception ex) {
636                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
637                         msoLogger.debug(msg)
638 //                      exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
639                 }
640                 msoLogger.debug(" ***** Exit sendSyncResopnse *****")
641         }
642 }