2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bmp.mock;
11 import static org.opendaylight.protocol.util.InetSocketAddressUtil.getInetSocketAddress;
13 import ch.qos.logback.classic.Level;
14 import com.google.common.net.InetAddresses;
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17 import net.sourceforge.argparse4j.ArgumentParsers;
18 import net.sourceforge.argparse4j.impl.Arguments;
19 import net.sourceforge.argparse4j.inf.ArgumentParser;
20 import net.sourceforge.argparse4j.inf.ArgumentParserException;
21 import net.sourceforge.argparse4j.inf.Namespace;
22 import org.opendaylight.protocol.util.ArgumentsInput;
24 public final class BmpMockArguments implements ArgumentsInput {
26 private static final int DEFAULT_LOCAL_PORT = 0;
27 private static final int DEFAULT_REMOTE_PORT = 12345;
28 private static final InetAddress LOCALHOST = InetAddresses.forString("127.0.0.1");
30 private static final InetSocketAddress REMOTE_ADDRESS = new InetSocketAddress(LOCALHOST, DEFAULT_REMOTE_PORT);
31 private static final InetSocketAddress LOCAL_ADDRESS = new InetSocketAddress(LOCALHOST, DEFAULT_LOCAL_PORT);
33 private static final String PROGRAM_NAME = "BGP Monitoring Protocol testing tool.";
34 private static final String ARG_PREFIX = "--";
35 private static final String ROUTERS_COUNT_DST = "routers_count";
36 private static final String PEERS_COUNT_DST = "peers_count";
37 private static final String PRE_POLICY_ROUTES_COUNT_DST = "pre_policy_routes";
38 private static final String POST_POLICY_ROUTES_COUNT_DST = "post_policy_routes";
39 private static final String LOCAL_ADDRESS_DST = "local_address";
40 private static final String REMOTE_ADDRESS_DST = "remote_address";
41 private static final String LOG_LEVEL_DST = "log_level";
42 // when set to true, the mock will operate as a server listening for incoming active monitoring request
43 private static final String PASSIVE_MODE_DST = "passive";
45 private static final ArgumentParser ARGUMENT_PARSER = initializeArgumentParser();
47 private final Namespace parseArgs;
49 private BmpMockArguments(final Namespace parseArgs) {
50 this.parseArgs = parseArgs;
53 public static BmpMockArguments parseArguments(final String[] args) {
55 final Namespace namespace = ARGUMENT_PARSER.parseArgs(args);
56 return new BmpMockArguments(namespace);
57 } catch (final ArgumentParserException e) {
58 throw new IllegalArgumentException(e);
62 public int getRoutersCount() {
63 return this.parseArgs.getInt(ROUTERS_COUNT_DST);
66 public int getPeersCount() {
67 return this.parseArgs.getInt(PEERS_COUNT_DST);
70 public int getPrePolicyRoutesCount() {
71 return this.parseArgs.getInt(PRE_POLICY_ROUTES_COUNT_DST);
74 public int getPostPolicyRoutesCount() {
75 return this.parseArgs.getInt(POST_POLICY_ROUTES_COUNT_DST);
78 public InetSocketAddress getLocalAddress() {
79 return this.parseArgs.get(LOCAL_ADDRESS_DST);
82 public InetSocketAddress getRemoteAddress() {
83 return this.parseArgs.get(REMOTE_ADDRESS_DST);
87 public Level getLogLevel() {
88 return this.parseArgs.get(LOG_LEVEL_DST);
91 public boolean isOnPassiveMode() {
92 return this.parseArgs.get(PASSIVE_MODE_DST);
95 private static ArgumentParser initializeArgumentParser() {
96 final ArgumentParser parser = ArgumentParsers.newArgumentParser(PROGRAM_NAME);
97 parser.addArgument(toArgName(ROUTERS_COUNT_DST)).type(Integer.class).setDefault(1);
98 parser.addArgument(toArgName(PEERS_COUNT_DST)).type(Integer.class).setDefault(0);
99 parser.addArgument(toArgName(PRE_POLICY_ROUTES_COUNT_DST)).type(Integer.class).setDefault(0);
100 parser.addArgument(toArgName(POST_POLICY_ROUTES_COUNT_DST)).type(Integer.class).setDefault(0);
101 parser.addArgument(toArgName(PASSIVE_MODE_DST)).action(Arguments.storeTrue());
102 parser.addArgument(toArgName(LOCAL_ADDRESS_DST)).type((parser13, arg, value) -> getInetSocketAddress(value, DEFAULT_LOCAL_PORT)).setDefault(LOCAL_ADDRESS);
103 parser.addArgument(toArgName(REMOTE_ADDRESS_DST)).type((parser12, arg, value) -> getInetSocketAddress(value, DEFAULT_REMOTE_PORT)).setDefault(REMOTE_ADDRESS);
104 parser.addArgument(toArgName(LOG_LEVEL_DST)).type((parser1, arg, value) -> Level.toLevel(value)).setDefault(Level.INFO);
108 private static String toArgName(final String dst) {
109 return ARG_PREFIX + dst;