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 LOGGER.info("Incoming connection from (remote address): " + switchAddress.toString());
51 if (!switchConnectionHandler.accept(switchAddress)) {
53 LOGGER.info("Incoming connection rejected");
56 LOGGER.info("Incoming connection accepted - building pipeline");
58 ConnectionFacade connectionFacade = null;
59 connectionFacade = ConnectionAdapterFactory.createConnectionFacade(ch);
61 LOGGER.debug("calling plugin: "+switchConnectionHandler);
62 switchConnectionHandler.onSwitchConnected(connectionFacade);
63 connectionFacade.checkListeners();
64 TlsDetector tlsDetector;
65 ch.pipeline().addLast(COMPONENT_NAMES.IDLE_HANDLER.name(), new IdleHandler(switchIdleTimeout, 0, 0, TimeUnit.MILLISECONDS));
67 tlsDetector = new TlsDetector();
68 tlsDetector.setConnectionFacade(connectionFacade);
69 ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), tlsDetector);
71 ch.pipeline().addLast(COMPONENT_NAMES.OF_FRAME_DECODER.name(), new OFFrameDecoder());
72 ch.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
73 ch.pipeline().addLast(COMPONENT_NAMES.OF_DECODER.name(), new OFDecoder());
74 ch.pipeline().addLast(COMPONENT_NAMES.OF_ENCODER.name(), new OFEncoder());
75 ch.pipeline().addLast(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
77 connectionFacade.fireConnectionReadyNotification();
79 } catch (Exception e) {
80 LOGGER.error(e.getMessage(), e);
86 * @return iterator through active connections
88 public Iterator<Channel> getConnectionIterator() {
89 return allChannels.iterator();
93 * @return amount of active channels
96 return allChannels.size();
100 * @param switchConnectionHandler the switchConnectionHandler to set
102 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
103 this.switchConnectionHandler = switchConnectionHandler;
107 * @param switchIdleTimeout the switchIdleTimeout to set
109 public void setSwitchIdleTimeout(long switchIdleTimeout) {
110 this.switchIdleTimeout = switchIdleTimeout;
114 * @param tlsSupported
116 public void setEncryption(boolean tlsSupported) {
117 encryption = tlsSupported;