2 * Copyright (C) 2013 EBay Software Foundation
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
8 * Authors : Ashwin Raveendran, Madhu Venugopal
10 package org.opendaylight.ovsdb.lib.jsonrpc;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelInboundHandlerAdapter;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.google.common.base.Strings;
21 public class JsonRpcServiceBinderHandler extends ChannelInboundHandlerAdapter {
22 private static final Logger LOG = LoggerFactory.getLogger(JsonRpcServiceBinderHandler.class);
23 JsonRpcEndpoint factory = null;
24 Object context = null;
26 public Object getContext() {
30 public void setContext(Object context) {
31 this.context = context;
34 public JsonRpcServiceBinderHandler(JsonRpcEndpoint factory) {
35 this.factory = factory;
39 public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
40 if (msg instanceof JsonNode) {
41 JsonNode jsonNode = (JsonNode) msg;
42 if (jsonNode.has("result")) {
44 factory.processResult(jsonNode);
45 } catch (NoSuchMethodException e) {
47 ChannelRead is a method invoked during Netty message receive event.
48 The only sane thing we can do is to print a meaningful error message.
50 LOG.error("NoSuchMethodException when handling {}", msg, e);
52 } else if (jsonNode.hasNonNull("method")) {
53 if (jsonNode.has("id") && !Strings.isNullOrEmpty(jsonNode.get("id").asText())) {
54 factory.processRequest(context, jsonNode);
56 LOG.debug("Request with null or empty id field: {} {}", jsonNode.get("method"),
57 jsonNode.get("params"));
63 ctx.channel().close();
67 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {