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.generated.fluentbuilders.AAIFluentTypeBuilder;
32 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
33 import org.onap.logging.filter.base.ONAPComponents;
34 import org.onap.so.bpmn.common.BuildingBlockExecution;
35 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
36 import org.onap.so.client.exception.ExceptionBuilder;
37 import org.onap.so.client.orchestration.ApiHandlerClient;
38 import org.onap.so.serviceinstancebeans.ModelInfo;
39 import org.onap.so.serviceinstancebeans.ModelType;
40 import org.onap.so.serviceinstancebeans.Service;
41 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
42 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_INSTANCE_ID;
48 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_CORRELATION_ID;
49 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ERROR;
50 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ID;
51 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_PAYLOAD;
56 public class DeleteChildServiceBB {
58 private static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
59 private static final String REQUEST_ACTION = "requestAction";
60 public static final String CHILD_SVC_REQ_STATUS = "CHILD_SVC_REQ_STATUS";
62 protected ExceptionBuilder exceptionBuilder;
65 private ApiHandlerClient apiHandlerClient;
67 private AAIResourcesClient aaiResourcesClient = new AAIResourcesClient();
69 private final Logger log = LoggerFactory.getLogger(this.getClass());
72 public void buildRequest(final BuildingBlockExecution buildingBlockExecution) {
73 log.info("Building Delete Service Request");
74 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
75 String childSvcInstanceId = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
76 String childServiceInstanceId =
77 buildingBlockExecution.getLookupMap().get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
78 String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
79 ServiceInstance childInstanceAAI = aaiResourcesClient.get(ServiceInstance.class,
82 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(childServiceInstanceId))
85 ServiceInstance parentInstanceAAI =
86 aaiResourcesClient.get(ServiceInstance.class,
87 AAIUriFactory.createResourceUri(
88 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
91 if (childInstanceAAI == null || parentInstanceAAI == null) {
92 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10004, "Service AAI request failed",
95 Service parentInstance = serviceInstanceToServiceBeanMapper(parentInstanceAAI);
96 Service childInstance = serviceInstanceToServiceBeanMapper(childInstanceAAI);
97 ServiceInstancesRequest sir = ChildServiceRequestBuilder
98 .getInstance(buildingBlockExecution, parentInstance, childInstance)
100 buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
101 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
102 buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
106 public void setRequestAction(final BuildingBlockExecution buildingBlockExecution) {
107 String action = buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getAction();
108 buildingBlockExecution.setVariable(REQUEST_ACTION, action);
111 public void checkIfChildInstantiated(final BuildingBlockExecution buildingBlockExecution) {
113 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
115 String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
117 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
119 ServiceInstance parentInstanceAAI =
120 aaiResourcesClient.get(ServiceInstance.class,
121 AAIUriFactory.createResourceUri(
122 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
128 if (parentInstanceAAI.getComposedResources() != null) {
129 List<ComposedResource> composedResources =
130 parentInstanceAAI.getComposedResources().getComposedResource();
134 List<List<Relationship>> relationship = new ArrayList<>();
135 for (ComposedResource composedResource : composedResources) {
136 if (composedResource.getRelationshipList() != null) {
137 relationship.add(composedResource.getRelationshipList().getRelationship());
141 List<List<RelationshipData>> relationshipData = new ArrayList<>();
142 List<List<RelatedToProperty>> relatedToProperties = new ArrayList<>();
144 for (int i = 0; i < relationship.size(); i++) {
145 for (Relationship relationshipList : relationship.get(i)) {
146 relatedToProperties.add(relationshipList.getRelatedToProperty());
147 relationshipData.add(relationshipList.getRelationshipData());
152 String childInstanceId = null;
153 for (int i = 0; i < relationship.size(); i++) {
154 for (RelatedToProperty relatedToProperty1 : relatedToProperties.get(i)) {
155 if (relatedToProperty1.getPropertyValue().equalsIgnoreCase(childSvcInstanceName)) {
156 for (RelationshipData relationshipData1 : relationshipData.get(i)) {
157 if (relationshipData1.getRelationshipKey()
158 .equals("service-instance.service-instance-id")) {
159 childInstanceId = relationshipData1.getRelationshipValue();
166 if (childInstanceId != null) {
167 lookupMap.put(ResourceKey.CHILD_SERVICE_INSTANCE_ID, childInstanceId);
168 buildingBlockExecution.setVariable(ROLLBACK_TARGET_STATE, "Rollback");
169 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
171 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
172 buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
175 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
176 buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
178 } catch (Exception e) {
179 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10005, e.getMessage(),
184 public void buildRequestRollBack(final BuildingBlockExecution buildingBlockExecution) {
186 log.info("buildRequestRollBack Create Service Request");
187 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
188 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
189 String childSvcInstanceId = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_ID);
190 Objects.requireNonNull(childSvcInstanceName, "Child service instance name is required");
192 ServiceInstancesRequest sir = ChildServiceRequestBuilder
193 .getInstance(buildingBlockExecution, childSvcInstanceName)
195 buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
196 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
197 buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
198 } catch (Exception e) {
199 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10002, e.getMessage(),
204 public void sendRequest(final BuildingBlockExecution buildingBlockExecution) {
206 ServiceInstancesRequest sir = buildingBlockExecution.getVariable(CHILD_SVC_REQ_PAYLOAD);
207 log.info("Sending Delete Service Request: \n{}", sir.toString());
208 buildingBlockExecution.setVariable(CHILD_SVC_REQ_CORRELATION_ID,
209 sir.getRequestDetails().getRequestInfo().getCorrelator());
210 ServiceInstancesResponse response = apiHandlerClient.deleteServiceInstance(sir);
211 buildingBlockExecution.setVariable(CHILD_SVC_REQ_ID, response.getRequestReferences().getRequestId());
212 buildingBlockExecution.setVariable(CHILD_SVC_INSTANCE_ID, response.getRequestReferences().getInstanceId());
213 } catch (Exception e) {
214 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10003, e.getMessage(),
219 public void handleFailure(final BuildingBlockExecution buildingBlockExecution) {
220 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
221 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
222 String childErrorMessage = buildingBlockExecution.getVariable(CHILD_SVC_REQ_ERROR);
223 String errorMessage =
224 String.format("Failed deleting child service %:qqs %s", childSvcInstanceName, childErrorMessage);
225 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10001, errorMessage, ONAPComponents.SO);
228 private static Service serviceInstanceToServiceBeanMapper(ServiceInstance serviceInstance) {
229 Service service = new Service();
230 service.setInstanceName(service.getInstanceName());
231 ModelInfo modelInfo = new ModelInfo();
232 modelInfo.setModelId(serviceInstance.getModelVersionId());
233 modelInfo.setModelType(ModelType.service);
234 modelInfo.setModelVersionId(serviceInstance.getModelVersionId());
235 modelInfo.setModelInstanceName(serviceInstance.getServiceInstanceName());
236 modelInfo.setModelInvariantId(serviceInstance.getModelInvariantId());
237 service.setModelInfo(modelInfo);