b7e2aa8ecf3276dc0bd8ac3b0feb39d1628ad12a
[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.BGPMessageFactory;
18 import org.opendaylight.protocol.bgp.rib.impl.BGPConnectionImpl;
19 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
20 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalCheckerImpl;
21 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionProposalChecker;
24 import org.opendaylight.protocol.concepts.ASNumber;
25 import org.opendaylight.protocol.concepts.IPv4Address;
26 import org.opendaylight.protocol.framework.DispatcherImpl;
27 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
28 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Starter class for testing.
34  */
35 public class Main {
36
37         private final static Logger logger = LoggerFactory.getLogger(Main.class);
38
39         public static String usage = "DESCRIPTION:\n" + "\tCreates a server with given parameters. As long as it runs, it accepts connections "
40                         + "from PCCs.\n" + "USAGE:\n" + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
41                         + "\t\tFormat: x.x.x.x:y where y is port number.\n\n"
42                         + "\t\tThis IP address will appear in BGP Open message as BGP Identifier of the server.\n" +
43
44                         "\t-as\n" + "\t\t value of AS in the initial open message\n\n" +
45
46                         "\t-h, --holdtimer\n" + "\t\tin seconds, value of the desired holdtimer\n"
47                         + "\t\tAccording to RFC4271, recommended value for deadtimer is 90 seconds.\n"
48                         + "\t\tIf not set, this will be the default value.\n\n" +
49
50                         "\t--help\n" + "\t\tdisplay this help and exits\n\n" +
51
52                         "With no parameters, this help is printed.";
53
54         BGPDispatcherImpl dispatcher;
55
56         public Main() throws IOException {
57                 this.dispatcher = new BGPDispatcherImpl(new DispatcherImpl(new BGPMessageFactory()));
58         }
59
60         public static void main(final String[] args) throws NumberFormatException, IOException {
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 = 90;
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                                 System.out.println("WARNING: Unrecognized argument: " + args[i]);
84                         }
85                         i++;
86                 }
87
88                 final Main m = new Main();
89
90                 final BGPSessionListener sessionListener = new TestingListener((DispatcherImpl) m.dispatcher.getDispatcher());
91
92                 final BGPSessionProposalImpl prop = new BGPSessionProposalImpl(holdTimerValue, as, new IPv4Address(InetAddress.getByName("25.25.25.2")));
93
94                 final BGPSessionPreferences proposal = prop.getProposal();
95
96                 prop.close();
97
98                 final BGPSessionProposalChecker checker = new BGPSessionProposalCheckerImpl();
99
100                 final ProtocolMessageFactory parser = new BGPMessageFactory();
101
102                 logger.debug(address + " " + sessionListener + " " + proposal + " " + checker);
103
104                 final InetSocketAddress addr = address;
105                 m.dispatcher.createClient(new BGPConnectionImpl(addr, sessionListener, proposal, checker), parser,
106                                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
107         }
108 }