Upgrade ietf-{inet,yang}-types to 2013-07-15
[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.Lists;
15 import com.google.common.collect.Sets;
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.rev150210.LinkstateAddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.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().setNlri(new NlriBuilder().setNlri(Lists.newArrayList(new Ipv4Prefix("1.1.1.1/32"))).build()).build();
54
55         MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
56         mpBuilder.setAfi(Ipv6AddressFamily.class);
57         mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
58
59         AttributesBuilder paBuilder = new AttributesBuilder().addAugmentation(Attributes1.class,
60                 new Attributes1Builder().setMpReachNlri(mpBuilder.build()).build());
61
62         this.ipv6m = new UpdateBuilder().setAttributes(paBuilder.build()).build();
63
64         mpBuilder = new MpReachNlriBuilder();
65         mpBuilder.setAfi(LinkstateAddressFamily.class);
66         mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
67
68         paBuilder = new AttributesBuilder().addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(
69                 mpBuilder.build()).build());
70
71         this.lsm = new UpdateBuilder().setAttributes(paBuilder.build()).build();
72
73         this.eorm = new UpdateBuilder().build();
74
75         this.bs = new BGPSynchronization(this.listener, Sets.newHashSet(this.ipv4, this.linkstate));
76     }
77
78     @Test
79     public void testSynchronize() {
80         // simulate sync
81         this.bs.updReceived(this.ipv6m);
82         this.bs.updReceived(this.ipv4m);
83         this.bs.updReceived(this.lsm);
84         this.bs.kaReceived(); // nothing yet
85         assertFalse(this.bs.syncStorage.get(this.linkstate).getEor());
86         assertFalse(this.bs.syncStorage.get(this.ipv4).getEor());
87         this.bs.updReceived(this.ipv4m);
88         this.bs.kaReceived(); // linkstate
89         assertTrue(this.bs.syncStorage.get(this.linkstate).getEor());
90         this.bs.kaReceived(); // ipv4 sync
91         assertTrue(this.bs.syncStorage.get(this.ipv4).getEor());
92     }
93
94     @Test
95     public void testSynchronizeWithEOR() {
96         this.bs.updReceived(this.ipv4m);
97         this.bs.updReceived(this.lsm);
98         // Ipv4 Unicast synchronized by EOR message
99         assertFalse(this.bs.syncStorage.get(this.ipv4).getEor());
100         this.bs.updReceived(this.eorm);
101         assertTrue(this.bs.syncStorage.get(this.ipv4).getEor());
102         // Linkstate not synchronized yet
103         assertFalse(this.bs.syncStorage.get(this.linkstate).getEor());
104         this.bs.kaReceived();
105         // no message sent by BGPSychchronization
106         assertEquals(0, this.listener.getListMsg().size());
107         this.bs.kaReceived();
108         assertTrue(this.bs.syncStorage.get(this.linkstate).getEor());
109     }
110 }