diff options
Diffstat (limited to 'grpc/grpc-client/src/main')
-rw-r--r-- | grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java | 10 | ||||
-rw-r--r-- | grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java | 91 |
2 files changed, 48 insertions, 53 deletions
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 <product-name> <command-name> <command-arguments"); - System.out.println("NOTE: Set environment variable " + OCLIP_GRPC_SERVER_ENV + " to OCLIP gRPC server. By default its " + OCLIP_GRPC_SERVER); - System.exit(0); - } - - List<String> argList = new ArrayList<>(); - - for (String arg: args) { - argList.add(arg); - } - - //-P - argList.remove(0); - - //<product-name> - 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 <String> 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 <String, String> 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 <String, String> 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(); + } } } |