Added more ignorable files to .gitignore
[ovsdb.git] / library / 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;
11
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelInboundHandlerAdapter;
14
15 import java.util.Map;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.fasterxml.jackson.databind.JsonNode;
21 import com.google.common.base.Strings;
22 import com.google.common.collect.Maps;
23 import com.google.common.util.concurrent.SettableFuture;
24
25 public class JsonRpcServiceBinderHandler extends ChannelInboundHandlerAdapter {
26     protected static final Logger logger = LoggerFactory.getLogger(JsonRpcServiceBinderHandler.class);
27     Map<Object, SettableFuture<Object>> waitingForReply = Maps.newHashMap();
28     JsonRpcEndpoint factory = null;
29     Object context = null;
30
31     public Object getContext() {
32         return context;
33     }
34
35     public void setContext(Object context) {
36         this.context = context;
37     }
38
39     public JsonRpcServiceBinderHandler(JsonRpcEndpoint factory) {
40         this.factory = factory;
41     }
42
43     @Override
44     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
45
46         if (msg instanceof JsonNode) {
47             JsonNode jsonNode = (JsonNode) msg;
48
49             if (jsonNode.has("result")) {
50                 factory.processResult(jsonNode);
51             } else if (jsonNode.hasNonNull("method")) {
52                 if (jsonNode.has("id") && !Strings.isNullOrEmpty(jsonNode.get("id").asText())) {
53                     factory.processRequest(context, jsonNode);
54                 }
55             }
56
57             return;
58         }
59
60         ctx.channel().close();
61     }
62
63     @Override
64     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
65         ctx.flush();
66     }
67 }