Merge "Fix junit dependencies in poms. Reuse existing from parent, add missing ones."
[bgpcep.git] / bgp / testtool / src / main / java / org / opendaylight / protocol / bgp / 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.bgp.testtool;
9
10 import io.netty.util.concurrent.GlobalEventExecutor;
11
12 import java.net.InetAddress;
13 import java.net.InetSocketAddress;
14
15 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
16 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
17 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
18 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
19 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
21 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Starter class for testing.
29  */
30 public class Main {
31
32         private static final Logger logger = LoggerFactory.getLogger(Main.class);
33
34         private static String usage = "DESCRIPTION:\n"
35                         + "\tCreates a server with given parameters. As long as it runs, it accepts connections " + "from PCCs.\n" + "USAGE:\n"
36                         + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
37                         + "\t\tFormat: x.x.x.x:y where y is port number.\n\n"
38                         + "\t\tThis IP address will appear in BGP Open message as BGP Identifier of the server.\n" +
39
40                         "\t-as\n" + "\t\t value of AS in the initial open message\n\n" +
41
42                         "\t-h, --holdtimer\n" + "\t\tin seconds, value of the desired holdtimer\n"
43                         + "\t\tAccording to RFC4271, recommended value for deadtimer is 90 seconds.\n"
44                         + "\t\tIf not set, this will be the default value.\n\n" +
45
46                         "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
47
48                         "With no parameters, this help is printed.";
49
50         private final BGPDispatcherImpl dispatcher;
51
52         private static final int INITIAL_HOLD_TIME = 90;
53
54         private static final int RECONNECT_MILLIS = 5000;
55
56         Main() throws Exception {
57                 this.dispatcher = new BGPDispatcherImpl(new BGPMessageFactoryImpl(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry()));
58         }
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                 short holdTimerValue = INITIAL_HOLD_TIME;
68                 AsNumber as = null;
69
70                 int i = 0;
71                 while (i < args.length) {
72                         if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
73                                 final String[] ip = args[i + 1].split(":");
74                                 address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.valueOf(ip[1]));
75                                 i++;
76                         } else if (args[i].equalsIgnoreCase("-h") || args[i].equalsIgnoreCase("--holdtimer")) {
77                                 holdTimerValue = Short.valueOf(args[i + 1]);
78                                 i++;
79                         } else if (args[i].equalsIgnoreCase("-as")) {
80                                 as = new AsNumber(Long.valueOf(args[i + 1]));
81                                 i++;
82                         } else {
83                                 logger.error("WARNING: Unrecognized argument: " + args[i]);
84                         }
85                         i++;
86                 }
87
88                 final Main m = new Main();
89
90                 final BGPSessionListener sessionListener = new TestingListener();
91
92                 final BGPSessionProposalImpl prop = new BGPSessionProposalImpl(holdTimerValue, as.getValue().intValue(), new Ipv4Address("25.25.25.2"));
93
94                 final BGPSessionPreferences proposal = prop.getProposal();
95
96                 logger.debug("{} {} {}", address, sessionListener, proposal);
97
98                 final InetSocketAddress addr = address;
99                 m.dispatcher.createClient(addr, proposal, sessionListener,
100                                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_MILLIS));
101         }
102 }