Javadoc update
[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     
32     /**
33      * default ctor
34      */
35     public PublishingChannelInitializer() {
36         allChannels = new DefaultChannelGroup("netty-receiver", null);
37     }
38     
39     @Override
40     protected void initChannel(SocketChannel ch) {
41         InetAddress switchAddress = ch.remoteAddress().getAddress();
42         LOGGER.info("Incoming connection from (remote address): " + switchAddress.toString());
43         if (!switchConnectionHandler.accept(switchAddress)) {
44             ch.disconnect();
45             LOGGER.info("Incoming connection rejected");
46             return;
47         }
48         LOGGER.info("Incoming connection accepted - building pipeline");
49         allChannels.add(ch);
50         ConnectionFacade connectionFacade = null;
51         connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
52         try {
53             LOGGER.debug("calling plugin: "+switchConnectionHandler);
54             switchConnectionHandler.onSwitchConnected(connectionFacade);
55             connectionFacade.checkListeners();
56
57             TlsDetector tlsDetector = new TlsDetector();
58             tlsDetector.setConnectionFacade(connectionFacade);
59             
60             ch.pipeline().addLast(COMPONENT_NAMES.IDLE_HANDLER.name(), new IdleHandler(switchIdleTimeout, 0, 0, TimeUnit.MILLISECONDS));
61             ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
62             ch.pipeline().addLast(COMPONENT_NAMES.OF_FRAME_DECODER.name(), new OFFrameDecoder());
63             ch.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
64             ch.pipeline().addLast(COMPONENT_NAMES.OF_DECODER.name(), new OF13Decoder());
65             ch.pipeline().addLast(COMPONENT_NAMES.OF_ENCODER.name(), new OF13Encoder());
66             ch.pipeline().addLast(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
67         } catch (Exception e) {
68             LOGGER.error(e.getMessage(), e);
69             ch.close();
70         }
71     }
72     
73     /**
74      * @return iterator through active connections
75      */
76     public Iterator<Channel> getConnectionIterator() {
77         return allChannels.iterator();
78     }
79
80     /**
81      * @return amount of active channels
82      */
83     public int size() {
84         return allChannels.size();
85     }
86     
87     /**
88      * @param switchConnectionHandler the switchConnectionHandler to set
89      */
90     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
91         this.switchConnectionHandler = switchConnectionHandler;
92     }
93
94     /**
95      * @param switchIdleTimeout the switchIdleTimeout to set
96      */
97     public void setSwitchIdleTimeout(long switchIdleTimeout) {
98         this.switchIdleTimeout = switchIdleTimeout;
99     }
100     
101 }