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