2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
26 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
27 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
28 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The Class WebSocketEventProducerClient.
37 public class WebSocketEventProducerClient implements WsStringMessageListener {
38 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerClient.class);
40 private final String host;
41 private final int port;
42 private final int eventCount;
43 private final boolean xmlEvents;
44 private final long eventInterval;
46 private long eventsSentCount = 0;
48 WsStringMessageClient client;
51 * Instantiates a new web socket event producer client.
53 * @param host the host
54 * @param port the port
55 * @param eventCount the event count
56 * @param xmlEvents the xml events
57 * @param eventInterval the event interval
58 * @throws MessagingException the messaging exception
60 public WebSocketEventProducerClient(final String host, final int port, final int eventCount,
61 final boolean xmlEvents, final long eventInterval) throws MessagingException {
64 this.eventCount = eventCount;
65 this.xmlEvents = xmlEvents;
66 this.eventInterval = eventInterval;
68 client = new WsStringMessageClient(host, port);
71 LOGGER.debug("{}: host {}, port {}, event count {}, xmlEvents {}", WebSocketEventProducerClient.class.getName(),
72 host, port, eventCount, xmlEvents);
78 public void sendEvents() {
79 LOGGER.debug("{}: sending events on host {}, port {}, event count {}, xmlEvents {}",
80 WebSocketEventProducerClient.class.getName(), host, port, eventCount, xmlEvents);
82 for (int i = 0; i < eventCount; i++) {
83 LOGGER.debug("{}: waiting {} milliseconds before sending next event",
84 WebSocketEventProducerClient.class.getName(), eventInterval);
85 ThreadUtilities.sleep(eventInterval);
87 String eventString = null;
89 eventString = EventGenerator.xmlEvent();
91 eventString = EventGenerator.jsonEvent();
93 client.sendString(eventString);
95 LOGGER.debug("{}: host {}, port {}, sent event {}", WebSocketEventProducerClient.class.getName(), host,
98 LOGGER.debug("{}: completed", WebSocketEventProducerClient.class.getName());
104 public void shutdown() {
106 LOGGER.debug("{}: stopped", WebSocketEventProducerClient.class.getName());
113 public void receiveString(final String eventString) {
114 LOGGER.debug("{}: host {}, port {}, received event {}", WebSocketEventProducerServer.class.getName(), host,
121 * @param args the arguments
122 * @throws MessagingException the messaging exception
124 public static void main(final String[] args) throws MessagingException {
125 if (args.length != 5) {
126 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
132 port = Integer.parseInt(args[1]);
133 } catch (final Exception e) {
134 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
141 eventCount = Integer.parseInt(args[2]);
142 } catch (final Exception e) {
143 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
148 long eventInterval = 0;
150 eventInterval = Long.parseLong(args[4]);
151 } catch (final Exception e) {
152 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
157 boolean xmlEvents = false;
158 if (args[3].equalsIgnoreCase("XML")) {
160 } else if (!args[3].equalsIgnoreCase("JSON")) {
161 LOGGER.error("usage WebSocketEventProducerClient host port #events XML|JSON eventInterval");
165 final WebSocketEventProducerClient client = new WebSocketEventProducerClient(args[0], port, eventCount,
166 xmlEvents, eventInterval);