aboutsummaryrefslogtreecommitdiffstats
path: root/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java
blob: 405013be465ce9e6b614f1b856c74dad7e018311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 *  Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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 jakarta.websocket.DeploymentException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Simple console application with main method.
 *
 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
 */
public final class WsClientMain {
    // Get a reference to the logger
    private static final Logger LOGGER = LoggerFactory.getLogger(WsClientMain.class);

    // String constants
    private static final String APP_NAME = "ws-client";
    private static final String APP_DESCRIPTION = "takes events from stdin and sends via WS to APEX"
                    + " and/or receives events from APEX via WS and prints them to standard out";

    /**
     * Run the command.
     *
     * @param args the command line arguments
     * @param outStream stream for output
     */
    WsClientMain(final String[] args, final PrintStream outStream) {
        var console = false;

        final var 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);

        // help is an exit option, print usage and exit
        if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) {
            outStream.println(getHelpString(cli));
            outStream.println();
            return;
        }

        if (cmd.hasOption('c') || cmd.hasOption("console")) {
            console = true;
        }

        // version is an exit option, print version and exit
        if (cmd.hasOption('v') || cmd.hasOption("version")) {
            outStream.println(APP_NAME + " " + cli.getAppVersion());
            outStream.println();
            return;
        }

        runConsoleOrEcho(console, cmd, outStream);
    }

    /**
     * Run the console or echo.
     *
     * @param console if true, run the console otherwise run echo
     * @param cmd the command line to run
     * @param outStream stream for output
     */
    private static void runConsoleOrEcho(final boolean console, final CommandLine cmd, final PrintStream outStream) {
        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, outStream);
        } else {
            runEcho(server, port, outStream);
        }
    }

    /**
     * Runs the simple echo client.
     *
     * @param server the server, must not be blank
     * @param port the port, must not be blank
     * @param outStream stream for output
     */
    public static void runEcho(final String server, final String port, final PrintStream outStream) {
        Validate.notBlank(server);
        Validate.notBlank(port);

        outStream.println();
        outStream.println(APP_NAME + ": starting simple event echo");
        outStream.println(" --> server: " + server);
        outStream.println(" --> port: " + port);
        outStream.println();
        outStream.println("Once started, the application will simply print out all received events to standard out.");
        outStream.println("Each received event will be prefixed by '---' and suffixed by '===='");
        outStream.println();
        outStream.println();

        try {
            new SimpleEcho(server, port, APP_NAME, outStream, outStream);
        } catch (final URISyntaxException uex) {
            String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
            outStream.println(message);
            LOGGER.warn(message, uex);
        } catch (final NullPointerException nex) {
            String message = APP_NAME + ": null pointer, server or port were null";
            outStream.println(message);
            LOGGER.warn(message, nex);
        } catch (final IllegalArgumentException iex) {
            String message = APP_NAME + ": illegal argument, server or port were blank";
            outStream.println(message);
            LOGGER.warn(message, iex);
        } catch (DeploymentException dex) {
            String message = APP_NAME + ": could not deploy client";
            outStream.println(message);
            LOGGER.warn(message, dex);
        } catch (IOException iox) {
            String message = APP_NAME + ": illegal argument, IO execption on client";
            outStream.println(message);
            LOGGER.warn(message, iox);
        } catch (Exception xex) {
            String message = APP_NAME + ": Unknown execption on client";
            outStream.println(message);
            LOGGER.warn(message, xex);
        }
    }

    /**
     * Runs the simple console.
     *
     * @param server the server, must not be blank
     * @param port the port, must not be blank
     * @param outStream stream for output
     */
    public static void runConsole(final String server, final String port, final PrintStream outStream) {
        Validate.notBlank(server);
        Validate.notBlank(port);
        Validate.notBlank(APP_NAME);

        outStream.println();
        outStream.println(APP_NAME + ": starting simple event console");
        outStream.println(" --> server: " + server);
        outStream.println(" --> port: " + port);
        outStream.println();
        outStream.println(" - terminate the application typing 'exit<enter>' or using 'CTRL+C'");
        outStream.println(" - events are created by a non-blank starting line and terminated by a blank line");
        outStream.println();
        outStream.println();

        try {
            final var simpleConsole = new SimpleConsole(server, port, APP_NAME, outStream, outStream);
            simpleConsole.runClient();
        } catch (final URISyntaxException uex) {
            String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
            outStream.println(message);
            LOGGER.warn(message, uex);
        } catch (final NullPointerException nex) {
            String message = APP_NAME + ": null pointer, server or port were null";
            outStream.println(message);
            LOGGER.warn(message, nex);
        } catch (final IllegalArgumentException iex) {
            String message = APP_NAME + ": illegal argument, server or port were blank";
            outStream.println(message);
            LOGGER.warn(message, iex);
        } catch (final NotYetConnectedException nex) {
            String message = APP_NAME + ": not yet connected, connection to server took too long";
            outStream.println(message);
            LOGGER.warn(message, nex);
        } catch (DeploymentException dex) {
            String message = APP_NAME + ": could not deploy client";
            outStream.println(message);
            LOGGER.warn(message, dex);
        } catch (final IOException ioe) {
            String message = APP_NAME + ": IO exception, something went wrong on the standard input";
            outStream.println(message);
            LOGGER.warn(message, ioe);
        } catch (Exception xex) {
            String message = APP_NAME + ": Unknown execption on client";
            outStream.println(message);
            LOGGER.warn(message, xex);
        }
    }

    /**
     * Get the help string for the application.
     *
     * @param cli the command line options
     * @return the help string
     */
    private String getHelpString(final CliParser cli) {
        var formatter = new HelpFormatter();

        final var helpStringWriter = new StringWriter();
        final var helpPrintWriter = new PrintWriter(helpStringWriter);

        formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");

        return helpStringWriter.toString();
    }

    /**
     * The main method for the WS applications.
     *
     * @param args command line argument s
     */
    public static void main(final String[] args) {
        new WsClientMain(args, System.out);
    }
}