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

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