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