2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.ovsdb.controller.impl;
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.channel.ChannelInboundHandlerAdapter;
21 import org.onosproject.ovsdb.controller.OvsdbNodeId;
22 import org.onosproject.ovsdb.controller.driver.OvsdbProviderService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.google.common.base.Strings;
30 * Channel handler deals with the node connection and dispatches
31 * ovsdb messages to the appropriate locations.
33 public final class OvsdbJsonRpcHandler extends ChannelInboundHandlerAdapter {
34 protected static final Logger log = LoggerFactory
35 .getLogger(OvsdbJsonRpcHandler.class);
36 private OvsdbNodeId ovsdbNodeId;
37 private OvsdbProviderService ovsdbProviderService;
40 * Constructor from a OvsdbNodeId ovsdbNodeId.
42 * @param ovsdbNodeId the ovsdbNodeId to use
44 public OvsdbJsonRpcHandler(OvsdbNodeId ovsdbNodeId) {
46 this.ovsdbNodeId = ovsdbNodeId;
50 * Gets the ovsdbProviderService instance.
52 * @return the instance of the ovsdbProviderService
54 public OvsdbProviderService getOvsdbProviderService() {
55 return ovsdbProviderService;
59 * Sets the ovsdbProviderService instance.
61 * @param ovsdbNodeDriver the ovsdbNodeDriver to use
63 public void setOvsdbProviderService(OvsdbProviderService ovsdbNodeDriver) {
64 this.ovsdbProviderService = ovsdbNodeDriver;
68 * Gets the OvsdbNodeId instance.
70 * @return the instance of the OvsdbNodeId
72 public OvsdbNodeId getNodeId() {
77 * Sets the ovsdb node id.
79 * @param ovsdbNodeId the ovsdbNodeId to use
81 public void setNodeId(OvsdbNodeId ovsdbNodeId) {
82 this.ovsdbNodeId = ovsdbNodeId;
86 * Processes an JsonNode message received on the channel.
88 * @param jsonNode The OvsdbJsonRpcHandler that received the message
90 private void processOvsdbMessage(JsonNode jsonNode) {
92 log.info("Handle ovsdb message");
94 if (jsonNode.has("result")) {
96 log.debug("Handle ovsdb result");
97 ovsdbProviderService.processResult(jsonNode);
99 } else if (jsonNode.hasNonNull("method")) {
101 log.debug("Handle ovsdb request");
102 if (jsonNode.has("id")
103 && !Strings.isNullOrEmpty(jsonNode.get("id").asText())) {
104 ovsdbProviderService.processRequest(jsonNode);
112 public void channelRead(ChannelHandlerContext ctx, Object msg)
114 log.debug("Receive message from ovsdb");
115 if (msg instanceof JsonNode) {
116 JsonNode jsonNode = (JsonNode) msg;
117 processOvsdbMessage(jsonNode);
122 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
127 public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
128 log.error("Exception inside channel handling pipeline.", cause);