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