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.ChannelFuture; |
| 19 | import io.netty.channel.ChannelFutureListener; |
| 20 | import io.netty.channel.nio.NioEventLoopGroup; |
| 21 | import org.onosproject.ofagent.api.OFController; |
| 22 | import org.onosproject.ofagent.api.OFSwitch; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import java.util.concurrent.atomic.AtomicInteger; |
| 27 | |
| 28 | /** |
| 29 | * Implementation of OpenFlow connection handler. |
| 30 | * It retries a connection for a certain amount of time and then give up. |
| 31 | */ |
| 32 | public final class OFConnectionHandler implements ChannelFutureListener { |
| 33 | |
| 34 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 35 | |
| 36 | private static final int MAX_RETRY = 10; |
| 37 | |
| 38 | private final AtomicInteger retryCount = new AtomicInteger(); |
| 39 | private final OFSwitch ofSwitch; |
| 40 | private final OFController controller; |
| 41 | private final NioEventLoopGroup workGroup; |
| 42 | |
| 43 | /** |
| 44 | * Default constructor. |
| 45 | * |
| 46 | * @param ofSwitch openflow switch that initiates this connection |
| 47 | * @param controller controller to connect |
| 48 | * @param workGroup work group for connection |
| 49 | */ |
| 50 | public OFConnectionHandler(OFSwitch ofSwitch, OFController controller, |
| 51 | NioEventLoopGroup workGroup) { |
| 52 | this.ofSwitch = ofSwitch; |
| 53 | this.controller = controller; |
| 54 | this.workGroup = workGroup; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a connection to the supplied controller. |
| 59 | */ |
| 60 | public void connect() { |
| 61 | // TODO initiates a connection to the controller |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void operationComplete(ChannelFuture future) throws Exception { |
| 66 | |
| 67 | if (future.isSuccess()) { |
| 68 | log.debug("{} is connected to controller {}", ofSwitch.device().id(), controller); |
| 69 | // TODO do something for a new connection if there's any |
| 70 | } else { |
| 71 | log.debug("{} failed to connect {}, retry..", ofSwitch.device().id(), controller); |
| 72 | // TODO retry connect if retry count is less than MAX |
| 73 | } |
| 74 | } |
| 75 | } |