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