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) {
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);
64 MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME, entityId);
66 getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
67 } catch(AssertionError e) {
68 throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
72 protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
73 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
74 AssertionError lastError = null;
75 Stopwatch sw = Stopwatch.createStarted();
76 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
77 NormalizedNode<?, ?> node = reader.apply(ENTITY_OWNERS_PATH);
79 verifyEntityCandidate(node, entityType, entityId, candidateName);
81 } catch (AssertionError e) {
83 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
90 protected MapEntryNode getMapEntryNodeChild(DataContainerNode<? extends PathArgument> parent, QName childMap,
91 QName child, Object key) {
92 Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
93 parent.getChild(new NodeIdentifier(childMap));
94 assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
96 MapNode entityTypeMapNode = (MapNode) childNode.get();
97 Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
98 childMap, child, key));
99 if(!entityTypeEntry.isPresent()) {
100 fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
102 return entityTypeEntry.get();
105 protected void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId,
106 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
107 AssertionError lastError = null;
108 YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
109 Stopwatch sw = Stopwatch.createStarted();
110 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
112 NormalizedNode<?, ?> node = reader.apply(entityPath);
113 Assert.assertNotNull("Owner was not set for entityId: " + entityId, node);
114 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
116 } catch(AssertionError e) {
118 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
125 protected void verifyNodeRemoved(YangInstanceIdentifier path,
126 Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
127 AssertionError lastError = null;
128 Stopwatch sw = Stopwatch.createStarted();
129 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
131 NormalizedNode<?, ?> node = reader.apply(path);
132 Assert.assertNull("Node was not removed at path: " + path, node);
134 } catch(AssertionError e) {
136 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
143 static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
144 throws DataValidationFailedException {
145 DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
146 modification.merge(path, node);
147 commit(shardDataTree, modification);
150 static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
151 throws DataValidationFailedException {
152 DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
153 modification.delete(path);
154 commit(shardDataTree, modification);
157 static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
158 throws DataValidationFailedException {
159 modification.ready();
161 shardDataTree.getDataTree().validate(modification);
162 DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
163 shardDataTree.getDataTree().commit(candidate);
164 shardDataTree.notifyListeners(candidate);