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