Merge "BUG-612 : defensive parser for LSPA"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import com.google.common.base.Preconditions;
11
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;
18
19 import java.net.InetSocketAddress;
20
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;
34
35 /**
36  * Implementation of PCEPDispatcher.
37  */
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;
45
46     /**
47      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
48      */
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);
53     }
54
55     /**
56      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
57      */
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);
63         this.cf = cf;
64         this.scf = scf;
65         this.snf = Preconditions.checkNotNull(negotiatorFactory);
66         this.hf = new PCEPHandlerFactory(registry);
67     }
68
69     @Override
70     public synchronized ChannelFuture createServer(final InetSocketAddress address,
71             final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
72         return createServer(address, null, listenerFactory);
73     }
74
75     @Override
76     public void close() {
77     }
78
79     @Override
80     protected void customizeBootstrap(final Bootstrap b) {
81         if (keys != null && !keys.isEmpty()) {
82             if (cf == null) {
83                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
84             }
85
86             LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
87             b.channelFactory(cf);
88             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
89         }
90     }
91
92     @Override
93     protected void customizeBootstrap(final ServerBootstrap b) {
94         if (keys != null && !keys.isEmpty()) {
95             if (scf == null) {
96                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
97             }
98
99             LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
100             b.channelFactory(scf);
101             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
102         }
103     }
104
105     @Override
106     public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
107             final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
108         this.keys = keys;
109         ChannelFuture ret = super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
110             @Override
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());
115             }
116         });
117
118         this.keys = null;
119         return ret;
120     }
121 }