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