2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.pcep.testtool;
10 import io.netty.channel.nio.NioEventLoopGroup;
11 import java.net.InetAddress;
12 import java.net.InetSocketAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.ServiceLoader;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.protocol.pcep.MessageRegistry;
19 import org.opendaylight.protocol.pcep.PCEPCapability;
20 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
21 import org.opendaylight.protocol.pcep.ietf.stateful.PCEPStatefulCapability;
22 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
23 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
24 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
25 import org.opendaylight.protocol.pcep.spi.PCEPExtensionConsumerContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.config.rev230112.PcepSessionErrorPolicy;
27 import org.opendaylight.yangtools.yang.common.Uint16;
28 import org.opendaylight.yangtools.yang.common.Uint8;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public final class Main {
34 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
36 private static final String USAGE = "DESCRIPTION:\n"
37 + "\tCreates a server with given parameters. As long as it runs, it accepts connections "
38 + "from PCCs.\n" + "USAGE:\n"
39 + "\t-a, --address\n" + "\t\tthe ip address to which is this server bound.\n"
40 + "\t\tFormat: x.x.x.x:y where y is port number.\n\n"
42 + "\t-d, --deadtimer\n" + "\t\tin seconds, value of the desired deadtimer\n"
43 + "\t\tAccording to RFC5440, recommended value for deadtimer is 4 times the value\n"
44 + "\t\tof KeepAlive timer. If it's not, a warning is printed.\n"
45 + "\t\tIf not set, it's value will be derived from KeepAlive timer value.\n\n"
47 + "\t-ka, --keepalive\n" + "\t\tin seconds, value of the desired KeepAlive timer.\n"
48 + "\t\tIf not present, KeepAlive timer will be set to recommended value (30s).\n\n"
50 + "\t--stateful\n" + "\t\tpassive stateful\n\n"
52 + "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n"
55 + "\t\tinstantiated stateful, <seconds> cleanup timeout "
56 + "(default value, if not included = 0) (implies --stateful)\n\n"
58 + "\t-arm, --autoResponseMessages <path to file>\n"
59 + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
60 + "\t\t Messages are used as auto response for every message received. Purely for testing puposes! \n\n"
62 + "\t-psm, --periodicallySendMessages <path to file> <period>\n"
63 + "\t\t <path to file> with groovy script which implements"
64 + " MessageGeneratorService followed by <period> in seconds.\n"
65 + "\t\t Messages which are sent periodically. Purely for testing puposes! \n\n"
67 + "\t-snm, --sendNowMessage <path to file>\n"
68 + "\t\t <path to file> with groovy script which implements MessageGeneratorService.\n"
69 + "\t\t Messages are sent in defined states defined by programmer. Purely for testing puposes! \n\n"
71 + "\t--help\n" + "\t\tdisplay this help and exits\n\n"
73 + "With no parameters, this help is printed.";
74 private static final int KA_TO_DEADTIMER_RATIO = 4;
75 private static final Uint8 KA_DEFAULT = Uint8.valueOf(30);
76 private static final PcepSessionErrorPolicy ERROR_POLICY = new PcepSessionErrorPolicy() {
78 public Uint16 getMaxUnknownMessages() {
79 return Uint16.valueOf(5);
83 public Class<? extends PcepSessionErrorPolicy> implementedInterface() {
84 throw new UnsupportedOperationException();
92 public static void main(final String[] args) throws UnknownHostException, InterruptedException, ExecutionException {
93 if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("--help")) {
98 InetSocketAddress address = null;
99 Uint8 keepAliveValue = KA_DEFAULT;
100 Uint8 deadTimerValue = Uint8.ZERO;
101 boolean stateful = false;
102 boolean active = false;
103 boolean instant = false;
106 while (pos < args.length) {
107 if (args[pos].equalsIgnoreCase("-a") || args[pos].equalsIgnoreCase("--address")) {
108 final String[] ip = args[pos + 1].split(":");
109 address = new InetSocketAddress(InetAddress.getByName(ip[0]), Integer.parseInt(ip[1]));
111 } else if (args[pos].equalsIgnoreCase("-d") || args[pos].equalsIgnoreCase("--deadtimer")) {
112 deadTimerValue = Uint8.valueOf(args[pos + 1]);
114 } else if (args[pos].equalsIgnoreCase("-ka") || args[pos].equalsIgnoreCase("--keepalive")) {
115 keepAliveValue = Uint8.valueOf(args[pos + 1]);
117 } else if (args[pos].equalsIgnoreCase("--stateful")) {
119 } else if (args[pos].equalsIgnoreCase("--active")) {
122 } else if (args[pos].equalsIgnoreCase("--instant")) {
126 LOG.warn("WARNING: Unrecognized argument: {}", args[pos]);
130 if (Uint8.ZERO.equals(deadTimerValue)) {
131 final var newValue = keepAliveValue.toJava() * KA_TO_DEADTIMER_RATIO;
132 deadTimerValue = newValue <= Uint8.MAX_VALUE.toJava() ? Uint8.valueOf(newValue) : Uint8.MAX_VALUE;
133 } else if (deadTimerValue.toJava() != keepAliveValue.toJava() * KA_TO_DEADTIMER_RATIO) {
134 LOG.warn("WARNING: The value of DeadTimer should be 4 times the value of KeepAlive.");
137 final List<PCEPCapability> caps = new ArrayList<>();
138 caps.add(new PCEPStatefulCapability(stateful, active, instant, false, false, false, false));
139 final PCEPSessionProposalFactory spf = new BasePCEPSessionProposalFactory(deadTimerValue, keepAliveValue, caps);
141 final MessageRegistry handlerRegistry = ServiceLoader.load(PCEPExtensionConsumerContext.class).findFirst()
143 .getMessageHandlerRegistry();
144 final PCEPDispatcherImpl dispatcher = new PCEPDispatcherImpl(handlerRegistry,
145 new DefaultPCEPSessionNegotiatorFactory(spf, ERROR_POLICY),
146 new NioEventLoopGroup(), new NioEventLoopGroup());
147 dispatcher.createServer(new TestToolPCEPDispatcherDependencies(address)).get();