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.EventLoopGroup;
16 import io.netty.channel.socket.SocketChannel;
17 import io.netty.util.concurrent.Promise;
19 import java.net.InetSocketAddress;
21 import org.opendaylight.bgpcep.tcpmd5.KeyMapping;
22 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelFactory;
23 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ChannelOption;
24 import org.opendaylight.bgpcep.tcpmd5.netty.MD5ServerChannelFactory;
25 import org.opendaylight.protocol.framework.AbstractDispatcher;
26 import org.opendaylight.protocol.framework.SessionListenerFactory;
27 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
28 import org.opendaylight.protocol.pcep.PCEPDispatcher;
29 import org.opendaylight.protocol.pcep.PCEPSessionListener;
30 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Implementation of PCEPDispatcher.
38 public class PCEPDispatcherImpl extends AbstractDispatcher<PCEPSessionImpl, PCEPSessionListener> implements PCEPDispatcher, AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
40 private final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf;
41 private final MD5ServerChannelFactory<?> scf;
42 private final MD5ChannelFactory<?> cf;
43 private final PCEPHandlerFactory hf;
44 private KeyMapping keys;
47 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
49 public PCEPDispatcherImpl(final MessageRegistry registry,
50 final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
51 final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
52 this(registry, negotiatorFactory, bossGroup, workerGroup, null, null);
56 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
58 public PCEPDispatcherImpl(final MessageRegistry registry,
59 final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
60 final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf,
61 final MD5ServerChannelFactory<?> scf) {
62 super(bossGroup, workerGroup);
65 this.snf = Preconditions.checkNotNull(negotiatorFactory);
66 this.hf = new PCEPHandlerFactory(registry);
70 public synchronized ChannelFuture createServer(final InetSocketAddress address,
71 final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
72 return createServer(address, null, listenerFactory);
80 protected void customizeBootstrap(final Bootstrap b) {
81 if (keys != null && !keys.isEmpty()) {
83 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
86 LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
88 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
93 protected void customizeBootstrap(final ServerBootstrap b) {
94 if (keys != null && !keys.isEmpty()) {
96 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
99 LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
100 b.channelFactory(scf);
101 b.option(MD5ChannelOption.TCP_MD5SIG, keys);
106 public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
107 final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
109 ChannelFuture ret = super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
111 public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
112 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
113 ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise));
114 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());