Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [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.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; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 22 | import io.netty.channel.EventLoop; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 23 | import io.netty.channel.EventLoopGroup; |
| 24 | import io.netty.channel.socket.nio.NioSocketChannel; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 25 | import org.onosproject.ofagent.api.OFController; |
| 26 | import org.onosproject.ofagent.api.OFSwitch; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 30 | import java.net.InetSocketAddress; |
| 31 | import java.net.SocketAddress; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 32 | import java.util.concurrent.TimeUnit; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 33 | import java.util.concurrent.atomic.AtomicInteger; |
| 34 | |
| 35 | /** |
| 36 | * Implementation of OpenFlow connection handler. |
| 37 | * It retries a connection for a certain amount of time and then give up. |
| 38 | */ |
| 39 | public final class OFConnectionHandler implements ChannelFutureListener { |
| 40 | |
| 41 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 42 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 43 | private static final String MSG_STATE = "Device %s %s to controller %s:%s"; |
| 44 | private static final String MSG_CONNECTING = "connecting"; |
| 45 | private static final String MSG_CONNECTED = "connected"; |
| 46 | private static final String MSG_FAILED = "failed to connect"; |
| 47 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 48 | private final AtomicInteger retryCount; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 49 | private final OFSwitch ofSwitch; |
| 50 | private final OFController controller; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 51 | private final EventLoopGroup workGroup; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 52 | |
| 53 | // TODO make this value configurable |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 54 | private static final int MAX_RETRY = 3; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Default constructor. |
| 58 | * |
| 59 | * @param ofSwitch openflow switch that initiates this connection |
| 60 | * @param controller controller to connect |
| 61 | * @param workGroup work group for connection |
| 62 | */ |
| 63 | public OFConnectionHandler(OFSwitch ofSwitch, OFController controller, |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 64 | EventLoopGroup workGroup) { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 65 | this.ofSwitch = ofSwitch; |
| 66 | this.controller = controller; |
| 67 | this.workGroup = workGroup; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 68 | this.retryCount = new AtomicInteger(); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates a connection to the supplied controller. |
| 73 | */ |
| 74 | public void connect() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 75 | SocketAddress remoteAddr = new InetSocketAddress( |
| 76 | controller.ip().toInetAddress(), controller.port().toInt()); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 77 | Bootstrap bootstrap = new Bootstrap(); |
| 78 | bootstrap.group(workGroup) |
| 79 | .channel(NioSocketChannel.class) |
| 80 | .option(ChannelOption.SO_KEEPALIVE, true) |
| 81 | .handler(new OFChannelInitializer(ofSwitch)); |
| 82 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 83 | log.debug(String.format(MSG_STATE, |
| 84 | ofSwitch.dpid(), |
| 85 | MSG_CONNECTING, |
| 86 | controller.ip(), |
| 87 | controller.port())); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 88 | bootstrap.connect(remoteAddr).addListener(this); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void operationComplete(ChannelFuture future) throws Exception { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 93 | if (future.isSuccess()) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 94 | log.info(String.format(MSG_STATE, |
| 95 | ofSwitch.dpid(), |
| 96 | MSG_CONNECTED, |
| 97 | controller.ip(), |
| 98 | controller.port())); |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 99 | // FIXME add close future listener to handle connection lost |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 100 | } else { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 101 | if (retryCount.getAndIncrement() > MAX_RETRY) { |
| 102 | log.warn(String.format(MSG_STATE, |
| 103 | ofSwitch.dpid(), |
| 104 | MSG_FAILED, |
| 105 | controller.ip(), |
| 106 | controller.port())); |
| 107 | } else { |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 108 | final EventLoop loop = future.channel().eventLoop(); |
| 109 | loop.schedule(this::connect, 1L, TimeUnit.SECONDS); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 110 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |