Reduce ShardDataTree#getDataTree() callsites
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / AbstractEntityOwnershipTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.cluster.datastore.entityownership;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NAME_QNAME;
15 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
16 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
17 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_QNAME;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME;
20 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
21 import com.google.common.base.Function;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Stopwatch;
24 import com.google.common.util.concurrent.Uninterruptibles;
25 import java.util.concurrent.TimeUnit;
26 import org.hamcrest.Description;
27 import org.junit.Assert;
28 import org.mockito.ArgumentMatcher;
29 import org.mockito.Matchers;
30 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
31 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
32 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
44 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
50
51 /**
52  * Abstract base class providing utility methods.
53  *
54  * @author Thomas Pantelis
55  */
56 public class AbstractEntityOwnershipTest extends AbstractActorTest {
57     protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
58             YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
59         try {
60             assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
61             assertTrue(node instanceof ContainerNode);
62
63             ContainerNode entityOwnersNode = (ContainerNode) node;
64
65             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
66                     ENTITY_TYPE_QNAME, entityType, true);
67
68             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
69                     entityId, true);
70
71             getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent);
72         } catch(AssertionError e) {
73             throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
74         }
75     }
76
77     protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
78             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader, boolean expectPresent) {
79         AssertionError lastError = null;
80         Stopwatch sw = Stopwatch.createStarted();
81         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
82             NormalizedNode<?, ?> node = reader.apply(ENTITY_OWNERS_PATH);
83             try {
84                 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
85                 return;
86             } catch (AssertionError e) {
87                 lastError = e;
88                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
89             }
90         }
91
92         throw lastError;
93     }
94
95     protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
96             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
97         verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
98     }
99
100     protected MapEntryNode getMapEntryNodeChild(DataContainerNode<? extends PathArgument> parent, QName childMap,
101             QName child, Object key, boolean expectPresent) {
102         Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
103                 parent.getChild(new NodeIdentifier(childMap));
104         assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
105
106         MapNode entityTypeMapNode = (MapNode) childNode.get();
107         Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
108                 childMap, child, key));
109         if(expectPresent && !entityTypeEntry.isPresent()) {
110             fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
111         } else if(!expectPresent && entityTypeEntry.isPresent()) {
112             fail("Found unexpected " + childMap.toString() + " entry for " + key);
113         }
114
115         return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
116     }
117
118     static void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId,
119             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
120         AssertionError lastError = null;
121         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
122         Stopwatch sw = Stopwatch.createStarted();
123         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
124             try {
125                 NormalizedNode<?, ?> node = reader.apply(entityPath);
126                 Assert.assertNotNull("Owner was not set for entityId: " + entityId, node);
127                 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
128                 return;
129             } catch(AssertionError e) {
130                 lastError = e;
131                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
132             }
133         }
134
135         throw lastError;
136     }
137
138     protected void verifyNodeRemoved(YangInstanceIdentifier path,
139             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
140         AssertionError lastError = null;
141         Stopwatch sw = Stopwatch.createStarted();
142         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
143             try {
144                 NormalizedNode<?, ?> node = reader.apply(path);
145                 Assert.assertNull("Node was not removed at path: " + path, node);
146                 return;
147             } catch(AssertionError e) {
148                 lastError = e;
149                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
150             }
151         }
152
153         throw lastError;
154     }
155
156     static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
157             throws DataValidationFailedException {
158         DataTreeModification modification = shardDataTree.newModification();
159         modification.merge(path, node);
160         commit(shardDataTree, modification);
161     }
162
163     static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
164             throws DataValidationFailedException {
165         DataTreeModification modification = shardDataTree.newModification();
166         modification.delete(path);
167         commit(shardDataTree, modification);
168     }
169
170     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
171             throws DataValidationFailedException {
172         shardDataTree.notifyListeners(shardDataTree.commit(modification));
173     }
174
175     static EntityOwnershipChange ownershipChange(final Entity expEntity, final boolean expWasOwner,
176             final boolean expIsOwner, final boolean expHasOwner) {
177         return Matchers.argThat(new ArgumentMatcher<EntityOwnershipChange>() {
178             @Override
179             public boolean matches(Object argument) {
180                 EntityOwnershipChange change = (EntityOwnershipChange) argument;
181                 return expEntity.equals(change.getEntity()) && expWasOwner == change.wasOwner() &&
182                         expIsOwner == change.isOwner() && expHasOwner == change.hasOwner();
183             }
184
185             @Override
186             public void describeTo(Description description) {
187                 description.appendValue(new EntityOwnershipChange(expEntity, expWasOwner, expIsOwner, expHasOwner));
188             }
189         });
190     }
191
192     static EntityOwnershipChange ownershipChange(final Entity expEntity) {
193         return Matchers.argThat(new ArgumentMatcher<EntityOwnershipChange>() {
194             @Override
195             public boolean matches(Object argument) {
196                 EntityOwnershipChange change = (EntityOwnershipChange) argument;
197                 return expEntity.equals(change.getEntity());
198             }
199
200             @Override
201             public void describeTo(Description description) {
202                 description.appendValue(new EntityOwnershipChange(expEntity, false, false, false));
203             }
204         });
205     }
206 }