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