2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.entityownership;
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;
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;
67 * Abstract base class providing utility methods.
69 * @author Thomas Pantelis
71 public class AbstractEntityOwnershipTest extends AbstractActorTest {
72 protected final Logger testLog = LoggerFactory.getLogger(getClass());
74 private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger();
76 protected void verifyEntityCandidate(final NormalizedNode node, final String entityType,
77 final YangInstanceIdentifier entityId, final String candidateName, final boolean expectPresent) {
79 assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
80 assertTrue(node instanceof ContainerNode);
82 ContainerNode entityOwnersNode = (ContainerNode) node;
84 MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
85 ENTITY_TYPE_QNAME, entityType, true);
87 MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
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);
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);
104 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
106 } catch (AssertionError e) {
108 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
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);
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
125 if (!expectPresent && !childNode.isPresent()) {
129 assertTrue("Missing " + childMap.toString(), childNode.isPresent());
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);
140 return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
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) {
150 NormalizedNode node = reader.apply(entityPath);
151 assertNotNull("Owner was not set for entityId: " + entityId, node);
152 assertEquals("Entity owner", expected, node.body().toString());
154 } catch (AssertionError e) {
156 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
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 -> {
168 return AbstractShardTest.readStore(shard, path);
169 } catch (Exception e) {
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) {
181 assertNull("Node was not removed at path: " + path, reader.apply(path));
183 } catch (AssertionError e) {
185 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
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);
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);
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);
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);
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());
227 static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) {
228 return argThat(change -> expEntity.equals(change.getEntity()));
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);
236 NormalizedNode node = AbstractShardTest.readStore(shard, entityPath);
238 fail("Owner " + node.body() + " was set for " + entityPath);
241 } catch (Exception e) {
242 throw new AssertionError("read failed", e);
246 static void verifyRaftState(final TestActorRef<? extends EntityOwnershipShard> shard,
247 final Consumer<OnDemandRaftState> verifier)
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);
256 verifier.accept(raftState);
258 } catch (AssertionError e) {
260 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
267 static ShardIdentifier newShardId(final String memberName) {
268 return ShardIdentifier.create("entity-ownership", MemberName.forName(memberName),
269 "operational" + NEXT_SHARD_NUM.getAndIncrement());
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 -> {
277 return AbstractShardTest.readStore(shard, path);
278 } catch (Exception e) {
279 throw new AssertionError("Failed to read " + path, e);
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 -> {
289 return AbstractShardTest.readStore(shard, path);
290 } catch (Exception e) {
291 throw new AssertionError("Failed to read " + path, e);
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 -> {
301 return AbstractShardTest.readStore(shard, path);
302 } catch (Exception e) {
303 throw new AssertionError("Failed to read " + path, e);