Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / testtool / src / main / java / org / opendaylight / protocol / pcep / testtool / Main.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.pcep.testtool;
9
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;
31
32 public final class Main {
33
34     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
35
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"
41
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"
46
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"
49
50             + "\t--stateful\n" + "\t\tpassive stateful\n\n"
51
52             + "\t--active\n" + "\t\tactive stateful (implies --stateful)\n\n"
53
54             + "\t--instant\n"
55             + "\t\tinstantiated stateful, <seconds> cleanup timeout "
56             + "(default value, if not included = 0) (implies --stateful)\n\n"
57
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"
61
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"
66
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"
70
71             + "\t--help\n" + "\t\tdisplay this help and exits\n\n"
72
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() {
77         @Override
78         public Uint16 getMaxUnknownMessages() {
79             return Uint16.valueOf(5);
80         }
81
82         @Override
83         public Class<? extends PcepSessionErrorPolicy> implementedInterface() {
84             throw new UnsupportedOperationException();
85         }
86     };
87
88     private Main() {
89
90     }
91
92     public static void main(final String[] args) throws UnknownHostException, InterruptedException, ExecutionException {
93         if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("--help")) {
94             LOG.info(Main.USAGE);
95             return;
96         }
97
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;
104
105         int pos = 0;
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]));
110                 pos++;
111             } else if (args[pos].equalsIgnoreCase("-d") || args[pos].equalsIgnoreCase("--deadtimer")) {
112                 deadTimerValue = Uint8.valueOf(args[pos + 1]);
113                 pos++;
114             } else if (args[pos].equalsIgnoreCase("-ka") || args[pos].equalsIgnoreCase("--keepalive")) {
115                 keepAliveValue = Uint8.valueOf(args[pos + 1]);
116                 pos++;
117             } else if (args[pos].equalsIgnoreCase("--stateful")) {
118                 stateful = true;
119             } else if (args[pos].equalsIgnoreCase("--active")) {
120                 stateful = true;
121                 active = true;
122             } else if (args[pos].equalsIgnoreCase("--instant")) {
123                 stateful = true;
124                 instant = true;
125             } else {
126                 LOG.warn("WARNING: Unrecognized argument: {}", args[pos]);
127             }
128             pos++;
129         }
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.");
135         }
136
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);
140
141         final MessageRegistry handlerRegistry = ServiceLoader.load(PCEPExtensionConsumerContext.class).findFirst()
142             .orElseThrow()
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();
148     }
149 }