Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / PublishingChannelInitializer.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.ChannelInitializer;
13 import io.netty.channel.group.DefaultChannelGroup;
14 import io.netty.channel.socket.SocketChannel;
15
16 import java.net.InetAddress;
17 import java.util.Iterator;
18 import java.util.concurrent.TimeUnit;
19
20 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
21 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterFactory;
22 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionFacade;
23 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Initializes the channel
29  * @author michal.polkorab
30  */
31 public class PublishingChannelInitializer extends ChannelInitializer<SocketChannel> {
32
33     private static final Logger LOGGER = LoggerFactory
34             .getLogger(PublishingChannelInitializer.class);
35     private DefaultChannelGroup allChannels;
36     private SwitchConnectionHandler switchConnectionHandler;
37     private long switchIdleTimeout;
38     private boolean encryption;
39     
40     /**
41      * default ctor
42      */
43     public PublishingChannelInitializer() {
44         allChannels = new DefaultChannelGroup("netty-receiver", null);
45     }
46     
47     @Override
48     protected void initChannel(SocketChannel ch) {
49         InetAddress switchAddress = ch.remoteAddress().getAddress();
50         LOGGER.info("Incoming connection from (remote address): " + switchAddress.toString());
51         if (!switchConnectionHandler.accept(switchAddress)) {
52             ch.disconnect();
53             LOGGER.info("Incoming connection rejected");
54             return;
55         }
56         LOGGER.info("Incoming connection accepted - building pipeline");
57         allChannels.add(ch);
58         ConnectionFacade connectionFacade = null;
59         connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
60         try {
61             LOGGER.debug("calling plugin: "+switchConnectionHandler);
62             switchConnectionHandler.onSwitchConnected(connectionFacade);
63             connectionFacade.checkListeners();
64             TlsDetector tlsDetector;
65             ch.pipeline().addLast(COMPONENT_NAMES.IDLE_HANDLER.name(), new IdleHandler(switchIdleTimeout, 0, 0, TimeUnit.MILLISECONDS));
66             if (encryption) {
67                 tlsDetector =  new TlsDetector();
68                 tlsDetector.setConnectionFacade(connectionFacade);
69                 ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
70             }
71             ch.pipeline().addLast(COMPONENT_NAMES.OF_FRAME_DECODER.name(), new OFFrameDecoder());
72             ch.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
73             ch.pipeline().addLast(COMPONENT_NAMES.OF_DECODER.name(), new OFDecoder());
74             ch.pipeline().addLast(COMPONENT_NAMES.OF_ENCODER.name(), new OFEncoder());
75             ch.pipeline().addLast(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
76             if (!encryption) {
77                 connectionFacade.fireConnectionReadyNotification();
78             }
79         } catch (Exception e) {
80             LOGGER.error(e.getMessage(), e);
81             ch.close();
82         }
83     }
84     
85     /**
86      * @return iterator through active connections
87      */
88     public Iterator<Channel> getConnectionIterator() {
89         return allChannels.iterator();
90     }
91
92     /**
93      * @return amount of active channels
94      */
95     public int size() {
96         return allChannels.size();
97     }
98     
99     /**
100      * @param switchConnectionHandler the switchConnectionHandler to set
101      */
102     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
103         this.switchConnectionHandler = switchConnectionHandler;
104     }
105
106     /**
107      * @param switchIdleTimeout the switchIdleTimeout to set
108      */
109     public void setSwitchIdleTimeout(long switchIdleTimeout) {
110         this.switchIdleTimeout = switchIdleTimeout;
111     }
112
113     /**
114      * @param tlsSupported
115      */
116     public void setEncryption(boolean tlsSupported) {
117         encryption = tlsSupported;
118     }
119     
120 }