From 73747e103b5ce41abe253af5d9b55994375eaa86 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Thu, 28 Feb 2019 14:32:18 +0530 Subject: CVC: Add support for execution, schema, product Issue-ID: CLI-129 Change-Id: I602859733b8f965ab031625eba6494a9b754cb64 Signed-off-by: Kanagaraj Manickam k00365106 --- .../infc/grpc/client/OpenInterfaceGrpcClient.java | 10 +-- .../org/open/infc/grpc/client/OpenRemoteCli.java | 91 ++++++++++------------ 2 files changed, 48 insertions(+), 53 deletions(-) (limited to 'grpc/grpc-client/src') 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 e61f54f6..2d9d32fc 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 @@ -18,8 +18,8 @@ package org.open.infc.grpc.client; import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.open.infc.grpc.Args; import org.open.infc.grpc.Input; @@ -32,7 +32,7 @@ import io.grpc.ManagedChannelBuilder; import io.grpc.StatusRuntimeException; public class OpenInterfaceGrpcClient { - private static final Logger logger = Logger.getLogger(OpenInterfaceGrpcClient.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(OpenInterfaceGrpcClient.class.getName()); private final ManagedChannel channel; private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub; @@ -61,7 +61,7 @@ public class OpenInterfaceGrpcClient { try { result = blockingStub.invoke(input); } catch (StatusRuntimeException e) { - logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + logger.warn("RPC failed: {0}", e.getStatus()); } logger.info("Output: " + result.toString()); return result; @@ -74,7 +74,7 @@ public class OpenInterfaceGrpcClient { try { result = blockingStub.remoteCli(args); } catch (StatusRuntimeException e) { - logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + logger.warn("RPC failed: {0}", e.getStatus()); } logger.info("Result: " + result.toString()); 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 0e9eba78..fc0c2aac 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 @@ -16,71 +16,66 @@ package org.open.infc.grpc.client; -import java.net.URL; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.logging.Logger; +import java.util.Map; import org.open.infc.grpc.Args; +import org.open.infc.grpc.Input; +import org.open.infc.grpc.Output; import org.open.infc.grpc.Result; public class OpenRemoteCli { - public static final String OCLIP_GRPC_SERVER = "http://localhost:50051"; - public static final String OCLIP_GRPC_SERVER_ENV = "OCLIP_GRPC_SERVER"; - - public static Result run (String[] args) throws Exception { - String oclipHome = System.getenv(OCLIP_GRPC_SERVER_ENV); - - if (oclipHome == null) { - oclipHome = OCLIP_GRPC_SERVER; - } - - if (System.getenv("OPEN_CLI_DEBUG") == null) { - Logger globalLogger = Logger.getLogger(OpenInterfaceGrpcClient.class.getName()); - globalLogger.setLevel(java.util.logging.Level.OFF); - } else { - System.out.println(OCLIP_GRPC_SERVER_ENV + "=" + oclipHome); - } - - if (args.length <= 2 || !args[0].equals("-P")) { - System.out.println("Usage: oclip -P argList = new ArrayList<>(); - - for (String arg: args) { - argList.add(arg); - } - - //-P - argList.remove(0); - - // - String product = argList.remove(0); - - URL oclipUrl = new URL(oclipHome); + /** + * Runs CLI remotely + * @param host + * @param port + * @param product + * @param cmd + * @param args + * @return + * @throws Exception + */ + public static Result run (String host, int port, String reqId, List args) throws Exception { OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( - oclipUrl.getHost(), oclipUrl.getPort()); + host, port); try { - Result result = client.remoteCli(Args.newBuilder().addAllArgs(argList).setProduct(product).build()); + Result result = client.remoteCli(Args.newBuilder().setRequestId(reqId).addAllArgs(args).build()); return result; } finally { client.shutdown(); } } + /** + * Runs commands as remote procedure call :) + * @param host + * @param port + * @param product + * @param action + * @param reqId + * @param params + * @return + * @throws Exception + */ + public static Output invoke (String host, int port, String product, String profile, String action, String reqId, Map params) throws Exception { + OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( + host, port); - public static void main(String[] args) throws Exception { - int exitCode = 1; try { - Result result = OpenRemoteCli.run(args); - System.out.println(result.getOutput()); - exitCode = result.getExitCode(); + + 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(reqId).putAllOptions(options).putAllParams(params).build(); + + Output output = client.invoke(input); + return output; } finally { - System.exit(exitCode); - } + client.shutdown(); + } } } -- cgit 1.2.3-korg