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.assertFalse;
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.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.AS_NUMBER;
16 import static org.opendaylight.protocol.bgp.rib.impl.AbstractAddPathTest.BGP_ID;
17 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
19 import com.google.common.collect.Collections2;
20 import com.google.common.collect.ImmutableList;
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.Collections;
27 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
36 import org.opendaylight.protocol.bgp.inet.RIBActivator;
37 import org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectionModeFactory;
38 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
39 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
41 import org.opendaylight.protocol.bgp.rib.mock.BGPMock;
42 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBExtensionProviderActivator;
43 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
44 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
45 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
46 import org.opendaylight.protocol.concepts.KeyMapping;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 public class ParserToSalTest extends DefaultRibPoliciesMockTest {
65 private static final String TEST_RIB_ID = "testRib";
66 private static final TablesKey TABLE_KEY
67 = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
68 private static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
69 private final IpAddressNoZone localAddress = new IpAddressNoZone(new Ipv4AddressNoZone("127.0.0.1"));
71 private AbstractRIBExtensionProviderActivator baseact;
72 private AbstractRIBExtensionProviderActivator lsact;
73 private RIBExtensionProviderContext ext1;
74 private RIBExtensionProviderContext ext2;
76 private BGPDispatcher dispatcher;
77 private CodecsRegistryImpl codecsRegistry;
81 public void setUp() throws Exception {
83 final String hexMessages = "/bgp_hex.txt";
84 final List<byte[]> bgpMessages = HexDumpBGPFileParser
85 .parseMessages(ParserToSalTest.class.getResourceAsStream(hexMessages));
86 this.mock = new BGPMock(new EventBus("test"), ServiceLoaderBGPExtensionProviderContext
87 .getSingletonInstance().getMessageRegistry(), Lists.newArrayList(fixMessages(bgpMessages)));
89 Mockito.doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(this.dispatcher)
90 .createReconnectingClient(any(InetSocketAddress.class), any(InetSocketAddress.class),
91 anyInt(), any(KeyMapping.class));
93 this.ext1 = new SimpleRIBExtensionProviderContext();
94 this.ext2 = new SimpleRIBExtensionProviderContext();
95 this.baseact = new RIBActivator();
96 this.lsact = new org.opendaylight.protocol.bgp.linkstate.impl.RIBActivator();
98 this.baseact.startRIBExtensionProvider(this.ext1, this.mappingService);
99 this.lsact.startRIBExtensionProvider(this.ext2, this.mappingService);
100 this.codecsRegistry = CodecsRegistryImpl.create(this.bindingCodecTreeFactory,
101 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
106 public void tearDown() {
108 this.baseact.close();
112 public void testWithLinkstate() throws InterruptedException, ExecutionException {
113 final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(LinkstateAddressFamily.class,
114 LinkstateSubsequentAddressFamily.class));
116 final RIBImpl rib = new RIBImpl(this.tableRegistry, new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext2,
117 this.dispatcher, this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies,
118 tables, Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
119 .createBestPathSelectionStrategy()));
120 rib.instantiateServiceInstance();
121 assertTablesExists(tables);
122 rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
123 final BGPPeer peer = AbstractAddPathTest.configurePeer(this.tableRegistry,
124 this.localAddress.getIpv4AddressNoZone(), rib, null, PeerRole.Ibgp, new StrictBGPPeerRegistry());
125 peer.instantiateServiceInstance();
126 final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
131 public void testWithoutLinkstate() throws InterruptedException, ExecutionException {
132 final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
133 UnicastSubsequentAddressFamily.class));
134 final RIBImpl rib = new RIBImpl(this.tableRegistry, new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext1,
135 this.dispatcher, this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies,
136 tables, Collections.singletonMap(TABLE_KEY,
137 BasePathSelectionModeFactory.createBestPathSelectionStrategy()));
138 rib.instantiateServiceInstance();
139 rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
140 assertTablesExists(tables);
141 final BGPPeer peer = AbstractAddPathTest.configurePeer(this.tableRegistry,
142 this.localAddress.getIpv4AddressNoZone(), rib, null, PeerRole.Ibgp, new StrictBGPPeerRegistry());
143 peer.instantiateServiceInstance();
144 final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
148 private static Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
149 return Collections2.transform(bgpMessages, input -> {
150 final byte[] ret = new byte[input.length + 1];
153 System.arraycopy(input, 0, ret, 1, input.length);
158 private void assertTablesExists(final List<BgpTableType> expectedTables) throws InterruptedException,
160 readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
161 final List<Tables> tables = bgpRib.getRib().get(0).getLocRib().getTables();
162 assertFalse(tables.isEmpty());
164 for (final BgpTableType tableType : expectedTables) {
165 boolean found = false;
166 for (final Tables table : tables) {
167 if (table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
169 assertEquals(Boolean.TRUE, table.getAttributes().isUptodate());