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