Yang model fixed
[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         int port = ch.localAddress().getPort();
51         LOGGER.info("Incoming connection from (remote address): " + switchAddress.toString() + ":" + port);
52         if (!switchConnectionHandler.accept(switchAddress)) {
53             ch.disconnect();
54             LOGGER.info("Incoming connection rejected");
55             return;
56         }
57         LOGGER.info("Incoming connection accepted - building pipeline");
58         allChannels.add(ch);
59         ConnectionFacade connectionFacade = null;
60         connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
61         try {
62             LOGGER.debug("calling plugin: "+switchConnectionHandler);
63             switchConnectionHandler.onSwitchConnected(connectionFacade);
64             connectionFacade.checkListeners();
65             TlsDetector tlsDetector;
66             ch.pipeline().addLast(COMPONENT_NAMES.IDLE_HANDLER.name(), new IdleHandler(switchIdleTimeout, 0, 0, TimeUnit.MILLISECONDS));
67             if (encryption) {
68                 tlsDetector =  new TlsDetector();
69                 tlsDetector.setConnectionFacade(connectionFacade);
70                 ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
71             }
72             ch.pipeline().addLast(COMPONENT_NAMES.OF_FRAME_DECODER.name(), new OFFrameDecoder());
73             ch.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
74             ch.pipeline().addLast(COMPONENT_NAMES.OF_DECODER.name(), new OFDecoder());
75             ch.pipeline().addLast(COMPONENT_NAMES.OF_ENCODER.name(), new OFEncoder());
76             ch.pipeline().addLast(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
77             if (!encryption) {
78                 connectionFacade.fireConnectionReadyNotification();
79             }
80         } catch (Exception e) {
81             LOGGER.error(e.getMessage(), e);
82             ch.close();
83         }
84     }
85     
86     /**
87      * @return iterator through active connections
88      */
89     public Iterator<Channel> getConnectionIterator() {
90         return allChannels.iterator();
91     }
92
93     /**
94      * @return amount of active channels
95      */
96     public int size() {
97         return allChannels.size();
98     }
99     
100     /**
101      * @param switchConnectionHandler the switchConnectionHandler to set
102      */
103     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
104         this.switchConnectionHandler = switchConnectionHandler;
105     }
106
107     /**
108      * @param switchIdleTimeout the switchIdleTimeout to set
109      */
110     public void setSwitchIdleTimeout(long switchIdleTimeout) {
111         this.switchIdleTimeout = switchIdleTimeout;
112     }
113
114     /**
115      * @param tlsSupported
116      */
117     public void setEncryption(boolean tlsSupported) {
118         encryption = tlsSupported;
119     }
120     
121 }