fc401576f3dbaba4f84e86624efe44d6a5376f32
[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 : connections()) {
95                 connection.closeConnection(0, "");
96             }
97             stop();
98         } catch (final IOException ioe) {
99             LOGGER.catching(ioe);
100         } catch (final InterruptedException e) {
101             // This can happen in normal operation so ignore
102         }
103         isStarted = false;
104     }
105
106     /**
107      * This method returns the current connection URI , if the server started otherwise it throws
108      * {@link IllegalStateException}.
109      *
110      * @return connection URI
111      */
112     public String getConnectionURI() {
113         if (connectionURI == null) {
114             throw new IllegalStateException("URI not set - The server is not started");
115         }
116         return connectionURI;
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(org.onap.policy.apex.core.
123      * infrastructure. messaging.MessageHolder)
124      */
125     @Override
126     public void send(final MessageHolder<MESSAGE> message) {
127         // Send the incoming message to all clients connected to this web socket
128         final Collection<WebSocket> connections = connections();
129         for (final WebSocket webSocket : connections) {
130             webSocket.send(MessagingUtils.serializeObject(message));
131         }
132     }
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#send(java.lang.String)
138      */
139     @Override
140     public void send(final String messageString) {
141         final Collection<WebSocket> connections = connections();
142         for (final WebSocket webSocket : connections) {
143             webSocket.send(messageString);
144         }
145     }
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see org.onap.policy.apex.core.infrastructure.messaging.MessagingService#isStarted()
151      */
152     @Override
153     public boolean isStarted() {
154         return isStarted;
155     }
156
157     @Override
158     public void onStart() {
159         LOGGER.debug("started deployment server on URI: {}", connectionURI);
160     }
161 }