Adjust to yangtools-2.0.0 changes
[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.Stopwatch;
28 import com.google.common.util.concurrent.Uninterruptibles;
29 import java.util.Optional;
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(final NormalizedNode<?, ?> node, final String entityType,
82             final YangInstanceIdentifier entityId, final String candidateName, final 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(final String entityType, final YangInstanceIdentifier entityId,
102             final String candidateName, final Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader,
103             final boolean expectPresent) {
104         AssertionError lastError = null;
105         Stopwatch sw = Stopwatch.createStarted();
106         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
107             NormalizedNode<?, ?> node = reader.apply(ENTITY_OWNERS_PATH);
108             try {
109                 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
110                 return;
111             } catch (AssertionError e) {
112                 lastError = e;
113                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
114             }
115         }
116
117         throw lastError;
118     }
119
120     protected void verifyEntityCandidate(final String entityType, final YangInstanceIdentifier entityId,
121             final String candidateName, final Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
122         verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
123     }
124
125     protected MapEntryNode getMapEntryNodeChild(final DataContainerNode<? extends PathArgument> parent,
126             final QName childMap, final QName child, final Object key, final boolean expectPresent) {
127         Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
128                 parent.getChild(new NodeIdentifier(childMap));
129         assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
130
131         MapNode entityTypeMapNode = (MapNode) childNode.get();
132         Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
133                 childMap, child, key));
134         if (expectPresent && !entityTypeEntry.isPresent()) {
135             fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
136         } else if (!expectPresent && entityTypeEntry.isPresent()) {
137             fail("Found unexpected " + childMap.toString() + " entry for " + key);
138         }
139
140         return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
141     }
142
143     static void verifyOwner(final String expected, final String entityType, final YangInstanceIdentifier entityId,
144             final Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
145         AssertionError lastError = null;
146         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
147         Stopwatch sw = Stopwatch.createStarted();
148         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
149             try {
150                 NormalizedNode<?, ?> node = reader.apply(entityPath);
151                 Assert.assertNotNull("Owner was not set for entityId: " + entityId, node);
152                 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
153                 return;
154             } catch (AssertionError e) {
155                 lastError = e;
156                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
157             }
158         }
159
160         throw lastError;
161     }
162
163     @SuppressWarnings("checkstyle:IllegalCatch")
164     static void verifyOwner(final TestActorRef<? extends EntityOwnershipShard> shard, final String entityType,
165             final YangInstanceIdentifier entityId, final String localMemberName) {
166         verifyOwner(localMemberName, entityType, entityId, path -> {
167             try {
168                 return AbstractShardTest.readStore(shard, path);
169             } catch (Exception e) {
170                 return null;
171             }
172         });
173     }
174
175     protected void verifyNodeRemoved(final YangInstanceIdentifier path,
176             final Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
177         AssertionError lastError = null;
178         Stopwatch sw = Stopwatch.createStarted();
179         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
180             try {
181                 NormalizedNode<?, ?> node = reader.apply(path);
182                 Assert.assertNull("Node was not removed at path: " + path, node);
183                 return;
184             } catch (AssertionError e) {
185                 lastError = e;
186                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
187             }
188         }
189
190         throw lastError;
191     }
192
193     static void writeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node,
194             final ShardDataTree shardDataTree) throws DataValidationFailedException {
195         DataTreeModification modification = shardDataTree.newModification();
196         modification.merge(path, node);
197         commit(shardDataTree, modification);
198     }
199
200     static void deleteNode(final YangInstanceIdentifier path, final ShardDataTree shardDataTree)
201             throws DataValidationFailedException {
202         DataTreeModification modification = shardDataTree.newModification();
203         modification.delete(path);
204         commit(shardDataTree, modification);
205     }
206
207     static void commit(final ShardDataTree shardDataTree, final DataTreeModification modification)
208             throws DataValidationFailedException {
209         modification.ready();
210         shardDataTree.getDataTree().validate(modification);
211         final DataTreeCandidate candidate = shardDataTree.getDataTree().prepare(modification);
212         shardDataTree.getDataTree().commit(candidate);
213         shardDataTree.notifyListeners(candidate);
214     }
215
216     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner,
217             final boolean expIsOwner, final boolean expHasOwner) {
218         return ownershipChange(expEntity, expWasOwner, expIsOwner, expHasOwner, false);
219     }
220
221     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner,
222             final boolean expIsOwner, final boolean expHasOwner, final boolean expInJeopardy) {
223         return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
224             @Override
225             public boolean matches(final Object argument) {
226                 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
227                 return expEntity.equals(change.getEntity()) && expWasOwner == change.getState().wasOwner()
228                         && expIsOwner == change.getState().isOwner() && expHasOwner == change.getState().hasOwner()
229                         && expInJeopardy == change.inJeopardy();
230             }
231
232             @Override
233             public void describeTo(final Description description) {
234                 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
235                         expWasOwner, expIsOwner, expHasOwner), expInJeopardy));
236             }
237         });
238     }
239
240     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) {
241         return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
242             @Override
243             public boolean matches(final Object argument) {
244                 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
245                 return expEntity.equals(change.getEntity());
246             }
247
248             @Override
249             public void describeTo(final Description description) {
250                 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
251                         false, false, false)));
252             }
253         });
254     }
255
256     @SuppressWarnings("checkstyle:IllegalCatch")
257     static void verifyNoOwnerSet(final TestActorRef<? extends EntityOwnershipShard> shard, final String entityType,
258             final YangInstanceIdentifier entityId) {
259         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
260         try {
261             NormalizedNode<?, ?> node = AbstractShardTest.readStore(shard, entityPath);
262             if (node != null) {
263                 Assert.fail("Owner " + node.getValue() + " was set for " + entityPath);
264             }
265
266         } catch (Exception e) {
267             throw new AssertionError("read failed", e);
268         }
269     }
270
271     static void verifyRaftState(final TestActorRef<? extends EntityOwnershipShard> shard,
272             final Consumer<OnDemandRaftState> verifier)
273             throws Exception {
274         AssertionError lastError = null;
275         Stopwatch sw = Stopwatch.createStarted();
276         while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
277             FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
278             Future<Object> future = Patterns.ask(shard, GetOnDemandRaftState.INSTANCE, new Timeout(operationDuration));
279             OnDemandRaftState raftState = (OnDemandRaftState)Await.result(future, operationDuration);
280             try {
281                 verifier.accept(raftState);
282                 return;
283             } catch (AssertionError e) {
284                 lastError = e;
285                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
286             }
287         }
288
289         throw lastError;
290     }
291
292     static ShardIdentifier newShardId(final String memberName) {
293         return ShardIdentifier.create("entity-ownership", MemberName.forName(memberName),
294             "operational" + NEXT_SHARD_NUM.getAndIncrement());
295     }
296
297     @SuppressWarnings("checkstyle:IllegalCatch")
298     void verifyEntityCandidateRemoved(final TestActorRef<EntityOwnershipShard> shard, final String entityType,
299             final YangInstanceIdentifier entityId, final String candidateName) {
300         verifyNodeRemoved(candidatePath(entityType, entityId, candidateName), path -> {
301             try {
302                 return AbstractShardTest.readStore(shard, path);
303             } catch (Exception e) {
304                 throw new AssertionError("Failed to read " + path, e);
305             }
306         });
307     }
308
309     @SuppressWarnings("checkstyle:IllegalCatch")
310     void verifyCommittedEntityCandidate(final TestActorRef<? extends EntityOwnershipShard> shard,
311             final String entityType, final YangInstanceIdentifier entityId, final String candidateName) {
312         verifyEntityCandidate(entityType, entityId, candidateName, path -> {
313             try {
314                 return AbstractShardTest.readStore(shard, path);
315             } catch (Exception e) {
316                 throw new AssertionError("Failed to read " + path, e);
317             }
318         });
319     }
320
321     @SuppressWarnings("checkstyle:IllegalCatch")
322     void verifyNoEntityCandidate(final TestActorRef<? extends EntityOwnershipShard> shard, final String entityType,
323             final YangInstanceIdentifier entityId, final String candidateName) {
324         verifyEntityCandidate(entityType, entityId, candidateName, path -> {
325             try {
326                 return AbstractShardTest.readStore(shard, path);
327             } catch (Exception e) {
328                 throw new AssertionError("Failed to read " + path, e);
329             }
330         }, false);
331     }
332 }

©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.