2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import javax.ws.rs.NotFoundException
24 import javax.ws.rs.core.Response
26 import org.apache.commons.lang3.StringUtils
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.json.JSONArray
30 import org.json.JSONObject
31 import org.onap.aai.domain.yang.SpPartner
32 import org.onap.so.bpmn.common.recipe.ResourceInput
33 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
34 import org.onap.so.bpmn.common.scripts.AaiUtil
35 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
36 import org.onap.so.bpmn.common.scripts.ExceptionUtil
37 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.onap.so.client.aai.AAIObjectType
40 import org.onap.so.client.aai.AAIResourcesClient
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory
43 import org.onap.so.logger.MsoLogger
46 * This groovy class supports the <class>Delete3rdONAPE2EServiceInstance.bpmn</class> process.
47 * flow for Delete 3rdONAPE2EServiceInstance in 3rdONAP
49 public class Delete3rdONAPE2EServiceInstance extends AbstractServiceTaskProcessor {
51 String Prefix = "CRE3rdONAPESI_"
53 ExceptionUtil exceptionUtil = new ExceptionUtil()
55 JsonUtils jsonUtil = new JsonUtils()
57 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, Delete3rdONAPE2EServiceInstance.class)
59 public void checkSPPartnerInfoFromAAI (DelegateExecution execution) {
60 msoLogger.info(" ***** Started checkSPPartnerInfo *****")
62 //get bpmn inputs from resource request.
63 String requestId = execution.getVariable("mso-request-id")
64 String requestAction = execution.getVariable("requestAction")
65 msoLogger.info("The requestAction is: " + requestAction)
66 String recipeParamsFromRequest = execution.getVariable("recipeParams")
67 msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
68 String resourceInput = execution.getVariable("resourceInput")
69 msoLogger.info("The resourceInput is: " + resourceInput)
70 //Get ResourceInput Object
71 ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
72 // set local resourceInput
73 execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
75 String resourceInstanceId = resourceInputObj.getResourceInstancenUuid()
76 String sppartnerId = resourceInstanceId
77 execution.setVariable(Prefix + "SppartnerId", sppartnerId)
79 // Get Sppartner from AAI
80 AaiUtil aaiUriUtil = new AaiUtil(this)
81 AAIResourceUri spPartner = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, sppartnerId)
82 String spPartnerUri = aaiUriUtil.createAaiUri(spPartner)
83 execution.setVariable(Prefix + "ServiceAaiPath", spPartnerUri)
85 getSPPartnerInAAI(execution)
87 String callSource = "UUI"
88 String sppartnerUrl = ""
89 if(execution.hasVariable(Prefix + "CallSource")) {
90 callSource = execution.getVariable(Prefix + "CallSource")
91 sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
94 boolean is3rdONAPExist = false
95 if(!isBlank(sppartnerUrl)) {
99 execution.setVariable("Is3rdONAPExist", is3rdONAPExist)
100 execution.setVariable(Prefix + "ServiceInstanceId", resourceInputObj.getServiceInstanceId())
101 execution.setVariable("mso-request-id", requestId)
102 execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
104 } catch (BpmnError e) {
106 } catch (Exception ex){
107 String msg = "Exception in checkSPPartnerInfoFromAAI " + ex.getMessage()
109 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
113 public void checkLocallCall (DelegateExecution execution) {
114 msoLogger.info(" ***** Started checkLocallCall *****")
116 boolean isLocalCall = true
117 String callSource = execution.getVariable(Prefix + "CallSource")
118 if("ExternalAPI".equalsIgnoreCase(callSource)) {
121 execution.setVariable("IsLocalCall", isLocalCall)
124 public void preProcessRequest(DelegateExecution execution){
125 msoLogger.info(" ***** Started preProcessRequest *****")
129 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
131 String globalSubscriberId = resourceInputObj.getGlobalSubscriberId()
132 if (isBlank(globalSubscriberId)) {
133 msg = "Input globalSubscriberId is null"
134 msoLogger.error( msg)
137 execution.setVariable("globalSubscriberId", globalSubscriberId)
138 msoLogger.info( "globalSubscriberId:" + globalSubscriberId)
140 String serviceType = resourceInputObj.getServiceType()
141 if (isBlank(serviceType)) {
142 msg = "Input serviceType is null"
143 msoLogger.error( msg)
145 execution.setVariable("serviceType", serviceType)
146 msoLogger.info( "serviceType:" + serviceType)
148 String operationId = resourceInputObj.getOperationId()
149 if (isBlank(operationId)) {
150 msg = "Input operationId is null"
151 msoLogger.error( msg)
153 execution.setVariable("operationId", operationId)
154 msoLogger.info( "operationId:" + operationId)
156 String resourceName = resourceInputObj.getResourceInstanceName()
157 if (isBlank(resourceName)) {
158 msg = "Input resourceName is null"
159 msoLogger.error( msg)
161 execution.setVariable("resourceName", resourceName)
162 msoLogger.info("resourceName:" + resourceName)
164 String resourceTemplateId = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
165 if (isBlank(resourceTemplateId)) {
166 msg = "Input resourceTemplateId is null"
167 msoLogger.error( msg)
169 execution.setVariable("resourceTemplateId", resourceTemplateId)
170 msoLogger.info( "resourceTemplateId:" + resourceTemplateId)
172 } catch (BpmnError e) {
174 } catch (Exception ex){
175 msg = "Exception in preProcessRequest " + ex.getMessage()
177 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
181 public void prepareUpdateProgress(DelegateExecution execution) {
182 msoLogger.info(" ***** Started prepareUpdateProgress *****")
183 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
184 String operType = resourceInputObj.getOperationType()
185 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
186 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
187 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
188 String operationId = resourceInputObj.getOperationId()
189 String progress = execution.getVariable("progress")
190 String status = execution.getVariable("status")
191 String statusDescription = execution.getVariable("statusDescription")
194 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
195 xmlns:ns="http://org.openecomp.mso/requestsdb">
198 <ns:updateResourceOperationStatus>
199 <operType>${operType}</operType>
200 <operationId>${operationId}</operationId>
201 <progress>${progress}</progress>
202 <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
203 <serviceId>${ServiceInstanceId}</serviceId>
204 <status>${status}</status>
205 <statusDescription>${statusDescription}</statusDescription>
206 </ns:updateResourceOperationStatus>
208 </soapenv:Envelope>"""
210 setProgressUpdateVariables(execution, body)
211 msoLogger.info(" ***** End prepareUpdateProgress *****")
214 public void prepare3rdONAPRequest(DelegateExecution execution) {
215 msoLogger.info(" ***** Started prepare3rdONAPRequest *****")
217 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
218 String extAPIPath = sppartnerUrl + '/serviceOrder'
219 execution.setVariable("ExternalAPIURL", extAPIPath)
221 // ExternalAPI message format
222 String externalId = execution.getVariable("resourceName")
223 String category = "E2E Service"
224 String description = "Service Order from SPPartner"
225 String requestedStartDate = utils.generateCurrentTimeInUtc()
226 String requestedCompletionDate = utils.generateCurrentTimeInUtc()
227 String priority = "1" // 0-4 0:highest
228 String subscriberId = execution.getVariable("globalSubscriberId")
229 String customerRole = "ONAPcustomer"
230 String subscriberName = subscriberId
231 String referredType = "Consumer"
232 String orderItemId = "1"
233 String action = "delete" //for delete
234 String serviceState = "active"
235 String serviceName = ""
236 String serviceType = execution.getVariable("serviceType")
237 String serviceId = execution.getVariable(Prefix + "SppartnerId")
239 queryServicefrom3rdONAP(execution)
240 String serviceSpecificationId = execution.getVariable(Prefix + "ServiceSpecificationId")
242 Map<String, String> valueMap = new HashMap<>()
243 valueMap.put("externalId", '"' + externalId + '"')
244 valueMap.put("category", '"' + category + '"')
245 valueMap.put("description", '"' + description + '"')
246 valueMap.put("requestedStartDate", '"' + requestedStartDate + '"')
247 valueMap.put("requestedCompletionDate", '"' + requestedCompletionDate + '"')
248 valueMap.put("priority", '"'+ priority + '"')
249 valueMap.put("subscriberId", '"' + subscriberId + '"')
250 valueMap.put("customerRole", '"' + customerRole + '"')
251 valueMap.put("subscriberName", '"' + subscriberName + '"')
252 valueMap.put("referredType", '"' + referredType + '"')
253 valueMap.put("orderItemId", '"' + orderItemId + '"')
254 valueMap.put("action", '"' + action + '"')
255 valueMap.put("serviceState", '"' + serviceState + '"')
256 valueMap.put("serviceId", '"' + serviceId + '"')
257 valueMap.put("serviceName", "null")
258 valueMap.put("serviceUuId", '"' + serviceSpecificationId + '"')
260 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
262 valueMap.put("_requestInputs_", "")
264 String payload = externalAPIUtil.setTemplate(ExternalAPIUtil.PostServiceOrderRequestsTemplate, valueMap)
265 execution.setVariable(Prefix + "Payload", payload)
266 msoLogger.info( "Exit " + prepare3rdONAPRequest)
269 private void queryServicefrom3rdONAP(DelegateExecution execution)
271 msoLogger.info(" ***** Started queryServicefrom3rdONAP *****")
273 String globalSubscriberId = execution.getVariable("globalSubscriberId")
274 String SppartnerServiceId = execution.getVariable(Prefix + "SppartnerId")
276 //https://{api_url}/nbi/api/v1/service?relatedParty.id=${globalSubscriberId}
277 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
278 String extAPIPath = sppartnerUrl + "/service?relatedParty.id=" + globalSubscriberId
279 msoLogger.debug("queryServicefrom3rdONAP externalAPIURL is: " + extAPIPath)
281 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
283 Response response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
285 int responseCode = response.getStatus()
286 execution.setVariable(Prefix + "GetServiceResponseCode", responseCode)
287 msoLogger.debug("Get Service response code is: " + responseCode)
289 String extApiResponse = response.readEntity(String.class)
291 execution.setVariable(Prefix + "GetServiceResponse", extApiResponse)
292 msoLogger.debug("queryServicefrom3rdONAP response body is: " + extApiResponse)
294 //Process Response //200 OK 201 CREATED 202 ACCEPTED
295 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
297 msoLogger.debug("Get Service Received a Good Response")
298 JSONArray responseList = new JSONArray(extApiResponse)
299 for(JSONObject obj : responseList) {
300 String svcId = obj.get("id")
301 if(StringUtils.equalsIgnoreCase(SppartnerServiceId, svcId)) {
302 JSONObject serviceSpecification = obj.get("serviceSpecification")
303 String serviceUuid = serviceSpecification.get("id")
304 execution.setVariable(Prefix + "ServiceSpecificationId", serviceUuid)
310 msoLogger.error("Get Service Received a Bad Response Code. Response Code is: " + responseCode)
311 // exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get Service Received a bad response from 3rdONAP External API")
314 msoLogger.info( "Exit " + queryServicefrom3rdONAP)
317 public void doDeleteE2ESIin3rdONAP(DelegateExecution execution) {
318 msoLogger.info(" ***** Started doDeleteE2ESIin3rdONAP *****")
320 String extAPIPath = execution.getVariable("ExternalAPIURL")
321 String payload = execution.getVariable(Prefix + "Payload")
322 msoLogger.debug("doDeleteE2ESIin3rdONAP externalAPIURL is: " + extAPIPath)
323 msoLogger.debug("doDeleteE2ESIin3rdONAP payload is: " + payload)
325 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
327 Response response = externalAPIUtil.executeExternalAPIPostCall(execution, extAPIPath, payload)
329 int responseCode = response.getStatus()
330 execution.setVariable(Prefix + "PostServiceOrderResponseCode", responseCode)
331 msoLogger.debug("Post ServiceOrder response code is: " + responseCode)
333 String extApiResponse = response.readEntity(String.class)
334 JSONObject responseObj = new JSONObject(extApiResponse)
335 execution.setVariable(Prefix + "PostServiceOrderResponse", extApiResponse)
337 msoLogger.debug("doDeleteE2ESIin3rdONAP response body is: " + extApiResponse)
340 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
341 //200 OK 201 CREATED 202 ACCEPTED
343 msoLogger.debug("Post ServiceOrder Received a Good Response")
344 String serviceOrderId = responseObj.get("id")
345 execution.setVariable(Prefix + "SuccessIndicator", true)
346 execution.setVariable("ServiceOrderId", serviceOrderId)
347 msoLogger.info("Post ServiceOrderid is: " + serviceOrderId)
350 msoLogger.error("Post ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
351 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Post ServiceOrder Received a bad response from 3rdONAP External API")
354 msoLogger.info( "Exit " + doDeleteE2ESIin3rdONAP)
358 public void getE2ESIProgressin3rdONAP(DelegateExecution execution) {
359 msoLogger.info(" ***** Started getE2ESIProgressin3rdONAP *****")
361 String extAPIPath = execution.getVariable("ExternalAPIURL")
362 extAPIPath += "/" + execution.getVariable("ServiceOrderId")
363 msoLogger.debug("getE2ESIProgressin3rdONAP delete externalAPIURL is: " + extAPIPath)
365 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtil()
367 Response response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
369 int responseCode = response.getStatus()
370 execution.setVariable(Prefix + "GetServiceOrderResponseCode", responseCode)
371 msoLogger.debug("Get ServiceOrder response code is: " + responseCode)
373 String extApiResponse = response.readEntity(String.class)
374 JSONObject responseObj = new JSONObject(extApiResponse)
375 execution.setVariable(Prefix + "GetServiceOrderResponse", extApiResponse)
377 utils.log("DEBUG", "getE2ESIProgressin3rdONAP delete response body is: " + extApiResponse)
379 //Process Response //200 OK 201 CREATED 202 ACCEPTED
380 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
382 msoLogger.debug("Get ServiceOrder Received a Good Response")
384 String orderState = responseObj.get("state")
385 if("REJECTED".equalsIgnoreCase(orderState)) {
386 execution.setVariable("progress", 100)
387 execution.setVariable("status", "error")
388 execution.setVariable("statusDescription", "Delete Service Order Status is REJECTED")
392 JSONArray items = responseObj.getJSONArray("orderItem")
393 JSONObject item = items[0]
394 JSONObject service = item.get("service")
395 String sppartnerServiceId = service.get("id")
396 if(sppartnerServiceId == null || sppartnerServiceId.equals("null")) {
397 execution.setVariable("progress", 100)
398 execution.setVariable("status", "error")
399 execution.setVariable("statusDescription", "Delete Service Order Status get null sppartnerServiceId")
400 msoLogger.error("null sppartnerServiceId while getting progress from externalAPI")
403 execution.setVariable(Prefix + "SppartnerServiceId", sppartnerServiceId)
405 String serviceOrderState = item.get("state")
406 execution.setVariable(Prefix + "SuccessIndicator", true)
407 execution.setVariable("ServiceOrderState", serviceOrderState)
409 // Get serviceOrder State and process progress
410 if("ACKNOWLEDGED".equalsIgnoreCase(serviceOrderState)) {
411 execution.setVariable("progress", 15)
412 execution.setVariable("status", "processing")
413 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
415 else if("INPROGRESS".equalsIgnoreCase(serviceOrderState)) {
416 execution.setVariable("progress", 40)
417 execution.setVariable("status", "processing")
418 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
420 else if("COMPLETED".equalsIgnoreCase(serviceOrderState)) {
421 execution.setVariable("progress", 100)
422 execution.setVariable("status", "finished")
423 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
425 else if("FAILED".equalsIgnoreCase(serviceOrderState)) {
426 execution.setVariable("progress", 100)
427 execution.setVariable("status", "error")
428 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
431 execution.setVariable("progress", 100)
432 execution.setVariable("status", "error")
433 execution.setVariable("statusDescription", "Delete Service Order Status is unknown")
437 msoLogger.debug("Get ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
438 execution.setVariable("progress", 100)
439 execution.setVariable("status", "error")
440 execution.setVariable("statusDescription", "Get Delete ServiceOrder Received a bad response")
441 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get Delete ServiceOrder Received a bad response from 3rdONAP External API")
444 msoLogger.info( "Exit " + getE2ESIProgressin3rdONAP)
450 public void timeDelay(DelegateExecution execution) {
453 } catch(InterruptedException e) {
454 msoLogger.error("Time Delay exception" + e )
458 private void getSPPartnerInAAI(DelegateExecution execution) {
459 msoLogger.info(" ***** Started getSPPartnerInAAI *****")
460 String id = execution.getVariable(Prefix + "SppartnerId")
462 AAIResourcesClient client = new AAIResourcesClient()
463 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, id)
464 SpPartner sp = client.get(uri).asBean(SpPartner.class).get()
466 msoLogger.debug("GET sppartner Received a Good Response")
467 execution.setVariable(Prefix + "SuccessIndicator", true)
468 execution.setVariable(Prefix + "FoundIndicator", true)
470 String sppartnerId = sp.getSpPartnerId()
471 execution.setVariable(Prefix + "SppartnerId", sppartnerId)
472 msoLogger.debug(" SppartnerId is: " + sppartnerId)
473 String sppartnerUrl = sp.getUrl()
474 execution.setVariable(Prefix + "SppartnerUrl", sppartnerUrl)
475 msoLogger.debug(" SppartnerUrl is: " + sppartnerUrl)
476 String callSource = sp.getCallsource()
477 execution.setVariable(Prefix + "CallSource", callSource)
478 msoLogger.debug(" CallSource is: " + callSource)
479 String sppartnerVersion = sp.getResourceVersion()
480 execution.setVariable(Prefix + "SppartnerVersion", sppartnerVersion)
481 msoLogger.debug(" Resource Version is: " + sppartnerVersion)
484 msoLogger.info( "Exit " + getSPPartnerInAAI)
487 public void deleteSPPartnerInAAI(DelegateExecution execution) {
488 msoLogger.info(" ***** Started deleteSPPartnerInAAI *****")
490 String sppartnerId = execution.getVariable(Prefix + "SppartnerId")
491 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
492 String sppartnerVersion = execution.getVariable(Prefix + "SppartnerVersion")
494 AAIResourcesClient client = new AAIResourcesClient()
495 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, sppartnerId)
498 msoLogger.debug("Delete sppartner Received a Good Response")
499 execution.setVariable(Prefix + "SuccessIndicator", true)
500 } catch (NotFoundException e) {
501 msoLogger.debug(" Delete sppartner Received a Not Found (404) Response")
502 execution.setVariable(Prefix + "FoundIndicator", false)
504 msoLogger.debug("Delete sppartner Received a Good Response")
506 msoLogger.info( "Exit " + deleteSPPartnerInAAI)
509 private void setProgressUpdateVariables(DelegateExecution execution, String body) {
510 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
511 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
512 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
515 public void postProcess(DelegateExecution execution){
516 msoLogger.info(" ***** Started postProcess *****")
517 String responseCode = execution.getVariable(Prefix + "PutSppartnerResponseCode")
518 String responseObj = execution.getVariable(Prefix + "PutSppartnerResponse")
520 msoLogger.info("response from AAI for put sppartner, response code :" + responseCode + " response object :" + responseObj)
521 msoLogger.info(" ***** Exit postProcess *****")
524 public void sendSyncResponse (DelegateExecution execution) {
525 msoLogger.debug(" *** sendSyncResponse *** ")
528 String operationStatus = "finished"
529 // RESTResponse for main flow
530 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
531 msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
532 sendWorkflowResponse(execution, 202, resourceOperationResp)
533 execution.setVariable("sentSyncResponse", true)
535 } catch (Exception ex) {
536 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
538 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
540 msoLogger.debug(" ***** Exit sendSyncResopnse *****")