Bug 4105: Remove candidates on PeerDown
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / AbstractEntityOwnershipTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.entityownership;
9
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;
46
47 /**
48  * Abstract base class providing utility methods.
49  *
50  * @author Thomas Pantelis
51  */
52 public class AbstractEntityOwnershipTest extends AbstractActorTest {
53     protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
54             YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
55         try {
56             assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
57             assertTrue(node instanceof ContainerNode);
58
59             ContainerNode entityOwnersNode = (ContainerNode) node;
60
61             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
62                     ENTITY_TYPE_QNAME, entityType, true);
63
64             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
65                     entityId, true);
66
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);
70         }
71     }
72
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);
79             try {
80                 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
81                 return;
82             } catch (AssertionError e) {
83                 lastError = e;
84                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
85             }
86         }
87
88         throw lastError;
89     }
90
91     protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
92             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
93         verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
94     }
95
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());
101
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);
109         }
110
111         return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
112     }
113
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) {
120             try {
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());
124                 return;
125             } catch(AssertionError e) {
126                 lastError = e;
127                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
128             }
129         }
130
131         throw lastError;
132     }
133
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) {
139             try {
140                 NormalizedNode<?, ?> node = reader.apply(path);
141                 Assert.assertNull("Node was not removed at path: " + path, node);
142                 return;
143             } catch(AssertionError e) {
144                 lastError = e;
145                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
146             }
147         }
148
149         throw lastError;
150     }
151
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);
157     }
158
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);
164     }
165
166     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
167             throws DataValidationFailedException {
168         modification.ready();
169
170         shardDataTree.getDataTree().validate(modification);
171         DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
172         shardDataTree.getDataTree().commit(candidate);
173         shardDataTree.notifyListeners(candidate);
174     }
175 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.