1951ddca9ebf0cfeb6d9e35308895d2eef65bcd0
[bgpcep.git] / pcep / pcc-mock / src / main / java / org / opendaylight / protocol / pcep / pcc / mock / PCCsBuilder.java
1 /*
2  * Copyright (c) 2015 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 com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import com.google.common.net.InetAddresses;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.Timer;
16 import java.math.BigInteger;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.util.List;
20 import java.util.concurrent.ExecutionException;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.protocol.concepts.KeyMapping;
24 import org.opendaylight.protocol.pcep.PCEPCapability;
25 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
26 import org.opendaylight.protocol.pcep.ietf.initiated00.CrabbeInitiatedActivator;
27 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
28 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
29 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
30 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
31 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
32 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
33 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
34 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
35 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
36 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
37
38 final class PCCsBuilder {
39     private final int lsps;
40     private final boolean pcError;
41     private final int pccCount;
42     private final InetSocketAddress localAddress;
43     private final List<InetSocketAddress> remoteAddress;
44     private final short keepAlive;
45     private final short deadTimer;
46     private final String password;
47     private final long reconnectTime;
48     private final int redelegationTimeout;
49     private final int stateTimeout;
50     private final PCEPCapability pcepCapabilities;
51     private final Timer timer = new HashedWheelTimer();
52     private PCCDispatcherImpl pccDispatcher;
53
54     PCCsBuilder(final int lsps, final boolean pcError, final int pccCount,
55             @Nonnull final InetSocketAddress localAddress, @Nonnull final List<InetSocketAddress> remoteAddress,
56             final short keepAlive, final short deadTimer, @Nullable final String password, final long reconnectTime,
57             final int redelegationTimeout, final int stateTimeout, @Nonnull final PCEPCapability pcepCapabilities) {
58         this.lsps = lsps;
59         this.pcError = pcError;
60         this.pccCount = pccCount;
61         this.localAddress = localAddress;
62         this.remoteAddress = remoteAddress;
63         this.keepAlive = keepAlive;
64         this.deadTimer = deadTimer;
65         this.password = password;
66         this.reconnectTime = reconnectTime;
67         this.redelegationTimeout = redelegationTimeout;
68         this.stateTimeout = stateTimeout;
69         this.pcepCapabilities = pcepCapabilities;
70         startActivators();
71     }
72
73     void createPCCs(final BigInteger initialDBVersion, final Optional<TimerHandler> timerHandler)
74         throws InterruptedException, ExecutionException {
75         InetAddress currentAddress = this.localAddress.getAddress();
76         this.pccDispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance()
77                 .getMessageHandlerRegistry());
78         if (timerHandler.isPresent()) {
79             timerHandler.get().setPCCDispatcher(this.pccDispatcher);
80         }
81         for (int i = 0; i < this.pccCount; i++) {
82             final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(this.lsps, currentAddress,
83                 this.redelegationTimeout, this.stateTimeout, this.timer, timerHandler);
84             createPCC(new InetSocketAddress(currentAddress, this.localAddress.getPort()), tunnelManager,
85                     initialDBVersion);
86             currentAddress = InetAddresses.increment(currentAddress);
87         }
88     }
89
90     private void createPCC(@Nonnull final InetSocketAddress plocalAddress,
91             final PCCTunnelManager tunnelManager, final BigInteger initialDBVersion)
92             throws InterruptedException, ExecutionException {
93         final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
94         for (final InetSocketAddress pceAddress : this.remoteAddress) {
95             this.pccDispatcher.createClient(pceAddress, this.reconnectTime, () -> new PCCSessionListener(
96                             this.remoteAddress.indexOf(pceAddress), tunnelManager, this.pcError), snf,
97                     KeyMapping.getKeyMapping(pceAddress.getAddress(), this.password), plocalAddress, initialDBVersion);
98         }
99     }
100
101     private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
102         final List<PCEPCapability> capabilities = Lists.newArrayList(this.pcepCapabilities);
103         return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(this.deadTimer,
104             this.keepAlive, capabilities), 0);
105     }
106
107     private static void startActivators() {
108         final PCCActivator pccActivator = new PCCActivator();
109         final StatefulActivator stateful = new StatefulActivator();
110         final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
111         final CrabbeInitiatedActivator activator = new CrabbeInitiatedActivator();
112         final PCEPExtensionProviderContext ctx = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
113         pccActivator.start(ctx);
114         stateful.start(ctx);
115         optimizationsActivator.start(ctx);
116         activator.start(ctx);
117     }
118 }