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.junit.Assert;
27 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
28 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
31 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;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
45 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
48 * Abstract base class providing utility methods.
50 * @author Thomas Pantelis
52 public class AbstractEntityOwnershipTest extends AbstractActorTest {
53 protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
54 YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
56 assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
57 assertTrue(node instanceof ContainerNode);
59 ContainerNode entityOwnersNode = (ContainerNode) node;
61 MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
62 ENTITY_TYPE_QNAME, entityType, true);
64 MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
67 getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent);
68 } catch(AssertionError e) {
69 throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
73 protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
74 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader, boolean expectPresent) {
75 AssertionError lastError = null;
76 Stopwatch sw = Stopwatch.createStarted();
77 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
78 NormalizedNode<?, ?> node = reader.apply(ENTITY_OWNERS_PATH);
80 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
82 } catch (AssertionError e) {
84 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
91 protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
92 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
93 verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
96 protected MapEntryNode getMapEntryNodeChild(DataContainerNode<? extends PathArgument> parent, QName childMap,
97 QName child, Object key, boolean expectPresent) {
98 Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
99 parent.getChild(new NodeIdentifier(childMap));
100 assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
102 MapNode entityTypeMapNode = (MapNode) childNode.get();
103 Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
104 childMap, child, key));
105 if(expectPresent && !entityTypeEntry.isPresent()) {
106 fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
107 } else if(!expectPresent && entityTypeEntry.isPresent()) {
108 fail("Found unexpected " + childMap.toString() + " entry for " + key);
111 return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
114 static void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId,
115 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
116 AssertionError lastError = null;
117 YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
118 Stopwatch sw = Stopwatch.createStarted();
119 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
121 NormalizedNode<?, ?> node = reader.apply(entityPath);
122 Assert.assertNotNull("Owner was not set for entityId: " + entityId, node);
123 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
125 } catch(AssertionError e) {
127 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
134 protected void verifyNodeRemoved(YangInstanceIdentifier path,
135 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
136 AssertionError lastError = null;
137 Stopwatch sw = Stopwatch.createStarted();
138 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
140 NormalizedNode<?, ?> node = reader.apply(path);
141 Assert.assertNull("Node was not removed at path: " + path, node);
143 } catch(AssertionError e) {
145 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
152 static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
153 throws DataValidationFailedException {
154 DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
155 modification.merge(path, node);
156 commit(shardDataTree, modification);
159 static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
160 throws DataValidationFailedException {
161 DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
162 modification.delete(path);
163 commit(shardDataTree, modification);
166 static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
167 throws DataValidationFailedException {
168 modification.ready();
170 shardDataTree.getDataTree().validate(modification);
171 DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
172 shardDataTree.getDataTree().commit(candidate);
173 shardDataTree.notifyListeners(candidate);