Feature uses features-parent as parent
[groupbasedpolicy.git] / renderers / opflex / src / main / java / org / opendaylight / groupbasedpolicy / renderer / opflex / 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, Thomas Bachman
9  */
10 package org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc;
11
12 import com.fasterxml.jackson.databind.JsonNode;
13
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class JsonRpcServiceBinderHandler extends ChannelInboundHandlerAdapter {
21     protected static final Logger logger = LoggerFactory.getLogger(JsonRpcServiceBinderHandler.class);
22     JsonRpcEndpoint endpoint = null;
23
24     public JsonRpcServiceBinderHandler(JsonRpcEndpoint endpoint) {
25         this.endpoint = endpoint;
26     }
27
28     // Setter to facilitate unit testing
29     public void setEndpoint(JsonRpcEndpoint endpoint) {
30         this.endpoint = endpoint;
31     }
32
33     @Override
34     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
35
36         if (msg instanceof JsonNode) {
37             JsonNode jsonNode = (JsonNode) msg;
38
39             if (jsonNode.has("result")) {
40                 endpoint.processResult(jsonNode);
41             } else if (jsonNode.hasNonNull("method")) {
42                 if (jsonNode.has("id") && (jsonNode.get("id") != null)) {
43                     endpoint.processRequest(jsonNode);
44                 }
45             }
46
47             return;
48         }
49
50         ctx.channel().close();
51     }
52
53     @Override
54     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
55         ctx.flush();
56     }
57 }