Bump odlparent/yangtools/mdsal
[controller.git] / opendaylight / md-sal / sal-distributed-eos / src / test / java / org / opendaylight / controller / cluster / 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.entityownership;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.ArgumentMatchers.argThat;
16 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.CANDIDATE_NAME_QNAME;
17 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
18 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
19 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_OWNER_QNAME;
20 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_QNAME;
21 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME;
22 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.candidatePath;
23 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.entityPath;
24
25 import akka.pattern.Patterns;
26 import akka.testkit.TestActorRef;
27 import akka.util.Timeout;
28 import com.google.common.base.Stopwatch;
29 import com.google.common.util.concurrent.Uninterruptibles;
30 import java.util.Optional;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.function.Consumer;
34 import java.util.function.Function;
35 import org.opendaylight.controller.cluster.access.concepts.MemberName;
36 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
37 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
38 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
39 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
40 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
41 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
42 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
43 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
46 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;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
51 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
53 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
58 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
59 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import scala.concurrent.Await;
63 import scala.concurrent.Future;
64 import scala.concurrent.duration.FiniteDuration;
65
66 /**
67  * Abstract base class providing utility methods.
68  *
69  * @author Thomas Pantelis
70  */
71 public class AbstractEntityOwnershipTest extends AbstractActorTest {
72     protected final Logger testLog = LoggerFactory.getLogger(getClass());
73
74     private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger();
75
76     protected void verifyEntityCandidate(final NormalizedNode node, final String entityType,
77             final YangInstanceIdentifier entityId, final String candidateName, final boolean expectPresent) {
78         try {
79             assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
80             assertTrue(node instanceof ContainerNode);
81
82             ContainerNode entityOwnersNode = (ContainerNode) node;
83
84             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
85                     ENTITY_TYPE_QNAME, entityType, true);
86
87             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
88                     entityId, true);
89
90             getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent);
91         } catch (AssertionError e) {
92             throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
93         }
94     }
95
96     protected void verifyEntityCandidate(final String entityType, final YangInstanceIdentifier entityId,
97             final String candidateName, final Function<YangInstanceIdentifier,NormalizedNode> reader,
98             final boolean expectPresent) {
99         AssertionError lastError = null;
100         Stopwatch sw = Stopwatch.createStarted();
101         while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
102             NormalizedNode node = reader.apply(ENTITY_OWNERS_PATH);
103             try {
104                 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
105                 return;
106             } catch (AssertionError e) {
107                 lastError = e;
108                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
109             }
110         }
111
112         throw lastError;
113     }
114
115     protected void verifyEntityCandidate(final String entityType, final YangInstanceIdentifier entityId,
116             final String candidateName, final Function<YangInstanceIdentifier,NormalizedNode> reader) {
117         verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
118     }
119
120     protected MapEntryNode getMapEntryNodeChild(final DataContainerNode parent,
121             final QName childMap, final QName child, final Object key, final boolean expectPresent) {
122         Optional<DataContainerChild> childNode = parent.findChildByArg(new NodeIdentifier(childMap));
123         // We have to account for empty maps disappearing. If we expect the entry to be non-present, tolerate a missing
124         // map.
125         if (!expectPresent && !childNode.isPresent()) {
126             return null;
127         }
128
129         assertTrue("Missing " + childMap.toString(), childNode.isPresent());
130
131         MapNode entityTypeMapNode = (MapNode) childNode.get();
132         Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.findChildByArg(NodeIdentifierWithPredicates.of(
133                 childMap, child, key));
134         if (expectPresent && !entityTypeEntry.isPresent()) {
135             fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.body());
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                 assertNotNull("Owner was not set for entityId: " + entityId, node);
152                 assertEquals("Entity owner", expected, node.body().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                 assertNull("Node was not removed at path: " + path, reader.apply(path));
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(final YangInstanceIdentifier path, final NormalizedNode node,
193             final ShardDataTree shardDataTree) throws DataValidationFailedException {
194         DataTreeModification modification = shardDataTree.newModification();
195         modification.merge(path, node);
196         commit(shardDataTree, modification);
197     }
198
199     static void deleteNode(final YangInstanceIdentifier path, final ShardDataTree shardDataTree)
200             throws DataValidationFailedException {
201         DataTreeModification modification = shardDataTree.newModification();
202         modification.delete(path);
203         commit(shardDataTree, modification);
204     }
205
206     static void commit(final ShardDataTree shardDataTree, final 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 argThat(change -> expEntity.equals(change.getEntity()) && expWasOwner == change.getState().wasOwner()
223                 && expIsOwner == change.getState().isOwner() && expHasOwner == change.getState().hasOwner()
224                 && expInJeopardy == change.inJeopardy());
225     }
226
227     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) {
228         return argThat(change -> expEntity.equals(change.getEntity()));
229     }
230
231     @SuppressWarnings("checkstyle:IllegalCatch")
232     static void verifyNoOwnerSet(final TestActorRef<? extends EntityOwnershipShard> shard, final String entityType,
233             final YangInstanceIdentifier entityId) {
234         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
235         try {
236             NormalizedNode node = AbstractShardTest.readStore(shard, entityPath);
237             if (node != null) {
238                 fail("Owner " + node.body() + " was set for " + entityPath);
239             }
240
241         } catch (Exception e) {
242             throw new AssertionError("read failed", e);
243         }
244     }
245
246     static void verifyRaftState(final TestActorRef<? extends EntityOwnershipShard> shard,
247             final Consumer<OnDemandRaftState> verifier)
248             throws Exception {
249         AssertionError lastError = null;
250         Stopwatch sw = Stopwatch.createStarted();
251         while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
252             FiniteDuration operationDuration = FiniteDuration.create(5, TimeUnit.SECONDS);
253             Future<Object> future = Patterns.ask(shard, GetOnDemandRaftState.INSTANCE, new Timeout(operationDuration));
254             OnDemandRaftState raftState = (OnDemandRaftState)Await.result(future, operationDuration);
255             try {
256                 verifier.accept(raftState);
257                 return;
258             } catch (AssertionError e) {
259                 lastError = e;
260                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
261             }
262         }
263
264         throw lastError;
265     }
266
267     static ShardIdentifier newShardId(final String memberName) {
268         return ShardIdentifier.create("entity-ownership", MemberName.forName(memberName),
269             "operational" + NEXT_SHARD_NUM.getAndIncrement());
270     }
271
272     @SuppressWarnings("checkstyle:IllegalCatch")
273     void verifyEntityCandidateRemoved(final TestActorRef<EntityOwnershipShard> shard, final String entityType,
274             final YangInstanceIdentifier entityId, final String candidateName) {
275         verifyNodeRemoved(candidatePath(entityType, entityId, candidateName), path -> {
276             try {
277                 return AbstractShardTest.readStore(shard, path);
278             } catch (Exception e) {
279                 throw new AssertionError("Failed to read " + path, e);
280             }
281         });
282     }
283
284     @SuppressWarnings("checkstyle:IllegalCatch")
285     void verifyCommittedEntityCandidate(final TestActorRef<? extends EntityOwnershipShard> shard,
286             final String entityType, final YangInstanceIdentifier entityId, final String candidateName) {
287         verifyEntityCandidate(entityType, entityId, candidateName, path -> {
288             try {
289                 return AbstractShardTest.readStore(shard, path);
290             } catch (Exception e) {
291                 throw new AssertionError("Failed to read " + path, e);
292             }
293         });
294     }
295
296     @SuppressWarnings("checkstyle:IllegalCatch")
297     void verifyNoEntityCandidate(final TestActorRef<? extends EntityOwnershipShard> shard, final String entityType,
298             final YangInstanceIdentifier entityId, final String candidateName) {
299         verifyEntityCandidate(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         }, false);
306     }
307 }