d73813517e9d38e2e27e7ddcd3b9dfb758d2edb8
[bgpcep.git] / pcep / testtool / src / main / java / org / opendaylight / protocol / pcep / testtool / Main.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.nio.NioEventLoopGroup;
11 import java.net.InetAddress;
12 import java.net.InetSocketAddress;
13 import java.net.UnknownHostException;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
16 import org.opendaylight.protocol.pcep.ietf.initiated00.Stateful07SessionProposalFactory;
17 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
18 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
19 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
20 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public final class Main {
26
27     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
28
29     public static final String USAGE = "DESCRIPTION:\n"
30         + "\tCreates a server with given parameters. As long as it runs, it accepts connections " + "from PCCs.\n" + "USAGE:\n"
31         + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
32         + "\t\tFormat: x.x.x.x:y where y is port number.\n\n" +
33
34             "\t-d, --deadtimer\n" + "\t\tin seconds, value of the desired deadtimer\n"
35             + "\t\tAccording to RFC5440, recommended value for deadtimer is 4 times the value\n"
36             + "\t\tof KeepAlive timer. If it's not, a warning is printed.\n"
37             + "\t\tIf not set, it's value will be derived from KeepAlive timer value.\n\n" +
38
39             "\t-ka, --keepalive\n" + "\t\tin seconds, value of the desired KeepAlive timer.\n"
40             + "\t\tIf not present, KeepAlive timer will be set to recommended value (30s).\n\n" +
41
42             "\t--stateful\n" + "\t\tpassive stateful\n\n" +
43
44             "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n" +
45
46             "\t--instant\n"
47             + "\t\tinstantiated stateful, <seconds> cleanup timeout (default value, if not included = 0) (implies --stateful)\n\n" +
48
49             "\t-arm, --autoResponseMessages <path to file>\n"
50             + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
51             + "\t\t Messages are used as auto response for every message received. Purely for testing puposes! \n\n" +
52
53             "\t-psm, --periodicallySendMessages <path to file> <period>\n"
54             + "\t\t <path to file> with groovy script which implements MessageGeneratorService followed by <period> in seconds.\n"
55             + "\t\t Messages which are sent periodically. Purely for testing puposes! \n\n" +
56
57             "\t-snm, --sendNowMessage <path to file>\n"
58             + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
59             + "\t\t Messages are sent in defined states defined by programmer. Purely for testing puposes! \n\n" +
60
61             "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
62
63             "With no parameters, this help is printed.";
64
65     private Main() {
66
67     }
68
69     private static final int KA_TO_DEADTIMER_RATIO = 4;
70
71     private static final int KA_DEFAULT = 30;
72
73     private static final int MAX_UNKNOWN_MESSAGES = 5;
74
75     public static void main(final String[] args) throws UnknownHostException, InterruptedException, ExecutionException {
76         if (args.length == 0 || (args.length == 1 && args[0].equalsIgnoreCase("--help"))) {
77             LOG.info(Main.USAGE);
78             return;
79         }
80
81         InetSocketAddress address = null;
82         int keepAliveValue = KA_DEFAULT;
83         int deadTimerValue = 0;
84         boolean stateful = false;
85         boolean active = false;
86         boolean instant = false;
87
88         int i = 0;
89         while (i < args.length) {
90             if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
91                 final String[] ip = args[i + 1].split(":");
92                 address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.parseInt(ip[1]));
93                 i++;
94             } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--deadtimer")) {
95                 deadTimerValue = Integer.valueOf(args[i + 1]);
96                 i++;
97             } else if (args[i].equalsIgnoreCase("-ka") || args[i].equalsIgnoreCase("--keepalive")) {
98                 keepAliveValue = Integer.valueOf(args[i + 1]);
99                 i++;
100             } else if (args[i].equalsIgnoreCase("--stateful")) {
101                 stateful = true;
102             } else if (args[i].equalsIgnoreCase("--active")) {
103                 stateful = true;
104                 active = true;
105             } else if (args[i].equalsIgnoreCase("--instant")) {
106                 stateful = true;
107                 instant = true;
108             } else {
109                 LOG.warn("WARNING: Unrecognized argument: {}", args[i]);
110             }
111             i++;
112         }
113         if (deadTimerValue != 0 && deadTimerValue != keepAliveValue * KA_TO_DEADTIMER_RATIO) {
114             LOG.warn("WARNING: The value of DeadTimer should be 4 times the value of KeepAlive.");
115         }
116         if (deadTimerValue == 0) {
117             deadTimerValue = keepAliveValue * KA_TO_DEADTIMER_RATIO;
118         }
119
120         final PCEPSessionProposalFactory spf = new Stateful07SessionProposalFactory(deadTimerValue, keepAliveValue, stateful, active, instant);
121
122         final Open prefs = spf.getSessionProposal(address, 0);
123
124         try (final StatefulActivator activator07 = new StatefulActivator()) {
125             activator07.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
126
127             try (final PCEPDispatcherImpl dispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(), new DefaultPCEPSessionNegotiatorFactory(prefs, MAX_UNKNOWN_MESSAGES), new NioEventLoopGroup(), new NioEventLoopGroup())) {
128                 dispatcher.createServer(address, new TestingSessionListenerFactory()).get();
129             }
130         }
131     }
132 }