BUG-54 : switched channel pipeline to be protocol specific.
[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.io.IOException;
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.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.concepts.ASNumber;
22 import org.opendaylight.protocol.concepts.IPv4Address;
23 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
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 final static Logger logger = LoggerFactory.getLogger(Main.class);
33
34         public static String usage = "DESCRIPTION:\n" + "\tCreates a server with given parameters. As long as it runs, it accepts connections "
35                         + "from PCCs.\n" + "USAGE:\n" + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
36                         + "\t\tFormat: x.x.x.x:y where y is port number.\n\n"
37                         + "\t\tThis IP address will appear in BGP Open message as BGP Identifier of the server.\n" +
38
39                         "\t-as\n" + "\t\t value of AS in the initial open message\n\n" +
40
41                         "\t-h, --holdtimer\n" + "\t\tin seconds, value of the desired holdtimer\n"
42                         + "\t\tAccording to RFC4271, recommended value for deadtimer is 90 seconds.\n"
43                         + "\t\tIf not set, this will be the default value.\n\n" +
44
45                         "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
46
47                         "With no parameters, this help is printed.";
48
49         BGPDispatcherImpl dispatcher;
50
51         public Main() throws IOException {
52                 this.dispatcher = new BGPDispatcherImpl(new BGPMessageFactoryImpl());
53         }
54
55         public static void main(final String[] args) throws NumberFormatException, IOException {
56                 if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("--help")) {
57                         System.out.println(Main.usage);
58                         return;
59                 }
60
61                 InetSocketAddress address = null;
62                 short holdTimerValue = 90;
63                 ASNumber as = null;
64
65                 int i = 0;
66                 while (i < args.length) {
67                         if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("--address")) {
68                                 final String[] ip = args[i + 1].split(":");
69                                 address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.valueOf(ip[1]));
70                                 i++;
71                         } else if (args[i].equalsIgnoreCase("-h") || args[i].equalsIgnoreCase("--holdtimer")) {
72                                 holdTimerValue = Short.valueOf(args[i + 1]);
73                                 i++;
74                         } else if (args[i].equalsIgnoreCase("-as")) {
75                                 as = new ASNumber(Long.valueOf(args[i + 1]));
76                                 i++;
77                         } else {
78                                 System.out.println("WARNING: Unrecognized argument: " + args[i]);
79                         }
80                         i++;
81                 }
82
83                 final Main m = new Main();
84
85                 final BGPSessionListener sessionListener = new TestingListener();
86
87                 final BGPSessionProposalImpl prop = new BGPSessionProposalImpl(holdTimerValue, as, new IPv4Address(InetAddress.getByName("25.25.25.2")));
88
89                 final BGPSessionPreferences proposal = prop.getProposal();
90
91                 logger.debug("{} {} {}", address, sessionListener, proposal);
92
93                 final InetSocketAddress addr = address;
94                 m.dispatcher.createClient(addr, proposal, sessionListener, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
95         }
96 }