11438d9525bdf27f29d2117c5b10d8c62192e4f4
[policy/apex-pdp.git] /
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.testsuites.integration.uservice.adapt.websocket;
22
23 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
24 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
25 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
26 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
27 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The Class WebSocketEventProducerServer.
33  */
34 public class WebSocketEventProducerServer implements WsStringMessageListener {
35     private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEventProducerServer.class);
36
37     private final int port;
38     private final int eventCount;
39     private final boolean xmlEvents;
40     private final long eventInterval;
41     private long eventsSentCount = 0;
42
43     WsStringMessageServer server;
44
45     /**
46      * Instantiates a new web socket event producer server.
47      *
48      * @param port the port
49      * @param eventCount the event count
50      * @param xmlEvents the xml events
51      * @param eventInterval the event interval
52      * @throws MessagingException the messaging exception
53      */
54     public WebSocketEventProducerServer(final int port, final int eventCount, final boolean xmlEvents,
55                     final long eventInterval) throws MessagingException {
56         this.port = port;
57         this.eventCount = eventCount;
58         this.xmlEvents = xmlEvents;
59         this.eventInterval = eventInterval;
60
61         server = new WsStringMessageServer(port);
62         server.start(this);
63
64         LOGGER.debug("{}: port {}, event count {}, xmlEvents {}", WebSocketEventProducerServer.class.getCanonicalName(),
65                         port, eventCount, xmlEvents);
66     }
67
68     /**
69      * Send events.
70      */
71     public void sendEvents() {
72         LOGGER.debug("{}: sending events on port {}, event count {}, xmlEvents {}",
73                         WebSocketEventProducerServer.class.getCanonicalName(), port, eventCount, xmlEvents);
74
75         for (int i = 0; i < eventCount; i++) {
76             LOGGER.debug("{}: waiting {} milliseconds before sending next event",
77                             WebSocketEventProducerServer.class.getCanonicalName(), eventInterval);
78             ThreadUtilities.sleep(eventInterval);
79
80             String eventString = null;
81             if (xmlEvents) {
82                 eventString = EventGenerator.xmlEvent();
83             } else {
84                 eventString = EventGenerator.jsonEvent();
85             }
86             server.sendString(eventString);
87             eventsSentCount++;
88             LOGGER.debug("{}: port {}, sent event {}", WebSocketEventProducerServer.class.getCanonicalName(), port,
89                             eventString);
90         }
91
92         LOGGER.debug("{}: event sending completed", WebSocketEventProducerServer.class.getCanonicalName());
93     }
94
95     /**
96      * Gets the events sent count.
97      *
98      * @return the events sent count
99      */
100     public long getEventsSentCount() {
101         return eventsSentCount;
102     }
103
104     /**
105      * Shutdown.
106      */
107     public void shutdown() {
108         server.stop();
109         LOGGER.debug("{}: stopped", WebSocketEventProducerServer.class.getCanonicalName());
110     }
111
112     /**
113      * {@inheritDoc}.
114      */
115     @Override
116     public void receiveString(final String eventString) {
117         LOGGER.debug("{}: port {}, received event {}", WebSocketEventProducerServer.class.getCanonicalName(), port,
118                         eventString);
119     }
120
121     /**
122      * The main method.
123      *
124      * @param args the arguments
125      * @throws MessagingException the messaging exception
126      */
127     public static void main(final String[] args) throws MessagingException {
128         if (args.length != 4) {
129             LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
130             return;
131         }
132
133         int port = 0;
134         try {
135             port = Integer.parseInt(args[0]);
136         } catch (final Exception e) {
137             LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
138             e.printStackTrace();
139             return;
140         }
141
142         int eventCount = 0;
143         try {
144             eventCount = Integer.parseInt(args[1]);
145         } catch (final Exception e) {
146             LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
147             e.printStackTrace();
148             return;
149         }
150
151         long eventInterval = 0;
152         try {
153             eventInterval = Long.parseLong(args[3]);
154         } catch (final Exception e) {
155             LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON eventInterval");
156             e.printStackTrace();
157             return;
158         }
159
160         boolean xmlEvents = false;
161         if (args[2].equalsIgnoreCase("XML")) {
162             xmlEvents = true;
163         } else if (!args[2].equalsIgnoreCase("JSON")) {
164             LOGGER.error("usage WebSocketEventProducerServer port #events XML|JSON startDelay eventInterval");
165             return;
166         }
167
168         final WebSocketEventProducerServer server = new WebSocketEventProducerServer(port, eventCount, xmlEvents,
169                         eventInterval);
170
171         server.sendEvents();
172         server.shutdown();
173     }
174 }