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