46841a37da120647164f026e7e181f9113644acd
[ovsdb.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / lib / jsonrpc / JsonRpcServiceBinderHandler.java
1 /*
2  * Copyright (C) 2013 EBay Software Foundation
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  * Authors : Ashwin Raveendran, Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.lib.jsonrpc;\r
11 \r
12 import com.fasterxml.jackson.databind.JsonNode;\r
13 import com.google.common.base.Strings;\r
14 import com.google.common.collect.Maps;\r
15 import com.google.common.util.concurrent.SettableFuture;\r
16 \r
17 import io.netty.channel.ChannelHandlerContext;\r
18 import io.netty.channel.ChannelInboundHandlerAdapter;\r
19 \r
20 import org.opendaylight.controller.sal.core.Node;\r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 \r
24 import java.util.Map;\r
25 \r
26 public class JsonRpcServiceBinderHandler extends ChannelInboundHandlerAdapter {\r
27     protected static final Logger logger = LoggerFactory.getLogger(JsonRpcServiceBinderHandler.class);\r
28     Map<Object, SettableFuture<Object>> waitingForReply = Maps.newHashMap();\r
29     JsonRpcEndpoint factory = null;\r
30     Node node = null;\r
31 \r
32     public Node getNode() {\r
33         return node;\r
34     }\r
35 \r
36     public void setNode(Node node) {\r
37         this.node = node;\r
38     }\r
39 \r
40     public JsonRpcServiceBinderHandler(JsonRpcEndpoint factory) {\r
41         this.factory = factory;\r
42     }\r
43 \r
44     @Override\r
45     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {\r
46 \r
47         if (msg instanceof JsonNode) {\r
48             JsonNode jsonNode = (JsonNode) msg;\r
49 \r
50             if (jsonNode.has("result")) {\r
51                 factory.processResult(jsonNode);\r
52             } else if (jsonNode.hasNonNull("method")) {\r
53                 if (jsonNode.has("id") && !Strings.isNullOrEmpty(jsonNode.get("id").asText())) {\r
54                     factory.processRequest(node, jsonNode);\r
55                 }\r
56             }\r
57 \r
58             return;\r
59         }\r
60 \r
61         ctx.channel().close();\r
62     }\r
63 \r
64     @Override\r
65     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {\r
66         ctx.flush();\r
67     }\r
68 }