Code improvement for pending sonar issues
[cli.git] / grpc / grpc-client / src / main / java / org / open / infc / grpc / client / OpenInterfaceGrpcClient.java
1 /*
2  * Copyright 2018 Huawei Technologies Co., Ltd.
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
17
18 package org.open.infc.grpc.client;
19
20 import java.util.concurrent.TimeUnit;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import org.open.infc.grpc.Args;
25 import org.open.infc.grpc.Input;
26 import org.open.infc.grpc.OpenInterfaceGrpc;
27 import org.open.infc.grpc.Output;
28 import org.open.infc.grpc.Result;
29
30 import io.grpc.ManagedChannel;
31 import io.grpc.ManagedChannelBuilder;
32 import io.grpc.StatusRuntimeException;
33
34 public class OpenInterfaceGrpcClient {
35       private static final Logger logger = LoggerFactory.getLogger(OpenInterfaceGrpcClient.class.getName());
36
37       private final ManagedChannel channel;
38       private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub;
39
40       //10 seconds
41       private int timeout = 60000;
42
43       public static class OpenInterfaceGrpcExecption extends Exception {
44           private static final long serialVersionUID = -8755636432217894246L;
45
46           private int errorCode = -1; //NOSONAR
47           public OpenInterfaceGrpcExecption(int errorCode, String message) {
48               super(message);
49               this.errorCode = errorCode;
50           }
51       }
52
53       public static class OpenInterfaceGrpcTimeoutExecption extends OpenInterfaceGrpcExecption {
54           private static final int ERROR_CODE = 1;
55
56           public OpenInterfaceGrpcTimeoutExecption(String message) {
57               super(ERROR_CODE, message);
58           }
59       }
60
61       public OpenInterfaceGrpcClient(String host, int port) {
62         this(ManagedChannelBuilder.forAddress(host, port)
63             // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
64             // needing certificates.
65             .usePlaintext(true)
66             .build());
67       }
68
69       public OpenInterfaceGrpcClient(String host, int port, int timeout) {
70           this(host, port);
71           this.timeout = timeout;
72
73       }
74       OpenInterfaceGrpcClient(ManagedChannel channel) {
75         this.channel = channel;
76         blockingStub = OpenInterfaceGrpc.newBlockingStub(channel);
77       }
78
79       public void shutdown() throws InterruptedException {
80         channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
81       }
82
83       public Output invoke(Input input) throws OpenInterfaceGrpcTimeoutExecption {
84         logger.info("Input {}", input);
85
86         Output result = Output.newBuilder().build();
87         try {
88             result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input);
89         } catch (StatusRuntimeException e) {
90           logger.warn("RPC failed: {}", e.getStatus());
91           //Status{code=DEADLINE_EXCEEDED}
92           throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
93         }
94         logger.info("Output: {}", result);
95         return result;
96       }
97
98       public Result remoteCli(Args args) throws OpenInterfaceGrpcTimeoutExecption {
99         logger.info(args.toString());
100
101         Result result = Result.newBuilder().setExitCode(1).build();
102         try {
103             result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args);
104         } catch (StatusRuntimeException e) {
105           logger.warn("RPC failed: {}", e.getStatus());
106           //Status{code=DEADLINE_EXCEEDED}
107           throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
108         }
109
110         logger.info("Result: {}", result);
111         return result;
112       }
113 }