Bug-2208: pcc-mock code refactoring
[bgpcep.git] / pcep / pcc-mock / src / main / java / org / opendaylight / protocol / pcep / pcc / mock / PCCDispatcher.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
16 import java.net.InetSocketAddress;
17 import org.opendaylight.protocol.framework.ReconnectStrategy;
18 import org.opendaylight.protocol.framework.SessionListenerFactory;
19 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
20 import org.opendaylight.protocol.pcep.PCEPSessionListener;
21 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
22 import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
23 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
24 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
26
27 public final class PCCDispatcher extends PCEPDispatcherImpl {
28
29     private InetSocketAddress localAddress;
30     private final PCEPHandlerFactory factory;
31
32     public PCCDispatcher(final MessageRegistry registry,
33             final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory) {
34         super(registry, negotiatorFactory, new NioEventLoopGroup(), new NioEventLoopGroup(), null, null);
35         this.factory = new PCEPHandlerFactory(registry);
36     }
37
38     @Override
39     protected void customizeBootstrap(final Bootstrap b) {
40         super.customizeBootstrap(b);
41         if (this.localAddress != null) {
42             b.localAddress(this.localAddress);
43         }
44     }
45
46     public synchronized Future<PCEPSessionImpl> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
47             final ReconnectStrategy strategy, final SessionListenerFactory<PCEPSessionListener> listenerFactory,
48             final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory) {
49         this.localAddress = localAddress;
50         final Future<PCEPSessionImpl> futureClient = super.createClient(remoteAddress, strategy, new PipelineInitializer<PCEPSessionImpl>() {
51             @Override
52             public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
53                 ch.pipeline().addLast(PCCDispatcher.this.factory.getDecoders());
54                 ch.pipeline().addLast("negotiator",
55                         negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
56                 ch.pipeline().addLast(PCCDispatcher.this.factory.getEncoders());
57             }
58         });
59         this.localAddress = null;
60         return futureClient;
61     }
62 }