Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / SynchronizationTest.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.bgp.rib.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Sets;
15 import java.util.Collections;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.UpdateBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.NlriBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
32
33 public class SynchronizationTest {
34
35     private final TablesKey ipv4 = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
36     private final TablesKey linkstate = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
37
38     private BGPSynchronization bs;
39
40     private SimpleSessionListener listener;
41
42     private Update ipv4m;
43
44     private Update ipv6m;
45
46     private Update lsm;
47
48     private Update eorm;
49
50     @Before
51     public void setUp() {
52         this.listener = new SimpleSessionListener();
53         this.ipv4m = new UpdateBuilder()
54                 .setNlri(Collections.singletonList(new NlriBuilder()
55                 .setPrefix(new Ipv4Prefix("1.1.1.1/32")).build()))
56                 .build();
57
58         MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
59         mpBuilder.setAfi(Ipv6AddressFamily.class);
60         mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
61
62         AttributesBuilder paBuilder = new AttributesBuilder().addAugmentation(Attributes1.class,
63                 new Attributes1Builder().setMpReachNlri(mpBuilder.build()).build());
64
65         this.ipv6m = new UpdateBuilder().setAttributes(paBuilder.build()).build();
66
67         mpBuilder = new MpReachNlriBuilder();
68         mpBuilder.setAfi(LinkstateAddressFamily.class);
69         mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
70
71         paBuilder = new AttributesBuilder().addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(
72                 mpBuilder.build()).build());
73
74         this.lsm = new UpdateBuilder().setAttributes(paBuilder.build()).build();
75
76         this.eorm = new UpdateBuilder().build();
77
78         this.bs = new BGPSynchronization(this.listener, Sets.newHashSet(this.ipv4, this.linkstate));
79     }
80
81     @Test
82     public void testSynchronize() {
83         // simulate sync
84         this.bs.updReceived(this.ipv6m);
85         this.bs.updReceived(this.ipv4m);
86         this.bs.updReceived(this.lsm);
87         this.bs.kaReceived(); // nothing yet
88         assertFalse(this.bs.syncStorage.get(this.linkstate).getEor());
89         assertFalse(this.bs.syncStorage.get(this.ipv4).getEor());
90         this.bs.updReceived(this.ipv4m);
91         this.bs.kaReceived(); // linkstate
92         assertTrue(this.bs.syncStorage.get(this.linkstate).getEor());
93         this.bs.kaReceived(); // ipv4 sync
94         assertTrue(this.bs.syncStorage.get(this.ipv4).getEor());
95     }
96
97     @Test
98     public void testSynchronizeWithEOR() {
99         this.bs.updReceived(this.ipv4m);
100         this.bs.updReceived(this.lsm);
101         // Ipv4 Unicast synchronized by EOR message
102         assertFalse(this.bs.syncStorage.get(this.ipv4).getEor());
103         this.bs.updReceived(this.eorm);
104         assertTrue(this.bs.syncStorage.get(this.ipv4).getEor());
105         // Linkstate not synchronized yet
106         assertFalse(this.bs.syncStorage.get(this.linkstate).getEor());
107         this.bs.kaReceived();
108         // no message sent by BGPSychchronization
109         assertEquals(0, this.listener.getListMsg().size());
110         this.bs.kaReceived();
111         assertTrue(this.bs.syncStorage.get(this.linkstate).getEor());
112     }
113 }