2 * Copyright (c) 2013 Cisco Systems, Inc. and others. 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;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Sets;
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;
38 public class SynchronizationTest {
40 private BGPSynchronization bs;
42 private SimpleSessionListener listener;
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();
57 MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
58 mpBuilder.setAfi(Ipv6AddressFamily.class);
59 mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
61 PathAttributesBuilder paBuilder = new PathAttributesBuilder().addAugmentation(PathAttributes1.class,
62 new PathAttributes1Builder().setMpReachNlri(mpBuilder.build()).build());
64 this.ipv6m = new UpdateBuilder().setPathAttributes(paBuilder.build()).build();
66 mpBuilder = new MpReachNlriBuilder();
67 mpBuilder.setAfi(LinkstateAddressFamily.class);
68 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
70 paBuilder = new PathAttributesBuilder().addAugmentation(PathAttributes1.class, new PathAttributes1Builder().setMpReachNlri(
71 mpBuilder.build()).build());
73 this.lsm = new UpdateBuilder().setPathAttributes(paBuilder.build()).build();
75 this.eorm = new UpdateBuilder().build();
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));
81 this.bs = new BGPSynchronization(new BGPSession() {
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));
96 public Ipv4Address getBgpId() {
97 return new Ipv4Address("127.0.0.1");
101 public AsNumber getAsNumber() {
102 return new AsNumber(30L);
104 }, this.listener, types);
108 public void testSynchronize() {
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());
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());