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