Remove deprecated ShardDataTree#commit method
[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.candidatePath;
21 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
22
23 import akka.pattern.Patterns;
24 import akka.testkit.TestActorRef;
25 import akka.util.Timeout;
26 import com.google.common.base.Function;
27 import com.google.common.base.Optional;
28 import com.google.common.base.Stopwatch;
29 import com.google.common.util.concurrent.Uninterruptibles;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicInteger;
32 import java.util.function.Consumer;
33 import org.hamcrest.Description;
34 import org.junit.Assert;
35 import org.mockito.ArgumentMatcher;
36 import org.mockito.Matchers;
37 import org.opendaylight.controller.cluster.access.concepts.MemberName;
38 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
39 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
40 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
41 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
42 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
43 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
44 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
45 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
46 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
49 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;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
55 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
57 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
62 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66 import scala.concurrent.Await;
67 import scala.concurrent.Future;
68 import scala.concurrent.duration.Duration;
69 import scala.concurrent.duration.FiniteDuration;
70
71 /**
72  * Abstract base class providing utility methods.
73  *
74  * @author Thomas Pantelis
75  */
76 public class AbstractEntityOwnershipTest extends AbstractActorTest {
77     protected final Logger testLog = LoggerFactory.getLogger(getClass());
78
79     private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger();
80
81     protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
82             YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
83         try {
84             assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
85             assertTrue(node instanceof ContainerNode);
86
87             ContainerNode entityOwnersNode = (ContainerNode) node;
88
89             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
90                     ENTITY_TYPE_QNAME, entityType, true);
91
92             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
93                     entityId, true);
94
95             getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent);
96         } catch (AssertionError e) {
97             throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
98         }
99     }
100
101     protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
102             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader, boolean expectPresent) {
103         AssertionError lastError = null;
104         Stopwatch sw = Stopwatch.createStarted();
105         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
106             NormalizedNode<?, ?> node = reader.apply(ENTITY_OWNERS_PATH);
107             try {
108                 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
109                 return;
110             } catch (AssertionError e) {
111                 lastError = e;
112                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
113             }
114         }
115
116         throw lastError;
117     }
118
119     protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
120             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
121         verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
122     }
123
124     protected MapEntryNode getMapEntryNodeChild(DataContainerNode<? extends PathArgument> parent, QName childMap,
125             QName child, Object key, boolean expectPresent) {
126         Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
127                 parent.getChild(new NodeIdentifier(childMap));
128         assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
129
130         MapNode entityTypeMapNode = (MapNode) childNode.get();
131         Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
132                 childMap, child, key));
133         if (expectPresent && !entityTypeEntry.isPresent()) {
134             fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
135         } else if (!expectPresent && entityTypeEntry.isPresent()) {
136             fail("Found unexpected " + childMap.toString() + " entry for " + key);
137         }
138
139         return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
140     }
141
142     static void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId,
143             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
144         AssertionError lastError = null;
145         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
146         Stopwatch sw = Stopwatch.createStarted();
147         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
148             try {
149                 NormalizedNode<?, ?> node = reader.apply(entityPath);
150                 Assert.assertNotNull("Owner was not set for entityId: " + entityId, node);
151                 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
152                 return;
153             } catch (AssertionError e) {
154                 lastError = e;
155                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
156             }
157         }
158
159         throw lastError;
160     }
161
162     @SuppressWarnings("checkstyle:IllegalCatch")
163     static void verifyOwner(final TestActorRef<? extends EntityOwnershipShard> shard, String entityType,
164             YangInstanceIdentifier entityId, String localMemberName) {
165         verifyOwner(localMemberName, entityType, entityId, path -> {
166             try {
167                 return AbstractShardTest.readStore(shard, path);
168             } catch (Exception e) {
169                 return null;
170             }
171         });
172     }
173
174     protected void verifyNodeRemoved(YangInstanceIdentifier path,
175             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
176         AssertionError lastError = null;
177         Stopwatch sw = Stopwatch.createStarted();
178         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
179             try {
180                 NormalizedNode<?, ?> node = reader.apply(path);
181                 Assert.assertNull("Node was not removed at path: " + path, node);
182                 return;
183             } catch (AssertionError e) {
184                 lastError = e;
185                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
186             }
187         }
188
189         throw lastError;
190     }
191
192     static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
193             throws DataValidationFailedException {
194         DataTreeModification modification = shardDataTree.newModification();
195         modification.merge(path, node);
196         commit(shardDataTree, modification);
197     }
198
199     static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
200             throws DataValidationFailedException {
201         DataTreeModification modification = shardDataTree.newModification();
202         modification.delete(path);
203         commit(shardDataTree, modification);
204     }
205
206     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
207             throws DataValidationFailedException {
208         modification.ready();
209         shardDataTree.getDataTree().validate(modification);
210         final DataTreeCandidate candidate = shardDataTree.getDataTree().prepare(modification);
211         shardDataTree.getDataTree().commit(candidate);
212         shardDataTree.notifyListeners(candidate);
213     }
214
215     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner,
216             final boolean expIsOwner, final boolean expHasOwner) {
217         return ownershipChange(expEntity, expWasOwner, expIsOwner, expHasOwner, false);
218     }
219
220     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner,
221             final boolean expIsOwner, final boolean expHasOwner, final boolean expInJeopardy) {
222         return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
223             @Override
224             public boolean matches(Object argument) {
225                 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
226                 return expEntity.equals(change.getEntity()) && expWasOwner == change.getState().wasOwner()
227                         && expIsOwner == change.getState().isOwner() && expHasOwner == change.getState().hasOwner()
228                         && expInJeopardy == change.inJeopardy();
229             }
230
231             @Override
232             public void describeTo(Description description) {
233                 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
234                         expWasOwner, expIsOwner, expHasOwner), expInJeopardy));
235             }
236         });
237     }
238
239     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) {
240         return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
241             @Override
242             public boolean matches(Object argument) {
243                 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
244                 return expEntity.equals(change.getEntity());
245             }
246
247             @Override
248             public void describeTo(Description description) {
249                 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
250                         false, false, false)));
251             }
252         });
253     }
254
255     @SuppressWarnings("checkstyle:IllegalCatch")
256     static void verifyNoOwnerSet(TestActorRef<? extends EntityOwnershipShard> shard, String entityType,
257             YangInstanceIdentifier entityId) {
258         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
259         try {
260             NormalizedNode<?, ?> node = AbstractShardTest.readStore(shard, entityPath);
261             if (node != null) {
262                 Assert.fail("Owner " + node.getValue() + " was set for " + entityPath);
263             }
264
265         } catch (Exception e) {
266             throw new AssertionError("read failed", e);
267         }
268     }
269
270     static void verifyRaftState(final TestActorRef<? extends EntityOwnershipShard> shard,
271             Consumer<OnDemandRaftState> verifier)
272             throws Exception {
273         AssertionError lastError = null;
274         Stopwatch sw = Stopwatch.createStarted();
275         while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
276             FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
277             Future<Object> future = Patterns.ask(shard, GetOnDemandRaftState.INSTANCE, new Timeout(operationDuration));
278             OnDemandRaftState raftState = (OnDemandRaftState)Await.result(future, operationDuration);
279             try {
280                 verifier.accept(raftState);
281                 return;
282             } catch (AssertionError e) {
283                 lastError = e;
284                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
285             }
286         }
287
288         throw lastError;
289     }
290
291     static ShardIdentifier newShardId(String memberName) {
292         return ShardIdentifier.create("entity-ownership", MemberName.forName(memberName),
293             "operational" + NEXT_SHARD_NUM.getAndIncrement());
294     }
295
296     @SuppressWarnings("checkstyle:IllegalCatch")
297     void verifyEntityCandidateRemoved(final TestActorRef<EntityOwnershipShard> shard, String entityType,
298             YangInstanceIdentifier entityId, String candidateName) {
299         verifyNodeRemoved(candidatePath(entityType, entityId, candidateName), path -> {
300             try {
301                 return AbstractShardTest.readStore(shard, path);
302             } catch (Exception e) {
303                 throw new AssertionError("Failed to read " + path, e);
304             }
305         });
306     }
307
308     @SuppressWarnings("checkstyle:IllegalCatch")
309     void verifyCommittedEntityCandidate(final TestActorRef<? extends EntityOwnershipShard> shard, String entityType,
310             YangInstanceIdentifier entityId, String candidateName) {
311         verifyEntityCandidate(entityType, entityId, candidateName, path -> {
312             try {
313                 return AbstractShardTest.readStore(shard, path);
314             } catch (Exception e) {
315                 throw new AssertionError("Failed to read " + path, e);
316             }
317         });
318     }
319
320     @SuppressWarnings("checkstyle:IllegalCatch")
321     void verifyNoEntityCandidate(final TestActorRef<? extends EntityOwnershipShard> shard, String entityType,
322             YangInstanceIdentifier entityId, String candidateName) {
323         verifyEntityCandidate(entityType, entityId, candidateName, path -> {
324             try {
325                 return AbstractShardTest.readStore(shard, path);
326             } catch (Exception e) {
327                 throw new AssertionError("Failed to read " + path, e);
328             }
329         }, false);
330     }
331 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.