BGPCEP-754: Rework EffectiveRibInWriter
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / ParserToSalTest.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.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.AS_NUMBER;
13 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.BGP_ID;
14 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
15
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Lists;
19 import com.google.common.eventbus.EventBus;
20 import io.netty.util.concurrent.GlobalEventExecutor;
21 import java.net.InetSocketAddress;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
32 import org.opendaylight.protocol.bgp.inet.RIBActivator;
33 import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
34 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
35 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
37 import org.opendaylight.protocol.bgp.rib.mock.BGPMock;
38 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBExtensionProviderActivator;
39 import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
40 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
41 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
42 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
43 import org.opendaylight.protocol.concepts.KeyMapping;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateAddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateSubsequentAddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.BgpRib;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.RibId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.Tables;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
57 import org.opendaylight.yangtools.concepts.ListenerRegistration;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59
60 public class ParserToSalTest extends DefaultRibPoliciesMockTest {
61
62     private static final String TEST_RIB_ID = "testRib";
63     private static final TablesKey TABLE_KEY = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
64     private static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
65     private BGPMock mock;
66     private AbstractRIBExtensionProviderActivator baseact, lsact;
67     private RIBExtensionProviderContext ext1, ext2;
68     private final IpAddress localAddress = new IpAddress(new Ipv4Address("127.0.0.1"));
69
70     @Mock
71     private BGPDispatcher dispatcher;
72     private BGPPeerTracker peerTracker = new BGPPeerTrackerImpl();
73     private CodecsRegistryImpl codecsRegistry;
74
75     @Before
76     public void setUp() throws Exception {
77         super.setUp();
78         final String hexMessages = "/bgp_hex.txt";
79         final List<byte[]> bgpMessages = HexDumpBGPFileParser
80                 .parseMessages(ParserToSalTest.class.getResourceAsStream(hexMessages));
81         this.mock = new BGPMock(new EventBus("test"), ServiceLoaderBGPExtensionProviderContext
82                 .getSingletonInstance().getMessageRegistry(), Lists.newArrayList(fixMessages(bgpMessages)));
83
84         Mockito.doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(this.dispatcher)
85                 .createReconnectingClient(Mockito.any(InetSocketAddress.class), Mockito.anyInt(),
86                         Mockito.any(KeyMapping.class));
87
88         this.ext1 = new SimpleRIBExtensionProviderContext();
89         this.ext2 = new SimpleRIBExtensionProviderContext();
90         this.baseact = new RIBActivator();
91         this.lsact = new org.opendaylight.protocol.bgp.linkstate.impl.RIBActivator();
92
93         this.baseact.startRIBExtensionProvider(this.ext1);
94         this.lsact.startRIBExtensionProvider(this.ext2);
95         this.codecsRegistry = CodecsRegistryImpl.create(this.bindingCodecTreeFactory,
96                 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
97     }
98
99     @After
100     public void tearDown() {
101         this.lsact.close();
102         this.baseact.close();
103     }
104
105     @Test
106     public void testWithLinkstate() throws ReadFailedException {
107         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(LinkstateAddressFamily.class,
108                 LinkstateSubsequentAddressFamily.class));
109
110         final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID),
111                 AS_NUMBER, new BgpId("127.0.0.1"), null, this.ext2, this.dispatcher,
112                 this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies, this.peerTracker, tables,
113                 Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
114                         .createBestPathSelectionStrategy(this.peerTracker)));
115         rib.instantiateServiceInstance();
116         assertTablesExists(tables);
117         rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
118         final BGPPeer peer = new BGPPeer(this.localAddress, rib, PeerRole.Ibgp, null, Collections.emptySet(),
119                 Collections.emptySet());
120         peer.instantiateServiceInstance();
121         final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
122         reg.close();
123     }
124
125     @Test
126     public void testWithoutLinkstate() throws ReadFailedException {
127         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
128                 UnicastSubsequentAddressFamily.class));
129         final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID,
130                 null, this.ext1, this.dispatcher, this.codecsRegistry, getDomBroker(), getDataBroker(),
131                 this.policies, this.peerTracker, tables,
132                 Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
133                         .createBestPathSelectionStrategy(this.peerTracker)));
134         rib.instantiateServiceInstance();
135         rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
136         assertTablesExists(tables);
137         final BGPPeer peer = new BGPPeer(this.localAddress, rib, PeerRole.Ibgp, null, Collections.emptySet(),
138                 Collections.emptySet());
139         peer.instantiateServiceInstance();
140         final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
141         reg.close();
142     }
143
144     private static Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
145         return Collections2.transform(bgpMessages, input -> {
146             final byte[] ret = new byte[input.length + 1];
147             // ff
148             ret[0] = -1;
149             System.arraycopy(input, 0, ret, 1, input.length);
150             return ret;
151         });
152     }
153
154     private void assertTablesExists(final List<BgpTableType> expectedTables) throws ReadFailedException {
155         readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
156             final List<Tables> tables = bgpRib.getRib().get(0).getLocRib().getTables();
157             assertFalse(tables.isEmpty());
158
159             for (final BgpTableType tableType : expectedTables) {
160                 boolean found = false;
161                 for (final Tables table : tables) {
162                     if (table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
163                         found = true;
164                         assertTrue(Boolean.valueOf(true).equals(table.getAttributes().isUptodate()));
165                     }
166                 }
167                 assertTrue(found);
168             }
169             return bgpRib;
170         });
171     }
172 }