2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core;
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;
16 import java.net.InetAddress;
17 import java.util.Iterator;
18 import java.util.concurrent.TimeUnit;
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;
29 * Initializes the channel
30 * @author michal.polkorab
32 public class PublishingChannelInitializer extends ChannelInitializer<SocketChannel> {
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;
45 public PublishingChannelInitializer() {
46 allChannels = new DefaultChannelGroup("netty-receiver", null);
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)) {
58 LOGGER.info("Incoming connection rejected");
61 LOGGER.info("Incoming connection accepted - building pipeline");
63 ConnectionFacade connectionFacade = null;
64 connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
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));
72 tlsDetector = new TlsDetector();
73 tlsDetector.setConnectionFacade(connectionFacade);
74 ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
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));
84 connectionFacade.fireConnectionReadyNotification();
86 } catch (Exception e) {
87 LOGGER.error(e.getMessage(), e);
93 * @return iterator through active connections
95 public Iterator<Channel> getConnectionIterator() {
96 return allChannels.iterator();
100 * @return amount of active channels
103 return allChannels.size();
107 * @param switchConnectionHandler the switchConnectionHandler to set
109 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
110 this.switchConnectionHandler = switchConnectionHandler;
114 * @param switchIdleTimeout the switchIdleTimeout to set
116 public void setSwitchIdleTimeout(long switchIdleTimeout) {
117 this.switchIdleTimeout = switchIdleTimeout;
121 * @param tlsSupported
123 public void setEncryption(boolean tlsSupported) {
124 encryption = tlsSupported;
128 * @param serializationFactory
130 public void setSerializationFactory(SerializationFactory serializationFactory) {
131 this.serializationFactory = serializationFactory;