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.service.engine.event.ApexEventException;
31 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
32 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Concrete implementation of an Apex event producer that sends events using a web socket.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class ApexWebSocketProducer extends ApexPluginsEventProducer implements WsStringMessageListener {
42 // Get a reference to the logger
43 private static final Logger LOGGER = LoggerFactory.getLogger(ApexWebSocketProducer.class);
45 // The web socket messager, may be WS a server or a client
46 private WsStringMessager wsStringMessager;
49 public void init(final String producerName, final EventHandlerParameters producerParameters)
50 throws ApexEventException {
51 this.name = producerName;
53 // Check and get the web socket Properties
54 if (!(producerParameters.getCarrierTechnologyParameters() instanceof WebSocketCarrierTechnologyParameters)) {
56 "specified producer properties for " + this.name + "are not applicable to a web socket producer";
58 throw new ApexEventException("specified producer properties are not applicable to a web socket producer");
60 // The Web Socket properties
61 WebSocketCarrierTechnologyParameters webSocketProducerProperties =
62 (WebSocketCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
64 // Check if this is a server or a client Web Socket
65 if (webSocketProducerProperties.isWsClient()) {
67 wsStringMessager = new WsStringMessageClient(webSocketProducerProperties.getHost(),
68 webSocketProducerProperties.getPort());
70 wsStringMessager = new WsStringMessageServer(webSocketProducerProperties.getPort());
73 // Start reception of event strings on the web socket
75 wsStringMessager.start(this);
76 } catch (final MessagingException e) {
77 String message = "could not start web socket producer (" + this.name + ")";
78 LOGGER.warn(message, e);
86 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
88 super.sendEvent(executionId, executionProperties, eventName, event);
90 wsStringMessager.sendString((String) event);
98 if (wsStringMessager != null) {
99 wsStringMessager.stop();
107 public void receiveString(final String messageString) {
108 String message = "received message \"" + messageString + "\" on web socket producer (" + this.name
109 + ") , no messages should be received on a web socket producer";
110 LOGGER.warn(message);