aboutsummaryrefslogtreecommitdiffstats
path: root/tools/simple-wsclient/src/main
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@ericsson.com>2018-07-16 15:23:45 +0100
committerramverma <ram.krishna.verma@ericsson.com>2018-07-16 16:44:19 +0100
commit1eb5198939bd1d60665ff49e49763d0067c1bcb5 (patch)
tree1962eae390022de560624a546d8a72909f34893b /tools/simple-wsclient/src/main
parent773cff1147e756eb9be6d4a56dc5f70537f172fc (diff)
Adding tools module to apex-pdp
The tools module contains the utility applications for apex like converting policy model to cli, generating json events from policy model, simple websocket client example etc. The tools are defined to work in console and hence contain sysouts which are actually needed. Change-Id: Ia6d40fa7d3f6609ef86abd91a438d6ccbcd1f277 Issue-ID: POLICY-863 Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'tools/simple-wsclient/src/main')
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java181
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java145
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java108
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/package-info.java27
4 files changed, 461 insertions, 0 deletions
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java
new file mode 100644
index 000000000..bfd6573bc
--- /dev/null
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java
@@ -0,0 +1,181 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.tools.simple.wsclient;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.channels.NotYetConnectedException;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.lang3.Validate;
+import org.onap.policy.apex.tools.common.CliOptions;
+import org.onap.policy.apex.tools.common.CliParser;
+
+/**
+ * Simple console application with main method.
+ *
+ * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
+ */
+public final class Application {
+
+ /**
+ * Private constructor prevents subclassing.
+ */
+ private Application() {}
+
+ /**
+ * The main method for the WS applications.
+ *
+ * @param args command line argument s
+ */
+ public static void main(final String[] args) {
+ String appName = "ws-simple-echo";
+ String appDescr = "receives events from APEX via WS and prints them to standard out";
+ boolean console = false;
+
+ final CliParser cli = new CliParser();
+ cli.addOption(CliOptions.HELP);
+ cli.addOption(CliOptions.VERSION);
+ cli.addOption(CliOptions.CONSOLE);
+ cli.addOption(CliOptions.SERVER);
+ cli.addOption(CliOptions.PORT);
+
+ final CommandLine cmd = cli.parseCli(args);
+
+ if (cmd.hasOption('c') || cmd.hasOption("console")) {
+ appName = "ws-simple-console";
+ appDescr = "takes events from stdin and sends via WS to APEX";
+ console = true;
+ }
+
+ // help is an exit option, print usage and exit
+ if (cmd.hasOption('h') || cmd.hasOption("help")) {
+ final HelpFormatter formatter = new HelpFormatter();
+ System.out.println(appName + " v" + cli.getAppVersion() + " - " + appDescr);
+ formatter.printHelp(appName, cli.getOptions());
+ System.out.println();
+ return;
+ }
+
+ // version is an exit option, print version and exit
+ if (cmd.hasOption('v') || cmd.hasOption("version")) {
+ System.out.println(appName + " " + cli.getAppVersion());
+ System.out.println();
+ return;
+ }
+
+ String server = cmd.getOptionValue('s');
+ if (server == null) {
+ server = cmd.getOptionValue("server");
+ }
+ if (server == null) {
+ server = "localhost";
+ }
+
+ String port = cmd.getOptionValue('p');
+ if (port == null) {
+ port = cmd.getOptionValue("port");
+ }
+ if (port == null) {
+ port = "8887";
+ }
+
+ if (console) {
+ runConsole(server, port, appName);
+ } else {
+ runEcho(server, port, appName);
+ }
+
+ }
+
+ /**
+ * Runs the simple echo client.
+ *
+ * @param server the server, must not be blank
+ * @param port the port, must not be blank
+ * @param appName the application name, must not be blank
+ */
+ public static void runEcho(final String server, final String port, final String appName) {
+ Validate.notBlank(server);
+ Validate.notBlank(port);
+ Validate.notBlank(appName);
+
+ System.out.println();
+ System.out.println(appName + ": starting simple event echo");
+ System.out.println(" --> server: " + server);
+ System.out.println(" --> port: " + port);
+ System.out.println();
+ System.out.println("Once started, the application will simply print out all received events to standard out.");
+ System.out.println("Each received event will be prefixed by '---' and suffixed by '===='");
+ System.out.println();
+ System.out.println();
+
+ try {
+ final SimpleEcho simpleEcho = new SimpleEcho(server, port, appName);
+ simpleEcho.connect();
+ } catch (final URISyntaxException uex) {
+ System.err.println(appName + ": URI exception, could not create URI from server and port settings");
+ } catch (final NullPointerException nex) {
+ System.err.println(appName + ": null pointer, server or port were null");
+ } catch (final IllegalArgumentException iex) {
+ System.err.println(appName + ": illegal argument, server or port were blank");
+ }
+ }
+
+ /**
+ * Runs the simple console.
+ *
+ * @param server the server, must not be blank
+ * @param port the port, must not be blank
+ * @param appName the application name, must not be blank
+ */
+ public static void runConsole(final String server, final String port, final String appName) {
+ Validate.notBlank(server);
+ Validate.notBlank(port);
+ Validate.notBlank(appName);
+
+ System.out.println();
+ System.out.println(appName + ": starting simple event console");
+ System.out.println(" --> server: " + server);
+ System.out.println(" --> port: " + port);
+ System.out.println();
+ System.out.println(" - terminate the application typing 'exit<enter>' or using 'CTRL+C'");
+ System.out.println(" - events are created by a non-blank starting line and terminated by a blank line");
+ System.out.println();
+ System.out.println();
+
+ try {
+ final SimpleConsole simpleConsole = new SimpleConsole(server, port, appName);
+ simpleConsole.runClient();
+ } catch (final URISyntaxException uex) {
+ System.err.println(appName + ": URI exception, could not create URI from server and port settings");
+ } catch (final NullPointerException nex) {
+ System.err.println(appName + ": null pointer, server or port were null");
+ } catch (final IllegalArgumentException iex) {
+ System.err.println(appName + ": illegal argument, server or port were blank");
+ } catch (final NotYetConnectedException nex) {
+ System.err.println(appName + ": not yet connected, connection to server took too long");
+ } catch (final IOException ioe) {
+ System.err.println(appName + ": IO exception, something went wrong on the standard input");
+ }
+ }
+}
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
new file mode 100644
index 000000000..810c74da5
--- /dev/null
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
@@ -0,0 +1,145 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.tools.simple.wsclient;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.channels.NotYetConnectedException;
+
+import org.apache.commons.lang3.Validate;
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.framing.CloseFrame;
+import org.java_websocket.handshake.ServerHandshake;
+
+/**
+ * Simple WS client with a console for events.
+ *
+ * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
+ */
+public class SimpleConsole extends WebSocketClient {
+
+ /** Application name, used as prompt. */
+ private final String appName;
+
+ /**
+ * Creates a new simple echo object.
+ *
+ * @param server the name of the server as either IP address or fully qualified host name, must not be blank
+ * @param port the port to be used, must not be blank
+ * @param appName the application name, used as prompt, must not be blank
+ * @throws URISyntaxException is URI could not be created from server/port settings
+ * @throws RuntimeException if server or port where blank
+ */
+ public SimpleConsole(final String server, final String port, final String appName) throws URISyntaxException {
+ super(new URI("ws://" + server + ":" + port));
+ Validate.notBlank(appName, "SimpleConsole: given application name was blank");
+ this.appName = appName;
+ }
+
+ @Override
+ public void onClose(final int code, final String reason, final boolean remote) {
+ System.out.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
+ System.out.print(" ==-->> ");
+ switch (code) {
+ case CloseFrame.NORMAL:
+ System.out.println("normal");
+ break;
+ case CloseFrame.GOING_AWAY:
+ System.out.println("APEX going away");
+ break;
+ case CloseFrame.PROTOCOL_ERROR:
+ System.out.println("some protocol error");
+ break;
+ case CloseFrame.REFUSE:
+ System.out.println("received unacceptable type of data");
+ break;
+ case CloseFrame.NO_UTF8:
+ System.out.println("expected UTF-8, found something else");
+ break;
+ case CloseFrame.TOOBIG:
+ System.out.println("message too big");
+ break;
+ case CloseFrame.UNEXPECTED_CONDITION:
+ System.out.println("unexpected server condition");
+ break;
+ default:
+ System.out.println("unkown close frame code");
+ break;
+ }
+ System.out.print(" ==-->> " + reason);
+ }
+
+ @Override
+ public void onError(final Exception ex) {
+ System.err.println(this.appName + ": " + ex.getMessage());
+ }
+
+ @Override
+ public void onMessage(final String message) {
+ // this client does not expect messages
+ }
+
+ @Override
+ public void onOpen(final ServerHandshake handshakedata) {
+ System.out.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
+ }
+
+ /**
+ * Runs the console client. In particular, it starts a new thread for the Websocket connection and then reads from
+ * standard input.
+ *
+ * @throws NotYetConnectedException if not connected to server when sending events
+ * @throws IOException on an IO problem on standard in
+ */
+ public void runClient() throws NotYetConnectedException, IOException {
+ final Thread thread = new Thread() {
+ @Override
+ public void run() {
+ connect();
+ }
+ };
+ thread.start();
+
+ final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ String event = "";
+ String line;
+ while ((line = in.readLine()) != null) {
+ if (line.equals("exit")) {
+ break;
+ }
+
+ final String current = line.trim();
+ if ("".equals(current)) {
+ this.send(event);
+ event = "";
+ } else {
+ event += current;
+ }
+ }
+
+ thread.interrupt();
+ this.close(CloseFrame.NORMAL);
+ }
+
+}
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java
new file mode 100644
index 000000000..4b67cb862
--- /dev/null
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java
@@ -0,0 +1,108 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.tools.simple.wsclient;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.commons.lang3.Validate;
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.framing.CloseFrame;
+import org.java_websocket.handshake.ServerHandshake;
+
+/**
+ * Simple WS client as an echo.
+ *
+ * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
+ */
+public class SimpleEcho extends WebSocketClient {
+
+ /** Application name, used as prompt. */
+ private final String appName;
+
+ /**
+ * Creates a new simple echo object.
+ *
+ * @param server the name of the server as either IP address or fully qualified host name, must not be blank
+ * @param port the port to be used, must not be blank
+ * @param appName the application name, used as prompt, must not be blank
+ * @throws URISyntaxException is URI could not be created from server/port settings
+ * @throws RuntimeException if server or port where blank
+ */
+ public SimpleEcho(final String server, final String port, final String appName) throws URISyntaxException {
+ super(new URI("ws://" + server + ":" + port));
+ Validate.notBlank(appName, "SimpleEcho: given application name was blank");
+ this.appName = appName;
+ }
+
+ @Override
+ public void onClose(final int code, final String reason, final boolean remote) {
+ System.out.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
+ System.out.print(" ==-->> ");
+ switch (code) {
+ case CloseFrame.NORMAL:
+ System.out.println("normal");
+ break;
+ case CloseFrame.GOING_AWAY:
+ System.out.println("APEX going away");
+ break;
+ case CloseFrame.PROTOCOL_ERROR:
+ System.out.println("some protocol error");
+ break;
+ case CloseFrame.REFUSE:
+ System.out.println("received unacceptable type of data");
+ break;
+ case CloseFrame.NO_UTF8:
+ System.out.println("expected UTF-8, found something else");
+ break;
+ case CloseFrame.TOOBIG:
+ System.out.println("message too big");
+ break;
+ case CloseFrame.UNEXPECTED_CONDITION:
+ System.out.println("unexpected server condition");
+ break;
+ default:
+ System.out.println("unkown close frame code");
+ break;
+ }
+ System.out.print(" ==-->> " + reason);
+ }
+
+ @Override
+ public void onError(final Exception ex) {
+ System.err.println(this.appName + ": " + ex.getMessage());
+ }
+
+ @Override
+ public void onMessage(final String message) {
+ System.out.println(this.appName + ": received");
+ System.out.println("---------------------------------");
+ System.out.println(message);
+ System.out.println("=================================");
+ System.out.println();
+ }
+
+ @Override
+ public void onOpen(final ServerHandshake handshakedata) {
+ System.out.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
+ }
+
+}
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/package-info.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/package-info.java
new file mode 100644
index 000000000..ea30f93b5
--- /dev/null
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/package-info.java
@@ -0,0 +1,27 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+/**
+ * A simple Websocket client, runs as console or echo client.
+ *
+ * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
+ */
+
+package org.onap.policy.apex.tools.simple.wsclient;