diff options
Diffstat (limited to 'grpc/grpc-client/src/main')
3 files changed, 201 insertions, 0 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 new file mode 100644 index 00000000..e61f54f6 --- /dev/null +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java @@ -0,0 +1,83 @@ +/* + * Copyright 2018 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.open.infc.grpc.client; + +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.open.infc.grpc.Args; +import org.open.infc.grpc.Input; +import org.open.infc.grpc.OpenInterfaceGrpc; +import org.open.infc.grpc.Output; +import org.open.infc.grpc.Result; + +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.StatusRuntimeException; + +public class OpenInterfaceGrpcClient { + private static final Logger logger = Logger.getLogger(OpenInterfaceGrpcClient.class.getName()); + + private final ManagedChannel channel; + private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub; + + public OpenInterfaceGrpcClient(String host, int port) { + this(ManagedChannelBuilder.forAddress(host, port) + // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid + // needing certificates. + .usePlaintext(true) + .build()); + } + + OpenInterfaceGrpcClient(ManagedChannel channel) { + this.channel = channel; + blockingStub = OpenInterfaceGrpc.newBlockingStub(channel); + } + + public void shutdown() throws InterruptedException { + channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); + } + + public Output invoke(Input input) { + logger.info("Input " + input.toString()); + + Output result = Output.newBuilder().build(); + try { + result = blockingStub.invoke(input); + } catch (StatusRuntimeException e) { + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + } + logger.info("Output: " + result.toString()); + return result; + } + + public Result remoteCli(Args args) { + logger.info(args.toString()); + + Result result = Result.newBuilder().setExitCode(1).build(); + try { + result = blockingStub.remoteCli(args); + } catch (StatusRuntimeException e) { + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + } + + logger.info("Result: " + result.toString()); + return result; + } +} 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 new file mode 100644 index 00000000..7d02c141 --- /dev/null +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java @@ -0,0 +1,86 @@ +/* + * Copyright 2018 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.open.infc.grpc.client; + +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +import org.open.infc.grpc.Args; +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_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); + OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient( + oclipUrl.getHost(), oclipUrl.getPort()); + + try { + Result result = client.remoteCli(Args.newBuilder().addAllArgs(argList).setProduct(product).build()); + return result; + } finally { + client.shutdown(); + } + } + + + 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(); + } finally { + System.exit(exitCode); + } + } +} diff --git a/grpc/grpc-client/src/main/resources/log4j.properties b/grpc/grpc-client/src/main/resources/log4j.properties new file mode 100644 index 00000000..778641ed --- /dev/null +++ b/grpc/grpc-client/src/main/resources/log4j.properties @@ -0,0 +1,32 @@ +# Copyright 2018 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +log4j.rootLogger=ERROR, file + +log4j.logger.org.onap.cli=DEBUG, file +log4j.logger.org.open.infc=DEBUG, file + +# Direct log messages to stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Redirect log messages to a log file, support file rolling. +log4j.appender.file=org.apache.log4j.RollingFileAppender +log4j.appender.file.File=${OPEN_CLI_HOME}/logs/open-cli.log +log4j.appender.file.MaxFileSize=5MB +log4j.appender.file.MaxBackupIndex=10 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n |