968ec97dbfd96de66179576778f7d779c58d3e69
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / messaging / stringmessaging / WsStringMessageClient.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.stringmessaging;
23
24 import com.google.common.eventbus.Subscribe;
25
26 import java.net.URI;
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.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * This class uses a web socket client to send and receive strings over a web socket.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class WsStringMessageClient implements WsStringMessager {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(WsStringMessageClient.class);
43
44     // Repeated string constants
45     private static final String MESSAGE_PREAMBLE = "web socket event consumer client to \"";
46
47     // Message service factory and the message service itself
48     private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
49     private MessagingService<String> service = null;
50
51     // The listener to use for reception of strings
52     private WsStringMessageListener wsStringMessageListener;
53
54     // Address of the server
55     private final String host;
56     private final int port;
57     private String uriString;
58
59     /**
60      * Constructor, define the host and port of the server to connect to.
61      *
62      * @param host the host of the server
63      * @param port the port of the server
64      */
65     public WsStringMessageClient(final String host, final int port) {
66         this.host = host;
67         this.port = port;
68     }
69
70     /**
71      * {@inheritDoc}.
72      */
73     @Override
74     public void start(final WsStringMessageListener newWsStringMessageListener) throws MessagingException {
75         this.wsStringMessageListener = newWsStringMessageListener;
76
77         uriString = "ws://" + host + ":" + port;
78         String messagePreamble = MESSAGE_PREAMBLE + uriString + "\" ";
79         LOGGER.entry(messagePreamble + "starting . . .");
80
81         try {
82             service = factory.createClient(new URI(uriString));
83             service.addMessageListener(new WsStringMessageClientListener());
84             service.startConnection();
85         } catch (final Exception e) {
86             String message = messagePreamble + "start failed";
87             LOGGER.warn(message, e);
88             throw new MessagingException(message, e);
89         }
90
91         LOGGER.exit(messagePreamble + "started");
92     }
93
94     /**
95      * {@inheritDoc}.
96      */
97     @Override
98     public void stop() {
99         LOGGER.entry(MESSAGE_PREAMBLE + uriString + "\" stopping . . .");
100         service.stopConnection();
101         LOGGER.exit(MESSAGE_PREAMBLE + uriString + "\" stopped");
102     }
103
104     /**
105      * {@inheritDoc}.
106      */
107     @Override
108     public void sendString(final String stringMessage) {
109         service.send(stringMessage);
110
111         if (LOGGER.isDebugEnabled()) {
112             String message = "message sent to server: " + stringMessage;
113             LOGGER.debug(message);
114         }
115     }
116
117     /**
118      * The Class WSStringMessageClientListener.
119      */
120     private class WsStringMessageClientListener implements MessageListener<String> {
121         /**
122          * {@inheritDoc}.
123          */
124         @Subscribe
125         @Override
126         public void onMessage(final MessageBlock<String> messageBlock) {
127             throw new UnsupportedOperationException("raw messages are not supported on string message clients");
128         }
129
130         /**
131          * {@inheritDoc}.
132          */
133         @Subscribe
134         @Override
135         public void onMessage(final String messageString) {
136             wsStringMessageListener.receiveString(messageString);
137         }
138     }
139
140     /**
141      * {@inheritDoc}.
142      */
143     @Override
144     public boolean isStarted() {
145         return service.isStarted();
146     }
147 }