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