BUG-608 : added prefix-sid tlv
[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
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Sets;
14 import java.util.Set;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
18 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
37
38 public class SynchronizationTest {
39
40     private BGPSynchronization bs;
41
42     private SimpleSessionListener listener;
43
44     private Update ipv4m;
45
46     private Update ipv6m;
47
48     private Update lsm;
49
50     private Update eorm;
51
52     @Before
53     public void setUp() {
54         this.listener = new SimpleSessionListener();
55         this.ipv4m = new UpdateBuilder().setNlri(new NlriBuilder().setNlri(Lists.newArrayList(new Ipv4Prefix("1.1.1.1/32"))).build()).build();
56
57         MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
58         mpBuilder.setAfi(Ipv6AddressFamily.class);
59         mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
60
61         PathAttributesBuilder paBuilder = new PathAttributesBuilder().addAugmentation(PathAttributes1.class,
62                 new PathAttributes1Builder().setMpReachNlri(mpBuilder.build()).build());
63
64         this.ipv6m = new UpdateBuilder().setPathAttributes(paBuilder.build()).build();
65
66         mpBuilder = new MpReachNlriBuilder();
67         mpBuilder.setAfi(LinkstateAddressFamily.class);
68         mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
69
70         paBuilder = new PathAttributesBuilder().addAugmentation(PathAttributes1.class, new PathAttributes1Builder().setMpReachNlri(
71                 mpBuilder.build()).build());
72
73         this.lsm = new UpdateBuilder().setPathAttributes(paBuilder.build()).build();
74
75         this.eorm = new UpdateBuilder().build();
76
77         final Set<TablesKey> types = Sets.newHashSet();
78         types.add(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
79         types.add(new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
80
81         this.bs = new BGPSynchronization(new BGPSession() {
82
83             @Override
84             public void close() {
85             }
86
87             @Override
88             public Set<BgpTableType> getAdvertisedTableTypes() {
89                 final Set<BgpTableType> types = Sets.newHashSet();
90                 types.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
91                 types.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
92                 return types;
93             }
94
95             @Override
96             public Ipv4Address getBgpId() {
97                 return new Ipv4Address("127.0.0.1");
98             }
99
100             @Override
101             public AsNumber getAsNumber() {
102                 return new AsNumber(30L);
103             }
104         }, this.listener, types);
105     }
106
107     @Test
108     public void testSynchronize() {
109         // simulate sync
110         this.bs.updReceived(this.ipv6m);
111         this.bs.updReceived(this.ipv4m);
112         this.bs.updReceived(this.lsm);
113         this.bs.kaReceived(); // nothing yet
114         this.bs.updReceived(this.ipv4m);
115         this.bs.kaReceived(); // linkstate
116         assertEquals(1, this.listener.getListMsg().size());
117         assertEquals(LinkstateAddressFamily.class, ((Update) this.listener.getListMsg().get(0)).getPathAttributes().getAugmentation(
118                 PathAttributes2.class).getMpUnreachNlri().getAfi());
119         this.bs.kaReceived(); // ipv4 sync
120         assertEquals(2, this.listener.getListMsg().size());
121     }
122
123     @Test
124     public void testSynchronizeWithEOR() {
125         this.bs.updReceived(this.ipv4m);
126         this.bs.updReceived(this.lsm);
127         // Ipv4 Unicast synchronized by EOR message
128         this.bs.updReceived(this.eorm);
129         // Linkstate not synchronized yet
130         this.bs.kaReceived();
131         // no message sent by BGPSychchronization
132         assertEquals(0, this.listener.getListMsg().size());
133         this.bs.kaReceived();
134         assertEquals(1, this.listener.getListMsg().size());
135         assertEquals(LinkstateAddressFamily.class, ((Update) this.listener.getListMsg().get(0)).getPathAttributes().getAugmentation(
136                 PathAttributes2.class).getMpUnreachNlri().getAfi());
137     }
138 }