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