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