646a997f5b9a97beb70df85c1962f44b8582e331
[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  * @author michal.polkorab
22  *
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 connectionAdapter = null;
51         connectionAdapter = ConnectionAdapterFactory.createConnectionAdapter(ch);
52         try {
53             LOGGER.debug("calling plugin: "+switchConnectionHandler);
54             switchConnectionHandler.onSwitchConnected(connectionAdapter);
55             connectionAdapter.checkListeners();
56             ch.pipeline().addLast(COMPONENT_NAMES.IDLE_HANDLER.name(), new IdleHandler(switchIdleTimeout, 0, 0, TimeUnit.MILLISECONDS));
57             ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), new TlsDetector());
58             ch.pipeline().addLast(COMPONENT_NAMES.OF_FRAME_DECODER.name(), new OFFrameDecoder());
59             ch.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
60             ch.pipeline().addLast(COMPONENT_NAMES.OF_DECODER.name(), new OF13Decoder());
61             ch.pipeline().addLast(COMPONENT_NAMES.OF_ENCODER.name(), new OF13Encoder());
62             ch.pipeline().addLast(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionAdapter));
63         } catch (Exception e) {
64             LOGGER.error(e.getMessage(), e);
65             ch.close();
66         }
67     }
68     
69     /**
70      * @return iterator through active connections
71      */
72     public Iterator<Channel> getConnectionIterator() {
73         return allChannels.iterator();
74     }
75
76     /**
77      * @return amount of active channels
78      */
79     public int size() {
80         return allChannels.size();
81     }
82     
83     /**
84      * @param switchConnectionHandler the switchConnectionHandler to set
85      */
86     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
87         this.switchConnectionHandler = switchConnectionHandler;
88     }
89
90     /**
91      * @param switchIdleTimeout the switchIdleTimeout to set
92      */
93     public void setSwitchIdleTimeout(long switchIdleTimeout) {
94         this.switchIdleTimeout = switchIdleTimeout;
95     }
96     
97 }