Fix Logger use
[openflowjava.git] / 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
16 import java.net.InetAddress;
17 import java.util.Iterator;
18 import java.util.concurrent.TimeUnit;
19
20 import javax.net.ssl.SSLEngine;
21
22 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
23 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Initializes TCP / TLS channel
30  * @author michal.polkorab
31  */
32 public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChannel> {
33
34     private static final Logger LOGGER = LoggerFactory
35             .getLogger(TcpChannelInitializer.class);
36     private final DefaultChannelGroup allChannels;
37     private ConnectionAdapterFactory connectionAdapterFactory;
38
39     /**
40      * default ctor
41      */
42     public TcpChannelInitializer() {
43         this( new DefaultChannelGroup("netty-receiver", null), new ConnectionAdapterFactoryImpl() );
44     }
45
46     /**
47      * Testing Constructor
48      *
49      */
50     protected TcpChannelInitializer( DefaultChannelGroup channelGroup, ConnectionAdapterFactory connAdaptorFactory ) {
51         allChannels = channelGroup ;
52         connectionAdapterFactory = connAdaptorFactory ;
53     }
54
55     @Override
56     protected void initChannel(final SocketChannel ch) {
57         if (ch.remoteAddress() != null) {
58             InetAddress switchAddress = ch.remoteAddress().getAddress();
59             int port = ch.localAddress().getPort();
60             int remotePort = ch.remoteAddress().getPort();
61             LOGGER.debug("Incoming connection from (remote address): {}:{} --> :{}",
62                             switchAddress.toString(), remotePort, port);
63
64             if (!getSwitchConnectionHandler().accept(switchAddress)) {
65                 ch.disconnect();
66                 LOGGER.debug("Incoming connection rejected");
67                 return;
68             }
69         }
70         LOGGER.debug("Incoming connection accepted - building pipeline");
71         allChannels.add(ch);
72         ConnectionFacade connectionFacade = null;
73         connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null);
74         try {
75             LOGGER.debug("calling plugin: {}", getSwitchConnectionHandler());
76             getSwitchConnectionHandler().onSwitchConnected(connectionFacade);
77             connectionFacade.checkListeners();
78             ch.pipeline().addLast(PipelineHandlers.IDLE_HANDLER.name(), new IdleHandler(getSwitchIdleTimeout(), TimeUnit.MILLISECONDS));
79             boolean tlsPresent = false;
80
81             // If this channel is configured to support SSL it will only support SSL
82             if (getTlsConfiguration() != null) {
83                 tlsPresent = true;
84                 SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
85                 SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
86                 engine.setNeedClientAuth(true);
87                 engine.setUseClientMode(false);
88                 ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), new SslHandler(engine));
89             }
90             ch.pipeline().addLast(PipelineHandlers.OF_FRAME_DECODER.name(),
91                     new OFFrameDecoder(connectionFacade, tlsPresent));
92             ch.pipeline().addLast(PipelineHandlers.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
93             OFDecoder ofDecoder = new OFDecoder();
94             ofDecoder.setDeserializationFactory(getDeserializationFactory());
95             ch.pipeline().addLast(PipelineHandlers.OF_DECODER.name(), ofDecoder);
96             OFEncoder ofEncoder = new OFEncoder();
97             ofEncoder.setSerializationFactory(getSerializationFactory());
98             ch.pipeline().addLast(PipelineHandlers.OF_ENCODER.name(), ofEncoder);
99             ch.pipeline().addLast(PipelineHandlers.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
100             if (!tlsPresent) {
101                 connectionFacade.fireConnectionReadyNotification();
102             }
103         } catch (Exception e) {
104             LOGGER.warn("Failed to initialize channel", e);
105             ch.close();
106         }
107     }
108
109     /**
110      * @return iterator through active connections
111      */
112     public Iterator<Channel> getConnectionIterator() {
113         return allChannels.iterator();
114     }
115
116     /**
117      * @return amount of active channels
118      */
119     public int size() {
120         return allChannels.size();
121     }
122 }