2 * Copyright (C) 2021 Bell Canada. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.so.bpmn.infrastructure.service.composition;
19 import org.onap.aai.domain.yang.ServiceInstance;
20 import org.onap.aaiclient.client.aai.AAIResourcesClient;
21 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
22 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
23 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
24 import org.onap.logging.filter.base.ONAPComponents;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
27 import org.onap.so.client.exception.ExceptionBuilder;
28 import org.onap.so.client.orchestration.ApiHandlerClient;
29 import org.onap.so.serviceinstancebeans.ModelInfo;
30 import org.onap.so.serviceinstancebeans.ModelType;
31 import org.onap.so.serviceinstancebeans.Service;
32 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
33 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
39 import java.util.UUID;
40 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_INSTANCE_ID;
41 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_CORRELATION_ID;
42 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ERROR;
43 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ID;
44 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_PAYLOAD;
48 public class DeleteChildServiceBB {
51 protected ExceptionBuilder exceptionBuilder;
54 private ApiHandlerClient apiHandlerClient;
56 private AAIResourcesClient aaiResourcesClient = new AAIResourcesClient();
58 private final Logger log = LoggerFactory.getLogger(this.getClass());
60 public void buildRequest(final BuildingBlockExecution buildingBlockExecution) {
61 log.info("Building Delete Service Request");
62 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
63 String childSvcInstanceId = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
64 String childServiceInstanceId =
65 buildingBlockExecution.getLookupMap().get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
66 String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
67 ServiceInstance childInstanceAAI = aaiResourcesClient.get(ServiceInstance.class,
70 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(childServiceInstanceId))
73 ServiceInstance parentInstanceAAI =
74 aaiResourcesClient.get(ServiceInstance.class,
75 AAIUriFactory.createResourceUri(
76 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
79 if (childInstanceAAI == null || parentInstanceAAI == null) {
80 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10004, "Service AAI request failed",
83 Service parentInstance = serviceInstanceToServiceBeanMapper(parentInstanceAAI);
84 Service childInstance = serviceInstanceToServiceBeanMapper(childInstanceAAI);
85 ServiceInstancesRequest sir = ChildServiceRequestBuilder
86 .getInstance(buildingBlockExecution, parentInstance, childInstance)
88 buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
89 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
90 buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
93 public void sendRequest(final BuildingBlockExecution buildingBlockExecution) {
95 ServiceInstancesRequest sir = buildingBlockExecution.getVariable(CHILD_SVC_REQ_PAYLOAD);
96 log.info("Sending Delete Service Request: \n{}", sir.toString());
97 buildingBlockExecution.setVariable(CHILD_SVC_REQ_CORRELATION_ID,
98 sir.getRequestDetails().getRequestInfo().getCorrelator());
99 ServiceInstancesResponse response = apiHandlerClient.deleteServiceInstance(sir);
100 buildingBlockExecution.setVariable(CHILD_SVC_REQ_ID, response.getRequestReferences().getRequestId());
101 buildingBlockExecution.setVariable(CHILD_SVC_INSTANCE_ID, response.getRequestReferences().getInstanceId());
102 } catch (Exception e) {
103 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10003, e.getMessage(),
108 public void handleFailure(final BuildingBlockExecution buildingBlockExecution) {
109 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
110 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
111 String childErrorMessage = buildingBlockExecution.getVariable(CHILD_SVC_REQ_ERROR);
112 String errorMessage =
113 String.format("Failed deleting child service %:qqs %s", childSvcInstanceName, childErrorMessage);
114 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10001, errorMessage, ONAPComponents.SO);
117 private static Service serviceInstanceToServiceBeanMapper(ServiceInstance serviceInstance) {
118 Service service = new Service();
119 service.setInstanceName(service.getInstanceName());
120 ModelInfo modelInfo = new ModelInfo();
121 modelInfo.setModelId(serviceInstance.getModelVersionId());
122 modelInfo.setModelType(ModelType.service);
123 modelInfo.setModelVersionId(serviceInstance.getModelVersionId());
124 modelInfo.setModelInstanceName(serviceInstance.getServiceInstanceName());
125 modelInfo.setModelInvariantId(serviceInstance.getModelInvariantId());
126 service.setModelInfo(modelInfo);