Initial framework migration to netty.
[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 java.io.IOException;
11 import java.net.InetAddress;
12 import java.net.InetSocketAddress;
13
14 import org.opendaylight.protocol.framework.DispatcherImpl;
15 import org.opendaylight.protocol.pcep.PCEPConnection;
16 import org.opendaylight.protocol.pcep.PCEPConnectionFactory;
17 import org.opendaylight.protocol.pcep.PCEPSessionListener;
18 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
19 import org.opendaylight.protocol.pcep.PCEPSessionProposal;
20 import org.opendaylight.protocol.pcep.PCEPSessionProposalChecker;
21 import org.opendaylight.protocol.pcep.PCEPSessionProposalCheckerFactory;
22 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
23 import org.opendaylight.protocol.pcep.impl.PCEPConnectionImpl;
24 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
25 import org.opendaylight.protocol.pcep.impl.PCEPMessageFactory;
26 import org.opendaylight.protocol.pcep.impl.PCEPSessionProposalCheckerFactoryImpl;
27 import org.opendaylight.protocol.pcep.impl.PCEPSessionProposalFactoryImpl;
28
29 public class Main {
30
31         public static String usage = "DESCRIPTION:\n" + "\tCreates a server with given parameters. As long as it runs, it accepts connections "
32                         + "from PCCs.\n" + "USAGE:\n" + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
33                         + "\t\tFormat: x.x.x.x:y where y is port number.\n\n" +
34
35                         "\t-d, --deadtimer\n" + "\t\tin seconds, value of the desired deadtimer\n"
36                         + "\t\tAccording to RFC5440, recommended value for deadtimer is 4 times the value\n"
37                         + "\t\tof KeepAlive timer. If it's not, a warning is printed.\n"
38                         + "\t\tIf not set, it's value will be derived from KeepAlive timer value.\n\n" +
39
40                         "\t-ka, --keepalive\n" + "\t\tin seconds, value of the desired KeepAlive timer.\n"
41                         + "\t\tIf not present, KeepAlive timer will be set to recommended value (30s).\n\n" +
42
43                         "\t--stateful\n" + "\t\tpassive stateful\n\n" +
44
45                         "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n" +
46
47                         "\t--versioned\n" + "\t\tversioned stateful (implies --stateful)\n\n" +
48
49                         "\t--instant\n"
50                         + "\t\tinstantiated stateful, <seconds> cleanup timeout (default value, if not included = 0) (implies --stateful)\n\n" +
51
52                         "\t-arm, --autoResponseMessages <path to file>\n"
53                         + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
54                         + "\t\t Messages are used as auto response for every message received. Purely for testing puposes! \n\n" +
55
56                         "\t-psm, --periodicallySendMessages <path to file> <period>\n"
57                         + "\t\t <path to file> with groovy script which implements MessageGeneratorService followed by <period> in seconds.\n"
58                         + "\t\t Messages which are sent periodically. Purely for testing puposes! \n\n" +
59
60                         "\t-snm, --sendNowMessage <path to file>\n"
61                         + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
62                         + "\t\t Messages are sent in defined states defined by programmer. Purely for testing puposes! \n\n" +
63
64                         "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
65
66                         "With no parameters, this help is printed.";
67
68         public static void main(final String[] args) throws IOException {
69                 if (args.length == 0 || (args.length == 1 && args[0].equalsIgnoreCase("--help"))) {
70                         System.out.println(Main.usage);
71                         return;
72                 }
73
74                 InetSocketAddress address = null;
75                 int keepAliveValue = 30;
76                 int deadTimerValue = 0;
77                 boolean stateful = false;
78                 boolean active = false;
79                 boolean versioned = false;
80                 boolean instant = false;
81                 String autoResponseMessagesSrc = null;
82                 String sendNowMessageSrc = null;
83                 String periodicallySendMessagesSrc = null;
84                 int period = 0;
85                 int timeout = 0;
86
87                 int i = 0;
88                 while (i < args.length) {
89                         if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
90                                 final String[] ip = args[i + 1].split(":");
91                                 address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.valueOf(ip[1]));
92                                 i++;
93                         } else if (args[i].equalsIgnoreCase("-d") || args[i].equalsIgnoreCase("--deadtimer")) {
94                                 deadTimerValue = Integer.valueOf(args[i + 1]);
95                                 i++;
96                         } else if (args[i].equalsIgnoreCase("-ka") || args[i].equalsIgnoreCase("--keepalive")) {
97                                 keepAliveValue = Integer.valueOf(args[i + 1]);
98                                 i++;
99                         } else if (args[i].equalsIgnoreCase("--stateful")) {
100                                 stateful = true;
101                         } else if (args[i].equalsIgnoreCase("--active")) {
102                                 stateful = true;
103                                 active = true;
104                         } else if (args[i].equalsIgnoreCase("--versioned")) {
105                                 stateful = true;
106                                 versioned = true;
107                         } else if (args[i].equalsIgnoreCase("--instant")) {
108                                 stateful = true;
109                                 instant = true;
110                                 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 if (args[i].equalsIgnoreCase("--autoResponseMessages") || args[i].equalsIgnoreCase("-arm")) {
115                                 autoResponseMessagesSrc = args[i + 1];
116                                 i++;
117                         } else if (args[i].equalsIgnoreCase("--periodicallySendMessages") || args[i].equalsIgnoreCase("-psm")) {
118                                 periodicallySendMessagesSrc = args[i + 1];
119                                 i++;
120                                 period = Integer.valueOf(args[i + 1]);
121                                 i++;
122                         } else if (args[i].equalsIgnoreCase("--sendNowMessage") || args[i].equalsIgnoreCase("-snm")) {
123                                 sendNowMessageSrc = args[i + 1];
124                                 i++;
125                         } else {
126                                 System.out.println("WARNING: Unrecognized argument: " + args[i]);
127                         }
128                         i++;
129                 }
130                 if (deadTimerValue != 0 && deadTimerValue != keepAliveValue * 4) {
131                         System.out.println("WARNING: The value of DeadTimer should be 4 times the value of KeepAlive.");
132                 }
133                 if (deadTimerValue == 0) {
134                         deadTimerValue = keepAliveValue * 4;
135                 }
136
137                 final PCEPSessionListenerFactory slf = new TestingSessionListenerFactory(autoResponseMessagesSrc, periodicallySendMessagesSrc, period, sendNowMessageSrc);
138                 final PCEPSessionProposalFactory spf = new PCEPSessionProposalFactoryImpl(deadTimerValue, keepAliveValue, stateful, active, versioned, instant, timeout);
139                 final PCEPSessionProposalCheckerFactory spcf = new PCEPSessionProposalCheckerFactoryImpl();
140
141                 final PCEPSessionProposal prefs = spf.getSessionProposal(address, 0);
142
143                 final DispatcherImpl d = new DispatcherImpl(new PCEPMessageFactory());
144                 final PCEPDispatcherImpl dispatcher = new PCEPDispatcherImpl(d, spf);
145
146                 try {
147                         dispatcher.createServer(address, new PCEPConnectionFactory() {
148                                 @Override
149                                 public PCEPConnection createProtocolConnection(final InetSocketAddress address) {
150                                         final PCEPSessionProposalChecker checker = spcf.getPreferencesChecker(address);
151                                         final PCEPSessionListener lsnr = slf.getSessionListener(address.getAddress());
152
153                                         return new PCEPConnectionImpl(address, lsnr, prefs.getProposal(), checker);
154                                 }
155
156                                 @Override
157                                 public void setProposal(final PCEPSessionProposalFactory proposals, final InetSocketAddress address, final int sessionId) {
158                                 }
159                         });
160                         // final ProtocolServer s = dispatcher.createServer(address, slf, spf, spcf);
161
162                         // try {
163                         // Thread.sleep(10000);
164                         // } catch (final InterruptedException e) {
165                         // e.printStackTrace();
166                         // }
167                         //
168                         // s.close();
169
170                 } finally {
171                         ((PCEPSessionProposalCheckerFactoryImpl) spcf).close();
172                         // d.stop();
173                 }
174         }
175 }