Bug 4105: Choose Owner for an Entity based on first come first served basis
[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_OWNER_QNAME;
17 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
20 import com.google.common.base.Function;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Stopwatch;
23 import com.google.common.util.concurrent.Uninterruptibles;
24 import java.util.concurrent.TimeUnit;
25 import org.junit.Assert;
26 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
27 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
30 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;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
45
46 /**
47  * Abstract base class providing utility methods.
48  *
49  * @author Thomas Pantelis
50  */
51 public class AbstractEntityOwnershipTest extends AbstractActorTest {
52     protected void verifyEntityCandidate(NormalizedNode<?, ?> node, String entityType,
53             YangInstanceIdentifier entityId, String candidateName) {
54         try {
55             assertNotNull("Missing " + EntityOwners.QNAME.toString(), node);
56             assertTrue(node instanceof ContainerNode);
57
58             ContainerNode entityOwnersNode = (ContainerNode) node;
59
60             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
61                     ENTITY_TYPE_QNAME, entityType);
62
63             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME, entityId);
64
65             getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
66         } catch(AssertionError e) {
67             throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e);
68         }
69     }
70
71     protected MapEntryNode getMapEntryNodeChild(DataContainerNode<? extends PathArgument> parent, QName childMap,
72             QName child, Object key) {
73         Optional<DataContainerChild<? extends PathArgument, ?>> childNode =
74                 parent.getChild(new NodeIdentifier(childMap));
75         assertEquals("Missing " + childMap.toString(), true, childNode.isPresent());
76
77         MapNode entityTypeMapNode = (MapNode) childNode.get();
78         Optional<MapEntryNode> entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates(
79                 childMap, child, key));
80         if(!entityTypeEntry.isPresent()) {
81             fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue());
82         }
83         return entityTypeEntry.get();
84     }
85
86     protected void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId,
87             Function<YangInstanceIdentifier,NormalizedNode<?,?>> reader) {
88         YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME);
89         Stopwatch sw = Stopwatch.createStarted();
90         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
91             NormalizedNode<?, ?> node = reader.apply(entityPath);
92             if(node != null) {
93                 Assert.assertEquals("Entity owner", expected, node.getValue().toString());
94                 return;
95             } else {
96                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
97             }
98         }
99
100         fail("Owner was not set for entityId: " + entityId);
101     }
102
103     static void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node, ShardDataTree shardDataTree)
104             throws DataValidationFailedException {
105         DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
106         modification.merge(path, node);
107         commit(shardDataTree, modification);
108     }
109
110     static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree)
111             throws DataValidationFailedException {
112         DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
113         modification.delete(path);
114         commit(shardDataTree, modification);
115     }
116
117     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
118             throws DataValidationFailedException {
119         modification.ready();
120
121         shardDataTree.getDataTree().validate(modification);
122         DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
123         shardDataTree.getDataTree().commit(candidate);
124         shardDataTree.notifyListeners(candidate);
125     }
126 }