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