ea7648efb9df918bd034f858f6d26eebd5a48afd
[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.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.DataTreeCandidateTip;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
51
52 /**
53  * Abstract base class providing utility methods.
54  *
55  * @author Thomas Pantelis
56  */
57 public class AbstractEntityOwnershipTest extends AbstractActorTest {
58     protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
59             YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) {
60         try {
61             assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
62             assertTrue(node instanceof ContainerNode);
63
64             ContainerNode entityOwnersNode = (ContainerNode) node;
65
66             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
67                     ENTITY_TYPE_QNAME, entityType, true);
68
69             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME,
70                     entityId, true);
71
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);
75         }
76     }
77
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);
84             try {
85                 verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent);
86                 return;
87             } catch (AssertionError e) {
88                 lastError = e;
89                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
90             }
91         }
92
93         throw lastError;
94     }
95
96     protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName,
97             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
98         verifyEntityCandidate(entityType, entityId, candidateName, reader, true);
99     }
100
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());
106
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);
114         }
115
116         return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null;
117     }
118
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) {
125             try {
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());
129                 return;
130             } catch(AssertionError e) {
131                 lastError = e;
132                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
133             }
134         }
135
136         throw lastError;
137     }
138
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) {
144             try {
145                 NormalizedNode<?, ?> node = reader.apply(path);
146                 Assert.assertNull("Node was not removed at path: " + path, node);
147                 return;
148             } catch(AssertionError e) {
149                 lastError = e;
150                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
151             }
152         }
153
154         throw lastError;
155     }
156
157     static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
158             throws DataValidationFailedException {
159         DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
160         modification.merge(path, node);
161         commit(shardDataTree, modification);
162     }
163
164     static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
165             throws DataValidationFailedException {
166         DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
167         modification.delete(path);
168         commit(shardDataTree, modification);
169     }
170
171     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
172             throws DataValidationFailedException {
173         modification.ready();
174
175         shardDataTree.getDataTree().validate(modification);
176         DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
177         shardDataTree.getDataTree().commit(candidate);
178         shardDataTree.notifyListeners(candidate);
179     }
180
181     static EntityOwnershipChange ownershipChange(final Entity expEntity, final boolean expWasOwner,
182             final boolean expIsOwner, final boolean expHasOwner) {
183         return Matchers.argThat(new ArgumentMatcher<EntityOwnershipChange>() {
184             @Override
185             public boolean matches(Object argument) {
186                 EntityOwnershipChange change = (EntityOwnershipChange) argument;
187                 return expEntity.equals(change.getEntity()) && expWasOwner == change.wasOwner() &&
188                         expIsOwner == change.isOwner() && expHasOwner == change.hasOwner();
189             }
190
191             @Override
192             public void describeTo(Description description) {
193                 description.appendValue(new EntityOwnershipChange(expEntity, expWasOwner, expIsOwner, expHasOwner));
194             }
195         });
196     }
197
198     static EntityOwnershipChange ownershipChange(final Entity expEntity) {
199         return Matchers.argThat(new ArgumentMatcher<EntityOwnershipChange>() {
200             @Override
201             public boolean matches(Object argument) {
202                 EntityOwnershipChange change = (EntityOwnershipChange) argument;
203                 return expEntity.equals(change.getEntity());
204             }
205
206             @Override
207             public void describeTo(Description description) {
208                 description.appendValue(new EntityOwnershipChange(expEntity, false, false, false));
209             }
210         });
211     }
212 }