29a5de08f9a3f37eb6b42f150fdaf73af1e6f202
[policy/apex-pdp.git] / tools / simple-wsclient / src / main / java / org / onap / policy / apex / tools / simple / wsclient / WsClientMain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.simple.wsclient;
22
23 import java.io.IOException;
24 import java.io.PrintStream;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.net.URISyntaxException;
28 import java.nio.channels.NotYetConnectedException;
29
30 import org.apache.commons.cli.CommandLine;
31 import org.apache.commons.cli.HelpFormatter;
32 import org.apache.commons.lang3.Validate;
33 import org.onap.policy.apex.tools.common.CliOptions;
34 import org.onap.policy.apex.tools.common.CliParser;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Simple console application with main method.
40  *
41  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
42  */
43 public final class WsClientMain {
44     // Get a reference to the logger
45     private static final Logger LOGGER = LoggerFactory.getLogger(WsClientMain.class);
46
47     // String constants
48     private static final String APP_NAME = "ws-client";
49     private static final String APP_DESCRIPTION = "takes events from stdin and sends via WS to APEX"
50                     + " and/or receives events from APEX via WS and prints them to standard out";
51
52     /**
53      * Run the command.
54      * 
55      * @param args the command line arguments
56      * @param outStream stream for output
57      */
58     WsClientMain(final String[] args, final PrintStream outStream) {
59         boolean console = false;
60
61         final CliParser cli = new CliParser();
62         cli.addOption(CliOptions.HELP);
63         cli.addOption(CliOptions.VERSION);
64         cli.addOption(CliOptions.CONSOLE);
65         cli.addOption(CliOptions.SERVER);
66         cli.addOption(CliOptions.PORT);
67
68         final CommandLine cmd = cli.parseCli(args);
69
70         // help is an exit option, print usage and exit
71         if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) {
72             outStream.println(getHelpString(cli));
73             outStream.println();
74             return;
75         }
76
77         if (cmd.hasOption('c') || cmd.hasOption("console")) {
78             console = true;
79         }
80
81         // version is an exit option, print version and exit
82         if (cmd.hasOption('v') || cmd.hasOption("version")) {
83             outStream.println(APP_NAME + " " + cli.getAppVersion());
84             outStream.println();
85             return;
86         }
87
88         runConsoleOrEcho(console, cmd, outStream);
89     }
90
91     /**
92      * Run the console or echo.
93      * 
94      * @param console if true, run the console otherwise run echo
95      * @param cmd the command line to run
96      * @param outStream stream for output
97      */
98     private static void runConsoleOrEcho(final boolean console, final CommandLine cmd, final PrintStream outStream) {
99         String server = cmd.getOptionValue('s');
100         if (server == null) {
101             server = cmd.getOptionValue("server");
102         }
103         if (server == null) {
104             server = "localhost";
105         }
106
107         String port = cmd.getOptionValue('p');
108         if (port == null) {
109             port = cmd.getOptionValue("port");
110         }
111         if (port == null) {
112             port = "8887";
113         }
114
115         if (console) {
116             runConsole(server, port, outStream);
117         } else {
118             runEcho(server, port, outStream);
119         }
120     }
121
122     /**
123      * Runs the simple echo client.
124      *
125      * @param server the server, must not be blank
126      * @param port the port, must not be blank
127      * @param outStream stream for output
128      */
129     public static void runEcho(final String server, final String port, final PrintStream outStream) {
130         Validate.notBlank(server);
131         Validate.notBlank(port);
132
133         outStream.println();
134         outStream.println(APP_NAME + ": starting simple event echo");
135         outStream.println(" --> server: " + server);
136         outStream.println(" --> port: " + port);
137         outStream.println();
138         outStream.println("Once started, the application will simply print out all received events to standard out.");
139         outStream.println("Each received event will be prefixed by '---' and suffixed by '===='");
140         outStream.println();
141         outStream.println();
142
143         try {
144             final SimpleEcho simpleEcho = new SimpleEcho(server, port, APP_NAME, outStream, outStream);
145             simpleEcho.connect();
146         } catch (final URISyntaxException uex) {
147             String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
148             outStream.println(message);
149             LOGGER.warn(message, uex);
150         } catch (final NullPointerException nex) {
151             String message = APP_NAME + ": null pointer, server or port were null";
152             outStream.println(message);
153             LOGGER.warn(message, nex);
154         } catch (final IllegalArgumentException iex) {
155             String message = APP_NAME + ": illegal argument, server or port were blank";
156             outStream.println(message);
157             LOGGER.warn(message, iex);
158         }
159     }
160
161     /**
162      * Runs the simple console.
163      *
164      * @param server the server, must not be blank
165      * @param port the port, must not be blank
166      * @param outStream stream for output
167      */
168     public static void runConsole(final String server, final String port, final PrintStream outStream) {
169         Validate.notBlank(server);
170         Validate.notBlank(port);
171         Validate.notBlank(APP_NAME);
172
173         outStream.println();
174         outStream.println(APP_NAME + ": starting simple event console");
175         outStream.println(" --> server: " + server);
176         outStream.println(" --> port: " + port);
177         outStream.println();
178         outStream.println(" - terminate the application typing 'exit<enter>' or using 'CTRL+C'");
179         outStream.println(" - events are created by a non-blank starting line and terminated by a blank line");
180         outStream.println();
181         outStream.println();
182
183         try {
184             final SimpleConsole simpleConsole = new SimpleConsole(server, port, APP_NAME, outStream, outStream);
185             simpleConsole.runClient();
186         } catch (final URISyntaxException uex) {
187             String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
188             outStream.println(message);
189             LOGGER.warn(message, uex);
190         } catch (final NullPointerException nex) {
191             String message = APP_NAME + ": null pointer, server or port were null";
192             outStream.println(message);
193             LOGGER.warn(message, nex);
194         } catch (final IllegalArgumentException iex) {
195             String message = APP_NAME + ": illegal argument, server or port were blank";
196             outStream.println(message);
197             LOGGER.warn(message, iex);
198         } catch (final NotYetConnectedException nex) {
199             String message = APP_NAME + ": not yet connected, connection to server took too long";
200             outStream.println(message);
201             LOGGER.warn(message, nex);
202         } catch (final IOException ioe) {
203             String message = APP_NAME + ": IO exception, something went wrong on the standard input";
204             outStream.println(message);
205             LOGGER.warn(message, ioe);
206         }
207     }
208
209     /**
210      * Get the help string for the application.
211      * 
212      * @param cli the command line options
213      * @return the help string
214      */
215     private String getHelpString(final CliParser cli) {
216         HelpFormatter formatter = new HelpFormatter();
217
218         final StringWriter helpStringWriter = new StringWriter();
219         final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
220
221         formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
222
223         return helpStringWriter.toString();
224     }
225
226     /**
227      * The main method for the WS applications.
228      *
229      * @param args command line argument s
230      */
231     public static void main(final String[] args) {
232         new WsClientMain(args, System.out);
233     }
234 }