OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / topology-manager / src / test / java / org / opendaylight / openflowplugin / applications / topology / manager / TestUtils.java
1 /*
2  * Copyright (c) 2015, 2017 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 package org.opendaylight.openflowplugin.applications.topology.manager;
9
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;
19
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;
25 import java.util.Set;
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;
47
48 public final class TestUtils {
49     private TestUtils() {
50     }
51
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));
56     }
57
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));
64         }
65     }
66
67     static void setReadFutureAsync(final Topology topology, final SettableFuture<Optional<Topology>> readFuture) {
68         new Thread() {
69             @Override
70             public void run() {
71                 Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
72                 readFuture.set(Optional.of(topology));
73             }
74         }.start();
75     }
76
77     static void waitForSubmit(CountDownLatch latch) {
78         assertEquals("Transaction submitted", true, Uninterruptibles.awaitUninterruptibly(latch, 5, TimeUnit.SECONDS));
79     }
80
81     static void waitForDeletes(int expDeleteCalls, final CountDownLatch latch) {
82         boolean done = Uninterruptibles.awaitUninterruptibly(latch, 5, TimeUnit.SECONDS);
83         if (!done) {
84             fail("Expected " + expDeleteCalls + " delete calls. Actual: " + (expDeleteCalls - latch.getCount()));
85         }
86     }
87
88     static CountDownLatch setupStubbedSubmit(ReadWriteTransaction mockTx) {
89         final CountDownLatch latch = new CountDownLatch(1);
90         doAnswer(invocation -> {
91             latch.countDown();
92             return Futures.immediateCheckedFuture(null);
93         }).when(mockTx).submit();
94
95         return latch;
96     }
97
98     @SuppressWarnings("rawtypes")
99     static void setupStubbedDeletes(ReadWriteTransaction mockTx, ArgumentCaptor<InstanceIdentifier> deletedLinkIDs,
100                                     final CountDownLatch latch) {
101         doAnswer(invocation -> {
102             latch.countDown();
103             return null;
104         }).when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), deletedLinkIDs.capture());
105     }
106
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));
111         return nodeKey;
112     }
113
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));
117     }
118
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,
125                        ncKey);
126     }
127
128     static Link newLink(String id, Source source, Destination dest) {
129         return new LinkBuilder().setLinkId(new LinkId(id)).setSource(source).setDestination(dest).build();
130     }
131
132     static Destination newDestTp(String id) {
133         return new DestinationBuilder().setDestTp(new TpId(id)).build();
134     }
135
136     static Source newSourceTp(String id) {
137         return new SourceBuilder().setSourceTp(new TpId(id)).build();
138     }
139
140     static Destination newDestNode(String id) {
141         return new DestinationBuilder().setDestNode(new NodeId(id)).build();
142     }
143
144     static Source newSourceNode(String id) {
145         return new SourceBuilder().setSourceNode(new NodeId(id)).build();
146     }
147
148 }