YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.ovsdb.controller.impl; |
| 17 | |
| 18 | import io.netty.channel.ChannelHandlerContext; |
| 19 | import io.netty.channel.ChannelInboundHandlerAdapter; |
| 20 | |
| 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; |
| 25 | |
| 26 | import com.fasterxml.jackson.databind.JsonNode; |
| 27 | import com.google.common.base.Strings; |
| 28 | |
| 29 | /** |
| 30 | * Channel handler deals with the node connection and dispatches |
| 31 | * ovsdb messages to the appropriate locations. |
| 32 | */ |
| 33 | public final class OvsdbJsonRpcHandler extends ChannelInboundHandlerAdapter { |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 34 | private static final Logger log = LoggerFactory |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 35 | .getLogger(OvsdbJsonRpcHandler.class); |
| 36 | private OvsdbNodeId ovsdbNodeId; |
| 37 | private OvsdbProviderService ovsdbProviderService; |
| 38 | |
| 39 | /** |
| 40 | * Constructor from a OvsdbNodeId ovsdbNodeId. |
| 41 | * |
| 42 | * @param ovsdbNodeId the ovsdbNodeId to use |
| 43 | */ |
| 44 | public OvsdbJsonRpcHandler(OvsdbNodeId ovsdbNodeId) { |
| 45 | super(); |
| 46 | this.ovsdbNodeId = ovsdbNodeId; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the ovsdbProviderService instance. |
| 51 | * |
| 52 | * @return the instance of the ovsdbProviderService |
| 53 | */ |
| 54 | public OvsdbProviderService getOvsdbProviderService() { |
| 55 | return ovsdbProviderService; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sets the ovsdbProviderService instance. |
| 60 | * |
| 61 | * @param ovsdbNodeDriver the ovsdbNodeDriver to use |
| 62 | */ |
| 63 | public void setOvsdbProviderService(OvsdbProviderService ovsdbNodeDriver) { |
| 64 | this.ovsdbProviderService = ovsdbNodeDriver; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the OvsdbNodeId instance. |
| 69 | * |
| 70 | * @return the instance of the OvsdbNodeId |
| 71 | */ |
| 72 | public OvsdbNodeId getNodeId() { |
| 73 | return ovsdbNodeId; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Sets the ovsdb node id. |
| 78 | * |
| 79 | * @param ovsdbNodeId the ovsdbNodeId to use |
| 80 | */ |
| 81 | public void setNodeId(OvsdbNodeId ovsdbNodeId) { |
| 82 | this.ovsdbNodeId = ovsdbNodeId; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Processes an JsonNode message received on the channel. |
| 87 | * |
| 88 | * @param jsonNode The OvsdbJsonRpcHandler that received the message |
| 89 | */ |
| 90 | private void processOvsdbMessage(JsonNode jsonNode) { |
| 91 | |
Hyunsun Moon | 5fb20a5 | 2015-09-25 17:02:33 -0700 | [diff] [blame] | 92 | log.debug("Handle ovsdb message"); |
YuanyouZhang | ebe1261 | 2015-08-05 18:19:09 +0800 | [diff] [blame] | 93 | |
| 94 | if (jsonNode.has("result")) { |
| 95 | |
| 96 | log.debug("Handle ovsdb result"); |
| 97 | ovsdbProviderService.processResult(jsonNode); |
| 98 | |
| 99 | } else if (jsonNode.hasNonNull("method")) { |
| 100 | |
| 101 | log.debug("Handle ovsdb request"); |
| 102 | if (jsonNode.has("id") |
| 103 | && !Strings.isNullOrEmpty(jsonNode.get("id").asText())) { |
| 104 | ovsdbProviderService.processRequest(jsonNode); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void channelRead(ChannelHandlerContext ctx, Object msg) |
| 113 | throws Exception { |
| 114 | log.debug("Receive message from ovsdb"); |
| 115 | if (msg instanceof JsonNode) { |
| 116 | JsonNode jsonNode = (JsonNode) msg; |
| 117 | processOvsdbMessage(jsonNode); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
| 123 | ctx.flush(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
| 128 | log.error("Exception inside channel handling pipeline.", cause); |
| 129 | context.close(); |
| 130 | } |
| 131 | } |