BUG-6701 Change name of link-state to linkstate-attribute
[bgpcep.git] / bgp / topology-provider / src / test / java / org / opendaylight / bgpcep / bgp / topology / provider / LinkstateTopologyBuilderTest.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
9 package org.opendaylight.bgpcep.bgp.topology.provider;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.mockito.Matchers.any;
19 import static org.mockito.Mockito.RETURNS_SMART_NULLS;
20 import static org.mockito.Mockito.doThrow;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.timeout;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27
28 import com.google.common.base.Optional;
29 import com.google.common.collect.Lists;
30 import io.netty.buffer.Unpooled;
31 import java.math.BigInteger;
32 import java.nio.charset.StandardCharsets;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import org.junit.After;
37 import org.junit.Test;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
40 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
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.IpPrefix;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AdministrativeGroup;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Identifier;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.IsisAreaIdentifier;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.attribute.UnreservedBandwidthBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.attribute.UnreservedBandwidthKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptorsBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptorsBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.RemoteNodeDescriptorsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.node._case.NodeDescriptorsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.AdvertisingNodeDescriptorsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptorsBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCaseBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributesBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutes;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRouteKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1Builder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCaseBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNodeBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IgpMetric;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributes;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix;
95 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
96
97 public class LinkstateTopologyBuilderTest extends AbstractTopologyBuilderTest {
98
99     private static final byte[] LINKSTATE_ROUTE_KEY = Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode("linkstate-route")).array();
100     private static final String ROUTER_1_ID = "127.0.0.1";
101     private static final String ROUTER_2_ID = "127.0.0.2";
102     private static final String NODE_1_PREFIX = "127.0.1.1/32";
103     private static final AsNumber NODE_1_AS = new AsNumber(1L);
104     private static final AsNumber NODE_2_AS = new AsNumber(2L);
105     private static final String NODE_1_ISIS_ID = "bgpls://IsisLevel2:1/type=node&as=1&router=0000.0102.0304";
106     private static final String NODE_2_ISIS_ID = "bgpls://IsisLevel2:1/type=node&as=2";
107     private static final String NODE_1_OSPF_ID = "bgpls://Ospf:1/type=node&as=1&router=0000.0102.0304";
108     private static final String NODE_2_OSPF_ID = "bgpls://Ospf:1/type=node&as=2";
109     private static final Identifier IDENTIFIER = new Identifier(new BigInteger("1"));
110     private static final long LISTENER_RESTART_TIME = 20000;
111     private static final int LISTENER_ENFORCE_COUNTER = 2;
112
113     private LinkstateTopologyBuilder linkstateTopoBuilder;
114     private InstanceIdentifier<LinkstateRoute> linkstateRouteIID;
115
116     @Override
117     protected void setupWithDataBroker(final DataBroker dataBroker) {
118         super.setupWithDataBroker(dataBroker);
119         this.linkstateTopoBuilder = new LinkstateTopologyBuilder(dataBroker, LOC_RIB_REF, TEST_TOPOLOGY_ID, LISTENER_RESTART_TIME, LISTENER_ENFORCE_COUNTER);
120         this.linkstateTopoBuilder.start();
121         final InstanceIdentifier<Tables> path = this.linkstateTopoBuilder.tableInstanceIdentifier(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
122         this.linkstateRouteIID = path.builder().child((Class)LinkstateRoutes.class).child(LinkstateRoute.class, new LinkstateRouteKey(LINKSTATE_ROUTE_KEY)).build();
123     }
124
125     @After
126     public void tearDown() throws Exception {
127         this.linkstateTopoBuilder.close();
128         assertFalse(getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).isPresent());
129     }
130
131     @Test
132     public void testLinkstateTopologyBuilderTopologyTypes() {
133         final Optional<Topology> topologyMaybe = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier());
134         assertTrue(topologyMaybe.isPresent());
135         final Topology topology = topologyMaybe.get();
136         assertNotNull(topology.getTopologyTypes().getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1.class));
137         assertNotNull(topology.getTopologyTypes().getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1.class).getBgpLinkstateTopology());
138     }
139
140     @Test
141     public void testIsisLinkstateTopologyBuilder() throws TransactionCommitFailedException {
142         // create node
143         updateLinkstateRoute(createLinkstateNodeRoute(ProtocolId.IsisLevel2, "node1", NODE_1_AS, ROUTER_1_ID));
144         final Optional<Topology> topologyMaybe = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier());
145         assertTrue(topologyMaybe.isPresent());
146         final Topology topology1 = topologyMaybe.get();
147         assertEquals(1, topology1.getNode().size());
148         final Node node1 = topology1.getNode().get(0);
149         assertEquals(NODE_1_ISIS_ID, node1.getNodeId().getValue());
150         final IgpNodeAttributes igpNode1 = node1.getAugmentation(Node1.class).getIgpNodeAttributes();
151         assertEquals(ROUTER_1_ID, igpNode1.getRouterId().get(0).getIpv4Address().getValue());
152         assertEquals("node1", igpNode1.getName().getValue());
153         final IgpNodeAttributes1 igpNodeAttributes1 = igpNode1.getAugmentation(IgpNodeAttributes1.class);
154         assertEquals("0000.0102.0304", igpNodeAttributes1.getIsisNodeAttributes().getIso().getIsoSystemId().getValue());
155         assertEquals(ROUTER_1_ID, igpNodeAttributes1.getIsisNodeAttributes().getTed().getTeRouterIdIpv4().getValue());
156         assertEquals("47.0000.0000.0000.0000.0102.0304", igpNodeAttributes1.getIsisNodeAttributes().getNet().get(0).getValue());
157         assertNull(igpNode1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class));
158
159         // create link
160         updateLinkstateRoute(createLinkstateLinkRoute(ProtocolId.IsisLevel2, NODE_1_AS, NODE_2_AS, "link1"));
161         final Topology topology2 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
162         assertEquals(1, topology2.getLink().size());
163         final Link link1 = topology2.getLink().get(0);
164         assertEquals(2, topology2.getNode().size());
165         assertEquals(1, topology2.getNode().get(0).getTerminationPoint().size());
166         assertEquals(1, topology2.getNode().get(1).getTerminationPoint().size());
167         assertEquals("bgpls://IsisLevel2:1/type=link&local-as=1&local-router=0000.0102.0304&remote-as=2&mt=1", link1.getLinkId().getValue());
168         assertEquals(NODE_1_ISIS_ID, link1.getSource().getSourceNode().getValue());
169         assertEquals(NODE_2_ISIS_ID, link1.getDestination().getDestNode().getValue());
170         final IgpLinkAttributes igpLink1 = link1.getAugmentation(Link1.class).getIgpLinkAttributes();
171         assertEquals("link1", igpLink1.getName());
172         assertEquals((short) 1, igpLink1.getAugmentation(IgpLinkAttributes1.class).getIsisLinkAttributes().getMultiTopologyId().shortValue());
173         assertNull(igpLink1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class));
174
175         // update node
176         updateLinkstateRoute(createLinkstateNodeRoute(ProtocolId.IsisLevel2, "updated-node", NODE_1_AS, ROUTER_2_ID));
177         final Topology topology3 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
178         final IgpNodeAttributes igpNode2 = topology3.getNode().get(0).getAugmentation(Node1.class).getIgpNodeAttributes();
179         assertEquals(ROUTER_2_ID, igpNode2.getRouterId().get(0).getIpv4Address().getValue());
180         assertEquals("updated-node", igpNode2.getName().getValue());
181
182         // remove
183         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
184         wTx.delete(LogicalDatastoreType.OPERATIONAL, this.linkstateRouteIID);
185         wTx.submit();
186         final Topology topology4 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
187         assertEquals(0, topology4.getNode().size());
188         assertEquals(0, topology4.getLink().size());
189     }
190
191     @Test
192     public void testOspfLinkstateTopologyBuilder() throws TransactionCommitFailedException {
193         // create node
194         updateLinkstateRoute(createLinkstateNodeRoute(ProtocolId.Ospf, "node1", NODE_1_AS, ROUTER_1_ID));
195         final Optional<Topology> topologyMaybe = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier());
196         assertTrue(topologyMaybe.isPresent());
197         final Topology topology1 = topologyMaybe.get();
198         assertEquals(1, topology1.getNode().size());
199         final Node node1 = topology1.getNode().get(0);
200         assertEquals(NODE_1_OSPF_ID, node1.getNodeId().getValue());
201         final IgpNodeAttributes igpNode1 = node1.getAugmentation(Node1.class).getIgpNodeAttributes();
202         assertEquals(ROUTER_1_ID, igpNode1.getRouterId().get(0).getIpv4Address().getValue());
203         assertEquals("node1", igpNode1.getName().getValue());
204         assertNull(igpNode1.getAugmentation(IgpNodeAttributes1.class));
205         assertEquals(ROUTER_1_ID, igpNode1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class).getOspfNodeAttributes().getTed().getTeRouterIdIpv4().getValue());
206
207         // update node with prefix
208         updateLinkstateRoute(createLinkstatePrefixRoute(ProtocolId.Ospf, NODE_1_AS, NODE_1_PREFIX, 500L, ROUTER_1_ID));
209         final Topology topology3 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
210         final IgpNodeAttributes igpNode2 = topology3.getNode().get(0).getAugmentation(Node1.class).getIgpNodeAttributes();
211         assertEquals(1, igpNode2.getPrefix().size());
212         final Prefix prefix = igpNode2.getPrefix().get(0);
213         assertEquals(NODE_1_PREFIX, prefix.getPrefix().getIpv4Prefix().getValue());
214         assertEquals(500L, prefix.getMetric().longValue());
215
216         // create link
217         updateLinkstateRoute(createLinkstateLinkRoute(ProtocolId.Ospf, NODE_1_AS, NODE_2_AS, "link1"));
218         final Topology topology2 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
219         assertEquals(1, topology2.getLink().size());
220         final Link link1 = topology2.getLink().get(0);
221         assertEquals(2, topology2.getNode().size());
222         assertEquals(1, topology2.getNode().get(0).getTerminationPoint().size());
223         assertEquals(1, topology2.getNode().get(1).getTerminationPoint().size());
224         assertEquals("bgpls://Ospf:1/type=link&local-as=1&local-router=0000.0102.0304&remote-as=2&mt=1", link1.getLinkId().getValue());
225         assertEquals(NODE_1_OSPF_ID, link1.getSource().getSourceNode().getValue());
226         assertEquals(NODE_2_OSPF_ID, link1.getDestination().getDestNode().getValue());
227         final IgpLinkAttributes igpLink1 = link1.getAugmentation(Link1.class).getIgpLinkAttributes();
228         assertEquals("link1", igpLink1.getName());
229         assertNull(igpLink1.getAugmentation(IgpLinkAttributes1.class));
230         assertEquals((short) 1, igpLink1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class).getOspfLinkAttributes().getMultiTopologyId().shortValue());
231         assertEquals(2, igpLink1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class).getOspfLinkAttributes().getTed().getSrlg().getSrlgValues().size());
232     }
233
234     /**
235      * This test is to verify if the AbstractTopologyBuilder/LinkstateTopologyBuilder is handling exception correctly
236      *
237      * @throws Exception
238      */
239     @Test
240     public void testRouteChangedError() throws Exception {
241         final LinkstateTopologyBuilder spiedLinkstateTopologyBuilder = spy(this.linkstateTopoBuilder);
242         doThrow(RuntimeException.class).when(spiedLinkstateTopologyBuilder).routeChanged(any(), any());
243         try {
244             spiedLinkstateTopologyBuilder.routeChanged(null, null);
245             fail("Mockito failed to spy routeChanged() method");
246         } catch (final Exception e) {
247             assertTrue(e instanceof RuntimeException);
248         }
249         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
250         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
251         // first we examine if the chain is being reset when no exception is thrown
252         spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
253         verify(spiedLinkstateTopologyBuilder, times(1)).restartTransactionChainOnDemand();
254         verify(spiedLinkstateTopologyBuilder, never()).scheduleListenerRestart();
255         verify(spiedLinkstateTopologyBuilder, never()).resetTransactionChain();
256         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
257         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
258         // now pass some invalid data to cause onDataTreeChanged fail
259         final DataTreeModification<LinkstateRoute> modification = mock(DataTreeModification.class, RETURNS_SMART_NULLS);
260         final List<DataTreeModification<LinkstateRoute>> changes = new ArrayList<>();
261         changes.add(modification);
262         spiedLinkstateTopologyBuilder.onDataTreeChanged(changes);
263         // one restart transaction chain check in onDataTreeChanged()
264         // we are introducing some timeout here as transaction may be executed in a delay manner
265         verify(spiedLinkstateTopologyBuilder, timeout(5000).times(1)).scheduleListenerRestart();
266         verify(spiedLinkstateTopologyBuilder, times(2)).restartTransactionChainOnDemand();
267         assertNotEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
268         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
269         final long listenerScheduledRestartTime = spiedLinkstateTopologyBuilder.listenerScheduledRestartTime;
270         // call again with empty change to invoke restartTransactionChainOnDemand()
271         spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
272         verify(spiedLinkstateTopologyBuilder, times(3)).restartTransactionChainOnDemand();
273         // transaction chain should be reset while listener should not
274         verify(spiedLinkstateTopologyBuilder, times(1)).resetTransactionChain();
275         verify(spiedLinkstateTopologyBuilder, never()).resetListener();
276         // now apply a change with bad modification again
277         spiedLinkstateTopologyBuilder.onDataTreeChanged(changes);
278         verify(spiedLinkstateTopologyBuilder, times(4)).restartTransactionChainOnDemand();
279         // listener scheduled again
280         verify(spiedLinkstateTopologyBuilder, timeout(5000).times(2)).scheduleListenerRestart();
281         // listener timer shouldn't have changed
282         assertEquals(listenerScheduledRestartTime, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
283         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
284         verify(spiedLinkstateTopologyBuilder, times(2)).resetTransactionChain();
285         verify(spiedLinkstateTopologyBuilder, never()).resetListener();
286         Thread.sleep(LISTENER_RESTART_TIME);
287         // manually invoke onTransactionChainFailed() to have the listener restart scheduled again
288         spiedLinkstateTopologyBuilder.onTransactionChainFailed(null, null, null);
289         assertTrue(spiedLinkstateTopologyBuilder.listenerScheduledRestartTime == listenerScheduledRestartTime + LISTENER_RESTART_TIME);
290         verify(spiedLinkstateTopologyBuilder, times(5)).restartTransactionChainOnDemand();
291         verify(spiedLinkstateTopologyBuilder, times(3)).scheduleListenerRestart();
292         // enforce counter get increased
293         assertEquals(1, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
294         verify(spiedLinkstateTopologyBuilder, times(3)).resetTransactionChain();
295         verify(spiedLinkstateTopologyBuilder, never()).resetListener();
296         // sleep to let the listener restart timer times out
297         Thread.sleep(LISTENER_RESTART_TIME);
298         // apply a good modification (empty change)
299         spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
300         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
301         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
302         verify(spiedLinkstateTopologyBuilder, times(6)).restartTransactionChainOnDemand();
303         // listener restarted didn't get rescheduled again
304         verify(spiedLinkstateTopologyBuilder, times(3)).scheduleListenerRestart();
305         verify(spiedLinkstateTopologyBuilder, times(4)).resetTransactionChain();
306         verify(spiedLinkstateTopologyBuilder, times(1)).resetListener();
307     }
308
309     private void updateLinkstateRoute(final LinkstateRoute data) {
310         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
311         wTx.put(LogicalDatastoreType.OPERATIONAL, this.linkstateRouteIID, data, true);
312         wTx.submit();
313     }
314
315     private LinkstateRoute createLinkstateNodeRoute(final ProtocolId protocolId, final String nodeName, final AsNumber asNumber, final String ipv4RouterId) {
316         return createBaseBuilder(protocolId)
317             .setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCaseBuilder().setNodeDescriptors(new NodeDescriptorsBuilder().setCRouterIdentifier(new IsisNodeCaseBuilder().setIsisNode(new IsisNodeBuilder().setIsoSystemId(new IsoSystemIdentifier(new byte[]{ 0, 0, 1, 2, 3, 4 })).build()).build()).setAsNumber(asNumber).build()).build())
318             .setAttributes(new AttributesBuilder()
319                 .addAugmentation(Attributes1.class, new Attributes1Builder().setLinkStateAttribute(new NodeAttributesCaseBuilder().setNodeAttributes(new NodeAttributesBuilder()
320                     .setDynamicHostname(nodeName)
321                     .setIpv4RouterId(new Ipv4RouterIdentifier(ipv4RouterId))
322                     .setIsisAreaId(Collections.singletonList(new IsisAreaIdentifier(new byte[]{0x47}))).build()).build()).build()).build()).build();
323     }
324
325     private LinkstateRoute createLinkstatePrefixRoute(final ProtocolId protocolId, final AsNumber asNumber, final String ipv4Prefix, final long igpMetric, final String ospfFwdAddress) {
326         return createBaseBuilder(protocolId)
327             .setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCaseBuilder()
328                 .setAdvertisingNodeDescriptors(new AdvertisingNodeDescriptorsBuilder().setAsNumber(asNumber).build())
329                 .setPrefixDescriptors(new PrefixDescriptorsBuilder().setIpReachabilityInformation(new IpPrefix(new Ipv4Prefix(ipv4Prefix))).build())
330                 .build())
331             .setAttributes(new AttributesBuilder()
332                 .addAugmentation(Attributes1.class, new Attributes1Builder().setLinkStateAttribute(new PrefixAttributesCaseBuilder().setPrefixAttributes(new PrefixAttributesBuilder().setOspfForwardingAddress(new IpAddress(new Ipv4Address(ospfFwdAddress))).setPrefixMetric(new IgpMetric(igpMetric)).build()).build()).build()).build())
333              .build();
334     }
335
336     private LinkstateRoute createLinkstateLinkRoute(final ProtocolId protocolId, final AsNumber localAs, final AsNumber remoteAs, final String linkName) {
337         return createBaseBuilder(protocolId)
338             .setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCaseBuilder()
339                 .setLocalNodeDescriptors(new LocalNodeDescriptorsBuilder().setAsNumber(localAs).setCRouterIdentifier(new IsisNodeCaseBuilder().setIsisNode(new IsisNodeBuilder().setIsoSystemId(new IsoSystemIdentifier(new byte[]{ 0, 0, 1, 2, 3, 4 })).build()).build()).build())
340                 .setRemoteNodeDescriptors(new RemoteNodeDescriptorsBuilder().setAsNumber(remoteAs).build())
341                 .setLinkDescriptors(new LinkDescriptorsBuilder().setMultiTopologyId(new TopologyIdentifier(1)).build())
342                 .build())
343             .setAttributes(new AttributesBuilder()
344                 .addAugmentation(Attributes1.class, new Attributes1Builder().setLinkStateAttribute(new LinkAttributesCaseBuilder().setLinkAttributes(
345                     new LinkAttributesBuilder().setSharedRiskLinkGroups(Lists.newArrayList(new SrlgId(5L), new SrlgId(15L))).setAdminGroup(new AdministrativeGroup(0L))
346                         .setMaxLinkBandwidth(new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0xff}))
347                         .setMaxReservableBandwidth(new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0x1f}))
348                         .setUnreservedBandwidth(Lists.newArrayList(new UnreservedBandwidthBuilder().setKey(new UnreservedBandwidthKey((short) 1)).setBandwidth(new Bandwidth(new byte[]{0x00, 0x00, 0x00, (byte) 0xff})).build()))
349                         .setTeMetric(new TeMetric(100L)).setLinkName(linkName).build()).build()).build())
350                 .build())
351             .build();
352     }
353
354     private LinkstateRouteBuilder createBaseBuilder(final ProtocolId protocolId) {
355         return new LinkstateRouteBuilder()
356             .setIdentifier(IDENTIFIER)
357             .setKey(new LinkstateRouteKey(LINKSTATE_ROUTE_KEY))
358             .setRouteKey(LINKSTATE_ROUTE_KEY)
359             .setProtocolId(protocolId);
360     }
361 }