85f99666f35704531f35e4054ece69db40ca4ab2
[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.ChannelOption;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.socket.SocketChannel;
18 import io.netty.util.concurrent.Promise;
19
20 import java.net.InetSocketAddress;
21
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;
35
36 /**
37  * Implementation of PCEPDispatcher.
38  */
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;
46
47     /**
48      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
49      */
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);
54     }
55
56     /**
57      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
58      */
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);
64         this.cf = cf;
65         this.scf = scf;
66         this.snf = Preconditions.checkNotNull(negotiatorFactory);
67         this.hf = new PCEPHandlerFactory(registry);
68     }
69
70     @Override
71     public synchronized ChannelFuture createServer(final InetSocketAddress address,
72             final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
73         return createServer(address, null, listenerFactory);
74     }
75
76     @Override
77     public void close() {
78     }
79
80     @Override
81     protected void customizeBootstrap(final Bootstrap b) {
82         if (keys != null && !keys.isEmpty()) {
83             if (cf == null) {
84                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
85             }
86
87             LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
88             b.channelFactory(cf);
89             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
90         }
91     }
92
93     @Override
94     protected void customizeBootstrap(final ServerBootstrap b) {
95         if (keys != null && !keys.isEmpty()) {
96             if (scf == null) {
97                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
98             }
99
100             LOG.debug("Adding MD5 keys {} to boostrap {}", keys, b);
101             b.channelFactory(scf);
102             b.option(MD5ChannelOption.TCP_MD5SIG, keys);
103         }
104
105         // Make sure we are doing round-robin processing
106         b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
107     }
108
109     @Override
110     public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
111             final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
112         this.keys = keys;
113         ChannelFuture ret = super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
114             @Override
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());
119             }
120         });
121
122         this.keys = null;
123         return ret;
124     }
125 }