Enable DeleteChildService functionality
[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 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;
38 import java.util.Map;
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;
45
46
47 @Component
48 public class DeleteChildServiceBB {
49
50     @Autowired
51     protected ExceptionBuilder exceptionBuilder;
52
53     @Autowired
54     private ApiHandlerClient apiHandlerClient;
55
56     private AAIResourcesClient aaiResourcesClient = new AAIResourcesClient();
57
58     private final Logger log = LoggerFactory.getLogger(this.getClass());
59
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,
68                 AAIUriFactory
69                         .createResourceUri(
70                                 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(childServiceInstanceId))
71                         .depth(Depth.TWO))
72                 .orElse(null);
73         ServiceInstance parentInstanceAAI =
74                 aaiResourcesClient.get(ServiceInstance.class,
75                         AAIUriFactory.createResourceUri(
76                                 AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(parentServiceInstanceId))
77                                 .depth(Depth.TWO))
78                         .orElse(null);
79         if (childInstanceAAI == null || parentInstanceAAI == null) {
80             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10004, "Service AAI request failed",
81                     ONAPComponents.SO);
82         }
83         Service parentInstance = serviceInstanceToServiceBeanMapper(parentInstanceAAI);
84         Service childInstance = serviceInstanceToServiceBeanMapper(childInstanceAAI);
85         ServiceInstancesRequest sir = ChildServiceRequestBuilder
86                 .getInstance(buildingBlockExecution, parentInstance, childInstance)
87                 .setParentRequestId(
88                         buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
89                 .setChildSvcInstanceId(childSvcInstanceId).setCorrelationId(UUID.randomUUID().toString()).build();
90         buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
91     }
92
93     public void sendRequest(final BuildingBlockExecution buildingBlockExecution) {
94         try {
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(),
104                     ONAPComponents.SO);
105         }
106     }
107
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);
115     }
116
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);
127         return service;
128     }
129 }