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