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