efc941f991e6a8cf60a43960027f178bfaa1b83b
[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;
47
48           public OpenInterfaceGrpcExecption(int errorCode, String message) {
49               super(message);
50               this.errorCode = errorCode;
51           }
52       }
53
54       public static class OpenInterfaceGrpcTimeoutExecption extends OpenInterfaceGrpcExecption {
55           private static int errorCode = 1;
56
57           public OpenInterfaceGrpcTimeoutExecption(String message) {
58               super(errorCode, message);
59           }
60       }
61
62       public OpenInterfaceGrpcClient(String host, int port) {
63         this(ManagedChannelBuilder.forAddress(host, port)
64             // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
65             // needing certificates.
66             .usePlaintext(true)
67             .build());
68       }
69
70       public OpenInterfaceGrpcClient(String host, int port, int timeout) {
71           this(host, port);
72           this.timeout = timeout;
73
74       }
75       OpenInterfaceGrpcClient(ManagedChannel channel) {
76         this.channel = channel;
77         blockingStub = OpenInterfaceGrpc.newBlockingStub(channel);
78       }
79
80       public void shutdown() throws InterruptedException {
81         channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
82       }
83
84       public Output invoke(Input input) throws OpenInterfaceGrpcTimeoutExecption {
85         logger.info("Input {}", input);
86
87         Output result = Output.newBuilder().build();
88         try {
89             result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input);
90         } catch (StatusRuntimeException e) {
91           logger.warn("RPC failed: {}", e.getStatus());
92           //Status{code=DEADLINE_EXCEEDED}
93           throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
94         }
95         logger.info("Output: {}", result);
96         return result;
97       }
98
99       public Result remoteCli(Args args) throws OpenInterfaceGrpcTimeoutExecption {
100         logger.info(args.toString());
101
102         Result result = Result.newBuilder().setExitCode(1).build();
103         try {
104             result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args);
105         } catch (StatusRuntimeException e) {
106           logger.warn("RPC failed: {}", e.getStatus());
107           //Status{code=DEADLINE_EXCEEDED}
108           throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
109         }
110
111         logger.info("Result: {}", result);
112         return result;
113       }
114 }