Fixed various findbugs issues in PCEP.
[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 import io.netty.bootstrap.Bootstrap;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.ChannelOption;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.socket.SocketChannel;
17 import io.netty.util.concurrent.Promise;
18 import java.net.InetSocketAddress;
19 import org.opendaylight.protocol.framework.AbstractDispatcher;
20 import org.opendaylight.protocol.framework.SessionListenerFactory;
21 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
22 import org.opendaylight.protocol.pcep.PCEPDispatcher;
23 import org.opendaylight.protocol.pcep.PCEPSessionListener;
24 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
25 import org.opendaylight.tcpmd5.api.KeyMapping;
26 import org.opendaylight.tcpmd5.netty.MD5ChannelFactory;
27 import org.opendaylight.tcpmd5.netty.MD5ChannelOption;
28 import org.opendaylight.tcpmd5.netty.MD5ServerChannelFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Implementation of PCEPDispatcher.
35  */
36 public class PCEPDispatcherImpl extends AbstractDispatcher<PCEPSessionImpl, PCEPSessionListener> implements PCEPDispatcher {
37     private static final Logger LOG = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
38     private final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf;
39     private final MD5ServerChannelFactory<?> scf;
40     private final MD5ChannelFactory<?> cf;
41     private final PCEPHandlerFactory hf;
42     private KeyMapping keys;
43
44     /**
45      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
46      */
47     public PCEPDispatcherImpl(final MessageRegistry registry,
48         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
49         final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
50         this(registry, negotiatorFactory, bossGroup, workerGroup, null, null);
51     }
52
53     /**
54      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
55      */
56     public PCEPDispatcherImpl(final MessageRegistry registry,
57         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
58         final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf,
59         final MD5ServerChannelFactory<?> scf) {
60         super(bossGroup, workerGroup);
61         this.cf = cf;
62         this.scf = scf;
63         this.snf = Preconditions.checkNotNull(negotiatorFactory);
64         this.hf = new PCEPHandlerFactory(registry);
65     }
66
67     @Override
68     public synchronized ChannelFuture createServer(final InetSocketAddress address,
69         final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
70         return createServer(address, null, listenerFactory);
71     }
72
73     @Override
74     public void close() {
75     }
76
77     @Override
78     protected void customizeBootstrap(final Bootstrap b) {
79         if (this.keys != null && !this.keys.isEmpty()) {
80             if (this.cf == null) {
81                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
82             }
83
84             LOG.debug("Adding MD5 keys {} to boostrap {}", this.keys, b);
85             b.channelFactory(this.cf);
86             b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
87         }
88     }
89
90     @Override
91     protected void customizeBootstrap(final ServerBootstrap b) {
92         if (this.keys != null && !this.keys.isEmpty()) {
93             if (this.scf == null) {
94                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
95             }
96
97             LOG.debug("Adding MD5 keys {} to boostrap {}", this.keys, b);
98             b.channelFactory(this.scf);
99             b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
100         }
101
102         // Make sure we are doing round-robin processing
103         b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
104     }
105
106     @Override
107     public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
108         final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
109         this.keys = keys;
110         final ChannelFuture ret = super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
111             @Override
112             public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
113                 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
114                 ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise));
115                 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());
116             }
117         });
118
119         this.keys = null;
120         return ret;
121     }
122 }