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