2 * Copyright (c) 2014 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.pcep.pcc.mock;
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.net.UnknownHostException;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import org.opendaylight.protocol.pcep.PCEPCapability;
24 import org.opendaylight.protocol.pcep.ietf.stateful07.PCEPStatefulCapability;
25 import org.opendaylight.protocol.util.InetSocketAddressUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public final class Main {
31 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
33 private static final int DEFAULT_REMOTE_PORT = 4189;
34 private static final int DEFAULT_LOCAL_PORT = 0;
35 private static final short DEFAULT_KEEP_ALIVE = 30;
36 private static final short DEFAULT_DEAD_TIMER = 120;
37 private static final InetAddress LOCALHOST = InetAddresses.forString("127.0.0.1");
38 private static boolean triggeredInitSync = Boolean.FALSE;
39 private static boolean includeDbv = Boolean.FALSE;
40 private static boolean incrementalSync = Boolean.FALSE;
41 private static boolean triggeredResync = Boolean.FALSE;
42 private static BigInteger syncOptDBVersion;
43 private static int reconnectAfterXSeconds;
44 private static int disonnectAfterXSeconds;
48 throw new UnsupportedOperationException();
51 public static void main(final String[] args) throws InterruptedException, ExecutionException, UnknownHostException {
52 InetSocketAddress localAddress = new InetSocketAddress(LOCALHOST, DEFAULT_LOCAL_PORT);
53 List<InetSocketAddress> remoteAddress = Collections
54 .singletonList(new InetSocketAddress(LOCALHOST, DEFAULT_REMOTE_PORT));
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;
66 getRootLogger(lc).setLevel(ch.qos.logback.classic.Level.INFO);
68 while (argIdx < args.length) {
69 switch (args[argIdx]) {
70 case "--local-address":
71 localAddress = InetSocketAddressUtil.getInetSocketAddress(args[++argIdx], DEFAULT_LOCAL_PORT);
73 case "--remote-address":
74 remoteAddress = InetSocketAddressUtil.parseAddresses(args[++argIdx], DEFAULT_REMOTE_PORT);
77 pccCount = Integer.parseInt(args[++argIdx]);
80 lsps = Integer.parseInt(args[++argIdx]);
86 getRootLogger(lc).setLevel(Level.toLevel(args[++argIdx], Level.INFO));
90 ka = Short.valueOf(args[++argIdx]);
94 dt = Short.valueOf(args[++argIdx]);
97 password = args[++argIdx];
100 reconnectTime = Integer.parseInt(args[++argIdx]);
102 case "--redelegation-timeout":
103 redelegationTimeout = Integer.parseInt(args[++argIdx]);
105 case "--state-timeout":
106 stateTimeout = Integer.parseInt(args[++argIdx]);
108 case "--state-sync-avoidance":
109 //"--state-sync-avoidance 10, 5, 10
110 includeDbv = Boolean.TRUE;
111 final Long dbVersionAfterReconnect = Long.valueOf(args[++argIdx]);
112 disonnectAfterXSeconds = Integer.parseInt(args[++argIdx]);
113 reconnectAfterXSeconds = Integer.parseInt(args[++argIdx]);
114 syncOptDBVersion = BigInteger.valueOf(dbVersionAfterReconnect);
116 case "--incremental-sync-procedure":
117 //TODO Check that DBv > Lsp always ??
118 includeDbv = Boolean.TRUE;
119 incrementalSync = Boolean.TRUE;
120 //Version of database to be used after restart
121 final Long initialDbVersionAfterReconnect = Long.valueOf(args[++argIdx]);
122 disonnectAfterXSeconds = Integer.parseInt(args[++argIdx]);
123 reconnectAfterXSeconds = Integer.parseInt(args[++argIdx]);
124 syncOptDBVersion = BigInteger.valueOf(initialDbVersionAfterReconnect);
126 case "--triggered-initial-sync":
127 triggeredInitSync = Boolean.TRUE;
129 case "--triggered-re-sync":
130 triggeredResync = Boolean.TRUE;
133 LOG.warn("WARNING: Unrecognized argument: {}", args[argIdx]);
139 if (incrementalSync) {
140 Preconditions.checkArgument(syncOptDBVersion.intValue() > lsps,
141 "Synchronization Database Version which will be used after "
142 + "reconnectes requires to be higher than lsps");
145 final Optional<BigInteger> dBVersion = Optional.fromNullable(syncOptDBVersion);
146 final PCCsBuilder pccs = new PCCsBuilder(lsps, pcError, pccCount, localAddress, remoteAddress, ka, dt,
147 password, reconnectTime, redelegationTimeout,
148 stateTimeout, getCapabilities());
149 final TimerHandler timerHandler = new TimerHandler(pccs, dBVersion, disonnectAfterXSeconds,
150 reconnectAfterXSeconds);
151 pccs.createPCCs(BigInteger.valueOf(lsps), Optional.fromNullable(timerHandler));
152 if (!triggeredInitSync) {
153 timerHandler.createDisconnectTask();
157 private static PCEPCapability getCapabilities() {
158 if (triggeredInitSync) {
159 Preconditions.checkArgument(includeDbv);
161 return new PCEPStatefulCapability(true, true, true, triggeredInitSync, triggeredResync,
162 incrementalSync, includeDbv);
165 private static ch.qos.logback.classic.Logger getRootLogger(final LoggerContext lc) {
166 return lc.getLoggerList().stream().filter(input -> (input != null) && input.getName()
167 .equals(Logger.ROOT_LOGGER_NAME)).findFirst().get();