2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 TechMahindra
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.cds;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicReference;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
29 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
31 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
32 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
33 import org.onap.so.client.PreconditionFailedException;
34 import org.onap.so.client.RestPropertiesLoader;
35 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
36 import org.onap.so.client.exception.ExceptionBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
42 import com.google.protobuf.InvalidProtocolBufferException;
43 import com.google.protobuf.Struct;
44 import com.google.protobuf.Struct.Builder;
45 import com.google.protobuf.util.JsonFormat;
47 import io.grpc.Status;
50 * Util class to support Call to CDS client
54 public class AbstractCDSProcessingBBUtils implements CDSProcessingListener {
56 private static final Logger logger = LoggerFactory.getLogger(AbstractCDSProcessingBBUtils.class);
58 private static final String SUCCESS = "Success";
59 private static final String FAILED = "Failed";
60 private static final String PROCESSING = "Processing";
62 private final AtomicReference<String> cdsResponse = new AtomicReference<>();
65 private ExceptionBuilder exceptionUtil;
68 * Extracting data from execution object and building the ExecutionServiceInput
72 * DelegateExecution object
74 public void constructExecutionServiceInputObject(DelegateExecution execution) {
75 logger.trace("Start AbstractCDSProcessingBBUtils.preProcessRequest ");
78 AbstractCDSPropertiesBean executionObject = (AbstractCDSPropertiesBean) execution
79 .getVariable("executionObject");
81 String payload = executionObject.getRequestObject();
83 CommonHeader commonHeader = CommonHeader.newBuilder().setOriginatorId(executionObject.getOriginatorId())
84 .setRequestId(executionObject.getRequestId()).setSubRequestId(executionObject.getSubRequestId())
86 ActionIdentifiers actionIdentifiers = ActionIdentifiers.newBuilder()
87 .setBlueprintName(executionObject.getBlueprintName())
88 .setBlueprintVersion(executionObject.getBlueprintVersion())
89 .setActionName(executionObject.getActionName()).setMode(executionObject.getMode()).build();
91 Builder struct = Struct.newBuilder();
93 JsonFormat.parser().merge(payload, struct);
94 } catch (InvalidProtocolBufferException e) {
95 logger.error("Failed to parse received message. blueprint({}:{}) for action({}). {}",
96 executionObject.getBlueprintVersion(), executionObject.getBlueprintName(),
97 executionObject.getActionName(), e);
100 ExecutionServiceInput executionServiceInput = ExecutionServiceInput.newBuilder()
101 .setCommonHeader(commonHeader).setActionIdentifiers(actionIdentifiers).setPayload(struct.build())
104 execution.setVariable("executionServiceInput", executionServiceInput);
106 } catch (Exception ex) {
107 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
112 * get the executionServiceInput object from execution and send a request to CDS
113 * Client and wait for TIMEOUT period
116 * DelegateExecution object
118 public void sendRequestToCDSClient(DelegateExecution execution) {
120 logger.trace("Start AbstractCDSProcessingBBUtils.sendRequestToCDSClient ");
122 CDSProperties props = RestPropertiesLoader.getInstance().getNewImpl(CDSProperties.class);
124 throw new PreconditionFailedException(
125 "No RestProperty.CDSProperties implementation found on classpath, can't create client.");
128 ExecutionServiceInput executionServiceInput = (ExecutionServiceInput) execution
129 .getVariable("executionServiceInput");
131 CDSProcessingListener cdsProcessingListener = new AbstractCDSProcessingBBUtils();
133 CDSProcessingClient cdsClient = new CDSProcessingClient(cdsProcessingListener);
134 CountDownLatch countDownLatch = cdsClient.sendRequest(executionServiceInput);
137 countDownLatch.await(props.getTimeout(), TimeUnit.SECONDS);
138 } catch (InterruptedException ex) {
139 logger.error("Caught exception in sendRequestToCDSClient in AbstractCDSProcessingBBUtils : ", ex);
144 if (cdsResponse != null) {
145 execution.setVariable("CDSStatus", cdsResponse.get());
148 } catch (Exception ex) {
149 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
154 * Get Response from CDS Client
158 public void onMessage(ExecutionServiceOutput message) {
159 logger.info("Received notification from CDS: {}", message);
160 EventType eventType = message.getStatus().getEventType();
164 case EVENT_COMPONENT_FAILURE:
165 // failed processing with failure
166 cdsResponse.set(FAILED);
168 case EVENT_COMPONENT_PROCESSING:
170 cdsResponse.set(PROCESSING);
172 case EVENT_COMPONENT_EXECUTED:
173 // done with async processing
174 cdsResponse.set(SUCCESS);
177 cdsResponse.set(FAILED);
184 * On error at CDS, log the error
187 public void onError(Throwable t) {
188 Status status = Status.fromThrowable(t);
189 logger.error("Failed processing blueprint {}", status, t);