diff options
23 files changed, 897 insertions, 10 deletions
diff --git a/deployment/zip/pom.xml b/deployment/zip/pom.xml index eba890af..fa370ebb 100644 --- a/deployment/zip/pom.xml +++ b/deployment/zip/pom.xml @@ -93,6 +93,8 @@ "${project.build.directory}/../../../products/target/lib/") fileset(dir: "${project.build.directory}/../../../profiles/target/lib/") + fileset(dir: + "${project.build.directory}/../../../grpc/target/lib/") } ant.copy(todir: "${deployUnzip}/conf") { diff --git a/deployment/zip/src/main/release/bin/oclip.sh b/deployment/zip/src/main/release/bin/oclip.sh index 90fcfc53..a841c926 100755 --- a/deployment/zip/src/main/release/bin/oclip.sh +++ b/deployment/zip/src/main/release/bin/oclip.sh @@ -29,7 +29,7 @@ else SEP=: fi -CLASSPATH=${OPEN_CLI_HOME}${SEP}${OPEN_CLI_HOME}/conf${SEP}${OPEN_CLI_HOME}/docs +CLASSPATH=${OPEN_CLI_HOME}/conf${SEP}${OPEN_CLI_HOME}${SEP}${OPEN_CLI_HOME}/docs for entry in "$OPEN_CLI_HOME/lib"/* do CLASSPATH=${CLASSPATH}${SEP}${entry} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaListCommand.java new file mode 100644 index 00000000..bbd350b7 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapSchemaListCommand.java @@ -0,0 +1,58 @@ +/* + * 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.onap.cli.fw.cmd; + +import java.util.List; + +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.schema.OnapCommandSchema; +import org.onap.cli.fw.schema.OnapCommandSchemaInfo; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; + +/** + * Refresh external schema. + * + */ +@OnapCommandSchema(schema = "schema-list.yaml") +public class OnapSchemaListCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + + String product = getParametersMap().get("product").getValue().toString(); + + List<OnapCommandSchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(true); + int i = 0; + for (OnapCommandSchemaInfo schema : schemas) { + if (schema.isIgnore()) { + continue; + } + + if (schema.getProduct().equalsIgnoreCase(product)) { + i++; + + this.getResult().getRecordsMap().get("sr.no").getValues().add(String.valueOf(i)); + this.getResult().getRecordsMap().get("command").getValues().add(schema.getCmdName()); + this.getResult().getRecordsMap().get("schema").getValues().add(schema.getSchemaName()); + this.getResult().getRecordsMap().get("ocs-version").getValues().add(schema.getVersion()); + + this.getResult().getRecordsMap().get("type").getValues().add(schema.getSchemaProfile()); + } + } + } + +} diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java index fd1ddbaf..614ef5d1 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java +++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java @@ -248,7 +248,8 @@ public class OnapCommandPrint { JSONObject rowO = new JSONObject(); for (int j=0; j<titleRow.size(); j++) { - rowO.put(titleRow.get(j).toString(), rows.get(i).get(j).toString()); + if (rows.get(i).get(j) != null) + rowO.put(titleRow.get(j).toString(), rows.get(i).get(j).toString()); } array.add(rowO); diff --git a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java index 6ea1931a..07c2dbe1 100644 --- a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java @@ -121,6 +121,7 @@ public class OnapCommandRegistrar { } this.registry.put(name + ":" + version, cmd); + log.info("REGISITER : " + name + ":" + version + " = " + cmd.getCanonicalName()); this.availableProductVersions.add(version); } diff --git a/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand b/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand index 285f8f38..85294cbb 100644 --- a/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand +++ b/framework/src/main/resources/META-INF/services/org.onap.cli.fw.cmd.OnapCommand @@ -13,4 +13,5 @@ # limitations under the License. org.onap.cli.fw.cmd.OnapSchemaValidateCommand -org.onap.cli.fw.cmd.OnapSchemaRefreshCommand
\ No newline at end of file +org.onap.cli.fw.cmd.OnapSchemaRefreshCommand +org.onap.cli.fw.cmd.OnapSchemaListCommand
\ No newline at end of file diff --git a/framework/src/main/resources/open-cli-schema/schema-list.yaml b/framework/src/main/resources/open-cli-schema/schema-list.yaml new file mode 100644 index 00000000..44172d75 --- /dev/null +++ b/framework/src/main/resources/open-cli-schema/schema-list.yaml @@ -0,0 +1,55 @@ +# 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. + +open_cli_schema_version: 1.0 +name: schema-list +description: OCLIP command to list available schema + +info: + product: open-cli + service: schema + type: cmd + author: Kanagaraj Manickam kanagaraj.manickam@huawei.com + +parameters: + - name: product + type: string + description: For a given product version + short_option: l + long_option: product + is_optional: false + +results: + direction: landscape + attributes: + - name: sr.no + description: Serial Number + scope: short + type: string + - name: command + description: Command name + scope: short + type: string + - name: schema + description: Schema name + scope: short + type: string + - name: ocs-version + description: Schema version + scope: short + type: string + - name: type + description: Command type + scope: short + type: string
\ No newline at end of file diff --git a/framework/src/main/resources/open-cli-schema/schema-refresh.yaml b/framework/src/main/resources/open-cli-schema/schema-refresh.yaml index cc3f2300..1e448c9f 100644 --- a/framework/src/main/resources/open-cli-schema/schema-refresh.yaml +++ b/framework/src/main/resources/open-cli-schema/schema-refresh.yaml @@ -19,7 +19,7 @@ description: OCLIP command to refresh schemas stored in open-cli-schema folders. info: product: open-cli service: schema - type: auth + type: cmd author: Kanagaraj Manickam kanagaraj.manickam@huawei.com results: diff --git a/framework/src/main/resources/open-cli-schema/schema-validate.yaml b/framework/src/main/resources/open-cli-schema/schema-validate.yaml index a8a04941..97999dde 100644 --- a/framework/src/main/resources/open-cli-schema/schema-validate.yaml +++ b/framework/src/main/resources/open-cli-schema/schema-validate.yaml @@ -19,7 +19,7 @@ description: OCLIP command to validate schema info: product: open-cli service: schema - type: auth + type: cmd author: Kanagaraj Manickam kanagaraj.manickam@huawei.com parameters: diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java index c038e2dc..dd956b93 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java @@ -242,7 +242,7 @@ public class OnapCommandUtilsTest { @Test public void findOnapCommandsTest() { List<Class<OnapCommand>> cmds = OnapCommandDiscoveryUtils.discoverCommandPlugins(); - assertTrue(cmds.size() == 3); + assertTrue(cmds.size() == 4); } @Test diff --git a/grpc/grpc-client/pom.xml b/grpc/grpc-client/pom.xml new file mode 100644 index 00000000..93d2b361 --- /dev/null +++ b/grpc/grpc-client/pom.xml @@ -0,0 +1,53 @@ +<!-- + 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>oclip-grpc</artifactId> + <version>1.0.0</version> + </parent> + <artifactId>oclip-grpc-client</artifactId> + <name>oclip/grpc/client</name> + <packaging>jar</packaging> + <dependencies> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>oclip-grpc-stub</artifactId> + <version>1.0.0</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <addClasspath>true</addClasspath> + <mainClass>org.onap.cli.grpc.client.OpenRemoteCli</mainClass> + </manifest> + </archive> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file 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 diff --git a/grpc/grpc-server/pom.xml b/grpc/grpc-server/pom.xml new file mode 100644 index 00000000..412c5c90 --- /dev/null +++ b/grpc/grpc-server/pom.xml @@ -0,0 +1,58 @@ +<!-- + 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>oclip-grpc</artifactId> + <version>1.0.0</version> + </parent> + <artifactId>oclip-grpc-server</artifactId> + <name>oclip/grpc/server</name> + <packaging>jar</packaging> + <dependencies> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>oclip-grpc-stub</artifactId> + <version>1.0.0</version> + </dependency> + <dependency> + <groupId>org.onap.cli</groupId> + <artifactId>cli-main</artifactId> + <version>2.0.2</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <configuration> + <archive> + <manifest> + <addClasspath>true</addClasspath> + <mainClass>org.onap.cli.grpc.server.OpenInterfaceGrpcServer</mainClass> + </manifest> + </archive> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file 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 new file mode 100644 index 00000000..0f389f31 --- /dev/null +++ b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java @@ -0,0 +1,156 @@ +/* + * 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.server; + +import java.io.IOException; +import java.util.Map.Entry; +import java.util.logging.Logger; + +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.output.OnapCommandResultType; +import org.onap.cli.fw.registrar.OnapCommandRegistrar; +import org.onap.cli.main.OnapCli; +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.Output.Builder; +import org.open.infc.grpc.Result; + +import io.grpc.Server; +import io.grpc.ServerBuilder; +import io.grpc.stub.StreamObserver; + +public class OpenInterfaceGrpcServer { + + private static final Logger logger = Logger.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"; + + static { + OnapCommandConfig.addProperties(CONF_FILE); + } + private Server server; + + private void start() throws IOException { + /* The port on which the server should run */ + int port = Integer.parseInt(OnapCommandConfig.getPropertyValue(CONF_SERVER_PORT)); + server = ServerBuilder.forPort(port) + .addService(new OpenInterfaceGrpcImpl()) + .build() + .start(); + logger.info("Server started, listening on " + port); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + // Use stderr here since the logger may have been reset by its JVM shutdown hook. + System.err.println("*** shutting down gRPC server since JVM is shutting down"); + OpenInterfaceGrpcServer.this.stop(); + System.err.println("*** server shut down"); + } + }); + } + + private void stop() { + if (server != null) { + server.shutdown(); + } + } + + /** + * Await termination on the main thread since the grpc library uses daemon threads. + */ + private void blockUntilShutdown() throws InterruptedException { + if (server != null) { + server.awaitTermination(); + } + } + + /** + * Main launches the server from the command line. + */ + public static void main(String[] args) throws IOException, InterruptedException { + final OpenInterfaceGrpcServer server = new OpenInterfaceGrpcServer(); + server.start(); + server.blockUntilShutdown(); + } + + static class OpenRemoteCli extends OnapCli { + private String outputs = ""; + public OpenRemoteCli(String product, String[] args) { + super(product, args); + } + + public void print(String msg) { + outputs += msg + "\n"; + } + + public String getResult() { + return outputs; + } + } + + static class OpenInterfaceGrpcImpl extends OpenInterfaceGrpc.OpenInterfaceImplBase { + + @Override + public void invoke(Input req, StreamObserver<Output> responseObserver) { + Builder reply = Output.newBuilder(); + logger.info(req.toString()); + + String product = req.getOptionsMap().get(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME); + String format = req.getOptionsMap().getOrDefault(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT, OnapCommandResultType.JSON.name().toLowerCase()); + String command = req.getAction(); + + try { + OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get(command, 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()); + } + cmd.execute(); + + reply.putAttrs(OnapCommandConstants.RESULTS, cmd.getResult().print()); + reply.setSuccess(true); + reply.putAttrs(OnapCommandConstants.ERROR, "{}"); + } catch (OnapCommandException e) { + logger.info(e.getMessage()); + reply.putAttrs(OnapCommandConstants.RESULTS, "{}"); + reply.setSuccess(false); + reply.putAttrs(OnapCommandConstants.ERROR, e.toJsonString()); + } + + responseObserver.onNext(reply.build()); + responseObserver.onCompleted(); + } + + @Override + public void remoteCli(Args req, StreamObserver<Result> responseObserver) { + logger.info(req.toString()); + + OpenRemoteCli cli = new OpenRemoteCli(req.getProduct(), req.getArgsList().toArray(new String [] {})); + cli.handle(); + + Result reply = Result.newBuilder().setExitCode(cli.getExitCode()).setOutput(cli.getResult()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + } +} diff --git a/grpc/grpc-server/src/main/resources/log4j.properties b/grpc/grpc-server/src/main/resources/log4j.properties new file mode 100644 index 00000000..29a491cd --- /dev/null +++ b/grpc/grpc-server/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, stdout +log4j.logger.org.open.infc=DEBUG, file, stdout + +# 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 diff --git a/grpc/grpc-server/src/main/resources/oclip-grpc-server.properties b/grpc/grpc-server/src/main/resources/oclip-grpc-server.properties new file mode 100644 index 00000000..972fdebb --- /dev/null +++ b/grpc/grpc-server/src/main/resources/oclip-grpc-server.properties @@ -0,0 +1,15 @@ +# 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. + +oclip.grpc_server_port=50051
\ No newline at end of file diff --git a/grpc/grpc-stub/pom.xml b/grpc/grpc-stub/pom.xml new file mode 100644 index 00000000..9e13c8ec --- /dev/null +++ b/grpc/grpc-stub/pom.xml @@ -0,0 +1,34 @@ +<!-- + 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>oclip-grpc</artifactId> + <version>1.0.0</version> + </parent> + <artifactId>oclip-grpc-stub</artifactId> + <name>oclip/grpc/stub</name> + <packaging>jar</packaging> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file diff --git a/grpc/grpc-stub/src/main/proto/oclip.proto b/grpc/grpc-stub/src/main/proto/oclip.proto new file mode 100644 index 00000000..03224823 --- /dev/null +++ b/grpc/grpc-stub/src/main/proto/oclip.proto @@ -0,0 +1,53 @@ +// 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. + + syntax = "proto3"; + +option java_package = "org.open.infc.grpc"; +option java_multiple_files = true; +option java_outer_classname = "Oclip"; + +package oclip; + +service OpenInterface { + rpc invoke(Input) returns (Output) {} + rpc remoteCli(Args) returns (Result) {} +} + +//Used for java rpc +message Input { + string requestId = 1; + string action = 2; + map<string, string> params = 3; + map<string, string> options = 4; +} + +message Output { + bool success = 1; + map<string, string> attrs = 2; + + //if backend wants to report back some info like response header + map<string, string> addons = 3; +} + +//Used for remote CLI +message Args{ + repeated string args = 1; + string product = 2; +} + +message Result { + int32 exitCode = 1; + string output = 2; +}
\ No newline at end of file diff --git a/grpc/pom.xml b/grpc/pom.xml new file mode 100644 index 00000000..187aa03d --- /dev/null +++ b/grpc/pom.xml @@ -0,0 +1,169 @@ +<!-- + 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>cli</artifactId> + <version>2.0.2</version> + <relativePath>..</relativePath> + </parent> + <version>1.0.0</version> + <artifactId>oclip-grpc</artifactId> + <packaging>pom</packaging> + <name>oclip/grpc</name> + <modules> + <module>grpc-stub</module> + <module>grpc-server</module> + <module>grpc-client</module> + </modules> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <grpc.version>1.8.0</grpc.version> + </properties> + <dependencies> + <dependency> + <groupId>io.grpc</groupId> + <artifactId>grpc-netty</artifactId> + <version>${grpc.version}</version> + </dependency> + <dependency> + <groupId>io.grpc</groupId> + <artifactId>grpc-protobuf</artifactId> + <version>${grpc.version}</version> + </dependency> + <dependency> + <groupId>io.grpc</groupId> + <artifactId>grpc-stub</artifactId> + <version>${grpc.version}</version> + </dependency> + <dependency> + <groupId>io.grpc</groupId> + <artifactId>grpc-testing</artifactId> + <version>${grpc.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.google.api.grpc</groupId> + <artifactId>proto-google-common-protos</artifactId> + <version>0.1.9</version> + </dependency> + </dependencies> + <build> + <extensions> + <extension> + <groupId>kr.motd.maven</groupId> + <artifactId>os-maven-plugin</artifactId> + <version>1.5.0.Final</version> + </extension> + </extensions> + <plugins> + <plugin> + <groupId>org.xolstice.maven.plugins</groupId> + <artifactId>protobuf-maven-plugin</artifactId> + <version>0.5.0</version> + <configuration> + <protocArtifact>com.google.protobuf:protoc:3.4.0:exe:${os.detected.classifier}</protocArtifact> + <pluginId>grpc-java</pluginId> + <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact> + </configuration> + <executions> + <execution> + <goals> + <goal>compile</goal> + <goal>compile-custom</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-enforcer-plugin</artifactId> + <version>1.4.1</version> + <executions> + <execution> + <id>enforce</id> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <requireUpperBoundDeps/> + </rules> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy-artifact</id> + <phase>package</phase> + <goals> + <goal>copy</goal> + <goal>copy-dependencies</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>${project.groupId}</groupId> + <artifactId>${project.artifactId}</artifactId> + <version>${project.version}</version> + <type>${project.packaging}</type> + </artifactItem> + </artifactItems> + <outputDirectory>${project.build.directory}/lib</outputDirectory> + <overWriteReleases>false</overWriteReleases> + <overWriteSnapshots>false</overWriteSnapshots> + <overWriteIfNewer>true</overWriteIfNewer> + <excludeArtifactIds>junit,jmockit</excludeArtifactIds> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy-artifact</id> + <phase>package</phase> + <goals> + <goal>copy</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>${project.groupId}</groupId> + <artifactId>${project.artifactId}</artifactId> + <version>${project.version}</version> + <type>${project.packaging}</type> + </artifactItem> + </artifactItems> + <outputDirectory>../../grpc/target/lib</outputDirectory> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </pluginManagement> + </build> +</project>
\ No newline at end of file @@ -41,8 +41,9 @@ <module>profiles</module> <module>products</module> <module>main</module> - <module>deployment</module> <module>validate</module> + <module>grpc</module> + <module>deployment</module> </modules> <distributionManagement> diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java index 7b74e6ca..8969c2b6 100644 --- a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.conf.OnapCommandCmdConstants; @@ -29,9 +28,7 @@ import org.onap.cli.fw.cmd.schema.OnapCommandSchemaCmdLoader; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandExecutionFailed; import org.onap.cli.fw.input.OnapCommandParameter; -import org.onap.cli.fw.output.OnapCommandResultType; import org.onap.cli.fw.schema.OnapCommandSchema; -import org.onap.cli.fw.utils.OnapCommandUtils; /** * Hello world. |