Bump MRI upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpChannelInitializer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.core;
10
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 java.net.InetAddress;
17 import java.util.Arrays;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import javax.net.ssl.SSLEngine;
22 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
23 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
24 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
25 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Initializes TCP / TLS channel.
31  * @author michal.polkorab
32  */
33 public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChannel> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(TcpChannelInitializer.class);
36     private final DefaultChannelGroup allChannels;
37     private final ConnectionAdapterFactory connectionAdapterFactory;
38
39     /**
40      * Default constructor.
41      */
42     public TcpChannelInitializer() {
43         this(new DefaultChannelGroup("netty-receiver", null), new ConnectionAdapterFactoryImpl());
44     }
45
46     /**
47      * Testing constructor.
48      */
49     protected TcpChannelInitializer(final DefaultChannelGroup channelGroup,
50             final ConnectionAdapterFactory connAdaptorFactory) {
51         allChannels = channelGroup;
52         connectionAdapterFactory = connAdaptorFactory;
53     }
54
55     @Override
56     @SuppressWarnings("checkstyle:IllegalCatch")
57     protected void initChannel(final SocketChannel ch) {
58         if (ch.remoteAddress() != null) {
59             final InetAddress switchAddress = ch.remoteAddress().getAddress();
60             final int port = ch.localAddress().getPort();
61             final int remotePort = ch.remoteAddress().getPort();
62             LOG.debug("Incoming connection from (remote address): {}:{} --> :{}",
63                 switchAddress.toString(), remotePort, port);
64
65             if (!getSwitchConnectionHandler().accept(switchAddress)) {
66                 ch.disconnect();
67                 LOG.debug("Incoming connection rejected");
68                 return;
69             }
70         }
71         LOG.debug("Incoming connection accepted - building pipeline");
72         allChannels.add(ch);
73         ConnectionFacade connectionFacade = null;
74         connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null, useBarrier(),
75                 getChannelOutboundQueueSize());
76         try {
77             LOG.debug("Calling OF plugin: {}", getSwitchConnectionHandler());
78             getSwitchConnectionHandler().onSwitchConnected(connectionFacade);
79             connectionFacade.checkListeners();
80
81             // If this channel is configured to support SSL it will only support SSL
82             final TlsConfiguration tlsConfig = getTlsConfiguration();
83             if (tlsConfig != null) {
84                 final SslContextFactory sslFactory = new SslContextFactory(tlsConfig);
85                 final SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
86                 engine.setNeedClientAuth(true);
87                 engine.setUseClientMode(false);
88                 List<String> suitesList = getTlsConfiguration().getCipherSuites();
89                 if (suitesList != null && !suitesList.isEmpty()) {
90                     LOG.debug("Requested Cipher Suites are: {}", suitesList);
91                     engine.setEnabledCipherSuites(suitesList.toArray(new String[0]));
92                     LOG.debug("Cipher suites enabled in SSLEngine are: {}",
93                             Arrays.toString(engine.getEnabledCipherSuites()));
94                 }
95                 final SslHandler ssl = new SslHandler(engine);
96                 final Future<Channel> handshakeFuture = ssl.handshakeFuture();
97                 final ConnectionFacade finalConnectionFacade = connectionFacade;
98                 handshakeFuture.addListener(future -> finalConnectionFacade.onSwitchCertificateIdentified(
99                     sslFactory.getSwitchCertificateChain()));
100                 handshakeFuture.addListener(future -> finalConnectionFacade.fireConnectionReadyNotification());
101                 ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), ssl);
102             }
103             ch.pipeline().addLast(PipelineHandlers.OF_FRAME_DECODER.name(),
104                     new OFFrameDecoder(connectionFacade, tlsConfig != null));
105             ch.pipeline().addLast(PipelineHandlers.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
106             final OFDecoder ofDecoder = new OFDecoder();
107             ofDecoder.setDeserializationFactory(getDeserializationFactory());
108             ch.pipeline().addLast(PipelineHandlers.OF_DECODER.name(), ofDecoder);
109             final OFEncoder ofEncoder = new OFEncoder();
110             ofEncoder.setSerializationFactory(getSerializationFactory());
111             ch.pipeline().addLast(PipelineHandlers.OF_ENCODER.name(), ofEncoder);
112             ch.pipeline().addLast(PipelineHandlers.IDLE_HANDLER.name(),
113                     new IdleHandler(getSwitchIdleTimeout(), TimeUnit.MILLISECONDS));
114             ch.pipeline().addLast(PipelineHandlers.DELEGATING_INBOUND_HANDLER.name(),
115                     new DelegatingInboundHandler(connectionFacade));
116
117             if (tlsConfig == null) {
118                 connectionFacade.fireConnectionReadyNotification();
119             }
120         } catch (RuntimeException e) {
121             LOG.warn("Failed to initialize channel", e);
122             ch.close();
123         }
124     }
125
126     /**
127      * Returns the connection iterator.
128      *
129      * @return iterator through active connections
130      */
131     public Iterator<Channel> getConnectionIterator() {
132         return allChannels.iterator();
133     }
134
135     /**
136      * Returns the number of active channels.
137      *
138      * @return amount of active channels
139      */
140     public int size() {
141         return allChannels.size();
142     }
143 }