2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import io.netty.channel.Channel;
12 import io.netty.channel.group.DefaultChannelGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.handler.ssl.SslHandler;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.GenericFutureListener;
17 import java.net.InetAddress;
18 import java.util.Iterator;
19 import java.util.concurrent.TimeUnit;
20 import javax.net.ssl.SSLEngine;
21 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
22 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Initializes TCP / TLS channel
29 * @author michal.polkorab
31 public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChannel> {
33 private static final Logger LOGGER = LoggerFactory
34 .getLogger(TcpChannelInitializer.class);
35 private final DefaultChannelGroup allChannels;
36 private final ConnectionAdapterFactory connectionAdapterFactory;
41 public TcpChannelInitializer() {
42 this( new DefaultChannelGroup("netty-receiver", null), new ConnectionAdapterFactoryImpl() );
49 protected TcpChannelInitializer( final DefaultChannelGroup channelGroup, final ConnectionAdapterFactory connAdaptorFactory ) {
50 allChannels = channelGroup ;
51 connectionAdapterFactory = connAdaptorFactory ;
55 protected void initChannel(final SocketChannel ch) {
56 if (ch.remoteAddress() != null) {
57 final InetAddress switchAddress = ch.remoteAddress().getAddress();
58 final int port = ch.localAddress().getPort();
59 final int remotePort = ch.remoteAddress().getPort();
60 LOGGER.debug("Incoming connection from (remote address): {}:{} --> :{}",
61 switchAddress.toString(), remotePort, port);
63 if (!getSwitchConnectionHandler().accept(switchAddress)) {
65 LOGGER.debug("Incoming connection rejected");
69 LOGGER.debug("Incoming connection accepted - building pipeline");
71 ConnectionFacade connectionFacade = null;
72 connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null, useBarrier());
74 LOGGER.debug("calling plugin: {}", getSwitchConnectionHandler());
75 getSwitchConnectionHandler().onSwitchConnected(connectionFacade);
76 connectionFacade.checkListeners();
77 ch.pipeline().addLast(PipelineHandlers.IDLE_HANDLER.name(), new IdleHandler(getSwitchIdleTimeout(), TimeUnit.MILLISECONDS));
78 boolean tlsPresent = false;
80 // If this channel is configured to support SSL it will only support SSL
81 if (getTlsConfiguration() != null) {
83 final SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
84 final SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
85 engine.setNeedClientAuth(true);
86 engine.setUseClientMode(false);
87 final SslHandler ssl = new SslHandler(engine);
88 final Future<Channel> handshakeFuture = ssl.handshakeFuture();
89 final ConnectionFacade finalConnectionFacade = connectionFacade;
90 handshakeFuture.addListener(new GenericFutureListener<Future<? super Channel>>() {
92 public void operationComplete(final Future<? super Channel> future) throws Exception {
93 finalConnectionFacade.fireConnectionReadyNotification();
96 ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), ssl);
98 ch.pipeline().addLast(PipelineHandlers.OF_FRAME_DECODER.name(),
99 new OFFrameDecoder(connectionFacade, tlsPresent));
100 ch.pipeline().addLast(PipelineHandlers.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
101 final OFDecoder ofDecoder = new OFDecoder();
102 ofDecoder.setDeserializationFactory(getDeserializationFactory());
103 ch.pipeline().addLast(PipelineHandlers.OF_DECODER.name(), ofDecoder);
104 final OFEncoder ofEncoder = new OFEncoder();
105 ofEncoder.setSerializationFactory(getSerializationFactory());
106 ch.pipeline().addLast(PipelineHandlers.OF_ENCODER.name(), ofEncoder);
107 ch.pipeline().addLast(PipelineHandlers.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
109 connectionFacade.fireConnectionReadyNotification();
111 } catch (final Exception e) {
112 LOGGER.warn("Failed to initialize channel", e);
118 * @return iterator through active connections
120 public Iterator<Channel> getConnectionIterator() {
121 return allChannels.iterator();
125 * @return amount of active channels
128 return allChannels.size();