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.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;
28 public final class Main {
30 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
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;
47 throw new UnsupportedOperationException();
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));
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;
65 getRootLogger(lc).setLevel(ch.qos.logback.classic.Level.INFO);
67 while (argIdx < args.length) {
68 switch (args[argIdx]) {
69 case "--local-address":
70 localAddress = InetSocketAddressUtil.getInetSocketAddress(args[++argIdx], DEFAULT_LOCAL_PORT);
72 case "--remote-address":
73 remoteAddress = InetSocketAddressUtil.parseAddresses(args[++argIdx], DEFAULT_REMOTE_PORT);
76 pccCount = Integer.parseInt(args[++argIdx]);
79 lsps = Integer.parseInt(args[++argIdx]);
85 getRootLogger(lc).setLevel(Level.toLevel(args[++argIdx], Level.INFO));
89 ka = Short.valueOf(args[++argIdx]);
93 dt = Short.valueOf(args[++argIdx]);
96 password = args[++argIdx];
99 reconnectTime = Integer.parseInt(args[++argIdx]);
101 case "--redelegation-timeout":
102 redelegationTimeout = Integer.parseInt(args[++argIdx]);
104 case "--state-timeout":
105 stateTimeout = Integer.parseInt(args[++argIdx]);
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);
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);
125 case "--triggered-initial-sync":
126 triggeredInitSync = Boolean.TRUE;
128 case "--triggered-re-sync":
129 triggeredResync = Boolean.TRUE;
132 LOG.warn("WARNING: Unrecognized argument: {}", args[argIdx]);
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");
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();
156 private static PCEPCapability getCapabilities() {
157 if (triggeredInitSync) {
158 Preconditions.checkArgument(includeDbv);
160 return new PCEPStatefulCapability(true, true, true, triggeredInitSync, triggeredResync,
161 incrementalSync, includeDbv);
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();