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 java.util.ArrayList;
20 import java.util.List;
22 import java.util.Objects;
23 import java.util.UUID;
24 import org.onap.aai.domain.yang.ComposedResource;
25 import org.onap.aai.domain.yang.RelatedToProperty;
26 import org.onap.aai.domain.yang.Relationship;
27 import org.onap.aai.domain.yang.RelationshipData;
28 import org.onap.aai.domain.yang.ServiceInstance;
29 import org.onap.aaiclient.client.aai.AAIResourcesClient;
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
31 import org.onap.aaiclient.client.aai.entities.uri.AAIClientUriFactory;
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
33 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
34 import org.onap.logging.filter.base.ONAPComponents;
35 import org.onap.so.bpmn.common.BuildingBlockExecution;
36 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
37 import org.onap.so.client.exception.ExceptionBuilder;
38 import org.onap.so.client.orchestration.ApiHandlerClient;
39 import org.onap.so.serviceinstancebeans.ModelInfo;
40 import org.onap.so.serviceinstancebeans.ModelType;
41 import org.onap.so.serviceinstancebeans.Service;
42 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
43 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.stereotype.Component;
48 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_INSTANCE_ID;
49 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_CORRELATION_ID;
50 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ERROR;
51 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ID;
52 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_PAYLOAD;
57 public class DeleteChildServiceBB {
59 private static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
60 private static final String REQUEST_ACTION = "requestAction";
61 public static final String CHILD_SVC_REQ_STATUS = "CHILD_SVC_REQ_STATUS";
63 protected ExceptionBuilder exceptionBuilder;
66 private ApiHandlerClient apiHandlerClient;
68 private AAIResourcesClient aaiResourcesClient = new AAIResourcesClient();
70 private final Logger log = LoggerFactory.getLogger(this.getClass());
73 public void buildRequest(final BuildingBlockExecution buildingBlockExecution) {
74 log.info("Building Delete Service Request");
75 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
76 String childSvcInstanceId = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
77 String childServiceInstanceId =
78 buildingBlockExecution.getLookupMap().get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
79 String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
80 ServiceInstance childInstanceAAI = aaiResourcesClient.get(ServiceInstance.class,
83 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(childServiceInstanceId))
86 ServiceInstance parentInstanceAAI =
87 aaiResourcesClient.get(ServiceInstance.class,
88 AAIClientUriFactory.createResourceUri(
89 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
92 if (childInstanceAAI == null || parentInstanceAAI == null) {
93 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10004, "Service AAI request failed",
96 Service parentInstance = serviceInstanceToServiceBeanMapper(parentInstanceAAI);
97 Service childInstance = serviceInstanceToServiceBeanMapper(childInstanceAAI);
98 ServiceInstancesRequest sir = ChildServiceRequestBuilder
99 .getInstance(buildingBlockExecution, parentInstance, childInstance)
101 buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
102 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
103 buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
107 public void setRequestAction(final BuildingBlockExecution buildingBlockExecution) {
108 String action = buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getAction();
109 buildingBlockExecution.setVariable(REQUEST_ACTION, action);
112 public void checkIfChildInstantiated(final BuildingBlockExecution buildingBlockExecution) {
114 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
116 String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
118 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
120 ServiceInstance parentInstanceAAI =
121 aaiResourcesClient.get(ServiceInstance.class,
122 AAIClientUriFactory.createResourceUri(
123 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
129 if (parentInstanceAAI.getComposedResources() != null) {
130 List<ComposedResource> composedResources =
131 parentInstanceAAI.getComposedResources().getComposedResource();
135 List<List<Relationship>> relationship = new ArrayList<>();
136 for (ComposedResource composedResource : composedResources) {
137 if (composedResource.getRelationshipList() != null) {
138 relationship.add(composedResource.getRelationshipList().getRelationship());
142 List<List<RelationshipData>> relationshipData = new ArrayList<>();
143 List<List<RelatedToProperty>> relatedToProperties = new ArrayList<>();
145 for (int i = 0; i < relationship.size(); i++) {
146 for (Relationship relationshipList : relationship.get(i)) {
147 relatedToProperties.add(relationshipList.getRelatedToProperty());
148 relationshipData.add(relationshipList.getRelationshipData());
153 String childInstanceId = null;
154 for (int i = 0; i < relationship.size(); i++) {
155 for (RelatedToProperty relatedToProperty1 : relatedToProperties.get(i)) {
156 if (relatedToProperty1.getPropertyValue().equalsIgnoreCase(childSvcInstanceName)) {
157 for (RelationshipData relationshipData1 : relationshipData.get(i)) {
158 if (relationshipData1.getRelationshipKey()
159 .equals("service-instance.service-instance-id")) {
160 childInstanceId = relationshipData1.getRelationshipValue();
167 if (childInstanceId != null) {
168 lookupMap.put(ResourceKey.CHILD_SERVICE_INSTANCE_ID, childInstanceId);
169 buildingBlockExecution.setVariable(ROLLBACK_TARGET_STATE, "Rollback");
170 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
172 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
173 buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
176 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
177 buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
179 } catch (Exception e) {
180 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10005, e.getMessage(),
185 public void buildRequestRollBack(final BuildingBlockExecution buildingBlockExecution) {
187 log.info("buildRequestRollBack Create Service Request");
188 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
189 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
190 String childSvcInstanceId = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
191 Objects.requireNonNull(childSvcInstanceName, "Child service instance name is required");
193 ServiceInstancesRequest sir = ChildServiceRequestBuilder
194 .getInstance(buildingBlockExecution, childSvcInstanceName)
196 buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
197 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
198 buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
199 } catch (Exception e) {
200 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10002, e.getMessage(),
205 public void sendRequest(final BuildingBlockExecution buildingBlockExecution) {
207 ServiceInstancesRequest sir = buildingBlockExecution.getVariable(CHILD_SVC_REQ_PAYLOAD);
208 log.info("Sending Delete Service Request: \n{}", sir.toString());
209 buildingBlockExecution.setVariable(CHILD_SVC_REQ_CORRELATION_ID,
210 sir.getRequestDetails().getRequestInfo().getCorrelator());
211 ServiceInstancesResponse response = apiHandlerClient.deleteServiceInstance(sir);
212 buildingBlockExecution.setVariable(CHILD_SVC_REQ_ID, response.getRequestReferences().getRequestId());
213 buildingBlockExecution.setVariable(CHILD_SVC_INSTANCE_ID, response.getRequestReferences().getInstanceId());
214 } catch (Exception e) {
215 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10003, e.getMessage(),
220 public void handleFailure(final BuildingBlockExecution buildingBlockExecution) {
221 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
222 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
223 String childErrorMessage = buildingBlockExecution.getVariable(CHILD_SVC_REQ_ERROR);
224 String errorMessage =
225 String.format("Failed deleting child service %:qqs %s", childSvcInstanceName, childErrorMessage);
226 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10001, errorMessage, ONAPComponents.SO);
229 private static Service serviceInstanceToServiceBeanMapper(ServiceInstance serviceInstance) {
230 Service service = new Service();
231 service.setInstanceName(service.getInstanceName());
232 ModelInfo modelInfo = new ModelInfo();
233 modelInfo.setModelId(serviceInstance.getModelVersionId());
234 modelInfo.setModelType(ModelType.service);
235 modelInfo.setModelVersionId(serviceInstance.getModelVersionId());
236 modelInfo.setModelInstanceName(serviceInstance.getServiceInstanceName());
237 modelInfo.setModelInvariantId(serviceInstance.getModelInvariantId());
238 service.setModelInfo(modelInfo);