Fixed minor sonar warnings.
[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      * @param registry a message registry
48      * @param negotiatorFactory a negotiation factory
49      * @param bossGroup accepts an incoming connection
50      * @param workerGroup handles the traffic of accepted connection
51      */
52     public PCEPDispatcherImpl(final MessageRegistry registry,
53         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
54         final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
55         this(registry, negotiatorFactory, bossGroup, workerGroup, null, null);
56     }
57
58     /**
59      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
60      *
61      * @param registry a message registry
62      * @param negotiatorFactory a negotiation factory
63      * @param bossGroup accepts an incoming connection
64      * @param workerGroup handles the traffic of accepted connection
65      * @param cf MD5ChannelFactory
66      * @param scf MD5ServerChannelFactory
67      */
68     public PCEPDispatcherImpl(final MessageRegistry registry,
69         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
70         final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MD5ChannelFactory<?> cf,
71         final MD5ServerChannelFactory<?> scf) {
72         super(bossGroup, workerGroup);
73         this.cf = cf;
74         this.scf = scf;
75         this.snf = Preconditions.checkNotNull(negotiatorFactory);
76         this.hf = new PCEPHandlerFactory(registry);
77     }
78
79     @Override
80     public synchronized ChannelFuture createServer(final InetSocketAddress address,
81         final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
82         return createServer(address, null, listenerFactory);
83     }
84
85     @Override
86     public void close() {
87     }
88
89     @Override
90     protected void customizeBootstrap(final Bootstrap b) {
91         if (this.keys != null && !this.keys.isEmpty()) {
92             if (this.cf == null) {
93                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
94             }
95
96             LOG.debug("Adding MD5 keys {} to boostrap {}", this.keys, b);
97             b.channelFactory(this.cf);
98             b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
99         }
100     }
101
102     @Override
103     protected void customizeBootstrap(final ServerBootstrap b) {
104         if (this.keys != null && !this.keys.isEmpty()) {
105             if (this.scf == null) {
106                 throw new UnsupportedOperationException("No key access instance available, cannot use key mapping");
107             }
108
109             LOG.debug("Adding MD5 keys {} to boostrap {}", this.keys, b);
110             b.channelFactory(this.scf);
111             b.option(MD5ChannelOption.TCP_MD5SIG, this.keys);
112         }
113
114         // Make sure we are doing round-robin processing
115         b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
116     }
117
118     @Override
119     public synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
120         final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
121         this.keys = keys;
122         final ChannelFuture ret = super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
123             @Override
124             public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
125                 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
126                 ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise));
127                 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());
128             }
129         });
130
131         this.keys = null;
132         return ret;
133     }
134 }