BUG-5790: BGP Test tool
[bgpcep.git] / pcep / pcc-mock / src / main / java / org / opendaylight / protocol / pcep / pcc / mock / Main.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import ch.qos.logback.classic.Level;
12 import ch.qos.logback.classic.LoggerContext;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Predicate;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Lists;
18 import com.google.common.net.InetAddresses;
19 import java.math.BigInteger;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.net.UnknownHostException;
23 import java.util.List;
24 import java.util.concurrent.ExecutionException;
25 import org.opendaylight.protocol.pcep.PCEPCapability;
26 import org.opendaylight.protocol.pcep.ietf.stateful07.PCEPStatefulCapability;
27 import org.opendaylight.protocol.util.InetSocketAddressUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class Main {
32
33     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
34
35     private static final int DEFAULT_REMOTE_PORT = 4189;
36     private static final int DEFAULT_LOCAL_PORT = 0;
37     private static final short DEFAULT_KEEP_ALIVE = 30;
38     private static final short DEFAULT_DEAD_TIMER = 120;
39     private static final InetAddress LOCALHOST = InetAddresses.forString("127.0.0.1");
40     private static boolean triggeredInitSync = Boolean.FALSE;
41     private static boolean includeDbv = Boolean.FALSE;
42     private static boolean incrementalSync = Boolean.FALSE;
43     private static boolean triggeredResync = Boolean.FALSE;
44     private static BigInteger syncOptDBVersion;
45     private static int reconnectAfterXSeconds;
46     private static int disonnectAfterXSeconds;
47
48
49     private Main() {
50         throw new UnsupportedOperationException();
51     }
52
53     public static void main(final String[] args) throws InterruptedException, ExecutionException, UnknownHostException {
54         InetSocketAddress localAddress = new InetSocketAddress(LOCALHOST, DEFAULT_LOCAL_PORT);
55         List<InetSocketAddress> remoteAddress = Lists.newArrayList(new InetSocketAddress(LOCALHOST, DEFAULT_REMOTE_PORT));
56         int pccCount = 1;
57         int lsps = 1;
58         boolean pcError = false;
59         final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
60         short ka = DEFAULT_KEEP_ALIVE;
61         short dt = DEFAULT_DEAD_TIMER;
62         String password = null;
63         long reconnectTime = -1;
64         int redelegationTimeout = 0;
65         int stateTimeout = -1;
66
67         getRootLogger(lc).setLevel(ch.qos.logback.classic.Level.INFO);
68         int argIdx = 0;
69         while (argIdx < args.length) {
70             if (args[argIdx].equals("--local-address")) {
71                 localAddress = InetSocketAddressUtil.getInetSocketAddress(args[++argIdx], DEFAULT_LOCAL_PORT);
72             } else if (args[argIdx].equals("--remote-address")) {
73                 remoteAddress = InetSocketAddressUtil.parseAddresses(args[++argIdx], DEFAULT_REMOTE_PORT);
74             } else if (args[argIdx].equals("--pcc")) {
75                 pccCount = Integer.valueOf(args[++argIdx]);
76             } else if (args[argIdx].equals("--lsp")) {
77                 lsps = Integer.valueOf(args[++argIdx]);
78             } else if (args[argIdx].equals("--pcerr")) {
79                 pcError = true;
80             } else if (args[argIdx].equals("--log-level")) {
81                 getRootLogger(lc).setLevel(Level.toLevel(args[++argIdx], ch.qos.logback.classic.Level.INFO));
82             } else if (args[argIdx].equals("--keepalive") || args[argIdx].equals("-ka")) {
83                 ka = Short.valueOf(args[++argIdx]);
84             } else if (args[argIdx].equals("--deadtimer") || args[argIdx].equals("-d")) {
85                 dt = Short.valueOf(args[++argIdx]);
86             } else if (args[argIdx].equals("--password")) {
87                 password = args[++argIdx];
88             } else if (args[argIdx].equals("--reconnect")) {
89                 reconnectTime = Integer.valueOf(args[++argIdx]).intValue();
90             } else if (args[argIdx].equals("--redelegation-timeout")) {
91                 redelegationTimeout = Integer.valueOf(args[++argIdx]);
92             } else if (args[argIdx].equals("--state-timeout")) {
93                 stateTimeout = Integer.valueOf(args[++argIdx]);
94             } else if (args[argIdx].equals("--state-sync-avoidance")) {
95                 //"--state-sync-avoidance 10, 5, 10
96                 includeDbv = Boolean.TRUE;
97                 final Long dbVersionAfterReconnect = Long.valueOf(args[++argIdx]);
98                 disonnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
99                 reconnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
100                 syncOptDBVersion = BigInteger.valueOf(dbVersionAfterReconnect);
101             } else if (args[argIdx].equals("--incremental-sync-procedure")) {
102                 //TODO Check that DBv > Lsp always ??
103                 includeDbv = Boolean.TRUE;
104                 incrementalSync = Boolean.TRUE;
105                 //Version of database to be used after restart
106                 final Long initialDbVersionAfterReconnect = Long.valueOf(args[++argIdx]);
107                 disonnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
108                 reconnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
109                 syncOptDBVersion = BigInteger.valueOf(initialDbVersionAfterReconnect);
110             } else if (args[argIdx].equals("--triggered-initial-sync")) {
111                 triggeredInitSync = Boolean.TRUE;
112             } else if (args[argIdx].equals("--triggered-re-sync")) {
113                 triggeredResync = Boolean.TRUE;
114             } else {
115                 LOG.warn("WARNING: Unrecognized argument: {}", args[argIdx]);
116             }
117             argIdx++;
118         }
119
120         if (incrementalSync) {
121             Preconditions.checkArgument(syncOptDBVersion.intValue() > lsps, "Synchronization Database Version which will be used after " +
122                 "reconnectes requires to be higher than lsps");
123         }
124
125         final Optional<BigInteger> dBVersion = Optional.fromNullable(syncOptDBVersion);
126         final PCCsBuilder pccs = new PCCsBuilder(lsps, pcError, pccCount, localAddress, remoteAddress, ka, dt, password, reconnectTime, redelegationTimeout,
127             stateTimeout, getCapabilities());
128         final TimerHandler timerHandler = new TimerHandler(pccs, dBVersion, disonnectAfterXSeconds, reconnectAfterXSeconds);
129         pccs.createPCCs(BigInteger.valueOf(lsps), Optional.fromNullable(timerHandler));
130         if (!triggeredInitSync) {
131             timerHandler.createDisconnectTask();
132         }
133     }
134
135     private static PCEPCapability getCapabilities() {
136         if (triggeredInitSync) {
137             Preconditions.checkArgument(includeDbv);
138         }
139         return new PCEPStatefulCapability(true, true, true, triggeredInitSync, triggeredResync, incrementalSync, includeDbv);
140     }
141
142     private static ch.qos.logback.classic.Logger getRootLogger(final LoggerContext lc) {
143         return Iterables.find(lc.getLoggerList(), new Predicate<Logger>() {
144             @Override
145             public boolean apply(final Logger input) {
146                 return (input != null) ? input.getName().equals(Logger.ROOT_LOGGER_NAME) : false;
147             }
148         });
149     }
150 }