2 * Copyright (c) 2018 AT&T Intellectual Property. 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.bgp.rib.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkIdleState;
14 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkStateIsNotRestarting;
15 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkUpState;
16 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
17 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
18 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
20 import com.google.common.collect.ImmutableMap;
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelFuture;
23 import java.net.InetSocketAddress;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.Optional;
33 import java.util.stream.Collectors;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
39 import org.opendaylight.protocol.bgp.mode.impl.add.all.paths.AllPathSelection;
40 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
41 import org.opendaylight.protocol.bgp.rib.impl.config.BgpPeer;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.OpenBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.ProtocolVersion;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.LocRib;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpOrigin;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.binding.Notification;
71 import org.opendaylight.yangtools.yang.common.Uint16;
72 import org.opendaylight.yangtools.yang.common.Uint8;
74 public class GracefulRestartTest extends AbstractAddPathTest {
76 private BGPSessionImpl session;
78 private final Set<TablesKey> afiSafiAdvertised = new HashSet<>();
79 private final Set<TablesKey> gracefulAfiSafiAdvertised = new HashSet<>();
80 private RIBImpl ribImpl;
81 private Channel serverChannel;
82 private final SimpleSessionListener listener = new SimpleSessionListener();
83 private final BgpParameters parameters = createParameter(false, true, Collections.singletonMap(TABLES_KEY, true));
84 private static final int DEFERRAL_TIMER = 5;
85 private static final RibId RIBID = new RibId("test-rib");
86 private static final Ipv4Prefix PREFIX2 = new Ipv4Prefix("2.2.2.2/32");
87 private static final Ipv6Prefix PREFIX3 = new Ipv6Prefix("dead:beef::/64");
88 private static final Ipv6AddressNoZone IPV6_NEXT_HOP = new Ipv6AddressNoZone("dead:beef::1");
89 private static final TablesKey IPV6_TABLES_KEY = new TablesKey(Ipv6AddressFamily.class,
90 UnicastSubsequentAddressFamily.class);
92 private static final InstanceIdentifier<LocRib> LOC_RIB_IID = InstanceIdentifier.builder(BgpRib.class)
93 .child(Rib.class, new RibKey(RIBID))
96 private static final InstanceIdentifier<Tables> IPV4_IID = LOC_RIB_IID.builder()
97 .child(Tables.class,TABLES_KEY)
99 private static final InstanceIdentifier<Tables> IPV6_IID = LOC_RIB_IID.builder()
100 .child(Tables.class, IPV6_TABLES_KEY)
105 public void setUp() throws Exception {
107 final Map<TablesKey, PathSelectionMode> pathTables
108 = ImmutableMap.of(TABLES_KEY, new AllPathSelection());
109 final ArrayList<BgpTableType> tableTypes = new ArrayList<>(TABLES_TYPE);
110 tableTypes.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
111 this.ribImpl = new RIBImpl(this.tableRegistry, RIBID, AS_NUMBER, BGP_ID, this.ribExtension,
112 this.serverDispatcher, this.codecsRegistry,
113 getDomBroker(), getDataBroker(), this.policies, tableTypes, pathTables);
115 this.ribImpl.instantiateServiceInstance();
116 final ChannelFuture channelFuture = this.serverDispatcher.createServer(
117 new InetSocketAddress(RIB_ID, PORT.toJava()));
118 waitFutureSuccess(channelFuture);
119 this.serverChannel = channelFuture.channel();
121 gracefulAfiSafiAdvertised.add(TABLES_KEY);
122 afiSafiAdvertised.add(TABLES_KEY);
123 afiSafiAdvertised.add(IPV6_TABLES_KEY);
124 final BgpPeer bgpPeer = Mockito.mock(BgpPeer.class);
125 Mockito.doReturn(GRACEFUL_RESTART_TIME).when(bgpPeer).getGracefulRestartTimer();
126 Mockito.doReturn(Optional.empty()).when(bgpPeer).getErrorHandling();
127 Mockito.doReturn(createParameter(false, true, Collections.singletonMap(TABLES_KEY, false))
128 .getOptionalCapabilities()).when(bgpPeer).getBgpFixedCapabilities();
129 this.peer = configurePeer(this.tableRegistry, PEER1, this.ribImpl, parameters, PeerRole.Ibgp,
130 this.serverRegistry, afiSafiAdvertised, gracefulAfiSafiAdvertised, Collections.emptyMap(), bgpPeer);
131 this.session = createPeerSession(PEER1, parameters, this.listener);
136 public void tearDown() throws Exception {
137 waitFutureSuccess(this.serverChannel.close());
138 this.session.close();
143 * Test correct behavior when connection restart is unnoticed.
144 * "Correct" means that the previous TCP session MUST be closed, and the new one retained.
145 * Since the previous connection is considered to be terminated, no NOTIFICATION message should be sent.
148 public void resetConnectionOnOpenTest() {
150 checkReceivedMessages(this.listener, 2);
151 final Open open = createClassicOpen(true);
152 this.session.writeAndFlush(open);
153 checkIdleState(this.peer);
154 checkReceivedMessages(this.listener, 2);
158 * Test that routes from peer that has advertised the Graceful Restart Capability MUST be retained
159 * for all the address families that were previously received in the Graceful Restart Capability.
161 * @throws Exception on reading Rib failure
164 public void retainRoutesOnPeerRestartTest() throws Exception {
165 final List<Ipv4Prefix> ipv4Prefixes = Arrays.asList(new Ipv4Prefix(PREFIX1), new Ipv4Prefix(PREFIX2));
166 final List<Ipv6Prefix> ipv6Prefixes = Collections.singletonList(new Ipv6Prefix(PREFIX3));
167 insertRoutes(ipv4Prefixes, ipv6Prefixes);
168 checkLocRibIpv4Routes(2);
169 checkLocRibIpv6Routes(1);
171 this.session.close();
172 checkIdleState(this.peer);
173 checkLocRibIpv4Routes(2);
174 checkLocRibIpv6Routes(0);
178 * If the session does not get re-established within the "Restart Time"
179 * that the peer advertised previously, the Receiving Speaker MUST
180 * delete all the stale routes from the peer that it is retaining.
182 * @throws Exception on reading Rib failure
185 public void removeRoutesOnHoldTimeExpireTest() throws Exception {
186 retainRoutesOnPeerRestartTest();
187 checkStateIsNotRestarting(peer, GRACEFUL_RESTART_TIME);
188 checkLocRibIpv4Routes(0);
189 checkLocRibIpv6Routes(0);
193 * Once the session is re-established, if the Graceful
194 * Restart Capability is not received in the re-established session at
195 * all, then the Receiving Speaker MUST immediately remove all the stale
196 * routes from the peer that it is retaining for that address family.
198 * @throws Exception on reading Rib failure
201 public void removeRoutesOnMissingGracefulRestartTest() throws Exception {
202 retainRoutesOnPeerRestartTest();
203 this.session = createPeerSession(PEER1, createParameter(false, true, null), this.listener);
204 checkUpState(listener);
205 checkLocRibIpv4Routes(0);
206 checkLocRibIpv6Routes(0);
210 * Once the session is re-established, if a specific address family is not included
211 * in the newly received Graceful Restart Capability, then the Receiving Speaker
212 * MUST immediately remove all the stale routes from the peer that it is retaining
213 * for that address family.
216 * @throws Exception on reading Rib failure
219 public void removeRoutesOnMissingGracefulRestartAfiSafiTest() throws Exception {
220 retainRoutesOnPeerRestartTest();
221 this.session = createPeerSession(PEER1, createParameter(false, true,
222 Collections.singletonMap(TABLES_KEY, false)), this.listener);
223 checkUpState(listener);
224 checkUpState(this.peer);
225 checkLocRibIpv4Routes(0);
226 checkLocRibIpv6Routes(0);
230 * Once the End-of-RIB marker for an address family is received from the peer, it MUST
231 * immediately remove any routes from the peer that are still marked as stale for that
234 * @throws Exception on reading Rib failure
237 public void removeStaleRoutesAfterRestartTest() throws Exception {
238 retainRoutesOnPeerRestartTest();
239 this.session = createPeerSession(PEER1, createParameter(false, true,
240 Collections.singletonMap(TABLES_KEY, true)), this.listener);
241 checkUpState(this.listener);
242 final List<Ipv4Prefix> ipv4prefixes = Arrays.asList(new Ipv4Prefix(PREFIX1));
243 insertRoutes(ipv4prefixes, null);
244 insertRoutes(null, null);
245 checkLocRibIpv4Routes(1);
246 checkLocRibIpv6Routes(0);
250 * Perform local graceful restart and verify routes are preserved.
252 * @throws Exception on reading Rib failure
255 public void performLocalGracefulRestart() throws Exception {
256 final List<Ipv4Prefix> ipv4prefixes = Arrays.asList(new Ipv4Prefix(PREFIX1), new Ipv4Prefix(PREFIX2));
257 final List<Ipv6Prefix> ipv6prefixes = Arrays.asList(new Ipv6Prefix(PREFIX3));
258 insertRoutes(ipv4prefixes, ipv6prefixes);
259 checkLocRibIpv4Routes(2);
260 checkLocRibIpv6Routes(1);
262 this.peer.restartGracefully(DEFERRAL_TIMER).get();
263 this.session = createPeerSession(PEER1, this.parameters, this.listener);
264 checkUpState(this.listener);
265 checkUpState(this.peer);
266 checkLocRibIpv4Routes(2);
267 checkLocRibIpv6Routes(0);
271 * Wait with route selection until EOT is received.
273 * @throws Exception on reading Rib failure
276 public void waitForEORonLocalGracefulRestart() throws Exception {
277 performLocalGracefulRestart();
278 final List<Ipv4Prefix> ipv4prefixes = Arrays.asList(new Ipv4Prefix(PREFIX1));
279 final List<Ipv6Prefix> ipv6prefixes = Arrays.asList(new Ipv6Prefix(PREFIX3));
280 insertRoutes(ipv4prefixes, ipv6prefixes);
281 checkLocRibIpv4Routes(2);
282 checkLocRibIpv6Routes(0);
283 insertRoutes(null, null);
284 checkLocRibIpv4Routes(2);
285 checkLocRibIpv6Routes(1);
289 * Wait with route selection until deferral time is expired.
291 * @throws Exception on reading Rib failure
294 public void waitForDeferralTimerOnLocalGracefulRestart() throws Exception {
295 performLocalGracefulRestart();
296 final List<Ipv4Prefix> ipv4prefixes = Arrays.asList(new Ipv4Prefix(PREFIX1));
297 final List<Ipv6Prefix> ipv6prefixes = Arrays.asList(new Ipv6Prefix(PREFIX3));
298 insertRoutes(ipv4prefixes, ipv6prefixes);
299 checkLocRibIpv4Routes(2);
300 checkLocRibIpv6Routes(0);
301 checkStateIsNotRestarting(this.peer, DEFERRAL_TIMER);
302 checkLocRibIpv4Routes(2);
303 checkLocRibIpv6Routes(1);
307 * After graceful restart is performed from peer side we have to re-advertise routes followed by
310 * @throws Exception on reading Rib failure
313 public void verifySendEORafterRestartTest() throws Exception {
314 final SimpleSessionListener listener2 = new SimpleSessionListener();
315 configurePeer(this.tableRegistry, PEER2, this.ribImpl, this.parameters, PeerRole.Ebgp,
316 this.serverRegistry, afiSafiAdvertised, gracefulAfiSafiAdvertised);
317 final BGPSessionImpl session2 = createPeerSession(PEER2, this.parameters, listener2);
318 final List<Ipv4Prefix> ipv4Prefixes = Arrays.asList(new Ipv4Prefix(PREFIX1));
319 final List<Ipv4Prefix> ipv4Prefixes2 = Arrays.asList(new Ipv4Prefix(PREFIX2));
320 final List<Ipv6Prefix> ipv6Prefixes = Collections.singletonList(new Ipv6Prefix(PREFIX3));
321 insertRoutes(ipv4Prefixes, ipv6Prefixes);
322 insertRoutes(ipv4Prefixes2, PEER2, null, null, session2, BgpOrigin.Egp);
323 checkLocRibIpv4Routes(2);
324 checkLocRibIpv6Routes(1);
325 org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages(this.listener, 3);
326 // verify sending of Ipv4 EOT, Ipv6 EOT and Ipv4 update with route
327 checkReceivedMessages(this.listener, 3);
328 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
329 assertTrue(BgpPeerUtil.isEndOfRib((Update)this.listener.getListMsg().get(0)));
330 assertTrue(this.listener.getListMsg().get(1) instanceof Update);
331 assertTrue(BgpPeerUtil.isEndOfRib((Update)this.listener.getListMsg().get(1)));
332 assertTrue(this.listener.getListMsg().get(2) instanceof Update);
333 assertFalse(BgpPeerUtil.isEndOfRib((Update)this.listener.getListMsg().get(2)));
335 this.session.close();
336 checkIdleState(this.peer);
337 checkLocRibIpv4Routes(2);
338 checkLocRibIpv6Routes(0);
339 // verify nothing new was sent
340 checkReceivedMessages(this.listener, 3);
342 this.session = createPeerSession(PEER1, createParameter(false, true,
343 Collections.singletonMap(TABLES_KEY, true)), this.listener);
344 checkUpState(listener);
345 checkUpState(this.peer);
346 org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages(this.listener, 6);
347 // verify sending of Ipv4 update with route, Ipv4 EOT and Ipv6 EOT; order can vary based on ODTC order
348 final List<Notification> subList = this.listener.getListMsg().subList(3, 6);
350 int routeUpdateCount = 0;
351 for (Notification message : subList) {
352 if (BgpPeerUtil.isEndOfRib((Update) message)) {
358 assertEquals(2, eotCount);
359 assertEquals(1, routeUpdateCount);
362 private void checkLocRibIpv4Routes(final int expectedRoutesOnDS) throws Exception {
363 readDataOperational(getDataBroker(), IPV4_IID, table -> {
365 final Ipv4RoutesCase routesCase = (Ipv4RoutesCase) table.getRoutes();
366 if (routesCase != null && routesCase.getIpv4Routes() != null
367 && routesCase.getIpv4Routes().getIpv4Route() != null) {
368 size = routesCase.getIpv4Routes().getIpv4Route().size();
370 assertEquals(expectedRoutesOnDS, size);
375 private void checkLocRibIpv6Routes(final int expectedRoutesOnDS) throws Exception {
376 readDataOperational(getDataBroker(), IPV6_IID, table -> {
378 final Ipv6RoutesCase routesCase = (Ipv6RoutesCase) table.getRoutes();
379 if (routesCase != null && routesCase.getIpv6Routes() != null
380 && routesCase.getIpv6Routes().getIpv6Route() != null) {
381 size = routesCase.getIpv6Routes().getIpv6Route().size();
383 assertEquals(expectedRoutesOnDS, size);
388 private void insertRoutes(final List<Ipv4Prefix> ipv4prefixes, final List<Ipv6Prefix> ipv6prefixes) {
389 insertRoutes(ipv4prefixes, PEER1, ipv6prefixes, IPV6_NEXT_HOP, this.session, BgpOrigin.Igp);
392 private static void insertRoutes(final List<Ipv4Prefix> ipv4prefixes, final Ipv4AddressNoZone ipv4NeighborAddress,
393 final List<Ipv6Prefix> ipv6prefixes, final Ipv6AddressNoZone ipv6NeighborAddress,
394 final BGPSessionImpl session, final BgpOrigin peerRole) {
395 if (ipv4prefixes == null && ipv6prefixes == null) {
396 waitFutureSuccess(session.writeAndFlush(BgpPeerUtil.createEndOfRib(TABLES_KEY)));
397 waitFutureSuccess(session.writeAndFlush(BgpPeerUtil.createEndOfRib(IPV6_TABLES_KEY)));
401 if (ipv4prefixes != null && !ipv4prefixes.isEmpty()) {
402 final MpReachNlri reachIpv4 = PeerUtil.createMpReachNlri(new IpAddressNoZone(ipv4NeighborAddress),
403 ipv4prefixes.stream()
405 .collect(Collectors.toList()));
406 final Update update1 = PeerUtil.createUpdate(peerRole, Collections.emptyList(), 100, reachIpv4, null);
407 waitFutureSuccess(session.writeAndFlush(update1));
410 if (ipv6prefixes != null && !ipv4prefixes.isEmpty()) {
411 final MpReachNlri reachIpv6 = PeerUtil.createMpReachNlri(new IpAddressNoZone(ipv6NeighborAddress),
412 ipv6prefixes.stream()
414 .collect(Collectors.toList()));
415 final Update update2 = PeerUtil.createUpdate(peerRole, Collections.emptyList(), 100, reachIpv6, null);
416 waitFutureSuccess(session.writeAndFlush(update2));
420 private static Open createClassicOpen(final boolean addGraceful) {
421 final Map<TablesKey, Boolean> graceful = new HashMap<>();
423 graceful.put(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class), true);
425 return new OpenBuilder()
426 .setMyAsNumber(Uint16.valueOf(AS))
427 .setHoldTimer(Uint16.valueOf(HOLDTIMER))
428 .setVersion(new ProtocolVersion(Uint8.valueOf(4)))
429 .setBgpParameters(Collections.singletonList(createParameter(false, true, graceful)))
430 .setBgpIdentifier(PEER1)