Fix the dispatcher not being threadsafe
[bgpcep.git] / pcep / testtool / src / test / java / org / opendaylight / protocol / pcep / testtool / PCCMock.java
index 8236bae2d39b2cbc73eb8fcf191fa859d001bd8a..d668e571adbcbd1f401ad825cc3c273e54684f6c 100644 (file)
@@ -7,96 +7,74 @@
  */
 package org.opendaylight.protocol.pcep.testtool;
 
-import org.opendaylight.protocol.framework.DispatcherImpl;
-import org.opendaylight.protocol.framework.SessionPreferences;
-import org.opendaylight.protocol.pcep.PCEPConnection;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.util.HashedWheelTimer;
+import io.netty.util.concurrent.DefaultPromise;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GlobalEventExecutor;
+import io.netty.util.concurrent.Promise;
+
+import java.net.InetSocketAddress;
+import java.util.List;
+
+import org.opendaylight.protocol.framework.AbstractDispatcher;
+import org.opendaylight.protocol.framework.NeverReconnectStrategy;
+import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
+import org.opendaylight.protocol.framework.ProtocolSession;
+import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.SessionListener;
+import org.opendaylight.protocol.framework.SessionListenerFactory;
+import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
+import org.opendaylight.protocol.pcep.PCEPMessage;
 import org.opendaylight.protocol.pcep.PCEPSessionListener;
-import org.opendaylight.protocol.pcep.PCEPSessionPreferences;
-import org.opendaylight.protocol.pcep.PCEPSessionProposal;
-import org.opendaylight.protocol.pcep.PCEPSessionProposalChecker;
-import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
 import org.opendaylight.protocol.pcep.PCEPTlv;
-import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
+import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
+import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
+import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
+
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.util.List;
-import java.util.concurrent.Executors;
+public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
+AbstractDispatcher<S, L> {
 
-public class PCCMock {
+       private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
+       private final ProtocolHandlerFactory<?> factory;
 
-       public static void main(final String[] args) throws IOException, InterruptedException {
-               final List<PCEPTlv> tlvs = Lists.newArrayList();
-               tlvs.add(new NodeIdentifierTlv(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }));
-               final PCEPSessionPreferences prop = new PCEPSessionPreferences(new PCEPOpenObject(30, 120, 0, tlvs));
-               final DispatcherImpl di = new DispatcherImpl(Executors.defaultThreadFactory());
-               final PCEPDispatcherImpl d = new PCEPDispatcherImpl(di, new PCEPSessionProposalFactory() {
+       public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final ProtocolHandlerFactory<?> factory,
+                       final DefaultPromise<PCEPSessionImpl> defaultPromise) {
+               this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
+               this.factory = Preconditions.checkNotNull(factory);
+       }
 
+       public Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy, final SessionListenerFactory<L> listenerFactory) {
+               return super.createClient(address, strategy, new PipelineInitializer<S>() {
                        @Override
-                       public PCEPSessionProposal getSessionProposal(final InetSocketAddress address, final int sessionId) {
-                               return new PCEPSessionProposal() {
-
-                                       @Override
-                                       public PCEPSessionPreferences getProposal() {
-                                               return prop;
-                                       }
-                               };
+                       public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
+                               ch.pipeline().addLast(factory.getDecoders());
+                               ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
+                               ch.pipeline().addLast(factory.getEncoders());
                        }
                });
+       }
 
-               try {
-
-                       final PCEPSessionProposalChecker check = new PCEPSessionProposalChecker() {
-                               @Override
-                               public Boolean checkSessionCharacteristics(final SessionPreferences openObj) {
-                                       return true;
-                               }
-
-                               @Override
-                               public PCEPSessionPreferences getNewProposal(final SessionPreferences open) {
-                                       return new PCEPSessionPreferences(new PCEPOpenObject(30, 120, 0, null));
-                               }
-                       };
-
-                       d.createClient(new PCEPConnection() {
-                               @Override
-                               public InetSocketAddress getPeerAddress() {
-                                       return new InetSocketAddress("127.0.0.1", 4189);
-                               }
+       public static void main(final String[] args) throws Exception {
+               final List<PCEPTlv> tlvs = Lists.newArrayList();
+               tlvs.add(new NodeIdentifierTlv(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }));
 
-                               @Override
-                               public PCEPSessionProposalChecker getProposalChecker() {
-                                       return check;
-                               }
+               final SessionNegotiatorFactory<PCEPMessage, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new PCEPOpenObject(30, 120, 0, tlvs), 0);
 
-                               @Override
-                               public PCEPSessionPreferences getProposal() {
-                                       return prop;
-                               }
+               final PCCMock<PCEPMessage, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
-                               @Override
-                               public PCEPSessionListener getListener() {
-                                       return new SimpleSessionListener();
-                               }
-                       });
-                       // Thread.sleep(5000);
-                       // final List<CompositeRequestObject> cro = new ArrayList<CompositeRequestObject>();
-                       // cro.add(new CompositeRequestObject(new PCEPRequestParameterObject(false, true, true, true, true, (short)
-                       // 4, 123, false, false),
-                       // new PCEPEndPointsObject<IPv4Address>(new IPv4Address(InetAddress.getByName("10.0.0.3")), new
-                       // IPv4Address(InetAddress.getByName("10.0.0.5")))));
-                       // for (int i = 0; i < 3; i++) {
-                       // Thread.sleep(1000);
-                       // session.sendMessage(new PCEPRequestMessage(cro));
-                       // }
-                       // Thread.sleep(5000);
-                       // Thread.sleep(1000);
+               pcc.createClient(new InetSocketAddress("127.0.0.3", 12345), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000),
+                               new SessionListenerFactory<PCEPSessionListener>() {
 
-               } finally {
-                       di.stop();
-               }
+                       @Override
+                       public PCEPSessionListener getSessionListener() {
+                               return new SimpleSessionListener();
+                       }
+               }).get();
        }
 }