Openstack adapter can't resolve HEAT parameter
[so.git] / common / src / main / java / org / onap / so / client / cds / CDSProcessingClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 Bell Canada.
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.cds;
22
23 import io.grpc.ManagedChannel;
24 import io.grpc.internal.DnsNameResolverProvider;
25 import io.grpc.internal.PickFirstLoadBalancerProvider;
26 import io.grpc.netty.NettyChannelBuilder;
27 import java.util.concurrent.CountDownLatch;
28 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput;
29 import org.onap.so.client.PreconditionFailedException;
30 import org.onap.so.client.RestPropertiesLoader;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * <p>
36  * The CDS processing client is using gRPC for communication between SO and CDS.
37  * That communication is configured to use a streaming approach, meaning that
38  * client can send an event to which server can reply will multiple sub-responses,
39  * until full completion of the processing.
40  * </p>
41  * <p>
42  * In order for the caller to manage the callback, it is the responsibility of the
43  * caller to implement and provide a {@link CDSProcessingListener} so received messages
44  * can be handled appropriately.
45  * </p>
46  *
47  * Here is an example of implementation of such listener:
48  * <pre>
49  * new CDSProcessingListener {
50  *
51  *     &#64;Override
52  *     public void onMessage(ExecutionServiceOutput message) {
53  *         log.info("Received notification from CDS: {}", message);
54  *     }
55  *
56  *     &#64;Override
57  *     public void onError(Throwable t) {
58  *         Status status = Status.fromThrowable(t);
59  *         log.error("Failed processing blueprint {}", status, t);
60  *     }
61  * }
62  * </pre>
63  */
64 public class CDSProcessingClient implements AutoCloseable {
65
66     private static final Logger log = LoggerFactory.getLogger(CDSProcessingClient.class);
67
68     private ManagedChannel channel;
69     private CDSProcessingHandler handler;
70
71     public CDSProcessingClient(final CDSProcessingListener listener) {
72         CDSProperties props = RestPropertiesLoader.getInstance().getNewImpl(CDSProperties.class);
73         if (props == null) {
74             throw new PreconditionFailedException(
75                 "No RestProperty.CDSProperties implementation found on classpath, can't create client.");
76         }
77         this.channel = NettyChannelBuilder
78             .forAddress(props.getHost(), props.getPort())
79             .nameResolverFactory(new DnsNameResolverProvider())
80             .loadBalancerFactory(new PickFirstLoadBalancerProvider())
81             .intercept(new BasicAuthClientInterceptor(props))
82             .usePlaintext()
83             .build();
84         this.handler = new CDSProcessingHandler(listener);
85         log.info("CDSProcessingClient started");
86     }
87
88     CDSProcessingClient(final ManagedChannel channel, final CDSProcessingHandler handler) {
89         this.channel = channel;
90         this.handler = handler;
91     }
92
93     /**
94      * Sends a request to the CDS backend micro-service.
95      *
96      * The caller will be returned a CountDownLatch that can be used
97      * to define how long the processing can wait. The CountDownLatch is
98      * initiated with just 1 count. When the client receives an #onCompleted callback,
99      * the counter will decrement.
100      *
101      * It is the user responsibility to close the client.
102      *
103      * @param input request to send
104      * @return CountDownLatch instance that can be use to #await for
105      * completeness of processing
106      */
107     public CountDownLatch sendRequest(ExecutionServiceInput input) {
108         return handler.process(input, channel);
109     }
110
111     @Override
112     public void close() {
113         if (channel != null) {
114             channel.shutdown();
115         }
116         log.info("CDSProcessingClient stopped");
117     }
118 }