2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.topology.manager;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.atLeast;
16 import static org.mockito.Mockito.doAnswer;
17 import static org.mockito.Mockito.inOrder;
18 import static org.mockito.Mockito.never;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.SettableFuture;
23 import com.google.common.util.concurrent.Uninterruptibles;
24 import java.util.HashSet;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.InOrder;
30 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Destination;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Source;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 public final class TestUtils {
52 static void verifyMockTx(ReadWriteTransaction mockTx) {
53 InOrder inOrder = inOrder(mockTx);
54 inOrder.verify(mockTx, atLeast(0)).submit();
55 inOrder.verify(mockTx, never()).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
58 @SuppressWarnings("rawtypes")
59 static void assertDeletedIDs(InstanceIdentifier[] expDeletedIIDs,
60 ArgumentCaptor<InstanceIdentifier> deletedLinkIDs) {
61 Set<InstanceIdentifier> actualIIDs = new HashSet<>(deletedLinkIDs.getAllValues());
62 for (InstanceIdentifier id : expDeletedIIDs) {
63 assertTrue("Missing expected deleted IID " + id, actualIIDs.contains(id));
67 static void setReadFutureAsync(final Topology topology, final SettableFuture<Optional<Topology>> readFuture) {
71 Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
72 readFuture.set(Optional.of(topology));
77 static void waitForSubmit(CountDownLatch latch) {
78 assertEquals("Transaction submitted", true, Uninterruptibles.awaitUninterruptibly(latch, 5, TimeUnit.SECONDS));
81 static void waitForDeletes(int expDeleteCalls, final CountDownLatch latch) {
82 boolean done = Uninterruptibles.awaitUninterruptibly(latch, 5, TimeUnit.SECONDS);
84 fail("Expected " + expDeleteCalls + " delete calls. Actual: " + (expDeleteCalls - latch.getCount()));
88 static CountDownLatch setupStubbedSubmit(ReadWriteTransaction mockTx) {
89 final CountDownLatch latch = new CountDownLatch(1);
90 doAnswer(invocation -> {
92 return Futures.immediateCheckedFuture(null);
93 }).when(mockTx).submit();
98 @SuppressWarnings("rawtypes")
99 static void setupStubbedDeletes(ReadWriteTransaction mockTx, ArgumentCaptor<InstanceIdentifier> deletedLinkIDs,
100 final CountDownLatch latch) {
101 doAnswer(invocation -> {
104 }).when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), deletedLinkIDs.capture());
107 static org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey newInvNodeKey(String id) {
108 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey nodeKey
109 = new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey(
110 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId(id));
114 static NodeConnectorKey newInvNodeConnKey(String id) {
115 return new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey(
116 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId(id));
119 static KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> newNodeConnID(
120 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey nodeKey,
121 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey ncKey) {
122 return InstanceIdentifier.create(Nodes.class)
123 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, nodeKey)
124 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector.class,
128 static Link newLink(String id, Source source, Destination dest) {
129 return new LinkBuilder().setLinkId(new LinkId(id)).setSource(source).setDestination(dest).build();
132 static Destination newDestTp(String id) {
133 return new DestinationBuilder().setDestTp(new TpId(id)).build();
136 static Source newSourceTp(String id) {
137 return new SourceBuilder().setSourceTp(new TpId(id)).build();
140 static Destination newDestNode(String id) {
141 return new DestinationBuilder().setDestNode(new NodeId(id)).build();
144 static Source newSourceNode(String id) {
145 return new SourceBuilder().setSourceNode(new NodeId(id)).build();