blob: c05bce46a30453d7ff19d978e504ab1ac7f8976c [file] [log] [blame]
YuanyouZhangebe12612015-08-05 18:19:09 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
YuanyouZhangebe12612015-08-05 18:19:09 +08003 *
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 */
16package org.onosproject.ovsdb.controller.impl;
17
18import io.netty.channel.ChannelHandlerContext;
19import io.netty.channel.ChannelInboundHandlerAdapter;
20
21import org.onosproject.ovsdb.controller.OvsdbNodeId;
22import org.onosproject.ovsdb.controller.driver.OvsdbProviderService;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.fasterxml.jackson.databind.JsonNode;
27import 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 */
33public final class OvsdbJsonRpcHandler extends ChannelInboundHandlerAdapter {
34 protected static final Logger log = LoggerFactory
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 Moon5fb20a52015-09-25 17:02:33 -070092 log.debug("Handle ovsdb message");
YuanyouZhangebe12612015-08-05 18:19:09 +080093
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}