BUG-54 : switched channel pipeline to be protocol specific.
[bgpcep.git] / pcep / testtool / src / test / java / org / opendaylight / protocol / pcep / testtool / PCCMock.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.testtool;
9
10 import io.netty.channel.socket.SocketChannel;
11 import io.netty.util.HashedWheelTimer;
12 import io.netty.util.concurrent.DefaultPromise;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import io.netty.util.concurrent.Promise;
15
16 import java.net.InetSocketAddress;
17 import java.util.List;
18
19 import org.opendaylight.protocol.framework.AbstractDispatcher;
20 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
21 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
22 import org.opendaylight.protocol.framework.ProtocolMessage;
23 import org.opendaylight.protocol.framework.ProtocolSession;
24 import org.opendaylight.protocol.framework.SessionListener;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
27 import org.opendaylight.protocol.pcep.PCEPMessage;
28 import org.opendaylight.protocol.pcep.PCEPSessionListener;
29 import org.opendaylight.protocol.pcep.PCEPTlv;
30 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
31 import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
32 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
33 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
34 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
35
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.Lists;
38
39 public class PCCMock<M extends ProtocolMessage, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
40                 AbstractDispatcher<S, L> {
41
42         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
43         private final ProtocolHandlerFactory<?> factory;
44
45         public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
46                         final DefaultPromise<PCEPSessionImpl> defaultPromise) {
47                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
48                 this.factory = Preconditions.checkNotNull(factory);
49         }
50
51         @Override
52         public void initializeChannel(final SocketChannel ch, final Promise<S> promise, final SessionListenerFactory<L> listenerFactory) {
53                 ch.pipeline().addLast(this.factory.getDecoders());
54                 ch.pipeline().addLast("negotiator", this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
55                 ch.pipeline().addLast(this.factory.getEncoders());
56         }
57
58         public static void main(final String[] args) throws Exception {
59                 final List<PCEPTlv> tlvs = Lists.newArrayList();
60                 tlvs.add(new NodeIdentifierTlv(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }));
61
62                 final SessionNegotiatorFactory<PCEPMessage, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new PCEPOpenObject(30, 120, 0, tlvs), 0);
63
64                 final PCCMock<PCEPMessage, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
65
66                 pcc.createClient(new InetSocketAddress("127.0.0.3", 12345), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000),
67                                 new SessionListenerFactory<PCEPSessionListener>() {
68
69                                         @Override
70                                         public PCEPSessionListener getSessionListener() {
71                                                 return new SimpleSessionListener();
72                                         }
73                                 }).get();
74         }
75 }