BUG-54 : switched channel pipeline to be protocol specific.
[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 io.netty.channel.ChannelFuture;
11 import io.netty.channel.socket.SocketChannel;
12 import io.netty.util.concurrent.Promise;
13
14 import java.io.IOException;
15 import java.net.InetSocketAddress;
16
17 import org.opendaylight.protocol.framework.AbstractDispatcher;
18 import org.opendaylight.protocol.framework.SessionListenerFactory;
19 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
20 import org.opendaylight.protocol.pcep.PCEPDispatcher;
21 import org.opendaylight.protocol.pcep.PCEPMessage;
22 import org.opendaylight.protocol.pcep.PCEPSessionListener;
23
24 import com.google.common.base.Preconditions;
25
26 /**
27  * Implementation of PCEPDispatcher.
28  */
29 public class PCEPDispatcherImpl extends AbstractDispatcher<PCEPSessionImpl, PCEPSessionListener> implements PCEPDispatcher {
30
31         private final SessionNegotiatorFactory<PCEPMessage, PCEPSessionImpl, PCEPSessionListener> snf;
32
33         private final PCEPHandlerFactory hf = new PCEPHandlerFactory();
34
35         /**
36          * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
37          * 
38          * @throws IOException if some error occurred during opening the selector
39          */
40         public PCEPDispatcherImpl(final SessionNegotiatorFactory<PCEPMessage, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory) {
41                 super();
42                 this.snf = Preconditions.checkNotNull(negotiatorFactory);
43         }
44
45         @Override
46         public ChannelFuture createServer(final InetSocketAddress address, final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
47                 return super.createServer(address, listenerFactory);
48         }
49
50         @Override
51         public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise,
52                         final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
53                 ch.pipeline().addLast(this.hf.getDecoders());
54                 ch.pipeline().addLast("negotiator", this.snf.getSessionNegotiator(listenerFactory, ch, promise));
55                 ch.pipeline().addLast(this.hf.getEncoders());
56         }
57 }