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.mdsal.eos.common.api.EntityOwnershipChangeState;
33 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
34 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
37 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;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
53 * Abstract base class providing utility methods.
55 * @author Thomas Pantelis
57 public class AbstractEntityOwnershipTest extends AbstractActorTest {
58 protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
59 YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
61 assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
62 assertTrue(node instanceof ContainerNode);
64 ContainerNode entityOwnersNode = (ContainerNode) node;
66 MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
67 ENTITY_TYPE_QNAME, entityType, true);
69 MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
72 getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent);
73 } catch(AssertionError e) {
74 throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
78 protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
79 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader, boolean expectPresent) {
80 AssertionError lastError = null;
81 Stopwatch sw = Stopwatch.createStarted();
82 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
83 NormalizedNode<?, ?> node = reader.apply(ENTITY_OWNERS_PATH);
85 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
87 } catch (AssertionError e) {
89 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
96 protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
97 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
98 verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
101 protected MapEntryNode getMapEntryNodeChild(DataContainerNode<? extends PathArgument> parent, QName childMap,
102 QName child, Object key, boolean expectPresent) {
103 Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
104 parent.getChild(new NodeIdentifier(childMap));
105 assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
107 MapNode entityTypeMapNode = (MapNode) childNode.get();
108 Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
109 childMap, child, key));
110 if(expectPresent && !entityTypeEntry.isPresent()) {
111 fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
112 } else if(!expectPresent && entityTypeEntry.isPresent()) {
113 fail("Found unexpected " + childMap.toString() + " entry for " + key);
116 return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
119 static void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId,
120 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
121 AssertionError lastError = null;
122 YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
123 Stopwatch sw = Stopwatch.createStarted();
124 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
126 NormalizedNode<?, ?> node = reader.apply(entityPath);
127 Assert.assertNotNull("Owner was not set for entityId: " + entityId, node);
128 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
130 } catch(AssertionError e) {
132 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
139 protected void verifyNodeRemoved(YangInstanceIdentifier path,
140 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
141 AssertionError lastError = null;
142 Stopwatch sw = Stopwatch.createStarted();
143 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
145 NormalizedNode<?, ?> node = reader.apply(path);
146 Assert.assertNull("Node was not removed at path: " + path, node);
148 } catch(AssertionError e) {
150 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
157 static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
158 throws DataValidationFailedException {
159 DataTreeModification modification = shardDataTree.newModification();
160 modification.merge(path, node);
161 commit(shardDataTree, modification);
164 static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
165 throws DataValidationFailedException {
166 DataTreeModification modification = shardDataTree.newModification();
167 modification.delete(path);
168 commit(shardDataTree, modification);
171 static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
172 throws DataValidationFailedException {
173 shardDataTree.notifyListeners(shardDataTree.commit(modification));
176 static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner,
177 final boolean expIsOwner, final boolean expHasOwner) {
178 return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
180 public boolean matches(Object argument) {
181 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
182 return expEntity.equals(change.getEntity()) && expWasOwner == change.getState().wasOwner() &&
183 expIsOwner == change.getState().isOwner() && expHasOwner == change.getState().hasOwner();
187 public void describeTo(Description description) {
188 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
189 expWasOwner, expIsOwner, expHasOwner)));
194 static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) {
195 return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
197 public boolean matches(Object argument) {
198 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
199 return expEntity.equals(change.getEntity());
203 public void describeTo(Description description) {
204 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
205 false, false, false)));