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