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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.websocket;
23 import java.util.EnumMap;
25 import java.util.Properties;
27 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
28 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
29 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
30 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
31 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessager;
32 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
33 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
34 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
35 import org.onap.policy.apex.service.engine.event.ApexEventException;
36 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
37 import org.onap.policy.apex.service.engine.event.PeeredReference;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
39 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Concrete implementation an Apex event consumer that receives events using Kafka.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class ApexWebSocketConsumer implements ApexEventConsumer, WsStringMessageListener, Runnable {
49 private static final int WEB_SOCKET_WAIT_SLEEP_TIME = 100;
51 // Get a reference to the logger
52 private static final Logger LOGGER = LoggerFactory.getLogger(ApexWebSocketConsumer.class);
54 // The web socket messager, may be WS a server or a client
55 private WsStringMessager wsStringMessager;
57 // The event receiver that will receive events from this consumer
58 private ApexEventReceiver eventReceiver;
60 // The name for this consumer
61 private String name = null;
63 // The peer references for this event handler
64 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
66 // The consumer thread and stopping flag
67 private Thread consumerThread;
68 private boolean stopOrderedFlag = false;
70 // The number of events read to date
71 private int eventsRead = 0;
74 public void init(final String consumerName, final EventHandlerParameters consumerParameters,
75 final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
76 this.eventReceiver = incomingEventReceiver;
77 this.name = consumerName;
79 // Check and get the Kafka Properties
80 if (!(consumerParameters.getCarrierTechnologyParameters() instanceof WebSocketCarrierTechnologyParameters)) {
81 LOGGER.warn("specified consumer properties are not applicable to a web socket consumer");
82 throw new ApexEventException("specified consumer properties are not applicable to a web socket consumer");
85 // The Web Socket properties
86 WebSocketCarrierTechnologyParameters webSocketConsumerProperties =
87 (WebSocketCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters();
89 // Check if this is a server or a client Web Socket
90 if (webSocketConsumerProperties.isWsClient()) {
92 wsStringMessager = new WsStringMessageClient(webSocketConsumerProperties.getHost(),
93 webSocketConsumerProperties.getPort());
95 wsStringMessager = new WsStringMessageServer(webSocketConsumerProperties.getPort());
98 // Start reception of event strings on the web socket
100 wsStringMessager.start(this);
101 } catch (final MessagingException e) {
102 LOGGER.warn("could not start web socket consumer", e);
110 public void start() {
111 // Configure and start the event reception thread
112 final String threadName = this.getClass().getName() + ":" + this.name;
113 consumerThread = new ApplicationThreadFactory(threadName).newThread(this);
114 consumerThread.setDaemon(true);
115 consumerThread.start();
122 public String getName() {
130 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
131 return peerReferenceMap.get(peeredMode);
138 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
139 peerReferenceMap.put(peeredMode, peeredReference);
147 while (consumerThread.isAlive() && !stopOrderedFlag) {
148 ThreadUtilities.sleep(WEB_SOCKET_WAIT_SLEEP_TIME);
157 if (wsStringMessager != null) {
158 wsStringMessager.stop();
160 stopOrderedFlag = true;
167 public void receiveString(final String eventString) {
169 eventReceiver.receiveEvent(new Properties(), eventString);
171 } catch (final Exception e) {
172 final String errorMessage = "Error sending event " + name + '_' + eventsRead + ", " + e.getMessage()
173 + ", event:\n" + eventString;
174 LOGGER.warn(errorMessage, e);