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.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Initializes the channel
29 * @author michal.polkorab
31 public class PublishingChannelInitializer extends ChannelInitializer<SocketChannel> {
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;
43 public PublishingChannelInitializer() {
44 allChannels = new DefaultChannelGroup("netty-receiver", null);
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)) {
54 LOGGER.info("Incoming connection rejected");
57 LOGGER.info("Incoming connection accepted - building pipeline");
59 ConnectionFacade connectionFacade = null;
60 connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
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));
68 tlsDetector = new TlsDetector();
69 tlsDetector.setConnectionFacade(connectionFacade);
70 ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
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));
78 connectionFacade.fireConnectionReadyNotification();
80 } catch (Exception e) {
81 LOGGER.error(e.getMessage(), e);
87 * @return iterator through active connections
89 public Iterator<Channel> getConnectionIterator() {
90 return allChannels.iterator();
94 * @return amount of active channels
97 return allChannels.size();
101 * @param switchConnectionHandler the switchConnectionHandler to set
103 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
104 this.switchConnectionHandler = switchConnectionHandler;
108 * @param switchIdleTimeout the switchIdleTimeout to set
110 public void setSwitchIdleTimeout(long switchIdleTimeout) {
111 this.switchIdleTimeout = switchIdleTimeout;
115 * @param tlsSupported
117 public void setEncryption(boolean tlsSupported) {
118 encryption = tlsSupported;