23e2d1ce9fb50d40fa9920c238d8fe570ae8186e
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AdjacencyRIBsInTest.java
1 /*
2  * Copyright (c) 2014 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
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import com.google.common.collect.Lists;
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
23 import org.opendaylight.protocol.bgp.rib.DefaultRibReference;
24 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
25 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsTransaction;
26 import org.opendaylight.protocol.bgp.rib.spi.BGPObjectComparator;
27 import org.opendaylight.protocol.bgp.rib.spi.Peer;
28 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
29 import org.opendaylight.protocol.bgp.rib.spi.RouteEncoder;
30 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
57
58
59 public class AdjacencyRIBsInTest extends AbstractDataBrokerTest {
60
61     private WriteTransaction trans;
62
63     private final DefaultRibReference rib = new DefaultRibReference(InstanceIdentifier.create(BgpRib.class).child(Rib.class, new RibKey(new RibId("test"))));
64
65     private final RIBActivator act = new RIBActivator();
66
67     private final RIBExtensionProviderContext ctx = new SimpleRIBExtensionProviderContext();
68
69     private AdjRIBsIn<?, ?> a1;
70
71     private RIBTables tables;
72
73     private final TablesKey ipv4key = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
74
75     private final TablesKey ipv6key = new TablesKey(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class);
76
77     private final TablesKey linkstateKey = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
78
79     @Mock
80     AdjRIBsTransaction adjTrans4;
81
82     @Mock
83     AdjRIBsTransaction adjTrans6;
84
85     @Mock
86     AdjRIBsTransaction adjTransLS;
87
88     @Mock
89     Peer peer;
90
91     @Mock
92     KeyedInstanceIdentifier<Tables, TablesKey> id;
93
94     @Before
95     public void setUp() {
96         MockitoAnnotations.initMocks(this);
97         this.trans = getDataBroker().newWriteOnlyTransaction();
98         this.act.startRIBExtensionProvider(this.ctx);
99         this.tables = new RIBTables(this.ctx);
100         Mockito.doReturn(new BGPObjectComparator(new AsNumber(72L))).when(this.adjTrans4).comparator();
101         Mockito.doReturn(new BGPObjectComparator(new AsNumber(72L))).when(this.adjTrans6).comparator();
102         Mockito.doReturn(new BGPObjectComparator(new AsNumber(72L))).when(this.adjTransLS).comparator();
103         Mockito.doReturn("test").when(this.peer).toString();
104         Mockito.doNothing().when(this.adjTrans4).advertise(Mockito.any(RouteEncoder.class), Mockito.anyObject(), Mockito.any(InstanceIdentifier.class), Mockito.eq(this.peer), Mockito.any(Route.class));
105         Mockito.doNothing().when(this.adjTrans4).setUptodate(Mockito.any(InstanceIdentifier.class), Mockito.anyBoolean());
106         Mockito.doNothing().when(this.adjTrans6).advertise(Mockito.any(RouteEncoder.class), Mockito.anyObject(), Mockito.any(InstanceIdentifier.class), Mockito.eq(this.peer), Mockito.any(Route.class));
107         Mockito.doNothing().when(this.adjTrans6).setUptodate(Mockito.any(InstanceIdentifier.class), Mockito.anyBoolean());
108         Mockito.doNothing().when(this.adjTransLS).advertise(Mockito.any(RouteEncoder.class), Mockito.anyObject(), Mockito.any(InstanceIdentifier.class), Mockito.eq(this.peer), Mockito.any(Route.class));
109         Mockito.doNothing().when(this.adjTransLS).setUptodate(Mockito.any(InstanceIdentifier.class), Mockito.anyBoolean());
110     }
111
112     @Test
113     public void testRIBTables() {
114         this.a1 = this.tables.create(this.trans, this.rib, this.ipv4key);
115         assertNotNull(this.a1);
116         assertEquals(this.a1, this.tables.get(this.ipv4key));
117     }
118
119     @Test
120     public void testAddRoutes() {
121         this.a1 = this.tables.create(this.trans, this.rib, this.ipv4key);
122         assertNotNull(this.a1);
123         MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
124         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
125             new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(
126                 Lists.newArrayList(new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix("127.0.0.1/32")).build())).build()).build()).build());
127         final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
128         this.a1.addRoutes(this.adjTrans4, this.peer, mpBuilder.build(), paBuilder.build());
129         Mockito.verify(this.adjTrans4).advertise(Mockito.any(RouteEncoder.class), Mockito.anyObject(), Mockito.any(InstanceIdentifier.class), Mockito.eq(this.peer), Mockito.any(Route.class));
130
131         final AdjRIBsIn<?, ?> a2 = this.tables.create(this.trans, this.rib, this.ipv6key);
132         assertNotNull(a2);
133         mpBuilder = new MpReachNlriBuilder();
134         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
135             new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(
136                 Lists.newArrayList(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/128")).build())).build()).build()).build());
137         a2.addRoutes(this.adjTrans6, this.peer, mpBuilder.build(), paBuilder.build());
138         Mockito.verify(this.adjTrans6).advertise(Mockito.any(RouteEncoder.class), Mockito.anyObject(), Mockito.any(InstanceIdentifier.class), Mockito.eq(this.peer), Mockito.any(Route.class));
139
140         final AdjRIBsIn<?, ?> a3 = this.tables.create(this.trans, this.rib, this.linkstateKey);
141         assertNull(a3);
142     }
143
144     @After
145     public void tearDown() {
146         this.act.close();
147     }
148
149 }