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