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.datastore.entityownership;
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;
52 * Abstract base class providing utility methods.
54 * @author Thomas Pantelis
56 public class AbstractEntityOwnershipTest extends AbstractActorTest {
57 protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
58 YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
60 assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
61 assertTrue(node instanceof ContainerNode);
63 ContainerNode entityOwnersNode = (ContainerNode) node;
65 MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
66 ENTITY_TYPE_QNAME, entityType, true);
68 MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
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);
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);
84 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
86 } catch (AssertionError e) {
88 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
95 protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
96 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
97 verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
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());
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);
115 return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
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) {
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());
129 } catch(AssertionError e) {
131 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
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) {
144 NormalizedNode<?, ?> node = reader.apply(path);
145 Assert.assertNull("Node was not removed at path: " + path, node);
147 } catch(AssertionError e) {
149 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
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);
163 static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
164 throws DataValidationFailedException {
165 DataTreeModification modification = shardDataTree.newModification();
166 modification.delete(path);
167 commit(shardDataTree, modification);
170 static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
171 throws DataValidationFailedException {
172 shardDataTree.notifyListeners(shardDataTree.commit(modification));
175 static EntityOwnershipChange ownershipChange(final Entity expEntity, final boolean expWasOwner,
176 final boolean expIsOwner, final boolean expHasOwner) {
177 return Matchers.argThat(new ArgumentMatcher<EntityOwnershipChange>() {
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();
186 public void describeTo(Description description) {
187 description.appendValue(new EntityOwnershipChange(expEntity, expWasOwner, expIsOwner, expHasOwner));
192 static EntityOwnershipChange ownershipChange(final Entity expEntity) {
193 return Matchers.argThat(new ArgumentMatcher<EntityOwnershipChange>() {
195 public boolean matches(Object argument) {
196 EntityOwnershipChange change = (EntityOwnershipChange) argument;
197 return expEntity.equals(change.getEntity());
201 public void describeTo(Description description) {
202 description.appendValue(new EntityOwnershipChange(expEntity, false, false, false));