1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
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;
9 import java.net.InetAddress;
10 import java.util.Iterator;
11 import java.util.concurrent.TimeUnit;
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;
21 * @author michal.polkorab
24 public class PublishingChannelInitializer extends ChannelInitializer<SocketChannel> {
26 private static final Logger LOGGER = LoggerFactory
27 .getLogger(PublishingChannelInitializer.class);
28 private DefaultChannelGroup allChannels;
29 private SwitchConnectionHandler switchConnectionHandler;
30 private long switchIdleTimeout;
35 public PublishingChannelInitializer() {
36 allChannels = new DefaultChannelGroup("netty-receiver", null);
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)) {
45 LOGGER.info("Incoming connection rejected");
48 LOGGER.info("Incoming connection accepted - building pipeline");
50 ConnectionFacade connectionAdapter = null;
51 connectionAdapter = ConnectionAdapterFactory.createConnectionAdapter(ch);
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);
70 * @return iterator through active connections
72 public Iterator<Channel> getConnectionIterator() {
73 return allChannels.iterator();
77 * @return amount of active channels
80 return allChannels.size();
84 * @param switchConnectionHandler the switchConnectionHandler to set
86 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
87 this.switchConnectionHandler = switchConnectionHandler;
91 * @param switchIdleTimeout the switchIdleTimeout to set
93 public void setSwitchIdleTimeout(long switchIdleTimeout) {
94 this.switchIdleTimeout = switchIdleTimeout;