Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.ofagent.impl; |
| 17 | |
| 18 | import io.netty.channel.Channel; |
| 19 | import io.netty.channel.ChannelDuplexHandler; |
| 20 | import io.netty.channel.ChannelHandlerContext; |
| 21 | import io.netty.util.ReferenceCountUtil; |
| 22 | import org.onosproject.ofagent.api.OFSwitch; |
| 23 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of OpenFlow channel handler. |
| 29 | * It processes OpenFlow message according to the channel state. |
| 30 | */ |
| 31 | public final class OFChannelHandler extends ChannelDuplexHandler { |
| 32 | |
| 33 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 34 | private final OFSwitch ofSwitch; |
| 35 | |
| 36 | private Channel channel; |
| 37 | private ChannelState state; |
| 38 | |
| 39 | private enum ChannelState { |
| 40 | |
| 41 | INIT { |
| 42 | @Override |
| 43 | void processOFMessage(final OFChannelHandler handler, |
| 44 | final OFMessage msg) { |
| 45 | // TODO implement |
| 46 | } |
| 47 | }, |
| 48 | WAIT_HELLO { |
| 49 | @Override |
| 50 | void processOFMessage(final OFChannelHandler handler, |
| 51 | final OFMessage msg) { |
| 52 | // TODO implement |
| 53 | } |
| 54 | }, |
| 55 | WAIT_FEATURE_REQUEST { |
| 56 | @Override |
| 57 | void processOFMessage(final OFChannelHandler handler, |
| 58 | final OFMessage msg) { |
| 59 | // TODO implement |
| 60 | } |
| 61 | }, |
| 62 | ESTABLISHED { |
| 63 | @Override |
| 64 | void processOFMessage(final OFChannelHandler handler, |
| 65 | final OFMessage msg) { |
| 66 | // TODO implement |
| 67 | // TODO add this channel to ofSwitch role service |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | abstract void processOFMessage(final OFChannelHandler handler, |
| 72 | final OFMessage msg); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Default constructor. |
| 77 | * |
| 78 | * @param ofSwitch openflow switch that owns this channel |
| 79 | */ |
| 80 | public OFChannelHandler(OFSwitch ofSwitch) { |
| 81 | this.ofSwitch = ofSwitch; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| 86 | super.channelActive(ctx); |
| 87 | this.channel = ctx.channel(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void channelRead(ChannelHandlerContext ctx, Object msg) |
| 92 | throws Exception { |
| 93 | try { |
| 94 | OFMessage ofMsg = (OFMessage) msg; |
| 95 | // TODO process OF message |
| 96 | |
| 97 | } finally { |
| 98 | ReferenceCountUtil.release(msg); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void channelReadComplete(ChannelHandlerContext ctx) |
| 104 | throws Exception { |
| 105 | ctx.flush(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 110 | ctx.close(); |
| 111 | } |
| 112 | |
| 113 | private void setState(ChannelState state) { |
| 114 | this.state = state; |
| 115 | } |
| 116 | } |