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