Rollback Scenario for recursive orchestration
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / service / composition / DeleteChildServiceBB.java
1 /*-
2  * Copyright (C) 2021 Bell Canada. All rights reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.so.bpmn.infrastructure.service.composition;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
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;
52
53
54
55 @Component
56 public class DeleteChildServiceBB {
57
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";
61     @Autowired
62     protected ExceptionBuilder exceptionBuilder;
63
64     @Autowired
65     private ApiHandlerClient apiHandlerClient;
66
67     private AAIResourcesClient aaiResourcesClient = new AAIResourcesClient();
68
69     private final Logger log = LoggerFactory.getLogger(this.getClass());
70
71
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,
80                 AAIUriFactory
81                         .createResourceUri(
82                                 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(childServiceInstanceId))
83                         .depth(Depth.TWO))
84                 .orElse(null);
85         ServiceInstance parentInstanceAAI =
86                 aaiResourcesClient.get(ServiceInstance.class,
87                         AAIUriFactory.createResourceUri(
88                                 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
89                                 .depth(Depth.TWO))
90                         .orElse(null);
91         if (childInstanceAAI == null || parentInstanceAAI == null) {
92             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10004, "Service AAI request failed",
93                     ONAPComponents.SO);
94         }
95         Service parentInstance = serviceInstanceToServiceBeanMapper(parentInstanceAAI);
96         Service childInstance = serviceInstanceToServiceBeanMapper(childInstanceAAI);
97         ServiceInstancesRequest sir = ChildServiceRequestBuilder
98                 .getInstance(buildingBlockExecution, parentInstance, childInstance)
99                 .setParentRequestId(
100                         buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
101                 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
102         buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
103     }
104
105
106     public void setRequestAction(final BuildingBlockExecution buildingBlockExecution) {
107         String action = buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getAction();
108         buildingBlockExecution.setVariable(REQUEST_ACTION, action);
109     }
110
111     public void checkIfChildInstantiated(final BuildingBlockExecution buildingBlockExecution) {
112         try {
113             Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
114
115             String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
116
117             String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
118
119             ServiceInstance parentInstanceAAI =
120                     aaiResourcesClient.get(ServiceInstance.class,
121                             AAIUriFactory.createResourceUri(
122                                     AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
123                                     .depth(Depth.TWO))
124                             .orElse(null);
125
126
127
128             if (parentInstanceAAI.getComposedResources() != null) {
129                 List<ComposedResource> composedResources =
130                         parentInstanceAAI.getComposedResources().getComposedResource();
131
132
133
134                 List<List<Relationship>> relationship = new ArrayList<>();
135                 for (ComposedResource composedResource : composedResources) {
136                     if (composedResource.getRelationshipList() != null) {
137                         relationship.add(composedResource.getRelationshipList().getRelationship());
138                     }
139                 }
140
141                 List<List<RelationshipData>> relationshipData = new ArrayList<>();
142                 List<List<RelatedToProperty>> relatedToProperties = new ArrayList<>();
143
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());
148                     }
149                 }
150
151
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();
160                                     break;
161                                 }
162                             }
163                         }
164                     }
165                 }
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");
170                 } else {
171                     buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
172                     buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
173                 }
174             } else {
175                 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
176                 buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
177             }
178         } catch (Exception e) {
179             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10005, e.getMessage(),
180                     ONAPComponents.SO);
181         }
182     }
183
184     public void buildRequestRollBack(final BuildingBlockExecution buildingBlockExecution) {
185         try {
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");
191
192             ServiceInstancesRequest sir = ChildServiceRequestBuilder
193                     .getInstance(buildingBlockExecution, childSvcInstanceName)
194                     .setParentRequestId(
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(),
200                     ONAPComponents.SO);
201         }
202     }
203
204     public void sendRequest(final BuildingBlockExecution buildingBlockExecution) {
205         try {
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(),
215                     ONAPComponents.SO);
216         }
217     }
218
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);
226     }
227
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);
238         return service;
239     }
240 }