2 * Copyright (c) 2016 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.bgp.state;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyBoolean;
15 import static org.mockito.ArgumentMatchers.anyLong;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.doAnswer;
18 import static org.mockito.Mockito.doNothing;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
25 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableSet;
29 import com.google.common.util.concurrent.Futures;
30 import com.google.common.util.concurrent.ListeningExecutorService;
31 import com.google.common.util.concurrent.MoreExecutors;
32 import java.math.BigDecimal;
33 import java.math.BigInteger;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Optional;
40 import java.util.concurrent.ScheduledExecutorService;
41 import java.util.concurrent.ScheduledFuture;
42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.atomic.LongAdder;
44 import org.junit.Assert;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.opendaylight.infrautils.testutils.LogCapture;
51 import org.opendaylight.infrautils.testutils.internal.RememberingLogger;
52 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
53 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTestCustomizer;
54 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
55 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
56 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
57 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
58 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
59 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
60 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
61 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
62 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
63 import org.opendaylight.protocol.bgp.rib.spi.State;
64 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
65 import org.opendaylight.protocol.bgp.rib.spi.state.BGPErrorHandlingState;
66 import org.opendaylight.protocol.bgp.rib.spi.state.BGPGracelfulRestartState;
67 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerMessagesState;
68 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
69 import org.opendaylight.protocol.bgp.rib.spi.state.BGPRibState;
70 import org.opendaylight.protocol.bgp.rib.spi.state.BGPSessionState;
71 import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateConsumer;
72 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
73 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTransportState;
74 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
75 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
76 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.afi.safi.GracefulRestartBuilder;
77 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.afi.safi.graceful.restart.StateBuilder;
78 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.BgpAfiSafiGracefulRestartState;
79 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.BgpNeighborState;
80 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.operational.rev151009.bgp.neighbor.prefix.counters_state.PrefixesBuilder;
81 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.graceful.restart.GracefulRestart;
82 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafis;
83 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafisBuilder;
84 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.ErrorHandling;
85 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.ErrorHandlingBuilder;
86 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Timers;
87 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.TimersBuilder;
88 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Transport;
89 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.TransportBuilder;
90 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
91 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroup;
92 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroupBuilder;
93 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
94 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
95 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.GlobalBuilder;
96 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
97 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.ADDPATHS;
98 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.ASN32;
99 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.BgpCapability;
100 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.CommunityType;
101 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.GRACEFULRESTART;
102 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
103 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.MPBGP;
104 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.ROUTEREFRESH;
105 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.NetworkInstances;
106 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstance;
107 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstanceKey;
108 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols;
109 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
110 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey;
111 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP;
112 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
113 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
114 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
115 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
116 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timeticks;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.BgpNeighborStateAugmentation;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.BgpNeighborStateAugmentationBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAfiSafiStateAugmentation;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAfiSafiStateAugmentationBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAfiSafiGracefulRestartStateAugmentation;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAfiSafiGracefulRestartStateAugmentationBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAfiSafiStateAugmentation;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAfiSafiStateAugmentationBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborErrorHandlingStateAugmentation;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborErrorHandlingStateAugmentationBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborGracefulRestartStateAugmentation;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborGracefulRestartStateAugmentationBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborStateAugmentation;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTimersStateAugmentation;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTimersStateAugmentationBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTransportStateAugmentation;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTransportStateAugmentationBuilder;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NetworkInstanceProtocol;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupStateAugmentation;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupStateAugmentationBuilder;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instances.network.instance.protocols.protocol.bgp.neighbors.neighbor.state.MessagesBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instances.network.instance.protocols.protocol.bgp.neighbors.neighbor.state.messages.ReceivedBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.network.instances.network.instance.protocols.protocol.bgp.neighbors.neighbor.state.messages.SentBuilder;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
148 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
149 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
150 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
151 import org.slf4j.LoggerFactory;
153 public class StateProviderImplTest extends AbstractDataBrokerTest {
154 private final LongAdder totalPathsCounter = new LongAdder();
155 private final LongAdder totalPrefixesCounter = new LongAdder();
156 private final PortNumber localPort = new PortNumber(1790);
157 private final PortNumber remotePort = new PortNumber(179);
158 private final int restartTime = 15;
159 private final String ribId = "identifier-test";
160 private final InstanceIdentifier<Bgp> bgpInstanceIdentifier = InstanceIdentifier.create(NetworkInstances.class)
161 .child(NetworkInstance.class, new NetworkInstanceKey("global-bgp")).child(Protocols.class)
162 .child(Protocol.class, new ProtocolKey(BGP.class, this.ribId)).augmentation(NetworkInstanceProtocol.class)
164 static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
165 private final AsNumber as = new AsNumber(72L);
166 private final BgpId bgpId = new BgpId("127.0.0.1");
167 private final IpAddress neighborAddress = new IpAddress(new Ipv4Address("127.0.0.2"));
168 private final List<Class<? extends BgpCapability>> supportedCap = Arrays.asList(ASN32.class, ROUTEREFRESH.class,
169 MPBGP.class, ADDPATHS.class, GRACEFULRESTART.class);
171 private BGPStateConsumer stateCollector;
173 private BGPTableTypeRegistryConsumer tableTypeRegistry;
175 private BGPRibState bgpRibState;
177 private BGPPeerState bgpPeerState;
179 private BGPSessionState bgpSessionState;
181 private BGPPeerMessagesState bgpPeerMessagesState;
183 private BGPTimersState timersState;
185 private BGPTransportState bgpTransportState;
187 private BGPErrorHandlingState bgpErrorHandlingState;
189 private BGPGracelfulRestartState bgpGracelfulRestartState;
191 private BGPAfiSafiState bgpAfiSafiState;
193 private final List<BGPPeerState> bgpPeerStates = new ArrayList<>();
194 private final List<BGPRibState> bgpRibStates = new ArrayList<>();
196 private InMemoryDOMDataStore realOperStore;
197 private InMemoryDOMDataStore spiedOperStore;
200 public void setUp() {
201 MockitoAnnotations.initMocks(this);
203 doReturn(Optional.of(IPV4UNICAST.class))
204 .when(this.tableTypeRegistry).getAfiSafiType(eq(TABLES_KEY));
206 doReturn(this.bgpRibStates).when(this.stateCollector).getRibStats();
207 doReturn(this.bgpPeerStates).when(this.stateCollector).getPeerStats();
209 final KeyedInstanceIdentifier<Rib, RibKey> iid = InstanceIdentifier.create(BgpRib.class)
210 .child(Rib.class, new RibKey(new RibId(this.ribId)));
211 doReturn(iid).when(this.bgpRibState).getInstanceIdentifier();
212 doReturn(this.as).when(this.bgpRibState).getAs();
213 doReturn(this.bgpId).when(this.bgpRibState).getRouteId();
215 doAnswer(invocation -> this.totalPathsCounter.longValue())
216 .when(this.bgpRibState).getTotalPathsCount();
217 doAnswer(invocation -> this.totalPrefixesCounter.longValue())
218 .when(this.bgpRibState).getTotalPrefixesCount();
219 doAnswer(invocation -> this.totalPathsCounter.longValue())
220 .when(this.bgpRibState).getPathCount(eq(TABLES_KEY));
221 doAnswer(invocation -> this.totalPrefixesCounter.longValue())
222 .when(this.bgpRibState).getPrefixesCount(eq(TABLES_KEY));
223 doAnswer(invocation -> Collections.singletonMap(TABLES_KEY,
224 this.totalPrefixesCounter.longValue())).when(this.bgpRibState).getTablesPrefixesCount();
225 doAnswer(invocation -> Collections.singletonMap(TABLES_KEY,
226 this.totalPathsCounter.longValue())).when(this.bgpRibState).getPathsCount();
229 doReturn("test-group").when(this.bgpPeerState).getGroupId();
230 doReturn(iid).when(this.bgpPeerState).getInstanceIdentifier();
231 doAnswer(invocation -> this.totalPrefixesCounter.longValue()).when(this.bgpPeerState).getTotalPrefixes();
232 doAnswer(invocation -> this.totalPathsCounter.longValue()).when(this.bgpPeerState).getTotalPathsCount();
233 doReturn(this.neighborAddress).when(this.bgpPeerState).getNeighborAddress();
234 doReturn(this.bgpSessionState).when(this.bgpPeerState).getBGPSessionState();
235 doReturn(this.bgpPeerMessagesState).when(this.bgpPeerState).getBGPPeerMessagesState();
237 doReturn(1L).when(this.bgpPeerMessagesState).getNotificationMessagesReceivedCount();
238 doReturn(1L).when(this.bgpPeerMessagesState).getNotificationMessagesSentCount();
239 doReturn(1L).when(this.bgpPeerMessagesState).getUpdateMessagesReceivedCount();
240 doReturn(1L).when(this.bgpPeerMessagesState).getUpdateMessagesSentCount();
241 doReturn(State.UP).when(this.bgpSessionState).getSessionState();
242 doReturn(true).when(this.bgpSessionState).isAddPathCapabilitySupported();
243 doReturn(true).when(this.bgpSessionState).isAsn32CapabilitySupported();
244 doReturn(true).when(this.bgpSessionState).isGracefulRestartCapabilitySupported();
245 doReturn(true).when(this.bgpSessionState).isMultiProtocolCapabilitySupported();
246 doReturn(true).when(this.bgpSessionState).isRouterRefreshCapabilitySupported();
248 doReturn(this.timersState).when(this.bgpPeerState).getBGPTimersState();
249 doReturn(10L).when(this.timersState).getNegotiatedHoldTime();
250 doReturn(10L).when(this.timersState).getUpTime();
252 doReturn(this.bgpTransportState).when(this.bgpPeerState).getBGPTransportState();
253 doReturn(this.localPort).when(this.bgpTransportState).getLocalPort();
254 doReturn(this.neighborAddress).when(this.bgpTransportState).getRemoteAddress();
255 doReturn(this.remotePort).when(this.bgpTransportState).getRemotePort();
257 doReturn(this.bgpErrorHandlingState).when(this.bgpPeerState).getBGPErrorHandlingState();
258 doReturn(1L).when(this.bgpErrorHandlingState).getErroneousUpdateReceivedCount();
260 doReturn(this.bgpGracelfulRestartState).when(this.bgpPeerState).getBGPGracelfulRestart();
261 doReturn(true).when(this.bgpGracelfulRestartState).isGracefulRestartAdvertized(any());
262 doReturn(true).when(this.bgpGracelfulRestartState).isGracefulRestartReceived(any());
263 doReturn(true).when(this.bgpGracelfulRestartState).isLocalRestarting();
264 doReturn(true).when(this.bgpGracelfulRestartState).isPeerRestarting();
265 doReturn(this.restartTime).when(this.bgpGracelfulRestartState).getPeerRestartTime();
266 doReturn(BgpAfiSafiGracefulRestartState.Mode.BILATERAL).when(this.bgpGracelfulRestartState).getMode();
268 doReturn(this.bgpAfiSafiState).when(this.bgpPeerState).getBGPAfiSafiState();
269 doReturn(Collections.singleton(TABLES_KEY)).when(this.bgpAfiSafiState).getAfiSafisAdvertized();
270 doReturn(Collections.singleton(TABLES_KEY)).when(this.bgpAfiSafiState).getAfiSafisReceived();
271 doReturn(1L).when(this.bgpAfiSafiState).getPrefixesInstalledCount(any());
272 doReturn(2L).when(this.bgpAfiSafiState).getPrefixesReceivedCount(any());
273 doReturn(1L).when(this.bgpAfiSafiState).getPrefixesSentCount(any());
274 doReturn(true).when(this.bgpAfiSafiState).isAfiSafiSupported(any());
275 doReturn(true).when(this.bgpAfiSafiState).isGracefulRestartAdvertized(any());
276 doReturn(true).when(this.bgpAfiSafiState).isGracefulRestartReceived(any());
277 doReturn(true).when(this.bgpAfiSafiState).isLlGracefulRestartAdvertised(any());
278 doReturn(true).when(this.bgpAfiSafiState).isLlGracefulRestartReceived(any());
279 doReturn(60).when(this.bgpAfiSafiState).getLlGracefulRestartTimer(any());
283 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
284 return ImmutableSet.of(BindingReflections.getModuleInfo(NetworkInstances.class),
285 BindingReflections.getModuleInfo(NetworkInstanceProtocol.class));
289 protected AbstractDataBrokerTestCustomizer createDataBrokerTestCustomizer() {
290 return new ConcurrentDataBrokerTestCustomizer(true) {
292 public DOMStore createOperationalDatastore() {
293 realOperStore = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
294 spiedOperStore = spy(realOperStore);
295 getSchemaService().registerSchemaContextListener(spiedOperStore);
296 return spiedOperStore;
300 public ListeningExecutorService getCommitCoordinatorExecutor() {
301 return MoreExecutors.newDirectExecutorService();
307 public void testActiveStateProvider() throws Exception {
308 doReturn(true).when(this.bgpRibState).isActive();
309 doReturn(true).when(this.bgpPeerState).isActive();
311 final StateProviderImpl stateProvider = new StateProviderImpl(getDataBroker(), 1, this.tableTypeRegistry,
312 this.stateCollector, "global-bgp");
313 stateProvider.init();
315 final Global globalExpected = buildGlobalExpected(0);
316 this.bgpRibStates.add(this.bgpRibState);
317 readDataOperational(getDataBroker(), this.bgpInstanceIdentifier, bgpRib -> {
318 final Global global = bgpRib.getGlobal();
319 assertEquals(globalExpected, global);
323 this.totalPathsCounter.increment();
324 this.totalPrefixesCounter.increment();
326 final Global globalExpected2 = buildGlobalExpected(1);
327 readDataOperational(getDataBroker(), this.bgpInstanceIdentifier, bgpRib -> {
328 final Global global = bgpRib.getGlobal();
329 assertEquals(globalExpected2, global);
333 this.totalPathsCounter.decrement();
334 this.totalPrefixesCounter.decrement();
336 final Global globalExpected3 = buildGlobalExpected(0);
337 readDataOperational(getDataBroker(), this.bgpInstanceIdentifier, bgpRib -> {
338 final Global global = bgpRib.getGlobal();
339 assertEquals(globalExpected3, global);
340 Assert.assertNull(bgpRib.getNeighbors());
341 Assert.assertNull(bgpRib.getPeerGroups());
345 this.bgpPeerStates.add(this.bgpPeerState);
346 final PeerGroup peerGroupExpected = buildGroupExpected();
348 this.totalPathsCounter.increment();
349 this.totalPrefixesCounter.increment();
351 final AfiSafis expectedAfiSafis = buildAfiSafis();
352 final ErrorHandling expectedErrorHandling = buildErrorHandling();
353 final GracefulRestart expectedGracefulRestart = buildGracefulRestart();
354 final Transport expectedTransport = buildTransport();
355 final Timers expectedTimers = buildTimers();
356 final BgpNeighborStateAugmentation expectedBgpNeighborState = buildBgpNeighborStateAugmentation();
358 readDataOperational(getDataBroker(), this.bgpInstanceIdentifier, bgpRib -> {
359 final Neighbors neighbors = bgpRib.getNeighbors();
360 Assert.assertNotNull(neighbors);
361 assertEquals(peerGroupExpected, bgpRib.getPeerGroups().getPeerGroup().get(0));
362 final Neighbor neighborResult = neighbors.getNeighbor().get(0);
363 assertEquals(this.neighborAddress, neighborResult.getNeighborAddress());
364 assertEquals(expectedAfiSafis, neighborResult.getAfiSafis());
365 assertEquals(expectedErrorHandling, neighborResult.getErrorHandling());
366 assertEquals(expectedGracefulRestart, neighborResult.getGracefulRestart());
367 assertEquals(expectedTransport, neighborResult.getTransport());
368 assertEquals(expectedTimers, neighborResult.getTimers());
369 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
370 .State stateResult = neighborResult.getState();
371 assertEquals(expectedBgpNeighborState, stateResult.augmentation(BgpNeighborStateAugmentation.class));
372 assertEquals(BgpNeighborState.SessionState.ESTABLISHED, stateResult
373 .augmentation(NeighborStateAugmentation.class).getSessionState());
374 final List<Class<? extends BgpCapability>> supportedCapabilitiesResult = stateResult
375 .augmentation(NeighborStateAugmentation.class).getSupportedCapabilities();
376 Assert.assertTrue(supportedCapabilitiesResult.containsAll(this.supportedCap));
380 this.bgpRibStates.clear();
381 checkNotPresentOperational(getDataBroker(), this.bgpInstanceIdentifier);
383 stateProvider.close();
387 public void testInactiveStateProvider() throws Exception {
388 doReturn(false).when(this.bgpRibState).isActive();
389 doReturn(false).when(this.bgpPeerState).isActive();
391 final StateProviderImpl stateProvider = new StateProviderImpl(getDataBroker(), 1, this.tableTypeRegistry,
392 this.stateCollector, "global-bgp");
393 stateProvider.init();
395 this.bgpRibStates.add(this.bgpRibState);
396 checkNotPresentOperational(getDataBroker(), this.bgpInstanceIdentifier);
398 this.bgpPeerStates.add(this.bgpPeerState);
399 checkNotPresentOperational(getDataBroker(), this.bgpInstanceIdentifier);
401 this.bgpRibStates.clear();
402 checkNotPresentOperational(getDataBroker(), this.bgpInstanceIdentifier);
404 stateProvider.close();
408 public void testTransactionChainFailure() throws Exception {
409 if (!(LoggerFactory.getLogger(StateProviderImpl.class) instanceof RememberingLogger)) {
410 throw new IllegalStateException("infrautils-testutils must be on the classpath BEFORE other logger impls"
411 + LoggerFactory.getLogger(StateProviderImpl.class).getClass());
414 doReturn(true).when(this.bgpRibState).isActive();
416 this.bgpRibStates.add(this.bgpRibState);
418 ScheduledFuture<?> mockScheduledFuture = mock(ScheduledFuture.class);
419 doReturn(true).when(mockScheduledFuture).cancel(anyBoolean());
421 ScheduledExecutorService mockScheduler = mock(ScheduledExecutorService.class);
422 doReturn(mockScheduledFuture).when(mockScheduler).scheduleAtFixedRate(any(Runnable.class), anyLong(),
423 anyLong(), any(TimeUnit.class));
424 doNothing().when(mockScheduler).shutdown();
426 DOMStoreTransactionChain mockTxChain = mock(DOMStoreTransactionChain.class);
427 doNothing().when(mockTxChain).close();
429 Throwable mockCommitEx = new Exception("mock commit failure");
430 doAnswer(invocation -> {
431 DOMStoreThreePhaseCommitCohort mockCohort = mock(DOMStoreThreePhaseCommitCohort.class);
432 doReturn(Futures.immediateFailedFuture(mockCommitEx)).when(mockCohort).canCommit();
433 doReturn(Futures.immediateFuture(null)).when(mockCohort).abort();
435 doAnswer(notused -> {
436 DOMStoreWriteTransaction mockWriteTx = mock(DOMStoreReadWriteTransaction .class);
437 doNothing().when(mockWriteTx).write(any(), any());
438 doNothing().when(mockWriteTx).merge(any(), any());
439 doNothing().when(mockWriteTx).delete(any());
440 doReturn(mockCohort).when(mockWriteTx).ready();
442 }).when(mockTxChain).newReadWriteTransaction();
445 }).doAnswer(invocation -> realOperStore.createTransactionChain()).when(spiedOperStore).createTransactionChain();
447 int timerInterval = 1;
448 try (StateProviderImpl stateProvider = new StateProviderImpl(getDataBroker(), timerInterval, tableTypeRegistry,
449 stateCollector, "global-bgp", mockScheduler)) {
450 stateProvider.init();
452 ArgumentCaptor<Runnable> timerTask = ArgumentCaptor.forClass(Runnable.class);
453 verify(mockScheduler).scheduleAtFixedRate(timerTask.capture(), eq(0L), eq((long)timerInterval),
454 eq(TimeUnit.SECONDS));
456 timerTask.getValue().run();
458 String lastError = RememberingLogger.getLastErrorThrowable().orElseThrow(
459 () -> new AssertionError("Expected logged ERROR")).toString();
460 assertTrue("Last logged ERROR didn't contain expected string: " + lastError,
461 lastError.contains(mockCommitEx.getMessage()));
463 RememberingLogger.resetLastError();
465 timerTask.getValue().run();
467 ImmutableList<LogCapture> loggedErrors = RememberingLogger.getErrorLogCaptures();
468 assertTrue("Expected no logged ERRORs: " + loggedErrors, loggedErrors.isEmpty());
470 verify(spiedOperStore, times(2)).createTransactionChain();
474 private static BgpNeighborStateAugmentation buildBgpNeighborStateAugmentation() {
475 final BgpNeighborStateAugmentation augmentation = new BgpNeighborStateAugmentationBuilder()
476 .setMessages(new MessagesBuilder().setReceived(new ReceivedBuilder()
477 .setNOTIFICATION(BigInteger.ONE).setUPDATE(BigInteger.ONE).build())
478 .setSent(new SentBuilder().setNOTIFICATION(BigInteger.ONE).setUPDATE(BigInteger.ONE).build())
483 private static AfiSafis buildAfiSafis() {
484 final NeighborAfiSafiStateAugmentationBuilder neighborAfiSafiStateAugmentation =
485 new NeighborAfiSafiStateAugmentationBuilder().setActive(true).setPrefixes(
486 new PrefixesBuilder().setSent(1L).setReceived(2L).setInstalled(1L).build());
487 final AfiSafi afiSafi = new AfiSafiBuilder()
488 .setAfiSafiName(IPV4UNICAST.class)
489 .setGracefulRestart(new GracefulRestartBuilder().setState(new StateBuilder().setEnabled(false)
490 .addAugmentation(NeighborAfiSafiGracefulRestartStateAugmentation.class,
491 new NeighborAfiSafiGracefulRestartStateAugmentationBuilder()
494 .setLlStaleTimer(60L)
495 .setLlAdvertised(true)
499 .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp
500 .common.afi.safi.list.afi.safi.StateBuilder().setEnabled(false).addAugmentation(
501 NeighborAfiSafiStateAugmentation.class, neighborAfiSafiStateAugmentation.build())
505 return new AfiSafisBuilder().setAfiSafi(Collections.singletonList(afiSafi)).build();
508 private static ErrorHandling buildErrorHandling() {
509 final ErrorHandling errorHandling = new ErrorHandlingBuilder().setState(
510 new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.error
511 .handling.StateBuilder().setTreatAsWithdraw(false)
512 .addAugmentation(NeighborErrorHandlingStateAugmentation.class,
513 new NeighborErrorHandlingStateAugmentationBuilder()
514 .setErroneousUpdateMessages(1L).build()).build()).build();
515 return errorHandling;
518 private static Timers buildTimers() {
519 final Timers timers = new TimersBuilder().setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang
520 .bgp.rev151009.bgp.neighbor.group.timers.StateBuilder()
521 .setConnectRetry(BigDecimal.valueOf(30))
522 .setHoldTime(BigDecimal.valueOf(90))
523 .setKeepaliveInterval(BigDecimal.valueOf(30))
524 .setMinimumAdvertisementInterval(BigDecimal.valueOf(30))
525 .addAugmentation(NeighborTimersStateAugmentation.class, new NeighborTimersStateAugmentationBuilder()
526 .setNegotiatedHoldTime(BigDecimal.TEN).setUptime(new Timeticks(1L)).build())
531 private Transport buildTransport() {
532 final Transport transport = new TransportBuilder().setState(new org.opendaylight.yang.gen.v1.http.openconfig
533 .net.yang.bgp.rev151009.bgp.neighbor.group.transport.StateBuilder()
534 .setMtuDiscovery(false)
535 .setPassiveMode(false)
536 .addAugmentation(NeighborTransportStateAugmentation.class,
537 new NeighborTransportStateAugmentationBuilder().setLocalPort(this.localPort)
538 .setRemotePort(this.remotePort)
539 .setRemoteAddress(this.neighborAddress).build())
544 private GracefulRestart buildGracefulRestart() {
545 final NeighborGracefulRestartStateAugmentationBuilder gracefulAugmentation
546 = new NeighborGracefulRestartStateAugmentationBuilder();
547 gracefulAugmentation.setPeerRestarting(false);
548 gracefulAugmentation.setLocalRestarting(false);
549 gracefulAugmentation.setPeerRestartTime(0);
550 gracefulAugmentation.setLocalRestarting(true)
551 .setPeerRestarting(true)
552 .setPeerRestartTime(this.restartTime)
553 .setMode(BgpAfiSafiGracefulRestartState.Mode.BILATERAL);
554 final GracefulRestart gracefulRestart = new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
555 .rev151009.bgp.graceful.restart.GracefulRestartBuilder().setState(new org.opendaylight.yang.gen.v1.http
556 .openconfig.net.yang.bgp.rev151009.bgp.graceful.restart.graceful.restart.StateBuilder()
557 .addAugmentation(NeighborGracefulRestartStateAugmentation.class,
558 gracefulAugmentation.build()).build()).build();
559 return gracefulRestart;
562 private Global buildGlobalExpected(final long prefixesAndPaths) {
563 return new GlobalBuilder()
564 .setState(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base
565 .StateBuilder().setRouterId(new Ipv4Address(this.bgpId.getValue()))
566 .setTotalPrefixes(prefixesAndPaths).setTotalPaths(prefixesAndPaths).setAs(this.as).build())
567 .setAfiSafis(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base
568 .AfiSafisBuilder().setAfiSafi(Collections.singletonList(new AfiSafiBuilder()
569 .setAfiSafiName(IPV4UNICAST.class).setState(new org.opendaylight.yang.gen.v1.http.openconfig
570 .net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.afi.safi.StateBuilder()
572 .addAugmentation(GlobalAfiSafiStateAugmentation.class,
573 new GlobalAfiSafiStateAugmentationBuilder()
574 .setTotalPaths(prefixesAndPaths).setTotalPrefixes(prefixesAndPaths)
575 .build()).build()).build()))
579 private static PeerGroup buildGroupExpected() {
580 return new PeerGroupBuilder().setPeerGroupName("test-group").setState(new org.opendaylight.yang.gen.v1.http
581 .openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.StateBuilder()
582 .setSendCommunity(CommunityType.NONE)
583 .setRouteFlapDamping(false)
584 .addAugmentation(PeerGroupStateAugmentation.class,
585 new PeerGroupStateAugmentationBuilder().setTotalPaths(1L).setTotalPrefixes(1L)