Migrate pcep to MD-SAL APIs
[bgpcep.git] / pcep / testtool / src / test / java / org / opendaylight / protocol / pcep / testtool / PCCMock.java
index 9e6ddf62b73005a3addc54f1aa599e157b6450e9..dc2d8bcbd4f24c9f08f5eed5600e54bafe97efa2 100644 (file)
@@ -7,75 +7,44 @@
  */
 package org.opendaylight.protocol.pcep.testtool;
 
-import io.netty.channel.nio.NioEventLoopGroup;
-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 com.google.common.base.Preconditions;
+import com.google.common.net.HostAndPort;
 import java.net.InetSocketAddress;
-
-import org.opendaylight.protocol.framework.AbstractDispatcher;
-import org.opendaylight.protocol.framework.NeverReconnectStrategy;
-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.PCEPSessionListener;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import org.opendaylight.protocol.concepts.KeyMapping;
+import org.opendaylight.protocol.pcep.PCEPCapability;
+import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
+import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
+import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
 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.pcc.mock.protocol.PCCDispatcherImpl;
 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.predundancy.group.id.tlv.PredundancyGroupIdBuilder;
-
-import com.google.common.base.Preconditions;
-
-public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends AbstractDispatcher<S, L> {
-
-       private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
-       private final PCEPHandlerFactory factory;
-
-       public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final PCEPHandlerFactory factory,
-                       final DefaultPromise<PCEPSessionImpl> defaultPromise) {
-               super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
-               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 void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
-                               ch.pipeline().addLast(PCCMock.this.factory.getDecoders());
-                               ch.pipeline().addLast("negotiator", PCCMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
-                               ch.pipeline().addLast(PCCMock.this.factory.getEncoders());
-                       }
-               });
-       }
-
-       public static void main(final String[] args) throws Exception {
-               final TlvsBuilder builder = new TlvsBuilder();
-               builder.setPredundancyGroupId(new PredundancyGroupIdBuilder().setIdentifier(new byte[] { (byte) 127, (byte) 2, (byte) 3, (byte) 7 }).build());
-
-               final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new OpenBuilder().setKeepalive(
-                               (short) 30).setDeadTimer((short) 120).setSessionId((short) 0).setTlvs(builder.build()).build(), 0);
-
-               final PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
-
-               pcc.createClient(new InetSocketAddress("127.0.0.3", 12345), new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000),
-                               new SessionListenerFactory<PCEPSessionListener>() {
-
-                       @Override
-                       public PCEPSessionListener getSessionListener() {
-                               return new SimpleSessionListener();
-                       }
-               }).get();
-       }
+import org.opendaylight.protocol.util.InetSocketAddressUtil;
+
+public final class PCCMock {
+
+    private PCCMock() {
+        throw new UnsupportedOperationException();
+    }
+
+    public static void main(final String[] args) throws InterruptedException, ExecutionException {
+        Preconditions.checkArgument(args.length > 0, "Host and port of server must be provided.");
+        final List<PCEPCapability> caps = new ArrayList<>();
+        final PCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory((short) 120, (short) 30, caps);
+        final PCEPSessionNegotiatorFactory<? extends PCEPSession> snf
+            = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
+        final HostAndPort serverHostAndPort = HostAndPort.fromString(args[0]);
+        final InetSocketAddress serverAddr = new InetSocketAddress(serverHostAndPort.getHost(), serverHostAndPort
+                .getPortOrDefault(12345));
+        final InetSocketAddress clientAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(0);
+
+        try (PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext
+                .getSingletonInstance().getMessageHandlerRegistry())) {
+            pccDispatcher.createClient(serverAddr, -1, SimpleSessionListener::new, snf,
+                    KeyMapping.getKeyMapping(), clientAddr).get();
+        }
+    }
 }