import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.RETURNS_SMART_NULLS;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.withSettings;
import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
import io.netty.buffer.Unpooled;
import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.MockMakers;
import org.opendaylight.mdsal.binding.api.DataTreeModification;
import org.opendaylight.mdsal.binding.api.WriteTransaction;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
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;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
import org.opendaylight.yangtools.yang.common.Uint16;
import org.opendaylight.yangtools.yang.common.Uint32;
import org.opendaylight.yangtools.yang.common.Uint64;
@Override
public void setUp() {
super.setUp();
- this.linkstateTopoBuilder = new LinkstateTopologyBuilder(getDataBroker(), LOC_RIB_REF, TEST_TOPOLOGY_ID,
+ linkstateTopoBuilder = new LinkstateTopologyBuilder(getDataBroker(), LOC_RIB_REF, TEST_TOPOLOGY_ID,
LISTENER_RESTART_TIME, LISTENER_ENFORCE_COUNTER);
- this.linkstateTopoBuilder.start();
- this.tablePathIID =
+ linkstateTopoBuilder.start();
+ tablePathIID =
LOC_RIB_REF.getInstanceIdentifier().builder().child(LocRib.class)
.child(Tables.class,
- new TablesKey(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class))
+ new TablesKey(LinkstateAddressFamily.VALUE, LinkstateSubsequentAddressFamily.VALUE))
.build();
- this.linkstateNodeRouteKey = getLinkstateRouteKey("node-route");
- this.linkstatePrefixRouteKey = getLinkstateRouteKey("prefix-route");
- this.linkstateLinkRouteKey = getLinkstateRouteKey("link-route");
- this.linkstateNodeRouteIID = createLinkstateRouteIID(this.linkstateNodeRouteKey);
- this.linkstatePrefixRouteIID = createLinkstateRouteIID(this.linkstatePrefixRouteKey);
- this.linkstateLinkRouteIID = createLinkstateRouteIID(this.linkstateLinkRouteKey);
+ linkstateNodeRouteKey = getLinkstateRouteKey("node-route");
+ linkstatePrefixRouteKey = getLinkstateRouteKey("prefix-route");
+ linkstateLinkRouteKey = getLinkstateRouteKey("link-route");
+ linkstateNodeRouteIID = createLinkstateRouteIID(linkstateNodeRouteKey);
+ linkstatePrefixRouteIID = createLinkstateRouteIID(linkstatePrefixRouteKey);
+ linkstateLinkRouteIID = createLinkstateRouteIID(linkstateLinkRouteKey);
}
@After
public void tearDown() throws Exception {
- this.linkstateTopoBuilder.close();
- checkNotPresentOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier());
+ linkstateTopoBuilder.close();
+ checkNotPresentOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier());
}
@Test
public void testLinkstateTopologyBuilderTopologyTypes() throws InterruptedException, ExecutionException {
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(LinkstateTopologyBuilder.LINKSTATE_TOPOLOGY_TYPE, topology.getTopologyTypes());
return topology;
});
@Test
public void testIsisLinkstateTopologyBuilder() throws InterruptedException, ExecutionException {
// create node
- updateLinkstateRoute(this.linkstateNodeRouteIID,
+ updateLinkstateRoute(linkstateNodeRouteIID,
createLinkstateNodeRoute(ProtocolId.IsisLevel2, "node1", NODE_1_AS, ROUTER_1_ID));
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(1, topology.nonnullNode().size());
final Node node1 = topology.nonnullNode().values().iterator().next();
assertEquals(NODE_1_ISIS_ID, node1.getNodeId().getValue());
final IgpNodeAttributes igpNode1 = node1.augmentation(Node1.class).getIgpNodeAttributes();
- assertEquals(ROUTER_1_ID, igpNode1.getRouterId().get(0).getIpv4Address().getValue());
+ assertEquals(ROUTER_1_ID, igpNode1.getRouterId().iterator().next().getIpv4Address().getValue());
assertEquals("node1", igpNode1.getName().getValue());
final IgpNodeAttributes1 igpNodeAttributes1 = igpNode1.augmentation(IgpNodeAttributes1.class);
assertEquals("0000.0102.0304", igpNodeAttributes1.getIsisNodeAttributes().getIso().getIsoSystemId()
assertEquals(ROUTER_1_ID, igpNodeAttributes1.getIsisNodeAttributes().getTed().getTeRouterIdIpv4()
.getValue());
assertEquals("47.0000.0000.0000.0000.0102.0304", igpNodeAttributes1.getIsisNodeAttributes()
- .getNet().get(0).getValue());
+ .getNet().iterator().next().getValue());
assertNull(igpNode1.augmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf
.topology.rev131021.IgpNodeAttributes1.class));
assertEquals(LinkstateTopologyBuilder.LINKSTATE_TOPOLOGY_TYPE, topology.getTopologyTypes());
});
// create link
- updateLinkstateRoute(this.linkstateLinkRouteIID,
+ updateLinkstateRoute(linkstateLinkRouteIID,
createLinkstateLinkRoute(ProtocolId.IsisLevel2, NODE_1_AS, NODE_2_AS, "link1"));
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(1, topology.nonnullLink().size());
final Link link1 = topology.nonnullLink().values().iterator().next();
assertEquals(2, topology.nonnullNode().size());
.getSegment().getValue().intValue());
return topology;
});
- removeLinkstateRoute(this.linkstateLinkRouteIID);
+ removeLinkstateRoute(linkstateLinkRouteIID);
// update node
- updateLinkstateRoute(this.linkstateNodeRouteIID,
+ updateLinkstateRoute(linkstateNodeRouteIID,
createLinkstateNodeRoute(ProtocolId.IsisLevel2, "updated-node", NODE_1_AS, ROUTER_2_ID));
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(1, topology.getNode().size());
final IgpNodeAttributes igpNode2 = topology.getNode().values().iterator().next().augmentation(Node1.class)
.getIgpNodeAttributes();
- assertEquals(ROUTER_2_ID, igpNode2.getRouterId().get(0).getIpv4Address().getValue());
+ assertEquals(ROUTER_2_ID, igpNode2.getRouterId().iterator().next().getIpv4Address().getValue());
assertEquals("updated-node", igpNode2.getName().getValue());
return topology;
});
// remove
- removeLinkstateRoute(this.linkstateNodeRouteIID);
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ removeLinkstateRoute(linkstateNodeRouteIID);
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(LinkstateTopologyBuilder.LINKSTATE_TOPOLOGY_TYPE, topology.getTopologyTypes());
assertNull(topology.getNode());
assertNull(topology.getLink());
@Test
public void testOspfLinkstateTopologyBuilder() throws InterruptedException, ExecutionException {
// create node
- updateLinkstateRoute(this.linkstateNodeRouteIID,
+ updateLinkstateRoute(linkstateNodeRouteIID,
createLinkstateNodeRoute(ProtocolId.Ospf, "node1", NODE_1_AS, ROUTER_1_ID));
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(1, topology.nonnullNode().size());
final Node node1 = topology.nonnullNode().values().iterator().next();
assertEquals(NODE_1_OSPF_ID, node1.getNodeId().getValue());
final IgpNodeAttributes igpNode1 = node1.augmentation(Node1.class).getIgpNodeAttributes();
- assertEquals(ROUTER_1_ID, igpNode1.getRouterId().get(0).getIpv4Address().getValue());
+ assertEquals(ROUTER_1_ID, igpNode1.getRouterId().iterator().next().getIpv4Address().getValue());
assertEquals("node1", igpNode1.getName().getValue());
assertNull(igpNode1.augmentation(IgpNodeAttributes1.class));
assertEquals(ROUTER_1_ID, igpNode1.augmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns
});
// update node with prefix
- updateLinkstateRoute(this.linkstatePrefixRouteIID,
+ updateLinkstateRoute(linkstatePrefixRouteIID,
createLinkstatePrefixRoute(ProtocolId.Ospf, NODE_1_AS, NODE_1_PREFIX, 500L, ROUTER_1_ID));
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
final Node node1 = topology.nonnullNode().values().iterator().next();
final IgpNodeAttributes igpNode1 = node1.augmentation(Node1.class).getIgpNodeAttributes();
assertEquals(1, igpNode1.nonnullPrefix().size());
});
// create link
- updateLinkstateRoute(this.linkstateLinkRouteIID,
+ updateLinkstateRoute(linkstateLinkRouteIID,
createLinkstateLinkRoute(ProtocolId.Ospf, NODE_1_AS, NODE_2_AS, "link1"));
- readDataOperational(getDataBroker(), this.linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
+ readDataOperational(getDataBroker(), linkstateTopoBuilder.getInstanceIdentifier(), topology -> {
assertEquals(1, topology.nonnullLink().size());
final Link link1 = topology.nonnullLink().values().iterator().next();
assertEquals(2, topology.getNode().size());
* This test is to verify if the AbstractTopologyBuilder/LinkstateTopologyBuilder is handling exception correctly.
*/
@Test
- @SuppressWarnings("checkstyle:IllegalCatch")
public void testRouteChangedError() throws Exception {
- final LinkstateTopologyBuilder spiedLinkstateTopologyBuilder = spy(this.linkstateTopoBuilder);
+ // FIXME: this is a very weird setup and does not work with inline mockmaker
+ // perhaps that is because of https://github.com/mockito/mockito/issues/2488, but we should be able to
+ // rework this test in some other way (i.e. pure mocking with real classes)?
+ final var spiedLinkstateTopologyBuilder = mock(LinkstateTopologyBuilder.class,
+ // this part is the same as spy(), but ...
+ withSettings().spiedInstance(linkstateTopoBuilder).defaultAnswer(CALLS_REAL_METHODS)
+ // ... here we use a different MockMaker
+ .mockMaker(MockMakers.SUBCLASS));
doThrow(RuntimeException.class).when(spiedLinkstateTopologyBuilder).routeChanged(any(), any());
- try {
- spiedLinkstateTopologyBuilder.routeChanged(null, null);
- fail("Mockito failed to spy routeChanged() method");
- } catch (final Exception e) {
- assertTrue(e instanceof RuntimeException);
- }
+ // Verify throws spying
+ assertThrows(RuntimeException.class, () -> spiedLinkstateTopologyBuilder.routeChanged(null, null));
+
assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
// first we examine if the chain is being reset when no exception is thrown
- spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
+ spiedLinkstateTopologyBuilder.onDataTreeChanged(List.of());
verify(spiedLinkstateTopologyBuilder, times(1)).restartTransactionChainOnDemand();
verify(spiedLinkstateTopologyBuilder, never()).scheduleListenerRestart();
verify(spiedLinkstateTopologyBuilder, never()).resetTransactionChain();
assertEquals(0L, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
// now pass some invalid data to cause onDataTreeChanged fail
final DataTreeModification<LinkstateRoute> modification = mock(DataTreeModification.class, RETURNS_SMART_NULLS);
- final List<DataTreeModification<LinkstateRoute>> changes = new ArrayList<>();
- changes.add(modification);
+ final List<DataTreeModification<LinkstateRoute>> changes = List.of(modification);
spiedLinkstateTopologyBuilder.onDataTreeChanged(changes);
// one restart transaction chain check in onDataTreeChanged()
// we are introducing some timeout here as transaction may be executed in a delay manner
assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
final long listenerScheduledRestartTime = spiedLinkstateTopologyBuilder.listenerScheduledRestartTime;
// call again with empty change to invoke restartTransactionChainOnDemand()
- spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
+ spiedLinkstateTopologyBuilder.onDataTreeChanged(List.of());
verify(spiedLinkstateTopologyBuilder, times(3)).restartTransactionChainOnDemand();
// transaction chain should be reset while listener should not
verify(spiedLinkstateTopologyBuilder, times(1)).resetTransactionChain();
verify(spiedLinkstateTopologyBuilder, never()).resetListener();
Thread.sleep(LISTENER_RESTART_TIME);
// manually invoke onTransactionChainFailed() to have the listener restart scheduled again
- spiedLinkstateTopologyBuilder.onTransactionChainFailed(null, null, null);
+ spiedLinkstateTopologyBuilder.onFailure(null);
assertEquals(spiedLinkstateTopologyBuilder.listenerScheduledRestartTime, listenerScheduledRestartTime
+ LISTENER_RESTART_TIME);
verify(spiedLinkstateTopologyBuilder, times(5)).restartTransactionChainOnDemand();
// sleep to let the listener restart timer times out
Thread.sleep(LISTENER_RESTART_TIME);
// apply a good modification (empty change)
- spiedLinkstateTopologyBuilder.onDataTreeChanged(new ArrayList<>());
+ spiedLinkstateTopologyBuilder.onDataTreeChanged(List.of());
assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartTime);
assertEquals(0, spiedLinkstateTopologyBuilder.listenerScheduledRestartEnforceCounter);
verify(spiedLinkstateTopologyBuilder, times(6)).restartTransactionChainOnDemand();
}
private InstanceIdentifier<LinkstateRoute> createLinkstateRouteIID(final String linkstateRouteKey) {
- return this.tablePathIID.builder().child(LinkstateRoutesCase.class, LinkstateRoutes.class)
+ return tablePathIID.builder().child(LinkstateRoutesCase.class, LinkstateRoutes.class)
.child(LinkstateRoute.class, new LinkstateRouteKey(new PathId(Uint32.ZERO), linkstateRouteKey)).build();
}
private LinkstateRoute createLinkstateNodeRoute(final ProtocolId protocolId, final String nodeName,
final AsNumber asNumber, final String ipv4RouterId) {
- return createBaseBuilder(this.linkstateNodeRouteKey, protocolId)
+ return createBaseBuilder(linkstateNodeRouteKey, protocolId)
.setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate
.rev200120.linkstate.object.type.NodeCaseBuilder()
.setNodeDescriptors(new NodeDescriptorsBuilder()
.setNodeAttributes(new NodeAttributesBuilder()
.setDynamicHostname(nodeName)
.setIpv4RouterId(new Ipv4RouterIdentifier(ipv4RouterId))
- .setIsisAreaId(Collections.singletonList(
- new IsisAreaIdentifier(new byte[]{0x47})))
+ .setIsisAreaId(Set.of(new IsisAreaIdentifier(new byte[]{0x47})))
.setSrCapabilities(new SrCapabilitiesBuilder()
.setRangeSize(new Uint24(Uint32.valueOf(SRGB_RANGE)))
.setSidLabelIndex(new LocalLabelCaseBuilder()
private LinkstateRoute createLinkstatePrefixRoute(final ProtocolId protocolId, final AsNumber asNumber,
final String ipv4Prefix, final long igpMetric, final String ospfFwdAddress) {
- return createBaseBuilder(this.linkstatePrefixRouteKey, protocolId)
+ return createBaseBuilder(linkstatePrefixRouteKey, protocolId)
.setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120
.linkstate.object.type.PrefixCaseBuilder()
.setAdvertisingNodeDescriptors(new AdvertisingNodeDescriptorsBuilder()
private LinkstateRoute createLinkstateLinkRoute(final ProtocolId protocolId, final AsNumber localAs,
final AsNumber remoteAs, final String linkName) {
- return createBaseBuilder(this.linkstateLinkRouteKey, protocolId)
+ return createBaseBuilder(linkstateLinkRouteKey, protocolId)
.setObjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate
.rev200120.linkstate.object.type.LinkCaseBuilder()
.setLocalNodeDescriptors(new LocalNodeDescriptorsBuilder().setAsNumber(localAs)
.addAugmentation(new Attributes1Builder()
.setLinkStateAttribute(new LinkAttributesCaseBuilder()
.setLinkAttributes(new LinkAttributesBuilder()
- .setSharedRiskLinkGroups(Lists.newArrayList(
+ .setSharedRiskLinkGroups(ImmutableSet.of(
new SrlgId(Uint32.valueOf(5)), new SrlgId(Uint32.valueOf(15))))
.setAdminGroup(new AdministrativeGroup(Uint32.ZERO))
.setMaxLinkBandwidth(
new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0xff}))
.setMaxReservableBandwidth(
new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0x1f}))
- .setUnreservedBandwidth(Lists.newArrayList(new UnreservedBandwidthBuilder()
+ .setUnreservedBandwidth(BindingMap.of(new UnreservedBandwidthBuilder()
.withKey(new UnreservedBandwidthKey(Uint8.ONE))
.setBandwidth(new Bandwidth(new byte[]{0x00, 0x00, 0x00, (byte) 0xff}))
.build()))
.setTeMetric(new TeMetric(Uint32.valueOf(100)))
.setLinkName(linkName)
- .setSrAdjIds(Collections.singletonList(new SrAdjIdsBuilder()
+ .setSrAdjIds(List.of(new SrAdjIdsBuilder()
.setSidLabelIndex(new LocalLabelCaseBuilder()
.setLocalLabel(new MplsLabel(Uint32.valueOf(ADJ_SID)))
.build())