Code clean up
[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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.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;
18
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 org.junit.After;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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.BGPPeerTracker;
44 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
45 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
46 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
47 import org.opendaylight.protocol.concepts.KeyMapping;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
60 import org.opendaylight.yangtools.concepts.ListenerRegistration;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62
63 @Ignore
64 public class ParserToSalTest extends DefaultRibPoliciesMockTest {
65
66     private static final String TEST_RIB_ID = "testRib";
67     private static final TablesKey TABLE_KEY
68             = new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
69     private static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
70     private BGPMock mock;
71     private AbstractRIBExtensionProviderActivator baseact, lsact;
72     private RIBExtensionProviderContext ext1, ext2;
73     private final IpAddress localAddress = new IpAddress(new Ipv4Address("127.0.0.1"));
74
75     @Mock
76     private BGPDispatcher dispatcher;
77     private final BGPPeerTracker peerTracker = new BGPPeerTrackerImpl();
78     private CodecsRegistryImpl codecsRegistry;
79
80     @Before
81     public void setUp() throws Exception {
82         super.setUp();
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)));
88
89         Mockito.doReturn(GlobalEventExecutor.INSTANCE.newSucceededFuture(null)).when(this.dispatcher)
90                 .createReconnectingClient(any(InetSocketAddress.class), any(InetSocketAddress.class),
91                         anyInt(), any(KeyMapping.class));
92
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();
97
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());
102     }
103
104     @After
105     public void tearDown() {
106         this.lsact.close();
107         this.baseact.close();
108     }
109
110     @Test
111     public void testWithLinkstate() throws ReadFailedException {
112         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(LinkstateAddressFamily.class,
113                 LinkstateSubsequentAddressFamily.class));
114
115         final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext2, this.dispatcher,
116                 this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies, this.peerTracker, tables,
117                 Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
118                         .createBestPathSelectionStrategy(this.peerTracker)));
119         rib.instantiateServiceInstance();
120         assertTablesExists(tables);
121         rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
122         final BGPPeer peer = new BGPPeer(this.localAddress, rib, PeerRole.Ibgp, null, Collections.emptySet(),
123                 Collections.emptySet());
124         peer.instantiateServiceInstance();
125         final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
126         reg.close();
127     }
128
129     @Test
130     public void testWithoutLinkstate() throws ReadFailedException {
131         final List<BgpTableType> tables = ImmutableList.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
132                 UnicastSubsequentAddressFamily.class));
133         final RIBImpl rib = new RIBImpl(new RibId(TEST_RIB_ID), AS_NUMBER, BGP_ID, this.ext1, this.dispatcher,
134                 this.codecsRegistry, getDomBroker(), getDataBroker(), this.policies, this.peerTracker, tables,
135                 Collections.singletonMap(TABLE_KEY, BasePathSelectionModeFactory
136                         .createBestPathSelectionStrategy(this.peerTracker)));
137         rib.instantiateServiceInstance();
138         rib.onGlobalContextUpdated(this.schemaService.getGlobalContext());
139         assertTablesExists(tables);
140         final BGPPeer peer = new BGPPeer(this.localAddress, rib, PeerRole.Ibgp, null, Collections.emptySet(),
141                 Collections.emptySet());
142         peer.instantiateServiceInstance();
143         final ListenerRegistration<?> reg = this.mock.registerUpdateListener(peer);
144         reg.close();
145     }
146
147     private static Collection<byte[]> fixMessages(final Collection<byte[]> bgpMessages) {
148         return Collections2.transform(bgpMessages, input -> {
149             final byte[] ret = new byte[input.length + 1];
150             // ff
151             ret[0] = -1;
152             System.arraycopy(input, 0, ret, 1, input.length);
153             return ret;
154         });
155     }
156
157     private void assertTablesExists(final List<BgpTableType> expectedTables) throws ReadFailedException {
158         readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
159             final List<Tables> tables = bgpRib.getRib().get(0).getLocRib().getTables();
160             assertFalse(tables.isEmpty());
161
162             for (final BgpTableType tableType : expectedTables) {
163                 boolean found = false;
164                 for (final Tables table : tables) {
165                     if (table.getAfi().equals(tableType.getAfi()) && table.getSafi().equals(tableType.getSafi())) {
166                         found = true;
167                         assertEquals(Boolean.valueOf(true), table.getAttributes().isUptodate());
168                     }
169                 }
170                 assertTrue(found);
171             }
172             return bgpRib;
173         });
174     }
175 }