Make private methods static
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipServiceTest.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.assertSame;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
17 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
20 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry;
21 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
22 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry;
23 import akka.actor.ActorRef;
24 import akka.actor.PoisonPill;
25 import akka.actor.Props;
26 import com.google.common.base.Function;
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.Uninterruptibles;
29 import java.util.Collections;
30 import java.util.Map;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
38 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
39 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
40 import org.opendaylight.controller.cluster.datastore.config.Configuration;
41 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
42 import org.opendaylight.controller.cluster.datastore.config.ModuleConfig;
43 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
44 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
45 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
46 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
47 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
48 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
49 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
50 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
51 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
52 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
53 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
54 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
55 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
56 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
57 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
62 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import scala.concurrent.Await;
65 import scala.concurrent.Future;
66 import scala.concurrent.duration.Duration;
67
68 /**
69  * Unit tests for DistributedEntityOwnershipService.
70  *
71  * @author Thomas Pantelis
72  */
73 public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
74     static String ENTITY_TYPE = "test";
75     static String ENTITY_TYPE2 = "test2";
76     static int ID_COUNTER = 1;
77     static final QName QNAME = QName.create("test", "2015-08-11", "foo");
78
79     private final String dataStoreType = "config" + ID_COUNTER++;
80     private DistributedDataStore dataStore;
81
82     @Before
83     public void setUp() {
84         DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
85                 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
86
87         Configuration configuration = new ConfigurationImpl(new ModuleShardConfigProvider() {
88             @Override
89             public Map<String, ModuleConfig> retrieveModuleConfigs(Configuration configuration) {
90                 return Collections.emptyMap();
91             }
92         });
93
94         dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext );
95
96         dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
97     }
98
99     @After
100     public void tearDown() {
101         dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
102     }
103
104     @Test
105     public void testEntityOwnershipShardCreated() throws Exception {
106         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore);
107         service.start();
108
109         Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
110                 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
111         ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
112         assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
113
114         service.close();
115     }
116
117     @Test
118     public void testRegisterCandidate() throws Exception {
119         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
120         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
121             @Override
122             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
123                 return shardPropsCreator;
124             }
125         };
126
127         service.start();
128
129         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
130
131         YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
132         Entity entity = new Entity(ENTITY_TYPE, entityId);
133
134         EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
135
136         verifyEntityOwnershipCandidateRegistration(entity, reg);
137         verifyRegisterCandidateLocal(shardPropsCreator, entity);
138         verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
139                 dataStore.getActorContext().getCurrentMemberName());
140
141         // Register the same entity - should throw exception
142
143         try {
144             service.registerCandidate(entity);
145             fail("Expected CandidateAlreadyRegisteredException");
146         } catch(CandidateAlreadyRegisteredException e) {
147             // expected
148             assertEquals("getEntity", entity, e.getEntity());
149         }
150
151         // Register a different entity - should succeed
152
153         Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
154         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
155
156         EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
157
158         verifyEntityOwnershipCandidateRegistration(entity2, reg2);
159         verifyRegisterCandidateLocal(shardPropsCreator, entity2);
160         verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
161                 dataStore.getActorContext().getCurrentMemberName());
162
163         service.close();
164     }
165
166     @Test
167     public void testCloseCandidateRegistration() throws Exception {
168         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
169         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
170             @Override
171             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
172                 return shardPropsCreator;
173             }
174         };
175
176         service.start();
177
178         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
179
180         Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
181
182         EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
183
184         verifyEntityOwnershipCandidateRegistration(entity, reg);
185         verifyRegisterCandidateLocal(shardPropsCreator, entity);
186
187         shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
188
189         reg.close();
190
191         UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
192         assertEquals("getEntity", entity, unregCandidate.getEntity());
193
194         // Re-register - should succeed.
195
196         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
197
198         service.registerCandidate(entity);
199
200         verifyRegisterCandidateLocal(shardPropsCreator, entity);
201
202         service.close();
203     }
204
205     @Test
206     public void testListenerRegistration() {
207         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
208         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
209             @Override
210             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
211                 return shardPropsCreator;
212             }
213         };
214
215         service.start();
216
217         shardPropsCreator.expectShardMessage(RegisterListenerLocal.class);
218
219         YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
220         Entity entity = new Entity(ENTITY_TYPE, entityId);
221         EntityOwnershipListener listener = mock(EntityOwnershipListener.class);
222
223         EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
224
225         assertNotNull("EntityOwnershipListenerRegistration null", reg);
226         assertEquals("getEntityType", entity.getType(), reg.getEntityType());
227         assertEquals("getInstance", listener, reg.getInstance());
228
229         RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage();
230         assertSame("getListener", listener, regListener.getListener());
231         assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
232
233         shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class);
234
235         reg.close();
236
237         UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage();
238         assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
239         assertSame("getListener", listener, unregListener.getListener());
240
241         service.close();
242     }
243
244     @Test
245     public void testGetOwnershipState() throws Exception {
246         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
247         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
248             @Override
249             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
250                 return shardPropsCreator;
251             }
252         };
253
254         service.start();
255
256         ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
257         shardPropsCreator.setDataTree(shardDataTree.getDataTree());
258
259         Entity entity1 = new Entity(ENTITY_TYPE, "one");
260         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
261                 entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
262         verifyGetOwnershipState(service, entity1, true, true);
263
264         writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
265                 shardDataTree);
266         verifyGetOwnershipState(service, entity1, false, true);
267
268         writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""),
269                 shardDataTree);
270         verifyGetOwnershipState(service, entity1, false, false);
271
272         Entity entity2 = new Entity(ENTITY_TYPE, "two");
273         Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
274         assertEquals("getOwnershipState present", false, state.isPresent());
275
276         writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
277                 ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
278         verifyGetOwnershipState(service, entity2, false, false);
279
280         service.close();
281     }
282
283     private static void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
284             boolean isOwner, boolean hasOwner) {
285         Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
286         assertEquals("getOwnershipState present", true, state.isPresent());
287         assertEquals("isOwner", isOwner, state.get().isOwner());
288         assertEquals("hasOwner", hasOwner, state.get().hasOwner());
289     }
290
291     private void verifyEntityCandidate(ActorRef entityOwnershipShard, String entityType,
292             YangInstanceIdentifier entityId, String candidateName) {
293         verifyEntityCandidate(entityType, entityId, candidateName,
294                 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
295                     @Override
296                     public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
297                         try {
298                             return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
299                         } catch (Exception e) {
300                             return null;
301                         }
302                     }
303                 });
304     }
305
306     private static void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity) {
307         RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
308         assertEquals("getEntity", entity, regCandidate.getEntity());
309     }
310
311     private static void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
312         assertNotNull("EntityOwnershipCandidateRegistration null", reg);
313         assertEquals("getInstance", entity, reg.getInstance());
314     }
315
316     static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
317         TestShardPropsCreator() {
318             super("member-1");
319         }
320
321         private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
322         private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
323         private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
324         private final AtomicReference<DataTree> dataTree = new AtomicReference<>();
325
326         @Override
327         public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
328                 DatastoreContext datastoreContext, SchemaContext schemaContext) {
329             return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
330                     schemaContext, "member-1", messageClass, messageReceived, receivedMessage, dataTree);
331         }
332
333         @SuppressWarnings("unchecked")
334         <T> T waitForShardMessage() {
335             assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
336                     Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
337             assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
338             return (T) receivedMessage.get();
339         }
340
341         void expectShardMessage(Class<?> ofType) {
342             messageReceived.set(new CountDownLatch(1));
343             receivedMessage.set(null);
344             messageClass.set(ofType);
345         }
346
347         void setDataTree(DataTree tree) {
348             this.dataTree.set(tree);
349         }
350     }
351
352     static class TestEntityOwnershipShard extends EntityOwnershipShard {
353         private final AtomicReference<CountDownLatch> messageReceived;
354         private final AtomicReference<Object> receivedMessage;
355         private final AtomicReference<Class<?>> messageClass;
356         private final AtomicReference<DataTree> dataTree;
357
358         protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
359                 DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
360                 AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
361                 AtomicReference<Object> receivedMessage, AtomicReference<DataTree> dataTree) {
362             super(name, peerAddresses, datastoreContext, schemaContext, localMemberName);
363             this.messageClass = messageClass;
364             this.messageReceived = messageReceived;
365             this.receivedMessage = receivedMessage;
366             this.dataTree = dataTree;
367         }
368
369         @Override
370         public void onReceiveCommand(final Object message) throws Exception {
371             try {
372                 if(dataTree.get() != null && message instanceof GetShardDataTree) {
373                     sender().tell(dataTree.get(), self());
374                 } else {
375                     super.onReceiveCommand(message);
376                 }
377             } finally {
378                 Class<?> expMsgClass = messageClass.get();
379                 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
380                     receivedMessage.set(message);
381                     messageReceived.get().countDown();
382                 }
383             }
384         }
385     }
386 }

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