Merge "BUG-731 : fixed pcep sonar warnings"
[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.Predicate;
14 import com.google.common.collect.Iterables;
15 import com.google.common.net.InetAddresses;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.net.UnknownHostException;
20 import java.util.concurrent.ExecutionException;
21 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
22 import org.opendaylight.protocol.framework.SessionListenerFactory;
23 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
24 import org.opendaylight.protocol.pcep.PCEPSessionListener;
25 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
26 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
27 import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
28 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
29 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public final class Main {
36
37     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
38
39     private static final int DEFAULT_PORT = 4189;
40     private static final short DEFAULT_KEEP_ALIVE = 30;
41     private static final short DEFAULT_DEAD_TIMER = 120;
42     private static final int RECONNECT_STRATEGY_TIMEOUT = 2000;
43
44     private Main() { }
45
46     public static void main(final String[] args) throws InterruptedException, ExecutionException, UnknownHostException {
47         InetAddress localAddress = InetAddress.getByName("127.0.0.1");
48         InetAddress remoteAddress = InetAddress.getByName("127.0.0.1");
49         int pccCount = 1;
50         int lsps = 1;
51         boolean pcError = false;
52         final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
53
54         getRootLogger(lc).setLevel(ch.qos.logback.classic.Level.INFO);
55         int argIdx = 0;
56         while (argIdx < args.length) {
57             if (args[argIdx].equals("--local-address")) {
58                 localAddress = InetAddress.getByName(args[++argIdx]);
59             } else if (args[argIdx].equals("--remote-address")) {
60                 remoteAddress = InetAddress.getByName(args[++argIdx]);
61             } else if (args[argIdx].equals("--pcc")) {
62                 pccCount = Integer.valueOf(args[++argIdx]);
63             } else if (args[argIdx].equals("--lsp")) {
64                 lsps = Integer.valueOf(args[++argIdx]);
65             } else if (args[argIdx].equals("--pcerr")) {
66                 pcError = true;
67             } else if (args[argIdx].equals("--log-level")) {
68                 getRootLogger(lc).setLevel(Level.toLevel(args[++argIdx], ch.qos.logback.classic.Level.INFO));
69             } else {
70                 LOG.warn("WARNING: Unrecognized argument: {}", args[argIdx]);
71             }
72             argIdx++;
73         }
74         createPCCs(lsps, pcError, pccCount, localAddress, remoteAddress);
75     }
76
77     public static void createPCCs(final int lspsPerPcc, final boolean pcerr, final int pccCount,
78             final InetAddress localAddress, final InetAddress remoteAddress) throws InterruptedException, ExecutionException {
79         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(
80                 new OpenBuilder().setKeepalive(DEFAULT_KEEP_ALIVE).setDeadTimer(DEFAULT_DEAD_TIMER).setSessionId((short) 0).build(), 0);
81
82         final StatefulActivator activator07 = new StatefulActivator();
83         final PCCActivator activator = new PCCActivator();
84         activator07.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
85         activator.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
86         final PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(
87                 ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()));
88
89         final InetAddress pceAddress = remoteAddress;
90         InetAddress currentAddress = localAddress;
91         int i = 0;
92         while (i < pccCount) {
93             final InetAddress pccAddress = currentAddress;
94             pcc.createClient(new InetSocketAddress(pccAddress, 0), new InetSocketAddress(pceAddress, DEFAULT_PORT),
95                     new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT),
96                     new SessionListenerFactory<PCEPSessionListener>() {
97
98                         @Override
99                         public PCEPSessionListener getSessionListener() {
100                             return new SimpleSessionListener(lspsPerPcc, pcerr, pccAddress);
101                         }
102                     }).get();
103             i++;
104             currentAddress = InetAddresses.increment(currentAddress);
105         }
106     }
107
108     private static ch.qos.logback.classic.Logger getRootLogger(final LoggerContext lc) {
109         return Iterables.find(lc.getLoggerList(), new Predicate<Logger>() {
110             @Override
111             public boolean apply(final Logger input) {
112                 return (input != null) ? input.getName().equals(Logger.ROOT_LOGGER_NAME) : false;
113             }
114         });
115     }
116
117 }