Bug-5878: PCErr not generated by configuring 32-pcep.xml file to not advertise statef...
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.buffer.PooledByteBufAllocator;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelInitializer;
16 import io.netty.channel.ChannelOption;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.epoll.Epoll;
19 import io.netty.channel.epoll.EpollChannelOption;
20 import io.netty.channel.epoll.EpollEventLoopGroup;
21 import io.netty.channel.epoll.EpollServerSocketChannel;
22 import io.netty.channel.socket.SocketChannel;
23 import io.netty.channel.socket.nio.NioServerSocketChannel;
24 import io.netty.util.concurrent.DefaultPromise;
25 import io.netty.util.concurrent.EventExecutor;
26 import io.netty.util.concurrent.GlobalEventExecutor;
27 import io.netty.util.concurrent.Promise;
28 import java.io.Closeable;
29 import java.net.InetSocketAddress;
30 import org.opendaylight.protocol.concepts.KeyMapping;
31 import org.opendaylight.protocol.pcep.PCEPDispatcher;
32 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
33 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
34 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
35 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Implementation of PCEPDispatcher.
41  */
42 public class PCEPDispatcherImpl implements PCEPDispatcher, Closeable {
43     private static final Logger LOG = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
44     private static final Integer SOCKET_BACKLOG_SIZE = 128;
45     private final PCEPSessionNegotiatorFactory snf;
46     private final PCEPHandlerFactory hf;
47
48
49     private final EventLoopGroup bossGroup;
50     private final EventLoopGroup workerGroup;
51     private final EventExecutor executor;
52     private Optional<KeyMapping> keys;
53
54     /**
55      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
56      *
57      * @param registry a message registry
58      * @param negotiatorFactory a negotiation factory
59      * @param bossGroup accepts an incoming connection
60      * @param workerGroup handles the traffic of accepted connection
61      */
62     public PCEPDispatcherImpl(final MessageRegistry registry,
63             final PCEPSessionNegotiatorFactory negotiatorFactory,
64             final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
65         this.snf = Preconditions.checkNotNull(negotiatorFactory);
66         this.hf = new PCEPHandlerFactory(registry);
67         if (Epoll.isAvailable()) {
68             this.bossGroup = new EpollEventLoopGroup();
69             this.workerGroup = new EpollEventLoopGroup();
70         } else {
71             this.bossGroup = Preconditions.checkNotNull(bossGroup);
72             this.workerGroup = Preconditions.checkNotNull(workerGroup);
73         }
74         this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
75     }
76
77     @Override
78     public synchronized ChannelFuture createServer(final InetSocketAddress address,
79             final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
80         return createServer(address, Optional.<KeyMapping>absent(), listenerFactory, peerProposal);
81     }
82
83     @Override
84     public synchronized ChannelFuture createServer(final InetSocketAddress address, final Optional<KeyMapping> keys,
85             final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
86         this.keys = keys;
87
88         final ChannelPipelineInitializer initializer = (ch, promise) -> {
89             ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
90             ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise, peerProposal));
91             ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());
92         };
93
94         final ServerBootstrap b = createServerBootstrap(initializer);
95         final ChannelFuture f = b.bind(address);
96         LOG.debug("Initiated server {} at {}.", f, address);
97
98         this.keys = Optional.absent();
99         return f;
100     }
101
102     protected ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
103         final ServerBootstrap b = new ServerBootstrap();
104         b.childHandler(new ChannelInitializer<SocketChannel>() {
105             @Override
106             protected void initChannel(final SocketChannel ch) {
107                 initializer.initializeChannel(ch, new DefaultPromise(PCEPDispatcherImpl.this.executor));
108             }
109         });
110         b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
111
112         b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
113
114         if (Epoll.isAvailable()) {
115             b.channel(EpollServerSocketChannel.class);
116         } else {
117             b.channel(NioServerSocketChannel.class);
118         }
119         if (this.keys.isPresent()) {
120             if (Epoll.isAvailable()) {
121                 b.option(EpollChannelOption.TCP_MD5SIG, this.keys.get());
122             } else {
123                 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
124             }
125         }
126
127         // Make sure we are doing round-robin processing
128         b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
129
130         if (b.group() == null) {
131             b.group(this.bossGroup, this.workerGroup);
132         }
133
134         return b;
135     }
136
137     @Override
138     public void close() {
139         if (Epoll.isAvailable()) {
140             this.workerGroup.shutdownGracefully().awaitUninterruptibly();
141             this.bossGroup.shutdownGracefully().awaitUninterruptibly();
142         }
143     }
144
145     protected interface ChannelPipelineInitializer {
146         void initializeChannel(SocketChannel socketChannel, Promise<PCEPSessionImpl> promise);
147     }
148
149     @Override
150     public PCEPSessionNegotiatorFactory getPCEPSessionNegotiatorFactory() {
151         return this.snf;
152     }
153 }