blob: 7f59cf5e1362611761f4c9a4083c310ca8a6d44f [file] [log] [blame]
Hyunsun Moon90163ba2016-10-12 13:35:14 -07001/*
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 */
16package org.onosproject.ofagent.impl;
17
Daniel Parkbe6b6732016-11-11 15:52:19 +090018import io.netty.bootstrap.Bootstrap;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070019import io.netty.channel.ChannelFuture;
20import io.netty.channel.ChannelFutureListener;
Daniel Parkbe6b6732016-11-11 15:52:19 +090021import io.netty.channel.ChannelOption;
22import io.netty.channel.EventLoopGroup;
23import io.netty.channel.socket.nio.NioSocketChannel;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070024import org.onosproject.ofagent.api.OFController;
25import org.onosproject.ofagent.api.OFSwitch;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
Daniel Parkbe6b6732016-11-11 15:52:19 +090029import java.net.InetSocketAddress;
30import java.net.SocketAddress;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070031import 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 */
37public final class OFConnectionHandler implements ChannelFutureListener {
38
39 private final Logger log = LoggerFactory.getLogger(getClass());
40
Daniel Parkbe6b6732016-11-11 15:52:19 +090041 private final AtomicInteger retryCount;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070042 private final OFSwitch ofSwitch;
43 private final OFController controller;
Daniel Parkbe6b6732016-11-11 15:52:19 +090044 private final EventLoopGroup workGroup;
45 private static final int MAX_RETRY = 3;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070046
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 Parkbe6b6732016-11-11 15:52:19 +090055 EventLoopGroup workGroup) {
Hyunsun Moon90163ba2016-10-12 13:35:14 -070056 this.ofSwitch = ofSwitch;
57 this.controller = controller;
58 this.workGroup = workGroup;
Daniel Parkbe6b6732016-11-11 15:52:19 +090059 this.retryCount = new AtomicInteger();
Hyunsun Moon90163ba2016-10-12 13:35:14 -070060 }
61
62 /**
63 * Creates a connection to the supplied controller.
Daniel Parkbe6b6732016-11-11 15:52:19 +090064 *
Hyunsun Moon90163ba2016-10-12 13:35:14 -070065 */
66 public void connect() {
Daniel Parkbe6b6732016-11-11 15:52:19 +090067
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 Moon90163ba2016-10-12 13:35:14 -070078 }
79
80 @Override
81 public void operationComplete(ChannelFuture future) throws Exception {
82
83 if (future.isSuccess()) {
Daniel Parkbe6b6732016-11-11 15:52:19 +090084 ofSwitch.addControllerChannel(future.channel());
85 log.debug("Connected to controller {}:{} for device {}",
86 controller.ip(), controller.port(), ofSwitch.device().id());
Hyunsun Moon90163ba2016-10-12 13:35:14 -070087 } else {
Daniel Parkbe6b6732016-11-11 15:52:19 +090088 log.info("Failed to connect controller {}:{}. Retry...", controller.ip(), controller.port());
89 if (retryCount.getAndIncrement() < MAX_RETRY) {
90 this.connect();
91 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -070092 }
93 }
94}