Merge "Using specific exceptions in RestApiCallNode"
[ccsdk/sli/plugins.git] / fabric-discovery-plugin / provider / src / main / java / org / onap / ccsdk / sli / plugins / fabricdiscovery / WebSocketClient.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.onap.ccsdk.sli.plugins.fabricdiscovery;
9
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.buffer.Unpooled;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelInitializer;
14 import io.netty.channel.ChannelPipeline;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.nio.NioEventLoopGroup;
17 import io.netty.channel.socket.SocketChannel;
18 import io.netty.channel.socket.nio.NioSocketChannel;
19 import io.netty.handler.codec.http.HttpClientCodec;
20 import io.netty.handler.codec.http.HttpObjectAggregator;
21 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
22 import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
23 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
24 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
25 import io.netty.handler.codec.http.websocketx.WebSocketVersion;
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.net.URI;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class WebSocketClient {
34
35     private static final Logger LOG = LoggerFactory.getLogger(WebSocketClient.class);
36
37     private final URI uri;
38     private final Bootstrap bootstrap = new Bootstrap();
39     private final WebSocketClientHandler clientHandler;
40     private Channel clientChannel;
41     private final EventLoopGroup group = new NioEventLoopGroup();
42
43     public WebSocketClient(final URI uri, final IClientMessageCallback clientMessageCallback) {
44         this.uri = uri;
45         clientHandler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri,
46                 WebSocketVersion.V13, null, false, null), clientMessageCallback);
47         // last null could be replaced with DefaultHttpHeaders
48         initialize();
49     }
50
51     public void initialize() {
52
53         String protocol = uri.getScheme();
54         if (!"ws".equals(protocol)) {
55             throw new IllegalArgumentException("Unsupported protocol: " + protocol);
56         }
57
58         bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
59             @Override
60             public void initChannel(final SocketChannel ch) throws Exception {
61                 ChannelPipeline pipeline = ch.pipeline();
62                 pipeline.addLast("http-codec", new HttpClientCodec());
63                 pipeline.addLast("aggregator", new HttpObjectAggregator(8192));
64                 pipeline.addLast("ws-handler", clientHandler);
65             }
66         });
67     }
68
69     public void connect() throws InterruptedException {
70         LOG.info("WebSocket Client connecting");
71         clientChannel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
72         clientHandler.handshakeFuture().sync();
73     }
74
75     public void writeAndFlush(final String message) {
76         clientChannel.writeAndFlush(new TextWebSocketFrame(message));
77     }
78
79     public void writeAndFlush(final Object message) {
80         clientChannel.writeAndFlush(message);
81     }
82
83     public void ping() {
84         clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
85     }
86
87     public void close(final String reasonText) throws InterruptedException {
88         CloseWebSocketFrame closeWebSocketFrame = new CloseWebSocketFrame(1000, reasonText);
89         clientChannel.writeAndFlush(closeWebSocketFrame);
90
91         // WebSocketClientHandler will close the connection when the server
92         // responds to the CloseWebSocketFrame.
93         clientChannel.closeFuture().sync();
94         group.shutdownGracefully();
95     }
96 }