f922f9ec5edd93d55230a53a0045d1a92eafff36
[ccsdk/sli/adaptors.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada.
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 package org.onap.ccsdk.sli.adaptors.grpc.cds;
17
18 import io.grpc.ManagedChannel;
19 import io.grpc.internal.DnsNameResolverProvider;
20 import io.grpc.internal.PickFirstLoadBalancerProvider;
21 import io.grpc.netty.NettyChannelBuilder;
22 import java.util.Map;
23 import org.onap.ccsdk.sli.adaptors.grpc.GrpcClient;
24 import org.onap.ccsdk.sli.adaptors.grpc.GrpcProperties;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class BlueprintProcessingClient implements GrpcClient {
31
32     private static final Logger log = LoggerFactory.getLogger(BlueprintProcessingClient.class);
33
34     private ManagedChannel channel;
35     private BlueprintProcessingHandler handler;
36
37     public BlueprintProcessingClient(GrpcProperties props) {
38         this.channel = NettyChannelBuilder
39             .forAddress(props.getUrl(), props.getPort())
40             .nameResolverFactory(new DnsNameResolverProvider())
41             .loadBalancerFactory(new PickFirstLoadBalancerProvider())
42             .intercept(new BasicAuthClientInterceptor(props))
43             .usePlaintext()
44             .build();
45         this.handler = new BlueprintProcessingHandler();
46     }
47
48     public BlueprintProcessingClient(ManagedChannel channel, BlueprintProcessingHandler handler) {
49         this.channel = channel;
50         this.handler = handler;
51     }
52
53     // Used by blueprint
54     public void start() {
55         log.info("BlueprintProcessingClient started");
56     }
57
58     // Used by blueprint
59     public void stop() {
60         if (channel != null) {
61             channel.shutdown();
62         }
63         log.info("BlueprintProcessingClient stopped");
64     }
65
66     /*
67      * @param parameters HashMap<String,String> of parameters passed by the DG to this function.
68      * <table border="1">
69      * <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
70      * <tbody>
71      * <tr><td>is_force</td><td>Optional</td><td>Whether to force or not the request.</td></tr>
72      * <tr><td>ttl</td><td>Optional</td><td>Duration of the request.</td></tr>
73      * <tr><td>blueprint_name</td><td>Mandatory</td><td>Name of the blueprint to process.</td></tr>
74      * <tr><td>blueprint_version</td><td>Mandatory</td><td>Version of the blueprint to process.</td></tr>
75      * <tr><td>action</td><td>Mandatory</td><td>Action of the blueprint to process.</td></tr>
76      * <tr><td>mode</td><td>Mandatory</td><td>Mode to operate the transaction.</td></tr>
77      * <tr><td>payload</td><td>Mandatory</td><td>Payload.</td></tr>
78      * <tr><td>prefix</td><td>Mandatory</td><td>Prefix string to put response in context.</td></tr>
79      * </tbody>
80      * </table>
81      */
82     @Override
83     public QueryStatus sendRequest(Map<String, String> parameters, SvcLogicContext ctx) {
84         return handler.process(parameters, channel, ctx);
85     }
86 }