389d04dcc17509211728891c6d082db59c180f0b
[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.impl.ws.server;
22
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
25 import java.util.Collection;
26
27 import org.java_websocket.WebSocket;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
29 import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * A messaging server implementation using web socket.
35  *
36  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
37  * @param <MESSAGE> the generic type of message being passed
38  */
39 public class MessageServerImpl<MESSAGE> extends InternalMessageBusServer<MESSAGE> {
40     // The logger for this class
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageServerImpl.class);
42
43     // The Web Socket protocol for URIs and URLs
44     private static final String PROTOCOL = "ws://";
45
46     // URI of this server
47     private final String connectionURI;
48
49     // Indicates if the web socket server is started or not
50     private boolean isStarted = false;
51
52     /**
53      * Instantiates a new web socket messaging server for Apex.
54      *
55      * @param address the address of the server machine on which to start the server
56      */
57     public MessageServerImpl(final InetSocketAddress address) {
58         // Call the super class to create the web socket and set up received message forwarding
59         super(address);
60         LOGGER.entry(address);
61
62         // Compose the Web Socket URI
63         connectionURI = PROTOCOL + address.getHostString() + ":" + address.getPort();
64         LOGGER.debug("Server connection URI: {}", connectionURI);
65
66         LOGGER.exit();
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.java_websocket.server.WebSocketServer#start()
73      */
74     @Override
75     public void startConnection() {
76         // Start reception of connections on the web socket
77         start();
78         isStarted = true;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.java_websocket.server.WebSocketServer#stop()
85      */
86     @Override
87     public void stopConnection() {
88         // Stop message listening using our super class
89         stopListener();
90
91         // Stop the web socket server
92         try {
93             // Close all connections on this web socket server
94             for (final WebSocket connection : getConnections()) {
95                 connection.closeConnection(0, "");
96             }
97             stop();
98         } catch (final IOException ioe) {
99             LOGGER.catching(ioe);
100         } catch (final InterruptedException e) {
101             // restore the interrupt status
102             Thread.currentThread().interrupt();
103             // This can happen in normal operation so ignore
104         }
105         isStarted = false;
106     }
107
108     /**
109      * This method returns the current connection URI , if the server started otherwise it throws
110      * {@link IllegalStateException}.
111      *
112      * @return connection URI
113      */
114     public String getConnectionURI() {
115         if (connectionURI == null) {
116             throw new IllegalStateException("URI not set - The server is not started");
117         }
118         return connectionURI;
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see
125      * org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(org.onap.policy.apex
126      * .core. infrastructure. messaging.MessageHolder)
127      */
128     @Override
129     public void send(final MessageHolder<MESSAGE> message) {
130         // Send the incoming message to all clients connected to this web socket
131         final Collection<WebSocket> connections = getConnections();
132         for (final WebSocket webSocket : connections) {
133             webSocket.send(MessagingUtils.serializeObject(message));
134         }
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see
141      * org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(java.lang.String)
142      */
143     @Override
144     public void send(final String messageString) {
145         final Collection<WebSocket> connections = getConnections();
146         for (final WebSocket webSocket : connections) {
147             webSocket.send(messageString);
148         }
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#isStarted()
155      */
156     @Override
157     public boolean isStarted() {
158         return isStarted;
159     }
160
161     @Override
162     public void onStart() {
163         LOGGER.debug("started deployment server on URI: {}", connectionURI);
164     }
165 }