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