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