2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Bell Canada.
4 * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.cds.client;
22 import com.google.common.base.Preconditions;
23 import io.grpc.ManagedChannel;
24 import io.grpc.netty.NettyChannelBuilder;
25 import java.util.concurrent.CountDownLatch;
26 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
27 import org.onap.policy.cds.api.CdsProcessorListener;
28 import org.onap.policy.cds.properties.CdsServerProperties;
29 import org.onap.policy.common.parameters.ValidationResult;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
35 * The CDS processor client uses gRPC for communication between Policy and CDS. This communication is configured to use
36 * a streaming approach, which means the client sends an event to which the server can reply with multiple
37 * sub-responses, until full completion of the processing.
40 public class CdsProcessorGrpcClient implements AutoCloseable {
42 private static final Logger LOGGER = LoggerFactory.getLogger(CdsProcessorGrpcClient.class);
44 private ManagedChannel channel;
45 private CdsProcessorHandler handler;
48 * Constructor, create a CDS processor gRPC client.
50 * @param listener the listener to listen on
52 public CdsProcessorGrpcClient(final CdsProcessorListener listener, CdsServerProperties props) {
53 final ValidationResult validationResult = props.validate();
54 Preconditions.checkState(validationResult.getStatus().isValid(), "Error validating CDS server "
55 + "properties: " + validationResult.getResult());
57 String url = "gRPC://" + props.getHost() + ":" + props.getPort() + "/";
59 this.channel = NettyChannelBuilder.forAddress(props.getHost(), props.getPort())
60 .intercept(new BasicAuthClientHeaderInterceptor(props)).usePlaintext().build();
61 this.handler = new CdsProcessorHandler(listener, url);
62 LOGGER.info("CdsProcessorListener started");
65 CdsProcessorGrpcClient(final ManagedChannel channel, final CdsProcessorHandler handler) {
66 this.channel = channel;
67 this.handler = handler;
71 * Sends a request to the CDS backend micro-service.
73 * <p>The caller will be returned a CountDownLatch that can be used to define how long the processing can wait. The
74 * CountDownLatch is initiated with just 1 count. When the client receives an #onCompleted callback, the counter
77 * <p>It is the user responsibility to close the client.</p>
79 * @param input request to send
80 * @return CountDownLatch instance that can be use to #await for completeness of processing
82 public CountDownLatch sendRequest(ExecutionServiceInput input) {
83 return handler.process(input, channel);
88 if (channel != null) {
91 LOGGER.info("CdsProcessorListener stopped");