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