Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-websocket / src / main / java / org / onap / policy / apex / plugins / event / carrier / websocket / ApexWebSocketProducer.java
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.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;
35
36 /**
37  * Concrete implementation of an Apex event producer that sends events using a web socket.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
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);
44
45     // The web socket messager, may be WS a server or a client
46     private WsStringMessager wsStringMessager;
47
48     @Override
49     public void init(final String producerName, final EventHandlerParameters producerParameters)
50             throws ApexEventException {
51         this.name = producerName;
52
53         // Check and get the web socket Properties
54         if (!(producerParameters.getCarrierTechnologyParameters() instanceof WebSocketCarrierTechnologyParameters)) {
55             String message =
56                     "specified producer properties for " + this.name + "are not applicable to a web socket producer";
57             LOGGER.warn(message);
58             throw new ApexEventException("specified producer properties are not applicable to a web socket producer");
59         }
60         // The Web Socket properties
61         WebSocketCarrierTechnologyParameters webSocketProducerProperties =
62                 (WebSocketCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
63
64         // Check if this is a server or a client Web Socket
65         if (webSocketProducerProperties.isWsClient()) {
66             // Create a WS client
67             wsStringMessager = new WsStringMessageClient(webSocketProducerProperties.getHost(),
68                     webSocketProducerProperties.getPort());
69         } else {
70             wsStringMessager = new WsStringMessageServer(webSocketProducerProperties.getPort());
71         }
72
73         // Start reception of event strings on the web socket
74         try {
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);
79         }
80     }
81
82     /**
83      * {@inheritDoc}.
84      */
85     @Override
86     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
87             final Object event) {
88         super.sendEvent(executionId, executionProperties, eventName, event);
89
90         wsStringMessager.sendString((String) event);
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public void stop() {
98         if (wsStringMessager != null) {
99             wsStringMessager.stop();
100         }
101     }
102
103     /**
104      * {@inheritDoc}.
105      */
106     @Override
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);
111     }
112 }