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 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 18 | import io.netty.bootstrap.Bootstrap; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 19 | import io.netty.channel.ChannelFuture; |
| 20 | import io.netty.channel.ChannelFutureListener; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 21 | import io.netty.channel.ChannelOption; |
| 22 | import io.netty.channel.EventLoopGroup; |
| 23 | import io.netty.channel.socket.nio.NioSocketChannel; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 24 | import org.onosproject.ofagent.api.OFController; |
| 25 | import org.onosproject.ofagent.api.OFSwitch; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 29 | import java.net.InetSocketAddress; |
| 30 | import java.net.SocketAddress; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 31 | import java.util.concurrent.atomic.AtomicInteger; |
| 32 | |
| 33 | /** |
| 34 | * Implementation of OpenFlow connection handler. |
| 35 | * It retries a connection for a certain amount of time and then give up. |
| 36 | */ |
| 37 | public final class OFConnectionHandler implements ChannelFutureListener { |
| 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 41 | private final AtomicInteger retryCount; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 42 | private final OFSwitch ofSwitch; |
| 43 | private final OFController controller; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 44 | private final EventLoopGroup workGroup; |
| 45 | private static final int MAX_RETRY = 3; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Default constructor. |
| 49 | * |
| 50 | * @param ofSwitch openflow switch that initiates this connection |
| 51 | * @param controller controller to connect |
| 52 | * @param workGroup work group for connection |
| 53 | */ |
| 54 | public OFConnectionHandler(OFSwitch ofSwitch, OFController controller, |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 55 | EventLoopGroup workGroup) { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 56 | this.ofSwitch = ofSwitch; |
| 57 | this.controller = controller; |
| 58 | this.workGroup = workGroup; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 59 | this.retryCount = new AtomicInteger(); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates a connection to the supplied controller. |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 64 | * |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 65 | */ |
| 66 | public void connect() { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 67 | |
| 68 | SocketAddress remoteAddr = new InetSocketAddress(controller.ip().toInetAddress(), controller.port().toInt()); |
| 69 | |
| 70 | log.debug("Connecting to controller {}:{}", controller.ip(), controller.port()); |
| 71 | Bootstrap bootstrap = new Bootstrap(); |
| 72 | bootstrap.group(workGroup) |
| 73 | .channel(NioSocketChannel.class) |
| 74 | .option(ChannelOption.SO_KEEPALIVE, true) |
| 75 | .handler(new OFChannelInitializer(ofSwitch)); |
| 76 | |
| 77 | bootstrap.connect(remoteAddr).addListener(this); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void operationComplete(ChannelFuture future) throws Exception { |
| 82 | |
| 83 | if (future.isSuccess()) { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 84 | ofSwitch.addControllerChannel(future.channel()); |
| 85 | log.debug("Connected to controller {}:{} for device {}", |
| 86 | controller.ip(), controller.port(), ofSwitch.device().id()); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 87 | } else { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 88 | log.info("Failed to connect controller {}:{}. Retry...", controller.ip(), controller.port()); |
| 89 | if (retryCount.getAndIncrement() < MAX_RETRY) { |
| 90 | this.connect(); |
| 91 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |