2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * * Modifications Copyright (C) 2020 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.plugins.event.carrier.websocket;
24 import java.util.Properties;
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.messaging.stringmessaging.WsStringMessageServer;
30 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessager;
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.service.engine.event.ApexEventException;
33 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
34 import org.onap.policy.apex.service.engine.event.ApexPluginsEventConsumer;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Concrete implementation an Apex event consumer that receives events using Kafka.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class ApexWebSocketConsumer extends ApexPluginsEventConsumer implements WsStringMessageListener {
45 private static final int WEB_SOCKET_WAIT_SLEEP_TIME = 100;
47 // Get a reference to the logger
48 private static final Logger LOGGER = LoggerFactory.getLogger(ApexWebSocketConsumer.class);
50 // The web socket messager, may be WS a server or a client
51 private WsStringMessager wsStringMessager;
53 // The event receiver that will receive events from this consumer
54 private ApexEventReceiver eventReceiver;
56 // The number of events read to date
57 private int eventsRead = 0;
60 public void init(final String consumerName, final EventHandlerParameters consumerParameters,
61 final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
62 this.eventReceiver = incomingEventReceiver;
63 this.name = consumerName;
65 // Check and get the Kafka Properties
66 if (!(consumerParameters.getCarrierTechnologyParameters() instanceof WebSocketCarrierTechnologyParameters)) {
67 LOGGER.warn("specified consumer properties are not applicable to a web socket consumer");
68 throw new ApexEventException("specified consumer properties are not applicable to a web socket consumer");
71 // The Web Socket properties
72 WebSocketCarrierTechnologyParameters webSocketConsumerProperties =
73 (WebSocketCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters();
75 // Check if this is a server or a client Web Socket
76 if (webSocketConsumerProperties.isWsClient()) {
78 wsStringMessager = new WsStringMessageClient(webSocketConsumerProperties.getHost(),
79 webSocketConsumerProperties.getPort());
81 wsStringMessager = new WsStringMessageServer(webSocketConsumerProperties.getPort());
84 // Start reception of event strings on the web socket
86 wsStringMessager.start(this);
87 } catch (final MessagingException e) {
88 LOGGER.warn("could not start web socket consumer", e);
97 while (consumerThread.isAlive() && !stopOrderedFlag) {
98 ThreadUtilities.sleep(WEB_SOCKET_WAIT_SLEEP_TIME);
107 if (wsStringMessager != null) {
108 wsStringMessager.stop();
110 stopOrderedFlag = true;
117 public void receiveString(final String eventString) {
119 eventReceiver.receiveEvent(new Properties(), eventString);
121 } catch (final Exception e) {
122 final String errorMessage = "Error sending event " + name + '_' + eventsRead + ", " + e.getMessage()
123 + ", event:\n" + eventString;
124 LOGGER.warn(errorMessage, e);