From b100c49fbd130e4fe58ce82f1e5f765f13917703 Mon Sep 17 00:00:00 2001 From: Kanagaraj M Date: Fri, 2 Aug 2019 14:10:30 +0530 Subject: Update framework Issue-ID: CLI-169 Change-Id: Ice38da575d2e349bb875149afd894e348bc20253 Signed-off-by: Kanagaraj Manickam k00365106 --- .../infc/grpc/client/OpenInterfaceGrpcClient.java | 12 +++++- .../org/open/infc/grpc/client/OpenRemoteCli.java | 47 +++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) (limited to 'grpc/grpc-client/src/main/java/org') diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java index 9a818d7a..e32110de 100644 --- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java @@ -37,6 +37,9 @@ public class OpenInterfaceGrpcClient { private final ManagedChannel channel; private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub; + //10 seconds + private int timeout = 60000; + public static class OpenInterfaceGrpcExecption extends Exception { private static final long serialVersionUID = -8755636432217894246L; @@ -64,6 +67,11 @@ public class OpenInterfaceGrpcClient { .build()); } + public OpenInterfaceGrpcClient(String host, int port, int timeout) { + this(host, port); + this.timeout = timeout; + + } OpenInterfaceGrpcClient(ManagedChannel channel) { this.channel = channel; blockingStub = OpenInterfaceGrpc.newBlockingStub(channel); @@ -78,7 +86,7 @@ public class OpenInterfaceGrpcClient { Output result = Output.newBuilder().build(); try { - result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).invoke(input); + result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input); } catch (StatusRuntimeException e) { logger.warn("RPC failed: {0}", e.getStatus()); //Status{code=DEADLINE_EXCEEDED} @@ -93,7 +101,7 @@ public class OpenInterfaceGrpcClient { Result result = Result.newBuilder().setExitCode(1).build(); try { - result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).remoteCli(args); + result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args); } catch (StatusRuntimeException e) { logger.warn("RPC failed: {0}", e.getStatus()); //Status{code=DEADLINE_EXCEEDED} diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java index fc0c2aac..724267e8 100644 --- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java @@ -26,6 +26,51 @@ import org.open.infc.grpc.Output; import org.open.infc.grpc.Result; public class OpenRemoteCli { + + private String host; + private int port; + private int timeout; + private String requestId; + + public OpenRemoteCli(String host, int port, int timeout, String requestId) { + this.host = host; + this.port = port; + this.timeout = timeout; + this.requestId = requestId; + + } + + public Result run (List args) throws Exception { + OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( + host, port, timeout); + try { + Result result = client.remoteCli(Args.newBuilder().setRequestId(this.requestId).addAllArgs(args).build()); + return result; + } finally { + client.shutdown(); + } + } + + public Output invoke (String product, String profile, String action, Map params) throws Exception { + OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( + host, port, timeout); + try { + + Map options = new HashMap<>(); + options.put("product", product); + if (profile != null && !profile.isEmpty()) + options.put("profile", profile); + params.put("format", "json"); + Input input = Input.newBuilder().setAction(action).setRequestId(requestId).putAllOptions(options).putAllParams(params).build(); + + Output output = client.invoke(input); + return output; + } finally { + client.shutdown(); + } + } + + //Absolute the static methods /** * Runs CLI remotely * @param host @@ -48,7 +93,7 @@ public class OpenRemoteCli { } } - /** + /** * Runs commands as remote procedure call :) * @param host * @param port -- cgit 1.2.3-korg