2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.websocket;
24 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
27 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
28 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The Class WebSocketEventProducerServer.
35 public class WebSocketEventProducerServer implements WsStringMessageListener {
36 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerServer.class);
38 private final int port;
39 private final int eventCount;
40 private final boolean xmlEvents;
41 private final long eventInterval;
42 private long eventsSentCount = 0;
44 WsStringMessageServer server;
47 * Instantiates a new web socket event producer server.
49 * @param port the port
50 * @param eventCount the event count
51 * @param xmlEvents the xml events
52 * @param eventInterval the event interval
53 * @throws MessagingException the messaging exception
55 public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
56 final long eventInterval) throws MessagingException {
58 this.eventCount = eventCount;
59 this.xmlEvents = xmlEvents;
60 this.eventInterval = eventInterval;
62 server = new WsStringMessageServer(port);
65 LOGGER.debug("{}: port {}, event count {}, xmlEvents {}", WebSocketEventProducerServer.class.getName(), port,
66 eventCount, xmlEvents);
72 public void sendEvents() {
73 LOGGER.debug("{}: sending events on port {}, event count {}, xmlEvents {}",
74 WebSocketEventProducerServer.class.getName(), port, eventCount, xmlEvents);
76 for (int i = 0; i < eventCount; i++) {
77 LOGGER.debug("{}: waiting {} milliseconds before sending next event",
78 WebSocketEventProducerServer.class.getName(), eventInterval);
79 ThreadUtilities.sleep(eventInterval);
81 String eventString = null;
83 eventString = EventGenerator.xmlEvent();
85 eventString = EventGenerator.jsonEvent();
87 server.sendString(eventString);
89 LOGGER.debug("{}: port {}, sent event {}", WebSocketEventProducerServer.class.getName(), port, eventString);
92 LOGGER.debug("{}: event sending completed", WebSocketEventProducerServer.class.getName());
96 * Gets the events sent count.
98 * @return the events sent count
100 public long getEventsSentCount() {
101 return eventsSentCount;
107 public void shutdown() {
109 LOGGER.debug("{}: stopped", WebSocketEventProducerServer.class.getName());
116 public void receiveString(final String eventString) {
117 LOGGER.debug("{}: port {}, received event {}", WebSocketEventProducerServer.class.getName(), port, eventString);
123 * @param args the arguments
124 * @throws MessagingException the messaging exception
126 public static void main(final String[] args) throws MessagingException {
127 if (args.length != 4) {
128 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
134 port = Integer.parseInt(args[0]);
135 } catch (final Exception e) {
136 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
143 eventCount = Integer.parseInt(args[1]);
144 } catch (final Exception e) {
145 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
150 long eventInterval = 0;
152 eventInterval = Long.parseLong(args[3]);
153 } catch (final Exception e) {
154 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
159 boolean xmlEvents = false;
160 if (args[2].equalsIgnoreCase("XML")) {
162 } else if (!args[2].equalsIgnoreCase("JSON")) {
163 LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
167 final WebSocketEventProducerServer server = new WebSocketEventProducerServer(port, eventCount, xmlEvents,