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 connectionFacade = null;
51 connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
53 LOGGER.debug("calling plugin: "+switchConnectionHandler);
54 switchConnectionHandler.onSwitchConnected(connectionFacade);
55 connectionFacade.checkListeners();
57 TlsDetector tlsDetector = new TlsDetector();
58 tlsDetector.setConnectionFacade(connectionFacade);
60 ch.pipeline().addLast(COMPONENT_NAMES.IDLE_HANDLER.name(), new IdleHandler(switchIdleTimeout, 0, 0, TimeUnit.MILLISECONDS));
61 ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
62 ch.pipeline().addLast(COMPONENT_NAMES.OF_FRAME_DECODER.name(), new OFFrameDecoder());
63 ch.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
64 ch.pipeline().addLast(COMPONENT_NAMES.OF_DECODER.name(), new OF13Decoder());
65 ch.pipeline().addLast(COMPONENT_NAMES.OF_ENCODER.name(), new OF13Encoder());
66 ch.pipeline().addLast(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
67 } catch (Exception e) {
68 LOGGER.error(e.getMessage(), e);
74 * @return iterator through active connections
76 public Iterator<Channel> getConnectionIterator() {
77 return allChannels.iterator();
81 * @return amount of active channels
84 return allChannels.size();
88 * @param switchConnectionHandler the switchConnectionHandler to set
90 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
91 this.switchConnectionHandler = switchConnectionHandler;
95 * @param switchIdleTimeout the switchIdleTimeout to set
97 public void setSwitchIdleTimeout(long switchIdleTimeout) {
98 this.switchIdleTimeout = switchIdleTimeout;