9bb1c24f48e462e8e54168c792688fb950099658
[so.git] /
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.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;
53
54
55
56 @Component
57 public class DeleteChildServiceBB {
58
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";
62     @Autowired
63     protected ExceptionBuilder exceptionBuilder;
64
65     @Autowired
66     private ApiHandlerClient apiHandlerClient;
67
68     private AAIResourcesClient aaiResourcesClient = new AAIResourcesClient();
69
70     private final Logger log = LoggerFactory.getLogger(this.getClass());
71
72
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,
81                 AAIClientUriFactory
82                         .createResourceUri(
83                                 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(childServiceInstanceId))
84                         .depth(Depth.TWO))
85                 .orElse(null);
86         ServiceInstance parentInstanceAAI =
87                 aaiResourcesClient.get(ServiceInstance.class,
88                         AAIClientUriFactory.createResourceUri(
89                                 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
90                                 .depth(Depth.TWO))
91                         .orElse(null);
92         if (childInstanceAAI == null || parentInstanceAAI == null) {
93             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10004, "Service AAI request failed",
94                     ONAPComponents.SO);
95         }
96         Service parentInstance = serviceInstanceToServiceBeanMapper(parentInstanceAAI);
97         Service childInstance = serviceInstanceToServiceBeanMapper(childInstanceAAI);
98         ServiceInstancesRequest sir = ChildServiceRequestBuilder
99                 .getInstance(buildingBlockExecution, parentInstance, childInstance)
100                 .setParentRequestId(
101                         buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
102                 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
103         buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
104     }
105
106
107     public void setRequestAction(final BuildingBlockExecution buildingBlockExecution) {
108         String action = buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getAction();
109         buildingBlockExecution.setVariable(REQUEST_ACTION, action);
110     }
111
112     public void checkIfChildInstantiated(final BuildingBlockExecution buildingBlockExecution) {
113         try {
114             Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
115
116             String parentServiceInstanceId = buildingBlockExecution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID);
117
118             String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
119
120             ServiceInstance parentInstanceAAI =
121                     aaiResourcesClient.get(ServiceInstance.class,
122                             AAIClientUriFactory.createResourceUri(
123                                     AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
124                                     .depth(Depth.TWO))
125                             .orElse(null);
126
127
128
129             if (parentInstanceAAI.getComposedResources() != null) {
130                 List<ComposedResource> composedResources =
131                         parentInstanceAAI.getComposedResources().getComposedResource();
132
133
134
135                 List<List<Relationship>> relationship = new ArrayList<>();
136                 for (ComposedResource composedResource : composedResources) {
137                     if (composedResource.getRelationshipList() != null) {
138                         relationship.add(composedResource.getRelationshipList().getRelationship());
139                     }
140                 }
141
142                 List<List<RelationshipData>> relationshipData = new ArrayList<>();
143                 List<List<RelatedToProperty>> relatedToProperties = new ArrayList<>();
144
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());
149                     }
150                 }
151
152
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();
161                                     break;
162                                 }
163                             }
164                         }
165                     }
166                 }
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");
171                 } else {
172                     buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
173                     buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
174                 }
175             } else {
176                 buildingBlockExecution.setVariable(REQUEST_ACTION, "createInstance");
177                 buildingBlockExecution.setVariable(CHILD_SVC_REQ_STATUS, "fail");
178             }
179         } catch (Exception e) {
180             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10005, e.getMessage(),
181                     ONAPComponents.SO);
182         }
183     }
184
185     public void buildRequestRollBack(final BuildingBlockExecution buildingBlockExecution) {
186         try {
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");
192
193             ServiceInstancesRequest sir = ChildServiceRequestBuilder
194                     .getInstance(buildingBlockExecution, childSvcInstanceName)
195                     .setParentRequestId(
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(),
201                     ONAPComponents.SO);
202         }
203     }
204
205     public void sendRequest(final BuildingBlockExecution buildingBlockExecution) {
206         try {
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(),
216                     ONAPComponents.SO);
217         }
218     }
219
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);
227     }
228
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);
239         return service;
240     }
241 }