a0f0e279f6328eb9d4ba8eb9ce5ef6567f1c329d
[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.AaiUtil
33 import org.onap.so.bpmn.common.scripts.MsoUtils
34 import org.onap.aai.domain.yang.SpPartner
35 import org.onap.so.bpmn.common.recipe.ResourceInput
36 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
37 import org.onap.so.bpmn.core.WorkflowException
38 import org.onap.so.bpmn.core.json.JsonUtils
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 import org.springframework.web.util.UriUtils
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 (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                 msoLogger.info(" ***** Started checkLocallCall *****")
158                 try {
159
160                         //Get ResourceInput Object
161                         ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
162
163                         //uuiRequest
164                         String incomingRequest = resourceInputObj.getRequestsInputs()
165                         String serviceParameters = JsonUtils.getJsonValue(incomingRequest, "service.parameters")
166                         String requestInputs = JsonUtils.getJsonValue(serviceParameters, "requestInputs")
167                         JSONObject inputParameters = new JSONObject(requestInputs)
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                                         String sppartnerId = inputParameters.get("SppartnerServiceId")
178                                         execution.setVariable(Prefix + "SppartnerServiceId", sppartnerId)
179                                         isLocalCall = false
180                                 }
181                         }
182                         execution.setVariable(Prefix + "CallSource", callSource)
183                         msoLogger.debug("callSource is: " + callSource )
184
185                         execution.setVariable("IsLocalCall", isLocalCall)
186
187                 } catch (Exception ex){
188                         String msg = "Exception in checkLocallCall " + ex.getMessage()
189                         msoLogger.debug(msg)
190                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
191                 }
192         }
193
194         public void preProcessRequest(DelegateExecution execution){
195                 msoLogger.info(" ***** Started preProcessRequest *****")
196                 String msg = ""
197
198                 try {
199                         ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
200
201                         String globalSubscriberId = resourceInputObj.getGlobalSubscriberId()
202                         if (isBlank(globalSubscriberId)) {
203                                 msg = "Input globalSubscriberId is null"
204                                 msoLogger.error(msg)
205                         }
206                         //set local variable
207                         execution.setVariable("globalSubscriberId", globalSubscriberId)
208                         msoLogger.info("globalSubscriberId:" + globalSubscriberId)
209
210                         String serviceType = resourceInputObj.getServiceType()
211                         if (isBlank(serviceType)) {
212                                 msg = "Input serviceType is null"
213                                 msoLogger.error(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.error(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.error(msg)
234                         }
235                         execution.setVariable(Prefix + "ServiceInstanceId", serviceInstanceId)
236                         msoLogger.info("serviceInstanceId:" + serviceInstanceId)
237
238                         String resourceModelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid()
239                         if (isBlank(resourceModelInvariantUuid)) {
240                                 msg = "Input resourceModelInvariantUuid is null"
241                                 msoLogger.error(msg)
242                         }
243                         execution.setVariable(Prefix + "ResourceModelInvariantUuid", resourceModelInvariantUuid)
244                         msoLogger.info("resourceModelInvariantUuid:" + resourceModelInvariantUuid)
245
246                         String resourceModelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
247                         if (isBlank(resourceModelUuid)) {
248                                 msg = "Input resourceModelUuid is null"
249                                 msoLogger.error(msg)
250                         }
251                         execution.setVariable(Prefix + "ResourceModelUuid", resourceModelUuid)
252                         msoLogger.info("resourceModelUuid:" + resourceModelUuid)
253
254                         String resourceModelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
255                         if (isBlank(resourceModelCustomizationUuid)) {
256                                 msg = "Input resourceModelCustomizationUuid is null"
257                                 msoLogger.error(msg)
258                         }
259                         execution.setVariable(Prefix + "ResourceModelCustomizationUuid", resourceModelCustomizationUuid)
260                         msoLogger.info("resourceModelCustomizationUuid:" + resourceModelCustomizationUuid)
261
262                 } catch (BpmnError e) {
263                         throw e
264                 } catch (Exception ex){
265                         msg = "Exception in preProcessRequest " + ex.getMessage()
266                         msoLogger.debug(msg)
267                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
268                 }
269         }
270
271         public void prepareUpdateProgress(DelegateExecution execution) {
272                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
273                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
274                 String operType = resourceInputObj.getOperationType()
275                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
276                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
277                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
278                 String operationId = resourceInputObj.getOperationId()
279                 String progress = execution.getVariable("progress")
280                 String status = execution.getVariable("status")
281                 String statusDescription = execution.getVariable("statusDescription")
282
283                 String body = """
284                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
285                         xmlns:ns="http://org.openecomp.mso/requestsdb">
286                         <soapenv:Header/>
287                 <soapenv:Body>
288                     <ns:updateResourceOperationStatus>
289                                <operType>${operType}</operType>
290                                <operationId>${operationId}</operationId>
291                                <progress>${progress}</progress>
292                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
293                                <serviceId>${ServiceInstanceId}</serviceId>
294                                <status>${status}</status>
295                                <statusDescription>${statusDescription}</statusDescription>
296                     </ns:updateResourceOperationStatus>
297                 </soapenv:Body>
298                 </soapenv:Envelope>"""
299
300                 setProgressUpdateVariables(execution, body)
301                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
302         }
303
304         public void allocateCrossONAPResource(DelegateExecution execution) {
305                 msoLogger.info(" ***** Started allocateCrossONAPResource *****")
306
307                 //get TP links from AAI for SOTN handoverMode only
308                 String handoverMode = execution.getVariable(Prefix + "HandoverMode")
309                 if("SOTN".equalsIgnoreCase(handoverMode)) {
310                         // Put TP Link info into serviceParameters
311                         JSONObject inputParameters = execution.getVariable(Prefix + "ServiceParameters")
312                         if(inputParameters.has("remote-access-provider-id")) {
313                                 Map<String, Object> crossTPs = new HashMap<String, Object>();
314                                 crossTPs.put("local-access-provider-id", inputParameters.get("remote-access-provider-id"));
315                                 crossTPs.put("local-access-client-id", inputParameters.get("remote-access-client-id"));
316                                 crossTPs.put("local-access-topology-id", inputParameters.get("remote-access-topology-id"));
317                                 crossTPs.put("local-access-node-id", inputParameters.get("remote-access-node-id"));
318                                 crossTPs.put("local-access-ltp-id", inputParameters.get("remote-access-ltp-id"));
319                                 crossTPs.put("remote-access-provider-id", inputParameters.get("local-access-provider-id"));
320                                 crossTPs.put("remote-access-client-id", inputParameters.get("local-access-client-id"));
321                                 crossTPs.put("remote-access-topology-id", inputParameters.get("local-access-topology-id"));
322                                 crossTPs.put("remote-access-node-id", inputParameters.get("local-access-node-id"));
323                                 crossTPs.put("remote-access-ltp-id", inputParameters.get("local-access-ltp-id"));
324
325                                 inputParameters.put("local-access-provider-id", crossTPs.get("local-access-provider-id"));
326                                 inputParameters.put("local-access-client-id", crossTPs.get("local-access-client-id"));
327                                 inputParameters.put("local-access-topology-id", crossTPs.get("local-access-topology-id"));
328                                 inputParameters.put("local-access-node-id", crossTPs.get("local-access-node-id"));
329                                 inputParameters.put("local-access-ltp-id", crossTPs.get("local-access-ltp-id"));
330                                 inputParameters.put("remote-access-provider-id", crossTPs.get("remote-access-provider-id"));
331                                 inputParameters.put("remote-access-client-id", crossTPs.get("remote-access-client-id"));
332                                 inputParameters.put("remote-access-topology-id", crossTPs.get("remote-access-topology-id"));
333                                 inputParameters.put("remote-access-node-id", crossTPs.get("remote-access-node-id"));
334                                 inputParameters.put("remote-access-ltp-id", crossTPs.get("remote-access-ltp-id"));
335
336                                 execution.setVariable(Prefix + "ServiceParameters", inputParameters)
337                         }
338                         else {
339                                         msoLogger.error("No allocated CrossONAPResource found in ServiceParameters")
340                         }
341                 }
342
343                 msoLogger.info("Exit " + allocateCrossONAPResource)
344         }
345
346         public void prepare3rdONAPRequest(DelegateExecution execution) {
347                 msoLogger.info(" ***** Started prepare3rdONAPRequest *****")
348
349                 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
350                 String extAPIPath = sppartnerUrl + '/serviceOrder'
351                 execution.setVariable("ExternalAPIURL", extAPIPath)
352
353                 // ExternalAPI message format
354                 String externalId = execution.getVariable("resourceName")
355                 String category = "E2E Service"
356                 String description = "Service Order from SPPartner"
357                 String requestedStartDate = utils.generateCurrentTimeInUtc()
358                 String requestedCompletionDate = utils.generateCurrentTimeInUtc()
359                 String priority = "1" // 0-4 0:highest
360                 String subscriberId = execution.getVariable("globalSubscriberId")
361                 String customerRole = "ONAPcustomer"
362                 String subscriberName = subscriberId
363                 String referredType = "Consumer"
364                 String orderItemId = "1"
365                 String action = "add" //for create
366                 String serviceState = "active"
367                 String serviceName = execution.getVariable("serviceInstanceName")
368                 String serviceUuId = execution.getVariable(Prefix + "SppartnerUUID")
369
370                 Map<String, String> valueMap = new HashMap<>()
371                 valueMap.put("externalId", '"' + externalId + '"')
372                 valueMap.put("category", '"' + category + '"')
373                 valueMap.put("description", '"' + description + '"')
374                 valueMap.put("requestedStartDate", '"' + requestedStartDate + '"')
375                 valueMap.put("requestedCompletionDate", '"' + requestedCompletionDate + '"')
376                 valueMap.put("priority", '"'+ priority + '"')
377                 valueMap.put("subscriberId", '"' + subscriberId + '"')
378                 valueMap.put("customerRole", '"' + customerRole + '"')
379                 valueMap.put("subscriberName", '"' + subscriberName + '"')
380                 valueMap.put("referredType", '"' + referredType + '"')
381                 valueMap.put("orderItemId", '"' + orderItemId + '"')
382                 valueMap.put("action", '"' + action + '"')
383                 valueMap.put("serviceState", '"' + serviceState + '"')
384                 valueMap.put("serviceId", "null") //null for add
385                 valueMap.put("serviceName", '"' + serviceName + '"')
386                 valueMap.put("serviceUuId", '"' + serviceUuId + '"')
387
388                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
389
390                 // insert CallSource='ExternalAPI' to uuiRequest
391                 Map<String, String> requestInputsMap = new HashMap<>()
392                 requestInputsMap.put("inputName",  '"CallSource"')
393                 requestInputsMap.put("inputValue", '"ExternalAPI"')
394                 String _requestInputs_ = externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
395
396                 requestInputsMap.clear()
397                 String serviceInstanceId = execution.getVariable(Prefix + "ServiceInstanceId")
398                 requestInputsMap.put("inputName", '"SppartnerServiceId"')
399                 requestInputsMap.put("inputValue", '"' + serviceInstanceId + '"')
400                 _requestInputs_ +=  ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
401
402                 requestInputsMap.clear()
403                 String serviceType = execution.getVariable("serviceType")
404                 requestInputsMap.put("inputName", '"serviceType"')
405                 requestInputsMap.put("inputValue", '"' + serviceType + '"')
406                 _requestInputs_ +=  ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
407
408                 // Transfer all uuiRequest incomeParameters to ExternalAPI format
409                 JSONObject inputParameters = execution.getVariable(Prefix + "ServiceParameters")
410                 for(String key : inputParameters.keySet()) {
411                         String inputName = key
412                         String inputValue = inputParameters.opt(key)
413                         requestInputsMap.clear()
414                         requestInputsMap.put("inputName", '"' + inputName+ '"')
415                         requestInputsMap.put("inputValue", '"' + inputValue + '"')
416                         _requestInputs_ += ",\n" + externalAPIUtil.setTemplate(ExternalAPIUtil.RequestInputsTemplate, requestInputsMap)
417                 }
418                 valueMap.put("_requestInputs_",  _requestInputs_)
419
420                 String payload = externalAPIUtil.setTemplate(ExternalAPIUtil.PostServiceOrderRequestsTemplate, valueMap)
421                 execution.setVariable(Prefix + "Payload", payload)
422                 msoLogger.info("Exit " + prepare3rdONAPRequest)
423         }
424
425         public void doCreateE2ESIin3rdONAP(DelegateExecution execution) {
426                 msoLogger.info(" ***** Started doCreateE2ESIin3rdONAP *****")
427
428                 String extAPIPath = execution.getVariable("ExternalAPIURL")
429                 String payload = execution.getVariable(Prefix + "Payload")
430                 msoLogger.debug("doCreateE2ESIin3rdONAP externalAPIURL is: " + extAPIPath)
431                 msoLogger.debug("doCreateE2ESIin3rdONAP payload is: " + payload)
432
433                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
434
435                 Response response = externalAPIUtil.executeExternalAPIPostCall(execution, extAPIPath, payload)
436
437                 int responseCode = response.getStatus()
438                 execution.setVariable(Prefix + "PostServiceOrderResponseCode", responseCode)
439                 msoLogger.debug("Post ServiceOrder response code is: " + responseCode)
440
441                 String extApiResponse = response.readEntity(String.class)
442                 JSONObject responseObj = new JSONObject(extApiResponse)
443                 execution.setVariable(Prefix + "PostServiceOrderResponse", extApiResponse)
444
445                 msoLogger.debug("doCreateE2ESIin3rdONAP response body is: " + extApiResponse)
446
447                 //Process Response
448                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
449                         //200 OK 201 CREATED 202 ACCEPTED
450                 {
451                         msoLogger.debug("Post ServiceOrder Received a Good Response")
452                         String serviceOrderId = responseObj.get("id")
453                         execution.setVariable(Prefix + "SuccessIndicator", true)
454                         execution.setVariable("ServiceOrderId", serviceOrderId)
455                         msoLogger.info("Post ServiceOrderid is: " + serviceOrderId)
456                 }
457                 else{
458                         msoLogger.error("Post ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
459 //                      exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Post ServiceOrder Received a bad response from 3rdONAP External API")
460                 }
461
462                 msoLogger.info("Exit " + doCreateE2ESIin3rdONAP)
463         }
464
465
466         public void getE2ESIProgressin3rdONAP(DelegateExecution execution) {
467                 msoLogger.info(" ***** Started getE2ESIProgressin3rdONAP *****")
468
469                 String extAPIPath = execution.getVariable("ExternalAPIURL")
470                 extAPIPath += "/" + execution.getVariable("ServiceOrderId")
471                 utils.log("DEBUG", "getE2ESIProgressin3rdONAP create externalAPIURL is: " + extAPIPath, isDebugEnabled)
472
473                 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
474
475                 Response response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
476
477                 int responseCode = response.getStatus()
478                 execution.setVariable(Prefix + "GetServiceOrderResponseCode", responseCode)
479                 msoLogger.debug("Get ServiceOrder response code is: " + responseCode)
480
481                 String extApiResponse = response.readEntity(String.class)
482                 JSONObject responseObj = new JSONObject(extApiResponse)
483                 execution.setVariable(Prefix + "GetServiceOrderResponse", extApiResponse)
484
485                 msoLogger.debug("getE2ESIProgressin3rdONAP create response body is: " + extApiResponse)
486
487                 //Process Response //200 OK 201 CREATED 202 ACCEPTED
488                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
489                 {
490                         msoLogger.debug("Get Create ServiceOrder Received a Good Response")
491
492                         String orderState = responseObj.get("state")
493                         if("REJECTED".equalsIgnoreCase(orderState)) {
494                                 execution.setVariable("progress", 100)
495                                 execution.setVariable("status", "error")
496                                 execution.setVariable("statusDescription", "Create Service Order Status is REJECTED")
497                                 return
498                         }
499
500                         JSONArray items = responseObj.getJSONArray("orderItem")
501                         JSONObject item = items[0]
502                         JSONObject service = item.get("service")
503                         String sppartnerServiceId = service.get("id")
504                         if(sppartnerServiceId == null || sppartnerServiceId.equals("null")) {
505                                 execution.setVariable("progress", 100)
506                                 execution.setVariable("status", "error")
507                                 execution.setVariable("statusDescription", "Create Service Order Status get null sppartnerServiceId")
508                                 msoLogger.error("null sppartnerServiceId while getting progress from externalAPI")
509                                 return
510                         }
511
512                         execution.setVariable(Prefix + "SppartnerServiceId", sppartnerServiceId)
513
514                         String serviceOrderState = item.get("state")
515                         execution.setVariable(Prefix + "SuccessIndicator", true)
516                         execution.setVariable("ServiceOrderState", serviceOrderState)
517
518                         // Get serviceOrder State and process progress
519                         if("ACKNOWLEDGED".equalsIgnoreCase(serviceOrderState)) {
520                                 execution.setVariable("progress", 15)
521                                 execution.setVariable("status", "processing")
522                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
523                         }
524                         else if("INPROGRESS".equalsIgnoreCase(serviceOrderState)) {
525                                 execution.setVariable("progress", 40)
526                                 execution.setVariable("status", "processing")
527                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
528                         }
529                         else if("COMPLETED".equalsIgnoreCase(serviceOrderState)) {
530                                 execution.setVariable("progress", 100)
531                                 execution.setVariable("status", "finished")
532                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
533                         }
534                         else if("FAILED".equalsIgnoreCase(serviceOrderState)) {
535                                 execution.setVariable("progress", 100)
536                                 execution.setVariable("status", "error")
537                                 execution.setVariable("statusDescription", "Create Service Order Status is " + serviceOrderState)
538                         }
539                         else {
540                                 execution.setVariable("progress", 100)
541                                 execution.setVariable("status", "error")
542                                 execution.setVariable("statusDescription", "Create Service Order Status is unknown")
543                         }
544                 }
545                 else{
546                         msoLogger.debug("Get ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
547                         execution.setVariable("progress", 100)
548                         execution.setVariable("status", "error")
549                         execution.setVariable("statusDescription", "Get Create ServiceOrder Received a bad response")
550                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get Create ServiceOrder Received a bad response from 3rdONAP External API")
551                 }
552
553                 msoLogger.info("Exit " + getE2ESIProgressin3rdONAP)
554         }
555
556         /**
557          * delay 5 sec
558          */
559         public void timeDelay(DelegateExecution execution) {
560                 try {
561                         Thread.sleep(5000)
562                 } catch(InterruptedException e) {
563                         msoLogger.error("Time Delay exception" + e)
564                 }
565         }
566
567         public void saveSPPartnerInAAI(DelegateExecution execution) {
568                 msoLogger.info(" ***** Started saveSPPartnerInAAI *****")
569
570                 String sppartnerId = execution.getVariable(Prefix + "SppartnerServiceId")
571                 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
572                 String callSource = execution.getVariable(Prefix + "CallSource")
573                 String serviceInstanceId = execution.getVariable(Prefix + "ServiceInstanceId")
574                 String globalSubscriberId = execution.getVariable("globalSubscriberId")
575                 String serviceType = execution.getVariable("serviceType")
576                 String resourceModelInvariantUuid = execution.getVariable(Prefix + "ResourceModelInvariantUuid")
577                 String resourceModelUuid = execution.getVariable(Prefix + "ResourceModelUuid")
578                 String resourceModelCustomizationUuid = execution.getVariable(Prefix + "ResourceModelCustomizationUuid")
579                 
580                 SpPartner partner = new SpPartner()
581                 partner.setSpPartnerId(sppartnerId)
582                 partner.setUrl(sppartnerUrl)
583                 partner.setCallsource(callSource)
584                 partner.setModelInvariantId(resourceModelInvariantUuid)
585                 partner.setModelVersionId(resourceModelUuid)
586                 partner.setModelCustomizationId(resourceModelCustomizationUuid)
587
588                 AAIResourcesClient client = new AAIResourcesClient()
589                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, sppartnerId)
590                 client.create(uri, partner)
591
592                 AAIResourceUri siUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceInstanceId)
593                 client.connect(uri, siUri)
594
595                 msoLogger.info("Exit " + saveSPPartnerInAAI)
596         }
597
598         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
599                 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
600                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
601                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
602         }
603
604         public void postProcess(DelegateExecution execution){
605                 msoLogger.info(" ***** Started postProcess *****")
606                 String responseCode = execution.getVariable(Prefix + "PutSppartnerResponseCode")
607                 String responseObj = execution.getVariable(Prefix + "PutSppartnerResponse")
608
609                 msoLogger.info("response from AAI for put sppartner, response code :" + responseCode + "  response object :" + responseObj)
610                 msoLogger.info(" ***** Exit postProcess *****")
611         }
612
613         public void sendSyncResponse (DelegateExecution execution) {
614                 msoLogger.debug(" *** sendSyncResponse *** ")
615
616                 try {
617                         String operationStatus = "finished"
618                         // RESTResponse for main flow
619                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
620                         msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
621                         sendWorkflowResponse(execution, 202, resourceOperationResp)
622                         execution.setVariable("sentSyncResponse", true)
623
624                 } catch (Exception ex) {
625                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
626                         msoLogger.debug(msg)
627                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
628                 }
629                 msoLogger.debug(" ***** Exit sendSyncResopnse *****")
630         }
631 }