2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
25 import org.onap.so.bpmn.common.scripts.ExternalAPIUtilFactory
27 import javax.ws.rs.NotFoundException
28 import javax.ws.rs.core.Response
30 import org.apache.commons.lang3.StringUtils
31 import static org.apache.commons.lang3.StringUtils.*
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34 import org.json.JSONArray
35 import org.json.JSONObject
36 import org.onap.aai.domain.yang.SpPartner
37 import org.onap.so.bpmn.common.recipe.ResourceInput
38 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
39 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
40 import org.onap.so.bpmn.common.scripts.ExceptionUtil
41 import org.onap.so.bpmn.common.scripts.ExternalAPIUtil
42 import org.onap.so.bpmn.core.json.JsonUtils
43 import org.onap.so.bpmn.core.UrnPropertiesReader
44 import org.onap.so.client.aai.AAIObjectType
45 import org.onap.so.client.aai.AAIResourcesClient
46 import org.onap.so.client.aai.entities.uri.AAIResourceUri
47 import org.onap.so.client.aai.entities.uri.AAIUriFactory
48 import org.slf4j.Logger
49 import org.slf4j.LoggerFactory
52 * This groovy class supports the <class>Delete3rdONAPE2EServiceInstance.bpmn</class> process.
53 * flow for Delete 3rdONAPE2EServiceInstance in 3rdONAP
55 public class Delete3rdONAPE2EServiceInstance extends AbstractServiceTaskProcessor {
57 String Prefix = "CRE3rdONAPESI_"
59 ExceptionUtil exceptionUtil = new ExceptionUtil()
61 JsonUtils jsonUtil = new JsonUtils()
63 private static final Logger logger = LoggerFactory.getLogger( Delete3rdONAPE2EServiceInstance.class);
65 public void checkSPPartnerInfoFromAAI (DelegateExecution execution) {
66 logger.info(" ***** Started checkSPPartnerInfo *****")
68 //get bpmn inputs from resource request.
69 String requestId = execution.getVariable("mso-request-id")
70 String requestAction = execution.getVariable("requestAction")
71 logger.info("The requestAction is: " + requestAction)
72 String recipeParamsFromRequest = execution.getVariable("recipeParams")
73 logger.info("The recipeParams is: " + recipeParamsFromRequest)
74 String resourceInput = execution.getVariable("resourceInput")
75 logger.info("The resourceInput is: " + resourceInput)
76 //Get ResourceInput Object
77 ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
78 // set local resourceInput
79 execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
81 String resourceInstanceId = resourceInputObj.getResourceInstancenUuid()
82 String sppartnerId = resourceInstanceId
83 execution.setVariable(Prefix + "SppartnerId", sppartnerId)
85 // Get Sppartner from AAI
86 getSPPartnerInAAI(execution)
88 String callSource = "UUI"
89 String sppartnerUrl = ""
90 if(execution.hasVariable(Prefix + "CallSource")) {
91 callSource = execution.getVariable(Prefix + "CallSource")
92 sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
95 boolean is3rdONAPExist = false
96 if(!isBlank(sppartnerUrl)) {
100 execution.setVariable("Is3rdONAPExist", is3rdONAPExist)
101 execution.setVariable(Prefix + "ServiceInstanceId", resourceInputObj.getServiceInstanceId())
102 execution.setVariable("mso-request-id", requestId)
103 execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
105 } catch (Exception ex){
106 String msg = "Exception in checkSPPartnerInfoFromAAI " + ex.getMessage()
108 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
112 public void checkLocallCall (DelegateExecution execution) {
113 logger.info(" ***** Started checkLocallCall *****")
115 boolean isLocalCall = true
116 String callSource = execution.getVariable(Prefix + "CallSource")
117 if("ExternalAPI".equalsIgnoreCase(callSource)) {
120 execution.setVariable("IsLocalCall", isLocalCall)
123 public void preProcessRequest(DelegateExecution execution){
124 logger.info(" ***** Started preProcessRequest *****")
128 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
130 String globalSubscriberId = resourceInputObj.getGlobalSubscriberId()
131 if (isBlank(globalSubscriberId)) {
132 msg = "Input globalSubscriberId is null"
136 execution.setVariable("globalSubscriberId", globalSubscriberId)
137 logger.info( "globalSubscriberId:" + globalSubscriberId)
139 String serviceType = resourceInputObj.getServiceType()
140 if (isBlank(serviceType)) {
141 msg = "Input serviceType is null"
144 execution.setVariable("serviceType", serviceType)
145 logger.info( "serviceType:" + serviceType)
147 String operationId = resourceInputObj.getOperationId()
148 if (isBlank(operationId)) {
149 msg = "Input operationId is null"
152 execution.setVariable("operationId", operationId)
153 logger.info( "operationId:" + operationId)
155 String resourceName = resourceInputObj.getResourceInstanceName()
156 if (isBlank(resourceName)) {
157 msg = "Input resourceName is null"
160 execution.setVariable("resourceName", resourceName)
161 logger.info("resourceName:" + resourceName)
163 String resourceTemplateId = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
164 if (isBlank(resourceTemplateId)) {
165 msg = "Input resourceTemplateId is null"
168 execution.setVariable("resourceTemplateId", resourceTemplateId)
169 logger.info( "resourceTemplateId:" + resourceTemplateId)
171 } catch (Exception ex){
172 msg = "Exception in preProcessRequest " + ex.getMessage()
174 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
178 public void prepareUpdateProgress(DelegateExecution execution) {
179 logger.info(" ***** Started prepareUpdateProgress *****")
180 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
181 String operType = resourceInputObj.getOperationType()
182 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
183 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
184 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
185 String operationId = resourceInputObj.getOperationId()
186 String progress = execution.getVariable("progress")
187 String status = execution.getVariable("status")
188 String statusDescription = execution.getVariable("statusDescription")
191 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
192 xmlns:ns="http://org.onap.so/requestsdb">
195 <ns:updateResourceOperationStatus>
196 <operType>${operType}</operType>
197 <operationId>${operationId}</operationId>
198 <progress>${progress}</progress>
199 <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
200 <serviceId>${ServiceInstanceId}</serviceId>
201 <status>${status}</status>
202 <statusDescription>${statusDescription}</statusDescription>
203 </ns:updateResourceOperationStatus>
205 </soapenv:Envelope>"""
207 setProgressUpdateVariables(execution, body)
208 logger.info(" ***** End prepareUpdateProgress *****")
211 public void prepare3rdONAPRequest(DelegateExecution execution) {
212 logger.info(" ***** Started prepare3rdONAPRequest *****")
214 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
215 String extAPIPath = sppartnerUrl + '/serviceOrder'
216 execution.setVariable("ExternalAPIURL", extAPIPath)
218 // ExternalAPI message format
219 String externalId = execution.getVariable("resourceName")
220 String category = "E2E Service"
221 String description = "Service Order from SPPartner"
222 String requestedStartDate = utils.generateCurrentTimeInUtc()
223 String requestedCompletionDate = utils.generateCurrentTimeInUtc()
224 String priority = "1" // 0-4 0:highest
225 String subscriberId = execution.getVariable("globalSubscriberId")
226 String customerRole = "ONAPcustomer"
227 String subscriberName = subscriberId
228 String referredType = "Consumer"
229 String orderItemId = "1"
230 String action = "delete" //for delete
231 String serviceState = "active"
232 String serviceName = ""
233 String serviceType = execution.getVariable("serviceType")
234 String serviceId = execution.getVariable(Prefix + "SppartnerId")
236 queryServicefrom3rdONAP(execution)
237 String serviceSpecificationId = execution.getVariable(Prefix + "ServiceSpecificationId")
239 Map<String, String> valueMap = new HashMap<>()
240 valueMap.put("externalId", '"' + externalId + '"')
241 valueMap.put("category", '"' + category + '"')
242 valueMap.put("description", '"' + description + '"')
243 valueMap.put("requestedStartDate", '"' + requestedStartDate + '"')
244 valueMap.put("requestedCompletionDate", '"' + requestedCompletionDate + '"')
245 valueMap.put("priority", '"'+ priority + '"')
246 valueMap.put("subscriberId", '"' + subscriberId + '"')
247 valueMap.put("customerRole", '"' + customerRole + '"')
248 valueMap.put("subscriberName", '"' + subscriberName + '"')
249 valueMap.put("referredType", '"' + referredType + '"')
250 valueMap.put("orderItemId", '"' + orderItemId + '"')
251 valueMap.put("action", '"' + action + '"')
252 valueMap.put("serviceState", '"' + serviceState + '"')
253 valueMap.put("serviceId", '"' + serviceId + '"')
254 valueMap.put("serviceName", "null")
255 valueMap.put("serviceUuId", '"' + serviceSpecificationId + '"')
257 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtilFactory().create()
259 valueMap.put("_requestInputs_", "")
261 String payload = externalAPIUtil.setTemplate(ExternalAPIUtil.PostServiceOrderRequestsTemplate, valueMap)
262 execution.setVariable(Prefix + "Payload", payload)
263 logger.info( "Exit " + prepare3rdONAPRequest)
266 private void queryServicefrom3rdONAP(DelegateExecution execution)
268 logger.info(" ***** Started queryServicefrom3rdONAP *****")
271 String globalSubscriberId = execution.getVariable("globalSubscriberId")
272 String SppartnerServiceId = execution.getVariable(Prefix + "SppartnerId")
274 //https://{api_url}/nbi/api/v1/service?relatedParty.id=${globalSubscriberId}
275 String sppartnerUrl = execution.getVariable(Prefix + "SppartnerUrl")
276 String extAPIPath = sppartnerUrl + "/service?relatedParty.id=" + globalSubscriberId
277 logger.debug("queryServicefrom3rdONAP externalAPIURL is: " + extAPIPath)
279 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtilFactory().create()
281 Response response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
283 int responseCode = response.getStatus()
284 execution.setVariable(Prefix + "GetServiceResponseCode", responseCode)
285 logger.debug("Get Service response code is: " + responseCode)
287 String extApiResponse = response.readEntity(String.class)
289 execution.setVariable(Prefix + "GetServiceResponse", extApiResponse)
290 logger.debug("queryServicefrom3rdONAP response body is: " + extApiResponse)
292 //Process Response //200 OK 201 CREATED 202 ACCEPTED
293 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
295 logger.debug("Get Service Received a Good Response")
296 JSONArray responseList = new JSONArray(extApiResponse)
297 for(JSONObject obj : responseList) {
298 String svcId = obj.get("id")
299 if(StringUtils.equalsIgnoreCase(SppartnerServiceId, svcId)) {
300 JSONObject serviceSpecification = obj.get("serviceSpecification")
301 String serviceUuid = serviceSpecification.get("id")
302 execution.setVariable(Prefix + "ServiceSpecificationId", serviceUuid)
308 logger.error("Get Service Received a Bad Response Code. Response Code is: " + responseCode)
309 // exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get Service Received a bad response from 3rdONAP External API")
311 }catch(Exception e) {
312 logger.error("queryServicefrom3rdONAP exception:" + e.getMessage())
314 logger.info( "Exit " + queryServicefrom3rdONAP)
317 public void doDeleteE2ESIin3rdONAP(DelegateExecution execution) {
318 logger.info(" ***** Started doDeleteE2ESIin3rdONAP *****")
320 String extAPIPath = execution.getVariable("ExternalAPIURL")
321 String payload = execution.getVariable(Prefix + "Payload")
322 logger.debug("doDeleteE2ESIin3rdONAP externalAPIURL is: " + extAPIPath)
323 logger.debug("doDeleteE2ESIin3rdONAP payload is: " + payload)
325 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtilFactory().create()
326 execution.setVariable("ServiceOrderId", "")
328 Response response = externalAPIUtil.executeExternalAPIPostCall(execution, extAPIPath, payload)
330 int responseCode = response.getStatus()
331 execution.setVariable(Prefix + "PostServiceOrderResponseCode", responseCode)
332 logger.debug("Post ServiceOrder response code is: " + responseCode)
334 String extApiResponse = response.readEntity(String.class)
335 JSONObject responseObj = new JSONObject(extApiResponse)
336 execution.setVariable(Prefix + "PostServiceOrderResponse", extApiResponse)
338 logger.debug("doDeleteE2ESIin3rdONAP response body is: " + extApiResponse)
341 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
342 //200 OK 201 CREATED 202 ACCEPTED
344 logger.debug("Post ServiceOrder Received a Good Response")
345 String serviceOrderId = responseObj.get("id")
346 execution.setVariable(Prefix + "SuccessIndicator", true)
347 execution.setVariable("ServiceOrderId", serviceOrderId)
348 logger.info("Post ServiceOrderid is: " + serviceOrderId)
351 logger.error("Post ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
352 // exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Post ServiceOrder Received a bad response from 3rdONAP External API")
354 }catch(Exception e) {
355 logger.error("doDeleteE2ESIin3rdONAP exception:" + e.getMessage())
357 logger.info( "Exit " + doDeleteE2ESIin3rdONAP)
361 public void getE2ESIProgressin3rdONAP(DelegateExecution execution) {
362 logger.info(" ***** Started getE2ESIProgressin3rdONAP *****")
365 String extAPIPath = execution.getVariable("ExternalAPIURL")
366 extAPIPath += "/" + execution.getVariable("ServiceOrderId")
367 logger.debug("getE2ESIProgressin3rdONAP delete externalAPIURL is: " + extAPIPath)
369 ExternalAPIUtil externalAPIUtil = new ExternalAPIUtilFactory().create()
371 Response response = externalAPIUtil.executeExternalAPIGetCall(execution, extAPIPath)
373 int responseCode = response.getStatus()
374 execution.setVariable(Prefix + "GetServiceOrderResponseCode", responseCode)
375 logger.debug("Get ServiceOrder response code is: " + responseCode)
377 String extApiResponse = response.readEntity(String.class)
378 JSONObject responseObj = new JSONObject(extApiResponse)
379 execution.setVariable(Prefix + "GetServiceOrderResponse", extApiResponse)
381 logger.debug( "getE2ESIProgressin3rdONAP delete response body is: " + extApiResponse)
383 //Process Response //200 OK 201 CREATED 202 ACCEPTED
384 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
386 logger.debug("Get ServiceOrder Received a Good Response")
388 String orderState = responseObj.get("state")
389 if("REJECTED".equalsIgnoreCase(orderState)) {
390 execution.setVariable("progress", 100)
391 execution.setVariable("status", "error")
392 execution.setVariable("statusDescription", "Delete Service Order Status is REJECTED")
396 JSONArray items = responseObj.getJSONArray("orderItem")
397 JSONObject item = items[0]
398 JSONObject service = item.get("service")
399 String sppartnerServiceId = service.get("id")
400 if(sppartnerServiceId == null || sppartnerServiceId.equals("null")) {
401 execution.setVariable("progress", 100)
402 execution.setVariable("status", "error")
403 execution.setVariable("statusDescription", "Delete Service Order Status get null sppartnerServiceId")
404 logger.error("null sppartnerServiceId while getting progress from externalAPI")
407 execution.setVariable(Prefix + "SppartnerServiceId", sppartnerServiceId)
409 String serviceOrderState = item.get("state")
410 execution.setVariable(Prefix + "SuccessIndicator", true)
411 execution.setVariable("ServiceOrderState", serviceOrderState)
413 // Get serviceOrder State and process progress
414 if("ACKNOWLEDGED".equalsIgnoreCase(serviceOrderState)) {
415 execution.setVariable("progress", 15)
416 execution.setVariable("status", "processing")
417 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
419 else if("INPROGRESS".equalsIgnoreCase(serviceOrderState)) {
420 execution.setVariable("progress", 40)
421 execution.setVariable("status", "processing")
422 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
424 else if("COMPLETED".equalsIgnoreCase(serviceOrderState)) {
425 execution.setVariable("progress", 100)
426 execution.setVariable("status", "finished")
427 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
429 else if("FAILED".equalsIgnoreCase(serviceOrderState)) {
430 execution.setVariable("progress", 100)
431 execution.setVariable("status", "error")
432 execution.setVariable("statusDescription", "Delete Service Order Status is " + serviceOrderState)
435 execution.setVariable("progress", 100)
436 execution.setVariable("status", "error")
437 execution.setVariable("statusDescription", "Delete Service Order Status is unknown")
441 logger.debug("Get ServiceOrder Received a Bad Response Code. Response Code is: " + responseCode)
442 execution.setVariable("progress", 100)
443 execution.setVariable("status", "error")
444 execution.setVariable("statusDescription", "Get Delete ServiceOrder Received a bad response")
445 // exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Get Delete ServiceOrder Received a bad response from 3rdONAP External API")
447 }catch(Exception e) {
448 execution.setVariable("progress", 100)
449 execution.setVariable("status", "error")
450 execution.setVariable("statusDescription", "Get Delete ServiceOrder Exception")
451 logger.error("getE2ESIProgressin3rdONAP exception:" + e.getMessage())
453 logger.info( "Exit " + getE2ESIProgressin3rdONAP)
459 public void timeDelay(DelegateExecution execution) {
462 } catch(InterruptedException e) {
463 logger.error("Time Delay exception" + e )
467 private void getSPPartnerInAAI(DelegateExecution execution) {
468 logger.info(" ***** Started getSPPartnerInAAI *****")
470 String id = execution.getVariable(Prefix + "SppartnerId")
472 AAIResourcesClient client = new AAIResourcesClient()
473 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, id)
474 SpPartner sp = client.get(uri).asBean(SpPartner.class).get()
476 logger.debug("GET sppartner Received a Good Response")
477 execution.setVariable(Prefix + "SuccessIndicator", true)
478 execution.setVariable(Prefix + "FoundIndicator", true)
480 // String sppartnerId = sp.getSpPartnerId()
481 // execution.setVariable(Prefix + "SppartnerId", sppartnerId)
482 // logger.debug(" SppartnerId is: " + sppartnerId)
483 String sppartnerUrl = sp.getUrl()
484 execution.setVariable(Prefix + "SppartnerUrl", sppartnerUrl)
485 logger.debug(" SppartnerUrl is: " + sppartnerUrl)
486 String callSource = sp.getCallsource()
487 execution.setVariable(Prefix + "CallSource", callSource)
488 logger.debug(" CallSource is: " + callSource)
489 String sppartnerVersion = sp.getResourceVersion()
490 execution.setVariable(Prefix + "SppartnerVersion", sppartnerVersion)
491 logger.debug(" Resource Version is: " + sppartnerVersion)
492 } catch (Exception ex) {
493 String msg = "Exception in Delete3rdONAPE2EServiceInstance.saveSPPartnerInAAI. " + ex.getMessage()
495 // throw new BpmnError("MSOWorkflowException")
498 logger.info( "Exit " + getSPPartnerInAAI)
501 public void deleteSPPartnerInAAI(DelegateExecution execution) {
502 logger.info(" ***** Started deleteSPPartnerInAAI *****")
505 String sppartnerId = execution.getVariable(Prefix + "SppartnerId")
507 AAIResourcesClient client = new AAIResourcesClient()
508 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SP_PARTNER, sppartnerId)
510 logger.debug("Delete sppartner Received a Good Response")
511 execution.setVariable(Prefix + "SuccessIndicator", true)
512 } catch (Exception ex) {
513 String msg = "Exception in Delete3rdONAPE2EServiceInstance.deleteSPPartnerInAAI. " + ex.getMessage()
515 // throw new BpmnError("MSOWorkflowException")
519 logger.info( "Exit " + deleteSPPartnerInAAI)
522 private void setProgressUpdateVariables(DelegateExecution execution, String body) {
523 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
524 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
525 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
528 public void postProcess(DelegateExecution execution){
529 logger.info(" ***** Started postProcess *****")
530 String responseCode = execution.getVariable(Prefix + "PutSppartnerResponseCode")
531 String responseObj = execution.getVariable(Prefix + "PutSppartnerResponse")
533 logger.info("response from AAI for put sppartner, response code :" + responseCode + " response object :" + responseObj)
534 logger.info(" ***** Exit postProcess *****")
537 public void sendSyncResponse (DelegateExecution execution) {
538 logger.debug(" *** sendSyncResponse *** ")
541 String operationStatus = "finished"
542 // RESTResponse for main flow
543 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
544 logger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
545 sendWorkflowResponse(execution, 202, resourceOperationResp)
546 execution.setVariable("sentSyncResponse", true)
548 } catch (Exception ex) {
549 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
551 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
553 logger.debug(" ***** Exit sendSyncResopnse *****")