BUG-731 : removed more warnings
[bgpcep.git] / pcep / testtool / src / main / java / org / opendaylight / protocol / pcep / testtool / Main.java
index bb0df5e2f128928a8e74296bfca5c97723f30004..5b1f0625b7cccb189ae81ffe50e4a9a5b30d02da 100644 (file)
  */
 package org.opendaylight.protocol.pcep.testtool;
 
-import io.netty.util.HashedWheelTimer;
-
+import io.netty.channel.nio.NioEventLoopGroup;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-
-import org.opendaylight.protocol.framework.DispatcherImpl;
+import java.net.UnknownHostException;
+import java.util.concurrent.ExecutionException;
 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
+import org.opendaylight.protocol.pcep.ietf.initiated00.Stateful07SessionProposalFactory;
+import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
-import org.opendaylight.protocol.pcep.impl.PCEPSessionProposalFactoryImpl;
-import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
-
-public class Main {
-
-       public static String usage = "DESCRIPTION:\n" + "\tCreates a server with given parameters. As long as it runs, it accepts connections "
-                       + "from PCCs.\n" + "USAGE:\n" + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
-                       + "\t\tFormat: x.x.x.x:y where y is port number.\n\n" +
-
-                       "\t-d, --deadtimer\n" + "\t\tin seconds, value of the desired deadtimer\n"
-                       + "\t\tAccording to RFC5440, recommended value for deadtimer is 4 times the value\n"
-                       + "\t\tof KeepAlive timer. If it's not, a warning is printed.\n"
-                       + "\t\tIf not set, it's value will be derived from KeepAlive timer value.\n\n" +
-
-                       "\t-ka, --keepalive\n" + "\t\tin seconds, value of the desired KeepAlive timer.\n"
-                       + "\t\tIf not present, KeepAlive timer will be set to recommended value (30s).\n\n" +
-
-                       "\t--stateful\n" + "\t\tpassive stateful\n\n" +
-
-                       "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n" +
-
-                       "\t--versioned\n" + "\t\tversioned stateful (implies --stateful)\n\n" +
-
-                       "\t--instant\n"
-                       + "\t\tinstantiated stateful, <seconds> cleanup timeout (default value, if not included = 0) (implies --stateful)\n\n" +
-
-                       "\t-arm, --autoResponseMessages <path to file>\n"
-                       + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
-                       + "\t\t Messages are used as auto response for every message received. Purely for testing puposes! \n\n" +
-
-                       "\t-psm, --periodicallySendMessages <path to file> <period>\n"
-                       + "\t\t <path to file> with groovy script which implements MessageGeneratorService followed by <period> in seconds.\n"
-                       + "\t\t Messages which are sent periodically. Purely for testing puposes! \n\n" +
-
-                       "\t-snm, --sendNowMessage <path to file>\n"
-                       + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
-                       + "\t\t Messages are sent in defined states defined by programmer. Purely for testing puposes! \n\n" +
-
-                       "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
-
-                       "With no parameters, this help is printed.";
-
-       public static void main(final String[] args) throws Exception {
-               if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("--help")) {
-                       System.out.println(Main.usage);
-                       return;
-               }
-
-               InetSocketAddress address = null;
-               int keepAliveValue = 30;
-               int deadTimerValue = 0;
-               boolean stateful = false;
-               boolean active = false;
-               boolean versioned = false;
-               boolean instant = false;
-               int timeout = 0;
-
-               int i = 0;
-               while (i < args.length) {
-                       if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
-                               final String[] ip = args[i + 1].split(":");
-                               address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.valueOf(ip[1]));
-                               i++;
-                       } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--deadtimer")) {
-                               deadTimerValue = Integer.valueOf(args[i + 1]);
-                               i++;
-                       } else if (args[i].equalsIgnoreCase("-ka") || args[i].equalsIgnoreCase("--keepalive")) {
-                               keepAliveValue = Integer.valueOf(args[i + 1]);
-                               i++;
-                       } else if (args[i].equalsIgnoreCase("--stateful")) {
-                               stateful = true;
-                       } else if (args[i].equalsIgnoreCase("--active")) {
-                               stateful = true;
-                               active = true;
-                       } else if (args[i].equalsIgnoreCase("--versioned")) {
-                               stateful = true;
-                               versioned = true;
-                       } else if (args[i].equalsIgnoreCase("--instant")) {
-                               stateful = true;
-                               instant = true;
-                               if (i == args.length - 1) {
-                                       timeout = 0;
-                               } else if (Integer.valueOf(args[i + 1]) > 0 && Integer.valueOf(args[i + 1]) < Integer.MAX_VALUE) {
-                                       timeout = Integer.valueOf(args[i + 1]);
-                                       i++;
-                               }
-                       } else {
-                               System.out.println("WARNING: Unrecognized argument: " + args[i]);
-                       }
-                       i++;
-               }
-               if (deadTimerValue != 0 && deadTimerValue != keepAliveValue * 4) {
-                       System.out.println("WARNING: The value of DeadTimer should be 4 times the value of KeepAlive.");
-               }
-               if (deadTimerValue == 0) {
-                       deadTimerValue = keepAliveValue * 4;
-               }
-
-               final PCEPSessionProposalFactory spf = new PCEPSessionProposalFactoryImpl(deadTimerValue, keepAliveValue, stateful, active, versioned, instant, timeout);
-
-               final PCEPOpenObject prefs = spf.getSessionProposal(address, 0);
-
-               final DispatcherImpl d = new DispatcherImpl();
-               final PCEPDispatcherImpl dispatcher = new PCEPDispatcherImpl(d, new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), prefs, 5));
-
-               dispatcher.createServer(address, new TestingSessionListenerFactory()).get();
-       }
+import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class Main {
+
+    private static final Logger LOG = LoggerFactory.getLogger(Main.class);
+
+    public static final String USAGE = "DESCRIPTION:\n"
+        + "\tCreates a server with given parameters. As long as it runs, it accepts connections " + "from PCCs.\n" + "USAGE:\n"
+        + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
+        + "\t\tFormat: x.x.x.x:y where y is port number.\n\n" +
+
+            "\t-d, --deadtimer\n" + "\t\tin seconds, value of the desired deadtimer\n"
+            + "\t\tAccording to RFC5440, recommended value for deadtimer is 4 times the value\n"
+            + "\t\tof KeepAlive timer. If it's not, a warning is printed.\n"
+            + "\t\tIf not set, it's value will be derived from KeepAlive timer value.\n\n" +
+
+            "\t-ka, --keepalive\n" + "\t\tin seconds, value of the desired KeepAlive timer.\n"
+            + "\t\tIf not present, KeepAlive timer will be set to recommended value (30s).\n\n" +
+
+            "\t--stateful\n" + "\t\tpassive stateful\n\n" +
+
+            "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n" +
+
+            "\t--instant\n"
+            + "\t\tinstantiated stateful, <seconds> cleanup timeout (default value, if not included = 0) (implies --stateful)\n\n" +
+
+            "\t-arm, --autoResponseMessages <path to file>\n"
+            + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
+            + "\t\t Messages are used as auto response for every message received. Purely for testing puposes! \n\n" +
+
+            "\t-psm, --periodicallySendMessages <path to file> <period>\n"
+            + "\t\t <path to file> with groovy script which implements MessageGeneratorService followed by <period> in seconds.\n"
+            + "\t\t Messages which are sent periodically. Purely for testing puposes! \n\n" +
+
+            "\t-snm, --sendNowMessage <path to file>\n"
+            + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
+            + "\t\t Messages are sent in defined states defined by programmer. Purely for testing puposes! \n\n" +
+
+            "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
+
+            "With no parameters, this help is printed.";
+
+    private Main() {
+
+    }
+
+    private static final int KA_TO_DEADTIMER_RATIO = 4;
+
+    private static final int KA_DEFAULT = 30;
+
+    public static void main(final String[] args) throws UnknownHostException, InterruptedException, ExecutionException {
+        if (args.length == 0 || (args.length == 1 && args[0].equalsIgnoreCase("--help"))) {
+            LOG.info(Main.USAGE);
+            return;
+        }
+
+        InetSocketAddress address = null;
+        int keepAliveValue = KA_DEFAULT;
+        int deadTimerValue = 0;
+        boolean stateful = false;
+        boolean active = false;
+        boolean instant = false;
+
+        int i = 0;
+        while (i < args.length) {
+            if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
+                final String[] ip = args[i + 1].split(":");
+                address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.parseInt(ip[1]));
+                i++;
+            } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--deadtimer")) {
+                deadTimerValue = Integer.valueOf(args[i + 1]);
+                i++;
+            } else if (args[i].equalsIgnoreCase("-ka") || args[i].equalsIgnoreCase("--keepalive")) {
+                keepAliveValue = Integer.valueOf(args[i + 1]);
+                i++;
+            } else if (args[i].equalsIgnoreCase("--stateful")) {
+                stateful = true;
+            } else if (args[i].equalsIgnoreCase("--active")) {
+                stateful = true;
+                active = true;
+            } else if (args[i].equalsIgnoreCase("--instant")) {
+                stateful = true;
+                instant = true;
+            } else {
+                LOG.warn("WARNING: Unrecognized argument: {}", args[i]);
+            }
+            i++;
+        }
+        if (deadTimerValue != 0 && deadTimerValue != keepAliveValue * KA_TO_DEADTIMER_RATIO) {
+            LOG.warn("WARNING: The value of DeadTimer should be 4 times the value of KeepAlive.");
+        }
+        if (deadTimerValue == 0) {
+            deadTimerValue = keepAliveValue * KA_TO_DEADTIMER_RATIO;
+        }
+
+        final PCEPSessionProposalFactory spf = new Stateful07SessionProposalFactory(deadTimerValue, keepAliveValue, stateful, active, instant);
+
+        final Open prefs = spf.getSessionProposal(address, 0);
+
+        try (final StatefulActivator activator07 = new StatefulActivator()) {
+            activator07.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
+
+            final PCEPDispatcherImpl dispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(), new DefaultPCEPSessionNegotiatorFactory(prefs, 5), new NioEventLoopGroup(), new NioEventLoopGroup());
+
+            dispatcher.createServer(address, new TestingSessionListenerFactory()).get();
+        }
+    }
 }