Migrate pcep to MD-SAL APIs
[bgpcep.git] / pcep / testtool / src / test / java / org / opendaylight / protocol / pcep / testtool / PCCMock.java
index 5b408f3d3668ea4e3feddb42ae0fdbc95c56c34a..dc2d8bcbd4f24c9f08f5eed5600e54bafe97efa2 100644 (file)
@@ -7,49 +7,44 @@
  */
 package org.opendaylight.protocol.pcep.testtool;
 
-import io.netty.util.HashedWheelTimer;
-import io.netty.util.concurrent.GlobalEventExecutor;
-
+import com.google.common.base.Preconditions;
+import com.google.common.net.HostAndPort;
 import java.net.InetSocketAddress;
+import java.util.ArrayList;
 import java.util.List;
-
-import org.opendaylight.protocol.framework.DispatcherImpl;
-import org.opendaylight.protocol.framework.NeverReconnectStrategy;
-import org.opendaylight.protocol.pcep.PCEPTlv;
+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.PCEPDispatcherImpl;
-import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
-import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
-
-import com.google.common.collect.Lists;
-
-public class PCCMock {
-
-       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 }));
-
-               final DispatcherImpl di = new DispatcherImpl();
-               final PCEPDispatcherImpl d = new PCEPDispatcherImpl(di, new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), new PCEPOpenObject(30, 120, 0, tlvs), 0));
-
-               try {
-                       d.createClient(new InetSocketAddress("127.0.0.3", 12345), new SimpleSessionListener(),
-                                       new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 2000)).get();
-
-                       // 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);
-               } finally {
-                       // di.stop();
-               }
-       }
+import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
+import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
+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();
+        }
+    }
 }