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;
16 import java.net.InetAddress;
17 import java.util.Iterator;
18 import java.util.concurrent.TimeUnit;
20 import javax.net.ssl.SSLEngine;
22 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterFactory;
23 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterFactoryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionFacade;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Initializes TCP / TLS channel
30 * @author michal.polkorab
32 public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChannel> {
34 private static final Logger LOGGER = LoggerFactory
35 .getLogger(TcpChannelInitializer.class);
36 private final DefaultChannelGroup allChannels;
37 private ConnectionAdapterFactory connectionAdapterFactory;
42 public TcpChannelInitializer() {
43 this( new DefaultChannelGroup("netty-receiver", null), new ConnectionAdapterFactoryImpl() );
50 protected TcpChannelInitializer( DefaultChannelGroup channelGroup, ConnectionAdapterFactory connAdaptorFactory ) {
51 allChannels = channelGroup ;
52 connectionAdapterFactory = connAdaptorFactory ;
56 protected void initChannel(final SocketChannel ch) {
57 InetAddress switchAddress = ch.remoteAddress().getAddress();
58 int port = ch.localAddress().getPort();
59 int remotePort = ch.remoteAddress().getPort();
60 LOGGER.info("Incoming connection from (remote address): " + switchAddress.toString()
61 + ":" + remotePort + " --> :" + port);
62 if (!getSwitchConnectionHandler().accept(switchAddress)) {
64 LOGGER.info("Incoming connection rejected");
67 LOGGER.info("Incoming connection accepted - building pipeline");
69 ConnectionFacade connectionFacade = null;
70 connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null);
72 LOGGER.debug("calling plugin: " + getSwitchConnectionHandler());
73 getSwitchConnectionHandler().onSwitchConnected(connectionFacade);
74 connectionFacade.checkListeners();
75 ch.pipeline().addLast(PipelineHandlers.IDLE_HANDLER.name(), new IdleHandler(getSwitchIdleTimeout(), TimeUnit.MILLISECONDS));
77 // If this channel is configured to support SSL it will only support SSL
78 if (getTlsConfiguration() != null) {
79 SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
80 SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
81 engine.setNeedClientAuth(true);
82 engine.setUseClientMode(false);
83 ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), new SslHandler(engine));
85 ch.pipeline().addLast(PipelineHandlers.OF_FRAME_DECODER.name(), new OFFrameDecoder(connectionFacade));
86 ch.pipeline().addLast(PipelineHandlers.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
87 OFDecoder ofDecoder = new OFDecoder();
88 ofDecoder.setDeserializationFactory(getDeserializationFactory());
89 ch.pipeline().addLast(PipelineHandlers.OF_DECODER.name(), ofDecoder);
90 OFEncoder ofEncoder = new OFEncoder();
91 ofEncoder.setSerializationFactory(getSerializationFactory());
92 ch.pipeline().addLast(PipelineHandlers.OF_ENCODER.name(), ofEncoder);
93 ch.pipeline().addLast(PipelineHandlers.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
94 if (getTlsConfiguration() == null) {
95 connectionFacade.fireConnectionReadyNotification();
97 } catch (Exception e) {
98 LOGGER.error("Failed to initialize channel", e);
104 * @return iterator through active connections
106 public Iterator<Channel> getConnectionIterator() {
107 return allChannels.iterator();
111 * @return amount of active channels
114 return allChannels.size();