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 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;
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;
40 public class CreateChildServiceBB {
42 private final Logger log = LoggerFactory.getLogger(this.getClass());
45 protected ExceptionBuilder exceptionBuilder;
48 private ApiHandlerClient apiHandlerClient;
50 public void buildRequest(final BuildingBlockExecution buildingBlockExecution) {
52 log.info("Building Create Service Request");
53 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
54 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
55 Objects.requireNonNull(childSvcInstanceName, "Child service instance name is required");
57 ServiceInstancesRequest sir = ChildServiceRequestBuilder
58 .getInstance(buildingBlockExecution, childSvcInstanceName)
60 buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId())
61 .setCorrelationId(UUID.randomUUID().toString()).build();
62 buildingBlockExecution.setVariable(CHILD_SVC_REQ_PAYLOAD, sir);
63 } catch (Exception e) {
64 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10002, e.getMessage(),
69 public void sendRequest(final BuildingBlockExecution buildingBlockExecution) throws Exception {
71 buildingBlockExecution.getLookupMap();
72 ServiceInstancesRequest sir = buildingBlockExecution.getVariable(CHILD_SVC_REQ_PAYLOAD);
73 log.info("Sending Create Service Request: \n{}", sir.toString());
74 buildingBlockExecution.setVariable(CHILD_SVC_REQ_CORRELATION_ID,
75 sir.getRequestDetails().getRequestInfo().getCorrelator());
77 ServiceInstancesResponse response = apiHandlerClient.createServiceInstance(sir);
78 buildingBlockExecution.setVariable(CHILD_SVC_REQ_ID, response.getRequestReferences().getRequestId());
79 buildingBlockExecution.setVariable(CHILD_SVC_INSTANCE_ID, response.getRequestReferences().getInstanceId());
80 } catch (Exception e) {
81 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10003, e.getMessage(),
86 public void handleFailure(final BuildingBlockExecution buildingBlockExecution) {
87 Map<ResourceKey, String> lookupMap = buildingBlockExecution.getLookupMap();
88 String childSvcInstanceName = lookupMap.get(ResourceKey.CHILD_SERVICE_INSTANCE_NAME);
89 String childErrorMessage = buildingBlockExecution.getVariable(CHILD_SVC_REQ_ERROR);
91 String.format("Failed creating child service %s %s", childSvcInstanceName, childErrorMessage);
92 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 10001, errorMessage, ONAPComponents.SO);