Merge "Fixed for bug 1168 : Issue while update subnet"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / websockets / client / WebSocketClientHandler.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.opendaylight.controller.sal.restconf.impl.websockets.client;
9
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelPromise;
14 import io.netty.channel.SimpleChannelInboundHandler;
15 import io.netty.handler.codec.http.FullHttpResponse;
16 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
17 import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
18 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
19 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
20 import io.netty.util.CharsetUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
25
26     private static final Logger logger = LoggerFactory.getLogger(WebSocketClientHandler.class.toString());
27     private final WebSocketClientHandshaker handshaker;
28     private ChannelPromise handshakeFuture;
29     private final IClientMessageCallback messageListener;
30
31     public WebSocketClientHandler(WebSocketClientHandshaker handshaker, IClientMessageCallback listener) {
32         this.handshaker = handshaker;
33         this.messageListener = listener;
34     }
35
36     public ChannelFuture handshakeFuture() {
37         return handshakeFuture;
38     }
39
40     @Override
41     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
42         handshakeFuture = ctx.newPromise();
43     }
44
45     @Override
46     public void channelActive(ChannelHandlerContext ctx) throws Exception {
47         handshaker.handshake(ctx.channel());
48     }
49
50     @Override
51     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
52         logger.info("WebSocket Client disconnected!");
53     }
54
55     @Override
56     public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
57         Channel ch = ctx.channel();
58         if (!handshaker.isHandshakeComplete()) {
59             handshaker.finishHandshake(ch, (FullHttpResponse) msg);
60             logger.info("WebSocket Client connected!");
61             handshakeFuture.setSuccess();
62             return;
63         }
64
65         if (msg instanceof FullHttpResponse) {
66             FullHttpResponse response = (FullHttpResponse) msg;
67             throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content="
68                     + response.content().toString(CharsetUtil.UTF_8) + ')');
69         }
70
71         messageListener.onMessageReceived(msg);
72         WebSocketFrame frame = (WebSocketFrame) msg;
73
74         if (frame instanceof PongWebSocketFrame) {
75             logger.info("WebSocket Client received pong");
76         } else if (frame instanceof CloseWebSocketFrame) {
77             logger.info("WebSocket Client received closing");
78             ch.close();
79         }
80     }
81
82     @Override
83     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
84         cause.printStackTrace();
85
86         if (!handshakeFuture.isDone()) {
87             handshakeFuture.setFailure(cause);
88         }
89
90         ctx.close();
91     }
92 }