f93ac28eeb6e0dae031461f370ca84df1c93e18f
[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.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;
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.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.newModification();
167         modification.delete(path);
168         commit(shardDataTree, modification);
169     }
170
171     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
172             throws DataValidationFailedException {
173         shardDataTree.notifyListeners(shardDataTree.commit(modification));
174     }
175
176     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner,
177             final boolean expIsOwner, final boolean expHasOwner) {
178         return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
179             @Override
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();
184             }
185
186             @Override
187             public void describeTo(Description description) {
188                 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
189                         expWasOwner, expIsOwner, expHasOwner)));
190             }
191         });
192     }
193
194     static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) {
195         return Matchers.argThat(new ArgumentMatcher<DOMEntityOwnershipChange>() {
196             @Override
197             public boolean matches(Object argument) {
198                 DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument;
199                 return expEntity.equals(change.getEntity());
200             }
201
202             @Override
203             public void describeTo(Description description) {
204                 description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from(
205                         false, false, false)));
206             }
207         });
208     }
209 }