2 * Copyright (c) 2015 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
9 package org.opendaylight.protocol.pcep.pcc.mock;
11 import com.google.common.base.Preconditions;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.bootstrap.ChannelFactory;
14 import io.netty.bootstrap.ServerBootstrap;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.ChannelOption;
17 import io.netty.channel.ServerChannel;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.util.concurrent.Promise;
21 import java.net.InetSocketAddress;
22 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
23 import org.opendaylight.protocol.pcep.PCEPDispatcher;
24 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
25 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
26 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
27 import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
28 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
29 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
30 import org.opendaylight.tcpmd5.api.DummyKeyAccessFactory;
31 import org.opendaylight.tcpmd5.api.KeyAccessFactory;
32 import org.opendaylight.tcpmd5.api.KeyMapping;
33 import org.opendaylight.tcpmd5.jni.NativeKeyAccessFactory;
34 import org.opendaylight.tcpmd5.jni.NativeSupportUnavailableException;
35 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
36 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
37 import org.opendaylight.tcpmd5.netty.MD5NioSocketChannelFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public final class PCCDispatcher extends AbstractPCCDispatcher implements PCEPDispatcher {
42 private static final Logger LOG = LoggerFactory.getLogger(PCCDispatcher.class);
43 private final PCEPHandlerFactory factory;
44 private final MD5ChannelFactory<?> cf;
45 private final PCEPSessionNegotiatorFactory snf;
46 private final PCEPHandlerFactory hf;
47 private final ChannelFactory<? extends ServerChannel> scf;
48 private InetSocketAddress localAddress;
49 private KeyMapping keys;
51 public PCCDispatcher(final MessageRegistry registry, final PCEPSessionNegotiatorFactory negotiatorFactory) {
52 super(new NioEventLoopGroup(), new NioEventLoopGroup());
53 this.snf = Preconditions.checkNotNull(negotiatorFactory);
54 this.factory = new PCEPHandlerFactory(registry);
55 this.cf = new MD5NioSocketChannelFactory(DeafultKeyAccessFactory.getKeyAccessFactory());
56 this.hf = new PCEPHandlerFactory(registry);
62 protected void customizeBootstrap(final Bootstrap b) {
63 if (this.keys != null && !this.keys.isEmpty()) {
64 if (this.cf == null) {
65 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
68 b.channelFactory(this.cf);
69 b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
71 if (this.localAddress != null) {
72 b.localAddress(this.localAddress);
76 public synchronized void createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
77 final ReconnectStrategyFactory strategyFactory, final PCEPSessionListenerFactory listenerFactory,
78 final PCEPSessionNegotiatorFactory negotiatorFactory, final KeyMapping keys) {
79 this.localAddress = localAddress;
81 super.createReconnectingClient(remoteAddress, strategyFactory, new AbstractPCCDispatcher.ChannelPipelineInitializer() {
83 public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
84 ch.pipeline().addLast(PCCDispatcher.this.factory.getDecoders());
85 ch.pipeline().addLast("negotiator",
86 negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise, null));
87 ch.pipeline().addLast(PCCDispatcher.this.factory.getEncoders());
90 this.localAddress = null;
95 public synchronized ChannelFuture createServer(final InetSocketAddress address, final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
96 return createServer(address, null, listenerFactory, peerProposal);
100 public void customizeBootstrap(final ServerBootstrap b) {
101 if (this.keys != null && !this.keys.isEmpty()) {
102 if (this.scf == null) {
103 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
106 LOG.debug("Adding MD5 keys {} to boostrap {}", this.keys, b);
107 b.channelFactory(this.scf);
108 b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
111 // Make sure we are doing round-robin processing
112 b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
116 public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
117 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
119 final ChannelFuture ret = super.createServer(address, new ChannelPipelineInitializer() {
121 public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
122 ch.pipeline().addLast(PCCDispatcher.this.hf.getDecoders());
123 ch.pipeline().addLast("negotiator", PCCDispatcher.this.snf.getSessionNegotiator(listenerFactory, ch, promise, peerProposal));
124 ch.pipeline().addLast(PCCDispatcher.this.hf.getEncoders());
132 private static final class DeafultKeyAccessFactory {
133 private static final Logger LOG = LoggerFactory.getLogger(DeafultKeyAccessFactory.class);
134 private static final KeyAccessFactory FACTORY;
137 KeyAccessFactory factory;
140 factory = NativeKeyAccessFactory.getInstance();
141 } catch (final NativeSupportUnavailableException e) {
142 LOG.debug("Native key access not available, using no-op fallback", e);
143 factory = DummyKeyAccessFactory.getInstance();
149 private DeafultKeyAccessFactory() {
150 throw new UnsupportedOperationException("Utility class should never be instantiated");
153 public static KeyAccessFactory getKeyAccessFactory() {