525e03bb37b9047c7f73119116d4ee2223853f44
[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) {
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);
63
64             MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME, entityId);
65
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);
69         }
70     }
71
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);
78             try {
79                 verifyEntityCandidate(node, entityType, entityId, candidateName);
80                 return;
81             } catch (AssertionError e) {
82                 lastError = e;
83                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
84             }
85         }
86
87         throw lastError;
88     }
89
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());
95
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());
101         }
102         return entityTypeEntry.get();
103     }
104
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) {
111             try {
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());
115                 return;
116             } catch(AssertionError e) {
117                 lastError = e;
118                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
119             }
120         }
121
122         throw lastError;
123     }
124
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) {
130             try {
131                 NormalizedNode<?, ?> node = reader.apply(path);
132                 Assert.assertNull("Node was not removed at path: " + path, node);
133                 return;
134             } catch(AssertionError e) {
135                 lastError = e;
136                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
137             }
138         }
139
140         throw lastError;
141     }
142
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);
148     }
149
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);
155     }
156
157     static void commit(ShardDataTree shardDataTree, DataTreeModification modification)
158             throws DataValidationFailedException {
159         modification.ready();
160
161         shardDataTree.getDataTree().validate(modification);
162         DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
163         shardDataTree.getDataTree().commit(candidate);
164         shardDataTree.notifyListeners(candidate);
165     }
166 }

©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.