422c91bc3d3542fc4f421a9f217bc5d0db476b8f
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.websocket;
23
24 import java.util.EnumMap;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
29 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
30 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
31 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
32 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessager;
33 import org.onap.policy.apex.service.engine.event.ApexEventException;
34 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
35 import org.onap.policy.apex.service.engine.event.PeeredReference;
36 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Concrete implementation of an Apex event producer that sends events using a web socket.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 public class ApexWebSocketProducer extends ApexPluginsEventProducer implements  WsStringMessageListener {
48     // Get a reference to the logger
49     private static final Logger LOGGER = LoggerFactory.getLogger(ApexWebSocketProducer.class);
50
51     // The web socket messager, may be WS a server or a client
52     private WsStringMessager wsStringMessager;
53
54     @Override
55     public void init(final String producerName, final EventHandlerParameters producerParameters)
56             throws ApexEventException {
57         this.name = producerName;
58
59         // Check and get the web socket Properties
60         if (!(producerParameters.getCarrierTechnologyParameters() instanceof WebSocketCarrierTechnologyParameters)) {
61             String message =
62                     "specified producer properties for " + this.name + "are not applicable to a web socket producer";
63             LOGGER.warn(message);
64             throw new ApexEventException("specified producer properties are not applicable to a web socket producer");
65         }
66         // The Web Socket properties
67         WebSocketCarrierTechnologyParameters webSocketProducerProperties =
68                 (WebSocketCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
69
70         // Check if this is a server or a client Web Socket
71         if (webSocketProducerProperties.isWsClient()) {
72             // Create a WS client
73             wsStringMessager = new WsStringMessageClient(webSocketProducerProperties.getHost(),
74                     webSocketProducerProperties.getPort());
75         } else {
76             wsStringMessager = new WsStringMessageServer(webSocketProducerProperties.getPort());
77         }
78
79         // Start reception of event strings on the web socket
80         try {
81             wsStringMessager.start(this);
82         } catch (final MessagingException e) {
83             String message = "could not start web socket producer (" + this.name + ")";
84             LOGGER.warn(message, e);
85         }
86     }
87
88     /**
89      * {@inheritDoc}.
90      */
91     @Override
92     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
93             final Object event) {
94         super.sendEvent(executionId, executionProperties, eventName, event );
95
96         wsStringMessager.sendString((String) event);
97     }
98
99     /**
100      * {@inheritDoc}.
101      */
102     @Override
103     public void stop() {
104         if (wsStringMessager != null) {
105             wsStringMessager.stop();
106         }
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public void receiveString(final String messageString) {
114         String message = "received message \"" + messageString + "\" on web socket producer (" + this.name
115                 + ") , no messages should be received on a web socket producer";
116         LOGGER.warn(message);
117     }
118 }