2 * Copyright (c) 2013 Cisco Systems, Inc. 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
8 package org.opendaylight.protocol.pcep.impl;
10 import com.google.common.base.Preconditions;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.bootstrap.ServerBootstrap;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelOption;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.socket.SocketChannel;
18 import io.netty.util.concurrent.Promise;
20 import java.net.InetSocketAddress;
22 import org.opendaylight.protocol.framework.AbstractDispatcher;
23 import org.opendaylight.protocol.framework.SessionListenerFactory;
24 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
25 import org.opendaylight.protocol.pcep.PCEPDispatcher;
26 import org.opendaylight.protocol.pcep.PCEPSessionListener;
27 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
28 import org.opendaylight.tcpmd5.api.KeyMapping;
29 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
30 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
31 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Implementation of PCEPDispatcher.
39 public class PCEPDispatcherImpl extends AbstractDispatcher<PCEPSessionImpl, PCEPSessionListener> implements PCEPDispatcher, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
41 private final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf;
42 private final MD5ServerChannelFactory<?> scf;
43 private final MD5ChannelFactory<?> cf;
44 private final PCEPHandlerFactory hf;
45 private KeyMapping keys;
48 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
50 public PCEPDispatcherImpl(final MessageRegistry registry,
51 final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
52 final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
53 this(registry, negotiatorFactory, bossGroup, workerGroup, null, null);
57 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
59 public PCEPDispatcherImpl(final MessageRegistry registry,
60 final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
61 final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf,
62 final MD5ServerChannelFactory<?> scf) {
63 super(bossGroup, workerGroup);
66 this.snf = Preconditions.checkNotNull(negotiatorFactory);
67 this.hf = new PCEPHandlerFactory(registry);
71 public synchronized ChannelFuture createServer(final InetSocketAddress address,
72 final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
73 return createServer(address, null, listenerFactory);
81 protected void customizeBootstrap(final Bootstrap b) {
82 if (keys != null && !keys.isEmpty()) {
84 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
87 LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
89 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
94 protected void customizeBootstrap(final ServerBootstrap b) {
95 if (keys != null && !keys.isEmpty()) {
97 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
100 LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
101 b.channelFactory(scf);
102 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
105 // Make sure we are doing round-robin processing
106 b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
110 public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
111 final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
113 ChannelFuture ret = super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
115 public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
116 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
117 ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise));
118 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());