diff options
author | subhash kumar singh <subhash.kumar.singh@huawei.com> | 2019-02-28 10:02:37 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-02-28 10:02:37 +0000 |
commit | adfc77d913cb8e54750d8fe167fde2bb4ffc57ef (patch) | |
tree | 91d238a78898420633991803b66db792b7d1a254 /grpc | |
parent | c5bd732f37c9b8b06c802cfe38dcf9ec64228341 (diff) | |
parent | 73747e103b5ce41abe253af5d9b55994375eaa86 (diff) |
Merge changes I60285973,I612ecfe2
* changes:
CVC: Add support for execution, schema, product
CMD: Enhace command profile with additional macros
Diffstat (limited to 'grpc')
5 files changed, 197 insertions, 75 deletions
diff --git a/grpc/grpc-client/pom.xml b/grpc/grpc-client/pom.xml index 37c02761..15e4f5b0 100644 --- a/grpc/grpc-client/pom.xml +++ b/grpc/grpc-client/pom.xml @@ -29,6 +29,11 @@ <artifactId>oclip-grpc-stub</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.7.16</version> + </dependency> </dependencies> <build> <plugins> 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(); + } } } diff --git a/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java index 05e3f675..45a64084 100644 --- a/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java +++ b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java @@ -17,15 +17,22 @@ package org.open.infc.grpc.server; import java.io.IOException; +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; -import java.util.logging.Logger; +import java.util.Set; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResultType; import org.onap.cli.fw.registrar.OnapCommandRegistrar; +import org.onap.cli.fw.store.OnapCommandExecutionStore; +import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext; import org.onap.cli.main.OnapCli; import org.open.infc.grpc.Args; import org.open.infc.grpc.Input; @@ -33,6 +40,10 @@ import org.open.infc.grpc.OpenInterfaceGrpc; import org.open.infc.grpc.Output; import org.open.infc.grpc.Output.Builder; import org.open.infc.grpc.Result; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; import io.grpc.Server; import io.grpc.ServerBuilder; @@ -40,7 +51,7 @@ import io.grpc.stub.StreamObserver; public class OpenInterfaceGrpcServer { - private static final Logger logger = Logger.getLogger(OpenInterfaceGrpcServer.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(OpenInterfaceGrpcServer.class.getName()); private static final String CONF_FILE = "oclip-grpc-server.properties"; private static final String CONF_SERVER_PORT = "oclip.grpc_server_port"; @@ -50,14 +61,22 @@ public class OpenInterfaceGrpcServer { } private Server server; - private void start() throws IOException { + private void start(String portArg) throws IOException { /* The port on which the server should run */ - int port = Integer.parseInt(OnapCommandConfig.getPropertyValue(CONF_SERVER_PORT)); + int port = Integer.parseInt(portArg == null ? OnapCommandConfig.getPropertyValue(CONF_SERVER_PORT) : portArg); server = ServerBuilder.forPort(port) .addService(new OpenInterfaceGrpcImpl()) .build() .start(); logger.info("Server started, listening on " + port); + + try { + OnapCommandRegistrar.getRegistrar().setHost(InetAddress.getLocalHost().getHostAddress().trim()); + OnapCommandRegistrar.getRegistrar().setPort(port); + } catch (OnapCommandException e) { + //Never Occurs + } + Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { @@ -89,15 +108,16 @@ public class OpenInterfaceGrpcServer { */ public static void main(String[] args) throws IOException, InterruptedException { final OpenInterfaceGrpcServer server = new OpenInterfaceGrpcServer(); - server.start(); + server.start(args.length ==1 ? args[0] : null); server.blockUntilShutdown(); } static class OpenRemoteCli extends OnapCli { - private String outputs = ""; - public OpenRemoteCli(String product, String[] args) { - super(product, args); - } + public OpenRemoteCli(String[] args) { + super(args); + } + + private String outputs = ""; public void print(String msg) { outputs += msg + "\n"; @@ -112,32 +132,126 @@ public class OpenInterfaceGrpcServer { @Override public void invoke(Input req, StreamObserver<Output> responseObserver) { - Builder reply = Output.newBuilder(); + Output output = null; logger.info(req.toString()); - String product = req.getOptionsMap().get(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME); + String product = req.getOptionsMap().get(OnapCommandConstants.RPC_PRODUCT); String format = req.getOptionsMap().getOrDefault(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT, OnapCommandResultType.JSON.name().toLowerCase()); - String command = req.getAction(); + String commandName = req.getAction(); + String profile = req.getOptionsMap().get(OnapCommandConstants.RPC_PROFILE); + OnapCommand cmd = null; + ExecutionStoreContext executionStoreContext = null; try { - OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get(command, product); + cmd = OnapCommandRegistrar.getRegistrar().get(commandName, product); cmd.getParametersMap().get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT).setValue(format); - for (Entry<String, String> arg: req.getParams().entrySet()) { - cmd.getParametersMap().get(arg.getKey()).setValue(arg.getValue()); + + if (!cmd.isRpc()) { + if (profile != null) { + //Set the profile to current one + OnapCommandRegistrar.getRegistrar().setProfile( + profile, + new ArrayList<String>(), + new ArrayList<String>()); + + //fill from profile + for (OnapCommandParameter param: cmd.getParameters()) { + Map<String, String> cache= OnapCommandRegistrar.getRegistrar().getParamCache(product); + if (cache.containsKey( + cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption())) { + param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get( + cmd.getInfo().getService() + ":" + cmd.getName() + ":" + param.getLongOption())); + } else if (cache.containsKey( + cmd.getInfo().getService() + ":" + param.getLongOption())) { + param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get( + cmd.getInfo().getService() + ":" + param.getLongOption())); + } else if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey(param.getLongOption())) { + param.setValue(OnapCommandRegistrar.getRegistrar().getParamCache().get(param.getLongOption())); + } + } + } + + Set <String> params = cmd.getParametersMap().keySet(); + for (Entry<String, String> arg: req.getParamsMap().entrySet()) { + if (params.contains(arg.getKey())) + cmd.getParametersMap().get(arg.getKey()).setValue(arg.getValue()); + } + } else { + cmd.getParametersMap().get(OnapCommandConstants.INFO_PRODUCT).setValue(product); + + if (profile != null) + cmd.getParametersMap().get(OnapCommandConstants.RPC_PROFILE).setValue(profile); + + cmd.getParametersMap().get(OnapCommandConstants.RPC_CMD).setValue(commandName); + cmd.getParametersMap().get(OnapCommandConstants.RPC_ARGS).setValue(req.getParamsMap()); + cmd.getParametersMap().get(OnapCommandConstants.RPC_MODE).setValue(OnapCommandConstants.RPC_MODE_RUN_RPC); } + + if (!cmd.isRpc()) { + //Start the execution + if (req.getRequestId() != null) { + String input = cmd.getArgsJson(true); + executionStoreContext = OnapCommandExecutionStore.getStore().storeExectutionStart( + req.getRequestId(), + cmd.getInfo().getProduct(), + cmd.getInfo().getService(), + cmd.getName(), + profile, + input); + } + } + cmd.execute(); - reply.putAttrs(OnapCommandConstants.RESULTS, cmd.getResult().print()); - reply.setSuccess(true); - reply.putAttrs(OnapCommandConstants.ERROR, "{}"); + if (!cmd.isRpc()) { + String printOut = cmd.getResult().print(); + Builder reply = Output.newBuilder(); + reply.setSuccess(true); + reply.putAttrs(OnapCommandConstants.ERROR, "{}"); + reply.putAddons("execution-id", executionStoreContext.getExecutionId()); + try { + reply.putAttrs(OnapCommandConstants.RESULTS, new ObjectMapper().readTree(printOut).toString()); + } catch (IOException e) { + reply.putAttrs(OnapCommandConstants.RESULTS, printOut); + } + + output = reply.build(); + + if (req.getRequestId() != null) { + //complete the execution recording + OnapCommandExecutionStore.getStore().storeExectutionEnd( + executionStoreContext, + printOut, + null, + cmd.getResult().isPassed()); + } + logger.info(output.toString()); + } else { + //Rpc command will set the output. + output = (Output) cmd.getResult().getOutput(); + } + } catch (OnapCommandException e) { logger.info(e.getMessage()); + + + Builder reply = Output.newBuilder(); reply.putAttrs(OnapCommandConstants.RESULTS, "{}"); reply.setSuccess(false); reply.putAttrs(OnapCommandConstants.ERROR, e.toJsonString()); + if (executionStoreContext != null) { + OnapCommandExecutionStore.getStore().storeExectutionEnd( + executionStoreContext, + null, + e.getMessage(), + false); + reply.putAddons("execution-id", executionStoreContext.getExecutionId()); + } + + output = reply.build(); } - responseObserver.onNext(reply.build()); + responseObserver.onNext(output); responseObserver.onCompleted(); } @@ -145,7 +259,14 @@ public class OpenInterfaceGrpcServer { public void remoteCli(Args req, StreamObserver<Result> responseObserver) { logger.info(req.toString()); - OpenRemoteCli cli = new OpenRemoteCli(req.getProduct(), req.getArgsList().toArray(new String [] {})); + List<String> args = new ArrayList<>(); + if (req.getRequestId() != null) { + args.add(OnapCommandParameter.printLongOption(OnapCommandConstants.RPC_REQID)); + args.add(req.getRequestId()); + } + + args.addAll(req.getArgsList()); + OpenRemoteCli cli = new OpenRemoteCli(args.toArray(new String [] {})); cli.handle(); logger.info(cli.getResult()); Result reply = Result.newBuilder().setExitCode(cli.getExitCode()).setOutput(cli.getResult()).build(); diff --git a/grpc/grpc-stub/src/main/proto/oclip.proto b/grpc/grpc-stub/src/main/proto/oclip.proto index 03224823..b4e77a3d 100644 --- a/grpc/grpc-stub/src/main/proto/oclip.proto +++ b/grpc/grpc-stub/src/main/proto/oclip.proto @@ -43,8 +43,9 @@ message Output { //Used for remote CLI message Args{ - repeated string args = 1; - string product = 2; + string requestId = 1; + string action = 2; + repeated string args = 3; } message Result { |