3e8db268ce2887722e60972a553e65bee69230d3
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.messaging.stringmessaging;
22
23 import com.google.common.eventbus.Subscribe;
24
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27
28 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
30 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
31 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
32 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
33 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * This class runs a web socket server for sending and receiving of strings over a web socket.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class WsStringMessageServer implements WsStringMessager {
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageServer.class);
44
45     // Message service factory and the message service itself
46     private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
47     private MessagingService<String> service = null;
48
49     // The listener to use for reception of strings
50     private WsStringMessageListener wsStringMessageListener;
51
52     // Address of the server
53     private final int port;
54
55     /**
56      * Constructor, define the port of the server.
57      *
58      * @param port the port of the server
59      */
60     public WsStringMessageServer(final int port) {
61         this.port = port;
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see
68      * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#start(org.onap.policy.
69      * apex. core.infrastructure.messaging. stringmessaging.WSStringMessageListener)
70      */
71     @Override
72     public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
73         this.wsStringMessageListener = newWsStringMessageListener;
74
75         LOGGER.entry("web socket event consumer server starting . . .");
76
77         try {
78             final InetAddress addrLan = MessagingUtils.getLocalHostLanAddress();
79             LOGGER.debug("web socket string message server LAN address=" + addrLan.getHostAddress());
80             final InetAddress addr = InetAddress.getLocalHost();
81             LOGGER.debug("web socket string message server host address=" + addr.getHostAddress());
82
83             service = factory.createServer(new InetSocketAddress(port));
84             service.addMessageListener(new WsStringMessageServerListener());
85
86             service.startConnection();
87         } catch (final Exception e) {
88             LOGGER.warn("web socket string message server start failed", e);
89             throw new MessagingException("web socket string message start failed", e);
90         }
91
92         LOGGER.exit("web socket string message server started");
93     }
94
95     /*
96      * (non-Javadoc)
97      *
98      * @see org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#stop()
99      */
100     @Override
101     public void stop() {
102         LOGGER.entry("web socket string message server stopping . . .");
103         service.stopConnection();
104         LOGGER.exit("web socket string message server stopped");
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see
111      * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#sendString(java.lang.
112      * String)
113      */
114     @Override
115     public void sendString(final String stringMessage) {
116         service.send(stringMessage);
117         if (LOGGER.isDebugEnabled()) {
118             LOGGER.debug("server sent message: {}", stringMessage);
119         }
120     }
121
122     /**
123      * The listener for strings coming into the server.
124      */
125     private class WsStringMessageServerListener implements MessageListener<String> {
126
127         /*
128          * (non-Javadoc)
129          *
130          * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(org.onap.policy.apex.core.
131          * infrastructure.messaging.impl.ws.messageblock. MessageBlock)
132          */
133         @Subscribe
134         @Override
135         public void onMessage(final MessageBlock<String> messageBlock) {
136             throw new UnsupportedOperationException("raw messages are not supported on string message clients");
137         }
138
139         /*
140          * (non-Javadoc)
141          *
142          * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(java.lang.String)
143          */
144         @Subscribe
145         @Override
146         public void onMessage(final String messageString) {
147             wsStringMessageListener.receiveString(messageString);
148         }
149     }
150 }