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

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