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