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;
25 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
26 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
27 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
28 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
29 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessager;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
33 import org.onap.policy.apex.service.engine.event.ApexPluginsEventConsumer;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Concrete implementation an Apex event consumer that receives events using Kafka.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class ApexWebSocketConsumer extends ApexPluginsEventConsumer implements WsStringMessageListener {
44 private static final int WEB_SOCKET_WAIT_SLEEP_TIME = 100;
46 // Get a reference to the logger
47 private static final Logger LOGGER = LoggerFactory.getLogger(ApexWebSocketConsumer.class);
49 // The web socket messager, may be WS a server or a client
50 private WsStringMessager wsStringMessager;
52 // The event receiver that will receive events from this consumer
53 private ApexEventReceiver eventReceiver;
55 // The number of events read to date
56 private int eventsRead = 0;
59 public void init(final String consumerName, final EventHandlerParameters consumerParameters,
60 final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
61 this.eventReceiver = incomingEventReceiver;
62 this.name = consumerName;
64 // Check and get the Kafka Properties
65 if (!(consumerParameters.getCarrierTechnologyParameters() instanceof WebSocketCarrierTechnologyParameters)) {
66 LOGGER.warn("specified consumer properties are not applicable to a web socket consumer");
67 throw new ApexEventException("specified consumer properties are not applicable to a web socket consumer");
70 // The Web Socket properties
71 WebSocketCarrierTechnologyParameters webSocketConsumerProperties =
72 (WebSocketCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters();
74 // Check if this is a server or a client Web Socket
75 if (webSocketConsumerProperties.isWsClient()) {
77 wsStringMessager = new WsStringMessageClient(webSocketConsumerProperties.getHost(),
78 webSocketConsumerProperties.getPort());
80 wsStringMessager = new WsStringMessageServer(webSocketConsumerProperties.getPort());
83 // Start reception of event strings on the web socket
85 wsStringMessager.start(this);
86 } catch (final MessagingException e) {
87 LOGGER.warn("could not start web socket consumer", e);
96 while (consumerThread.isAlive() && !stopOrderedFlag) {
97 ThreadUtilities.sleep(WEB_SOCKET_WAIT_SLEEP_TIME);
106 if (wsStringMessager != null) {
107 wsStringMessager.stop();
109 stopOrderedFlag = true;
116 public void receiveString(final String eventString) {
118 eventReceiver.receiveEvent(new Properties(), eventString);
120 } catch (final Exception e) {
121 final String errorMessage = "Error sending event " + name + '_' + eventsRead + ", " + e.getMessage()
122 + ", event:\n" + eventString;
123 LOGGER.warn(errorMessage, e);