Merge "Uncommented now working tests."
[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.channel.ChannelFuture;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.concurrent.Promise;
15 import org.opendaylight.protocol.framework.AbstractDispatcher;
16 import org.opendaylight.protocol.framework.SessionListenerFactory;
17 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
18 import org.opendaylight.protocol.pcep.PCEPDispatcher;
19 import org.opendaylight.protocol.pcep.PCEPSessionListener;
20 import org.opendaylight.protocol.pcep.spi.MessageHandlerRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
25
26 /**
27  * Implementation of PCEPDispatcher.
28  */
29 public class PCEPDispatcherImpl extends AbstractDispatcher<PCEPSessionImpl, PCEPSessionListener> implements PCEPDispatcher, AutoCloseable {
30
31         private final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf;
32         private final PCEPHandlerFactory hf;
33
34         /**
35          * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
36          *
37          * @throws IOException if some error occurred during opening the selector
38          */
39         public PCEPDispatcherImpl(final MessageHandlerRegistry registry,
40                         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
41                         EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
42                 super(bossGroup, workerGroup);
43                 this.snf = Preconditions.checkNotNull(negotiatorFactory);
44                 this.hf = new PCEPHandlerFactory(registry);
45         }
46
47         @Override
48         public ChannelFuture createServer(final InetSocketAddress address, final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
49                 return super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
50                         @Override
51                         public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
52                                 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
53                                 ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise));
54                                 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());
55                         }
56                 });
57         }
58
59     @Override
60     public void close() {
61     }
62 }