Merge "Enable long-running processes in ControllerExecutionBB"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / service / composition / CreateChildServiceBB.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 static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ERROR;
20 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_PAYLOAD;
21 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ID;
22 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_INSTANCE_ID;
23 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_CORRELATION_ID;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.UUID;
27 import org.onap.logging.filter.base.ONAPComponents;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.client.orchestration.ApiHandlerClient;
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 org.onap.aai.domain.yang.ComposedResource;
39 import org.onap.aai.domain.yang.Relationship;
40 import org.onap.aaiclient.client.aai.AAIResourcesClient;
41 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
42 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
43 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
44 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
45
46
47 @Component
48 public class CreateChildServiceBB {
49
50     private final Logger log = LoggerFactory.getLogger(this.getClass());
51
52     @Autowired
53     protected ExceptionBuilder exceptionBuilder;
54
55     @Autowired
56     private ApiHandlerClient apiHandlerClient;
57
58     public void buildRequest(final BuildingBlockExecution buildingBlockExecution) {
59         try {
60             log.info("Building Create Service Request");
61             Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
62             String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
63             Objects.requireNonNull(childSvcInstanceName, "Child service instance name is required");
64
65             ServiceInstancesRequest sir = ChildServiceRequestBuilder
66                     .getInstance(buildingBlockExecution, childSvcInstanceName)
67                     .setParentRequestId(
68                             buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
69                     .setCorrelationId(UUID.randomUUID().toString()).build();
70             buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
71         } catch (Exception e) {
72             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10002, e.getMessage(),
73                     ONAPComponents.SO);
74         }
75     }
76
77     public void sendRequest(final BuildingBlockExecution buildingBlockExecution) throws Exception {
78         try {
79             buildingBlockExecution.getLookupMap();
80             ServiceInstancesRequest sir = buildingBlockExecution.getVariable(CHILD_SVC_REQ_PAYLOAD);
81             log.info("Sending Create Service Request: \n{}", sir.toString());
82             buildingBlockExecution.setVariable(CHILD_SVC_REQ_CORRELATION_ID,
83                     sir.getRequestDetails().getRequestInfo().getCorrelator());
84             ServiceInstancesResponse response = apiHandlerClient.createServiceInstance(sir);
85             buildingBlockExecution.setVariable(CHILD_SVC_REQ_ID, response.getRequestReferences().getRequestId());
86             buildingBlockExecution.setVariable(CHILD_SVC_INSTANCE_ID, response.getRequestReferences().getInstanceId());
87         } catch (Exception e) {
88             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10003, e.getMessage(),
89                     ONAPComponents.SO);
90         }
91     }
92
93     /*
94      * This method is to create Relation between Parent & Child Services with Node as Composed Resource.
95      * 
96      */
97
98     public void updateRelations(BuildingBlockExecution buildingBlockExecution) throws Exception {
99
100         Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
101
102         String childSvcInstanceId = buildingBlockExecution.getVariable(CHILD_SVC_INSTANCE_ID);
103         String parentSvcInstanceId = lookupMap.get(ResourceKey.SERVICE_INSTANCE_ID);
104
105         ComposedResource composedResource = new ComposedResource();
106         composedResource.setId(UUID.randomUUID().toString());
107
108         AAIResourcesClient client = new AAIResourcesClient();
109
110         AAIResourceUri composedResourceURI = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
111                 .customer(buildingBlockExecution.getGeneralBuildingBlock().getCustomer().getGlobalCustomerId())
112                 .serviceSubscription(buildingBlockExecution.getGeneralBuildingBlock().getRequestContext()
113                         .getSubscriptionServiceType())
114                 .serviceInstance(parentSvcInstanceId).composedResource(composedResource.getId()));
115
116         client.create(composedResourceURI, composedResource);
117
118         AAIResourceUri childURI =
119                 AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(childSvcInstanceId));
120
121         client.connect(composedResourceURI, childURI);
122
123     }
124
125     public void handleFailure(final BuildingBlockExecution buildingBlockExecution) {
126         Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
127         String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
128         String childErrorMessage = buildingBlockExecution.getVariable(CHILD_SVC_REQ_ERROR);
129         String errorMessage =
130                 String.format("Failed creating child service %s %s", childSvcInstanceName, childErrorMessage);
131         exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10001, errorMessage, ONAPComponents.SO);
132     }
133
134 }