e8dd2b4820c1ff02915e5dca3f7f9713f25a3370
[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.protocol.pcep.spi.pojo.PCEPExtensionProviderContextImpl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class Main {
25
26         private static final Logger logger = LoggerFactory.getLogger(Main.class);
27
28         public static String usage = "DESCRIPTION:\n" + "\tCreates a server with given parameters. As long as it runs, it accepts connections "
29                         + "from PCCs.\n" + "USAGE:\n" + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
30                         + "\t\tFormat: x.x.x.x:y where y is port number.\n\n" +
31
32                         "\t-d, --deadtimer\n" + "\t\tin seconds, value of the desired deadtimer\n"
33                         + "\t\tAccording to RFC5440, recommended value for deadtimer is 4 times the value\n"
34                         + "\t\tof KeepAlive timer. If it's not, a warning is printed.\n"
35                         + "\t\tIf not set, it's value will be derived from KeepAlive timer value.\n\n" +
36
37                         "\t-ka, --keepalive\n" + "\t\tin seconds, value of the desired KeepAlive timer.\n"
38                         + "\t\tIf not present, KeepAlive timer will be set to recommended value (30s).\n\n" +
39
40                         "\t--stateful\n" + "\t\tpassive stateful\n\n" +
41
42                         "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n" +
43
44                         "\t--versioned\n" + "\t\tversioned 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         public static void main(final String[] args) throws Exception {
70                 if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("--help")) {
71                         System.out.println(Main.usage);
72                         return;
73                 }
74
75                 InetSocketAddress address = null;
76                 int keepAliveValue = 30;
77                 int deadTimerValue = 0;
78                 boolean stateful = false;
79                 boolean active = false;
80                 boolean versioned = false;
81                 boolean instant = false;
82                 int timeout = 0;
83
84                 int i = 0;
85                 while (i < args.length) {
86                         if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
87                                 final String[] ip = args[i + 1].split(":");
88                                 address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.valueOf(ip[1]));
89                                 i++;
90                         } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--deadtimer")) {
91                                 deadTimerValue = Integer.valueOf(args[i + 1]);
92                                 i++;
93                         } else if (args[i].equalsIgnoreCase("-ka") || args[i].equalsIgnoreCase("--keepalive")) {
94                                 keepAliveValue = Integer.valueOf(args[i + 1]);
95                                 i++;
96                         } else if (args[i].equalsIgnoreCase("--stateful")) {
97                                 stateful = true;
98                         } else if (args[i].equalsIgnoreCase("--active")) {
99                                 stateful = true;
100                                 active = true;
101                         } else if (args[i].equalsIgnoreCase("--versioned")) {
102                                 stateful = true;
103                                 versioned = true;
104                         } else if (args[i].equalsIgnoreCase("--instant")) {
105                                 stateful = true;
106                                 instant = true;
107                                 if (i == args.length - 1) {
108                                         timeout = 0;
109                                 } else if (Integer.valueOf(args[i + 1]) > 0 && Integer.valueOf(args[i + 1]) < Integer.MAX_VALUE) {
110                                         timeout = Integer.valueOf(args[i + 1]);
111                                         i++;
112                                 }
113                         } else {
114                                 logger.warn("WARNING: Unrecognized argument: {}", args[i]);
115                         }
116                         i++;
117                 }
118                 if (deadTimerValue != 0 && deadTimerValue != keepAliveValue * 4) {
119                         logger.warn("WARNING: The value of DeadTimer should be 4 times the value of KeepAlive.");
120                 }
121                 if (deadTimerValue == 0) {
122                         deadTimerValue = keepAliveValue * 4;
123                 }
124
125                 final PCEPSessionProposalFactory spf = new PCEPSessionProposalFactoryImpl(deadTimerValue, keepAliveValue, stateful, active, versioned, instant, timeout);
126
127                 final Open prefs = spf.getSessionProposal(address, 0);
128
129                 final PCEPDispatcherImpl dispatcher = new PCEPDispatcherImpl(PCEPExtensionProviderContextImpl.getSingletonInstance().getMessageHandlerRegistry(), new DefaultPCEPSessionNegotiatorFactory(new HashedWheelTimer(), prefs, 5));
130
131                 dispatcher.createServer(address, new TestingSessionListenerFactory()).get();
132         }
133 }