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;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyInt;
15 import static org.mockito.Mockito.doReturn;
16 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.AS_NUMBER;
17 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.BGP_ID;
18 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
20 import com.google.common.collect.Collections2;
21 import com.google.common.collect.Lists;
22 import com.google.common.eventbus.EventBus;
23 import io.netty.util.concurrent.GlobalEventExecutor;
24 import java.net.InetSocketAddress;
25 import java.util.Collection;
26 import java.util.List;
28 import java.util.ServiceLoader;
29 import java.util.concurrent.ExecutionException;
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
35 import org.opendaylight.protocol.bgp.inet.RIBActivator;
36 import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
37 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
38 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
40 import org.opendaylight.protocol.bgp.rib.mock.BGPMock;
41 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderActivator;
42 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
43 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
44 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
45 import org.opendaylight.protocol.concepts.KeyMapping;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateAddressFamily;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateSubsequentAddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
58 import org.opendaylight.yangtools.concepts.Registration;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 public class ParserToSalTest extends DefaultRibPoliciesMockTest {
64 private static final String TEST_RIB_ID = "testRib";
65 private static final TablesKey TABLE_KEY
66 = new TablesKey(LinkstateAddressFamily.VALUE, LinkstateSubsequentAddressFamily.VALUE);
67 private static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
68 private final IpAddressNoZone localAddress = new IpAddressNoZone(new Ipv4AddressNoZone("127.0.0.1"));
70 private final RIBExtensionProviderActivator baseact = new RIBActivator();
71 private final RIBExtensionProviderActivator lsact = new org.opendaylight.protocol.bgp.linkstate.impl.RIBActivator();
72 private final RIBExtensionProviderContext ext1 = new SimpleRIBExtensionProviderContext();
73 private final RIBExtensionProviderContext ext2 = new SimpleRIBExtensionProviderContext();
75 private BGPDispatcher dispatcher;
76 private ConstantCodecsRegistry codecsRegistry;
80 public void setUp() throws Exception {
82 final String hexMessages = "/bgp_hex.txt";
83 final List<byte[]> bgpMessages = HexDumpBGPFileParser
84 .parseMessages(ParserToSalTest.class.getResourceAsStream(hexMessages));
85 mock = new BGPMock(new EventBus("test"),
86 ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst().orElseThrow().getMessageRegistry(),
87 Lists.newArrayList(fixMessages(bgpMessages)));
89 doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(dispatcher)
90 .createReconnectingClient(any(InetSocketAddress.class), any(InetSocketAddress.class),
91 anyInt(), any(KeyMapping.class));
93 final CurrentAdapterSerializer serializer = mappingService.currentSerializer();
94 baseact.startRIBExtensionProvider(ext1, serializer);
95 lsact.startRIBExtensionProvider(ext2, serializer);
96 codecsRegistry = new ConstantCodecsRegistry(serializer);
100 public void testWithLinkstate() throws InterruptedException, ExecutionException {
101 final List<BgpTableType> tables = List.of(new BgpTableTypeImpl(LinkstateAddressFamily.VALUE,
102 LinkstateSubsequentAddressFamily.VALUE));
104 final RIBImpl rib = new RIBImpl(tableRegistry, new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, ext2,
105 dispatcher, codecsRegistry, getDomBroker(), policies,
106 tables, Map.of(TABLE_KEY, BasePathSelectionModeFactory.createBestPathSelectionStrategy()));
107 rib.instantiateServiceInstance();
108 assertTablesExists(tables);
109 final BGPPeer peer = AbstractAddPathTest.configurePeer(tableRegistry,
110 localAddress.getIpv4AddressNoZone(), rib, null, PeerRole.Ibgp, new StrictBGPPeerRegistry());
111 peer.instantiateServiceInstance();
112 final Registration reg = mock.registerUpdateListener(peer);
117 public void testWithoutLinkstate() throws InterruptedException, ExecutionException {
118 final List<BgpTableType> tables = List.of(new BgpTableTypeImpl(Ipv4AddressFamily.VALUE,
119 UnicastSubsequentAddressFamily.VALUE));
120 final RIBImpl rib = new RIBImpl(tableRegistry, new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, ext1,
121 dispatcher, codecsRegistry, getDomBroker(), policies,
122 tables, Map.of(TABLE_KEY, BasePathSelectionModeFactory.createBestPathSelectionStrategy()));
123 rib.instantiateServiceInstance();
124 assertTablesExists(tables);
125 final BGPPeer peer = AbstractAddPathTest.configurePeer(tableRegistry,
126 localAddress.getIpv4AddressNoZone(), rib, null, PeerRole.Ibgp, new StrictBGPPeerRegistry());
127 peer.instantiateServiceInstance();
128 final Registration reg = mock.registerUpdateListener(peer);
132 private static Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
133 return Collections2.transform(bgpMessages, input -> {
134 final byte[] ret = new byte[input.length + 1];
137 System.arraycopy(input, 0, ret, 1, input.length);
142 private void assertTablesExists(final List<BgpTableType> expectedTables) throws InterruptedException,
144 readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
145 final var tables = bgpRib.nonnullRib().values().iterator().next().getLocRib().getTables();
146 assertNotNull(tables);
148 for (final BgpTableType tableType : expectedTables) {
149 boolean found = false;
150 for (final Tables table : tables.values()) {
151 if (table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
153 assertEquals(Boolean.TRUE, table.getAttributes().getUptodate());