BUG-6111 Fix the AbstractTopologyBuilder to reset PingPongTransactionChain
[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.link.state.UnreservedBandwidthBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.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         final InstanceIdentifier<Tables> path = this.linkstateTopoBuilder.tableInstanceIdentifier(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
121         this.linkstateRouteIID = path.builder().child((Class)LinkstateRoutes.class).child(LinkstateRoute.class, new LinkstateRouteKey(LINKSTATE_ROUTE_KEY)).build();
122     }
123
124     @After
125     public void tearDown() throws Exception {
126         this.linkstateTopoBuilder.close();
127         assertFalse(getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).isPresent());
128     }
129
130     @Test
131     public void testLinkstateTopologyBuilderTopologyTypes() {
132         final Optional<Topology> topologyMaybe = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier());
133         assertTrue(topologyMaybe.isPresent());
134         final Topology topology = topologyMaybe.get();
135         assertNotNull(topology.getTopologyTypes().getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1.class));
136         assertNotNull(topology.getTopologyTypes().getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1.class).getBgpLinkstateTopology());
137     }
138
139     @Test
140     public void testIsisLinkstateTopologyBuilder() throws TransactionCommitFailedException {
141         // create node
142         updateLinkstateRoute(createLinkstateNodeRoute(ProtocolId.IsisLevel2, "node1", NODE_1_AS, ROUTER_1_ID));
143         final Optional<Topology> topologyMaybe = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier());
144         assertTrue(topologyMaybe.isPresent());
145         final Topology topology1 = topologyMaybe.get();
146         assertEquals(1, topology1.getNode().size());
147         final Node node1 = topology1.getNode().get(0);
148         assertEquals(NODE_1_ISIS_ID, node1.getNodeId().getValue());
149         final IgpNodeAttributes igpNode1 = node1.getAugmentation(Node1.class).getIgpNodeAttributes();
150         assertEquals(ROUTER_1_ID, igpNode1.getRouterId().get(0).getIpv4Address().getValue());
151         assertEquals("node1", igpNode1.getName().getValue());
152         final IgpNodeAttributes1 igpNodeAttributes1 = igpNode1.getAugmentation(IgpNodeAttributes1.class);
153         assertEquals("0000.0102.0304", igpNodeAttributes1.getIsisNodeAttributes().getIso().getIsoSystemId().getValue());
154         assertEquals(ROUTER_1_ID, igpNodeAttributes1.getIsisNodeAttributes().getTed().getTeRouterIdIpv4().getValue());
155         assertEquals("47.0000.0000.0000.0000.0102.0304", igpNodeAttributes1.getIsisNodeAttributes().getNet().get(0).getValue());
156         assertNull(igpNode1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class));
157
158         // create link
159         updateLinkstateRoute(createLinkstateLinkRoute(ProtocolId.IsisLevel2, NODE_1_AS, NODE_2_AS, "link1"));
160         final Topology topology2 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
161         assertEquals(1, topology2.getLink().size());
162         final Link link1 = topology2.getLink().get(0);
163         assertEquals(2, topology2.getNode().size());
164         assertEquals(1, topology2.getNode().get(0).getTerminationPoint().size());
165         assertEquals(1, topology2.getNode().get(1).getTerminationPoint().size());
166         assertEquals("bgpls://IsisLevel2:1/type=link&local-as=1&local-router=0000.0102.0304&remote-as=2&mt=1", link1.getLinkId().getValue());
167         assertEquals(NODE_1_ISIS_ID, link1.getSource().getSourceNode().getValue());
168         assertEquals(NODE_2_ISIS_ID, link1.getDestination().getDestNode().getValue());
169         final IgpLinkAttributes igpLink1 = link1.getAugmentation(Link1.class).getIgpLinkAttributes();
170         assertEquals("link1", igpLink1.getName());
171         assertEquals((short) 1, igpLink1.getAugmentation(IgpLinkAttributes1.class).getIsisLinkAttributes().getMultiTopologyId().shortValue());
172         assertNull(igpLink1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class));
173
174         // update node
175         updateLinkstateRoute(createLinkstateNodeRoute(ProtocolId.IsisLevel2, "updated-node", NODE_1_AS, ROUTER_2_ID));
176         final Topology topology3 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
177         final IgpNodeAttributes igpNode2 = topology3.getNode().get(0).getAugmentation(Node1.class).getIgpNodeAttributes();
178         assertEquals(ROUTER_2_ID, igpNode2.getRouterId().get(0).getIpv4Address().getValue());
179         assertEquals("updated-node", igpNode2.getName().getValue());
180
181         // remove
182         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
183         wTx.delete(LogicalDatastoreType.OPERATIONAL, this.linkstateRouteIID);
184         wTx.submit();
185         final Topology topology4 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
186         assertEquals(0, topology4.getNode().size());
187         assertEquals(0, topology4.getLink().size());
188     }
189
190     @Test
191     public void testOspfLinkstateTopologyBuilder() throws TransactionCommitFailedException {
192         // create node
193         updateLinkstateRoute(createLinkstateNodeRoute(ProtocolId.Ospf, "node1", NODE_1_AS, ROUTER_1_ID));
194         final Optional<Topology> topologyMaybe = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier());
195         assertTrue(topologyMaybe.isPresent());
196         final Topology topology1 = topologyMaybe.get();
197         assertEquals(1, topology1.getNode().size());
198         final Node node1 = topology1.getNode().get(0);
199         assertEquals(NODE_1_OSPF_ID, node1.getNodeId().getValue());
200         final IgpNodeAttributes igpNode1 = node1.getAugmentation(Node1.class).getIgpNodeAttributes();
201         assertEquals(ROUTER_1_ID, igpNode1.getRouterId().get(0).getIpv4Address().getValue());
202         assertEquals("node1", igpNode1.getName().getValue());
203         assertNull(igpNode1.getAugmentation(IgpNodeAttributes1.class));
204         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());
205
206         // update node with prefix
207         updateLinkstateRoute(createLinkstatePrefixRoute(ProtocolId.Ospf, NODE_1_AS, NODE_1_PREFIX, 500L, ROUTER_1_ID));
208         final Topology topology3 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
209         final IgpNodeAttributes igpNode2 = topology3.getNode().get(0).getAugmentation(Node1.class).getIgpNodeAttributes();
210         assertEquals(1, igpNode2.getPrefix().size());
211         final Prefix prefix = igpNode2.getPrefix().get(0);
212         assertEquals(NODE_1_PREFIX, prefix.getPrefix().getIpv4Prefix().getValue());
213         assertEquals(500L, prefix.getMetric().longValue());
214
215         // create link
216         updateLinkstateRoute(createLinkstateLinkRoute(ProtocolId.Ospf, NODE_1_AS, NODE_2_AS, "link1"));
217         final Topology topology2 = getTopology(this.linkstateTopoBuilder.getInstanceIdentifier()).get();
218         assertEquals(1, topology2.getLink().size());
219         final Link link1 = topology2.getLink().get(0);
220         assertEquals(2, topology2.getNode().size());
221         assertEquals(1, topology2.getNode().get(0).getTerminationPoint().size());
222         assertEquals(1, topology2.getNode().get(1).getTerminationPoint().size());
223         assertEquals("bgpls://Ospf:1/type=link&local-as=1&local-router=0000.0102.0304&remote-as=2&mt=1", link1.getLinkId().getValue());
224         assertEquals(NODE_1_OSPF_ID, link1.getSource().getSourceNode().getValue());
225         assertEquals(NODE_2_OSPF_ID, link1.getDestination().getDestNode().getValue());
226         final IgpLinkAttributes igpLink1 = link1.getAugmentation(Link1.class).getIgpLinkAttributes();
227         assertEquals("link1", igpLink1.getName());
228         assertNull(igpLink1.getAugmentation(IgpLinkAttributes1.class));
229         assertEquals((short) 1, igpLink1.getAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class).getOspfLinkAttributes().getMultiTopologyId().shortValue());
230         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());
231     }
232
233     /**
234      * This test is to verify if the AbstractTopologyBuilder/LinkstateTopologyBuilder is handling exception correctly
235      *
236      * @throws Exception
237      */
238     @Test
239     public void testRouteChangedError() throws Exception {
240         LinkstateTopologyBuilder spiedLinkstateTopologyBuilder = spy(this.linkstateTopoBuilder);
241         doThrow(RuntimeException.class).when(spiedLinkstateTopologyBuilder).routeChanged(any(), any());
242         try {
243             spiedLinkstateTopologyBuilder.routeChanged(null, null);
244             fail("Mockito failed to spy routeChanged() method");
245         } catch (Exception e) {
246             assertTrue(e instanceof RuntimeException);
247         }
248         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
249         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
250         // first we examine if the chain is being reset when no exception is thrown
251         spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
252         verify(spiedLinkstateTopologyBuilder, times(1)).restartTransactionChainOnDemand();
253         verify(spiedLinkstateTopologyBuilder, never()).scheduleListenerRestart();
254         verify(spiedLinkstateTopologyBuilder, never()).resetTransactionChain();
255         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
256         assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
257         // now pass some invalid data to cause onDataTreeChanged fail
258         DataTreeModification<LinkstateRoute> modification = (DataTreeModification<LinkstateRoute>) mock(DataTreeModification.class, RETURNS_SMART_NULLS);
259         final List<DataTreeModification<LinkstateRoute>> changes = new ArrayList<>();
260         changes.add(modification);
261         spiedLinkstateTopologyBuilder.onDataTreeChanged(changes);
262         // one restart transaction chain check in onDataTreeChanged()
263         // we are introducing some timeout here as transaction may be executed in a delay manner
264         verify(spiedLinkstateTopologyBuilder, timeout(5000).times(1)).scheduleListenerRestart();
265         verify(spiedLinkstateTopologyBuilder, times(2)).restartTransactionChainOnDemand();
266         assertNotEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
267         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
268         final long listenerScheduledRestartTime = spiedLinkstateTopologyBuilder.listenerScheduledRestartTime;
269         // call again with empty change to invoke restartTransactionChainOnDemand()
270         spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
271         verify(spiedLinkstateTopologyBuilder, times(3)).restartTransactionChainOnDemand();
272         // transaction chain should be reset while listener should not
273         verify(spiedLinkstateTopologyBuilder, times(1)).resetTransactionChain();
274         verify(spiedLinkstateTopologyBuilder, never()).resetListener();
275         // now apply a change with bad modification again
276         spiedLinkstateTopologyBuilder.onDataTreeChanged(changes);
277         verify(spiedLinkstateTopologyBuilder, times(4)).restartTransactionChainOnDemand();
278         // listener scheduled again
279         verify(spiedLinkstateTopologyBuilder, timeout(5000).times(2)).scheduleListenerRestart();
280         // listener timer shouldn't have changed
281         assertEquals(listenerScheduledRestartTime, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
282         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
283         verify(spiedLinkstateTopologyBuilder, times(2)).resetTransactionChain();
284         verify(spiedLinkstateTopologyBuilder, never()).resetListener();
285         Thread.sleep(LISTENER_RESTART_TIME);
286         // manually invoke onTransactionChainFailed() to have the listener restart scheduled again
287         spiedLinkstateTopologyBuilder.onTransactionChainFailed(null, null, null);
288         assertTrue(spiedLinkstateTopologyBuilder.listenerScheduledRestartTime == listenerScheduledRestartTime + LISTENER_RESTART_TIME);
289         verify(spiedLinkstateTopologyBuilder, times(5)).restartTransactionChainOnDemand();
290         verify(spiedLinkstateTopologyBuilder, times(3)).scheduleListenerRestart();
291         // enforce counter get increased
292         assertEquals(1, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
293         verify(spiedLinkstateTopologyBuilder, times(3)).resetTransactionChain();
294         verify(spiedLinkstateTopologyBuilder, never()).resetListener();
295         // sleep to let the listener restart timer times out
296         Thread.sleep(LISTENER_RESTART_TIME);
297         // apply a good modification (empty change)
298         spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
299         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
300         assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
301         verify(spiedLinkstateTopologyBuilder, times(6)).restartTransactionChainOnDemand();
302         // listener restarted didn't get rescheduled again
303         verify(spiedLinkstateTopologyBuilder, times(3)).scheduleListenerRestart();
304         verify(spiedLinkstateTopologyBuilder, times(4)).resetTransactionChain();
305         verify(spiedLinkstateTopologyBuilder, times(1)).resetListener();
306     }
307
308     private void updateLinkstateRoute(final LinkstateRoute data) {
309         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
310         wTx.put(LogicalDatastoreType.OPERATIONAL, this.linkstateRouteIID, data, true);
311         wTx.submit();
312     }
313
314     private LinkstateRoute createLinkstateNodeRoute(final ProtocolId protocolId, final String nodeName, final AsNumber asNumber, final String ipv4RouterId) {
315         return createBaseBuilder(protocolId)
316             .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())
317             .setAttributes(new AttributesBuilder()
318                 .addAugmentation(Attributes1.class, new Attributes1Builder().setLinkStateAttribute(new NodeAttributesCaseBuilder().setNodeAttributes(new NodeAttributesBuilder()
319                     .setDynamicHostname(nodeName)
320                     .setIpv4RouterId(new Ipv4RouterIdentifier(ipv4RouterId))
321                     .setIsisAreaId(Collections.singletonList(new IsisAreaIdentifier(new byte[]{0x47}))).build()).build()).build()).build()).build();
322     }
323
324     private LinkstateRoute createLinkstatePrefixRoute(final ProtocolId protocolId, final AsNumber asNumber, final String ipv4Prefix, final long igpMetric, final String ospfFwdAddress) {
325         return createBaseBuilder(protocolId)
326             .setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCaseBuilder()
327                 .setAdvertisingNodeDescriptors(new AdvertisingNodeDescriptorsBuilder().setAsNumber(asNumber).build())
328                 .setPrefixDescriptors(new PrefixDescriptorsBuilder().setIpReachabilityInformation(new IpPrefix(new Ipv4Prefix(ipv4Prefix))).build())
329                 .build())
330             .setAttributes(new AttributesBuilder()
331                 .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())
332              .build();
333     }
334
335     private LinkstateRoute createLinkstateLinkRoute(final ProtocolId protocolId, final AsNumber localAs, final AsNumber remoteAs, final String linkName) {
336         return createBaseBuilder(protocolId)
337             .setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCaseBuilder()
338                 .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())
339                 .setRemoteNodeDescriptors(new RemoteNodeDescriptorsBuilder().setAsNumber(remoteAs).build())
340                 .setLinkDescriptors(new LinkDescriptorsBuilder().setMultiTopologyId(new TopologyIdentifier(1)).build())
341                 .build())
342             .setAttributes(new AttributesBuilder()
343                 .addAugmentation(Attributes1.class, new Attributes1Builder().setLinkStateAttribute(new LinkAttributesCaseBuilder().setLinkAttributes(
344                     new LinkAttributesBuilder().setSharedRiskLinkGroups(Lists.newArrayList(new SrlgId(5L), new SrlgId(15L))).setAdminGroup(new AdministrativeGroup(0L))
345                         .setMaxLinkBandwidth(new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0xff}))
346                         .setMaxReservableBandwidth(new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0x1f}))
347                         .setUnreservedBandwidth(Lists.newArrayList(new UnreservedBandwidthBuilder().setKey(new UnreservedBandwidthKey((short) 1)).setBandwidth(new Bandwidth(new byte[]{0x00, 0x00, 0x00, (byte) 0xff})).build()))
348                         .setTeMetric(new TeMetric(100L)).setLinkName(linkName).build()).build()).build())
349                 .build())
350             .build();
351     }
352
353     private LinkstateRouteBuilder createBaseBuilder(final ProtocolId protocolId) {
354         return new LinkstateRouteBuilder()
355             .setIdentifier(IDENTIFIER)
356             .setKey(new LinkstateRouteKey(LINKSTATE_ROUTE_KEY))
357             .setRouteKey(LINKSTATE_ROUTE_KEY)
358             .setProtocolId(protocolId);
359     }
360 }