BUG 4615 : Add method on EOS to check if a candidate is registered locally
[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.opendaylight.controller.cluster.datastore.DatastoreContext;
39 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
40 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
41 import org.opendaylight.controller.cluster.datastore.config.Configuration;
42 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
43 import org.opendaylight.controller.cluster.datastore.config.ModuleConfig;
44 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
45 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
46 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
47 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
48 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
49 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
50 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
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 org.opendaylight.yangtools.yang.model.api.SchemaContext;
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         dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext );
97
98         dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
99     }
100
101     @After
102     public void tearDown() {
103         dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
104     }
105
106     @Test
107     public void testEntityOwnershipShardCreated() throws Exception {
108         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build());
109         service.start();
110
111         Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
112                 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
113         ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
114         assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
115
116         service.close();
117     }
118
119     @Test
120     public void testRegisterCandidate() throws Exception {
121         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
122         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
123             @Override
124             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
125                 return shardPropsCreator;
126             }
127         };
128
129         service.start();
130
131         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
132
133         YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
134         Entity entity = new Entity(ENTITY_TYPE, entityId);
135
136         EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
137
138         verifyEntityOwnershipCandidateRegistration(entity, reg);
139         verifyRegisterCandidateLocal(shardPropsCreator, entity);
140         verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
141                 dataStore.getActorContext().getCurrentMemberName());
142
143         // Register the same entity - should throw exception
144
145         try {
146             service.registerCandidate(entity);
147             fail("Expected CandidateAlreadyRegisteredException");
148         } catch(CandidateAlreadyRegisteredException e) {
149             // expected
150             assertEquals("getEntity", entity, e.getEntity());
151         }
152
153         // Register a different entity - should succeed
154
155         Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
156         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
157
158         EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
159
160         verifyEntityOwnershipCandidateRegistration(entity2, reg2);
161         verifyRegisterCandidateLocal(shardPropsCreator, entity2);
162         verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
163                 dataStore.getActorContext().getCurrentMemberName());
164
165         service.close();
166     }
167
168     @Test
169     public void testCloseCandidateRegistration() throws Exception {
170         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
171         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
172             @Override
173             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
174                 return shardPropsCreator;
175             }
176         };
177
178         service.start();
179
180         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
181
182         Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
183
184         EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
185
186         verifyEntityOwnershipCandidateRegistration(entity, reg);
187         verifyRegisterCandidateLocal(shardPropsCreator, entity);
188
189         shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
190
191         reg.close();
192
193         UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
194         assertEquals("getEntity", entity, unregCandidate.getEntity());
195
196         // Re-register - should succeed.
197
198         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
199
200         service.registerCandidate(entity);
201
202         verifyRegisterCandidateLocal(shardPropsCreator, entity);
203
204         service.close();
205     }
206
207     @Test
208     public void testListenerRegistration() {
209         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
210         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
211             @Override
212             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
213                 return shardPropsCreator;
214             }
215         };
216
217         service.start();
218
219         shardPropsCreator.expectShardMessage(RegisterListenerLocal.class);
220
221         YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
222         Entity entity = new Entity(ENTITY_TYPE, entityId);
223         EntityOwnershipListener listener = mock(EntityOwnershipListener.class);
224
225         EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
226
227         assertNotNull("EntityOwnershipListenerRegistration null", reg);
228         assertEquals("getEntityType", entity.getType(), reg.getEntityType());
229         assertEquals("getInstance", listener, reg.getInstance());
230
231         RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage();
232         assertSame("getListener", listener, regListener.getListener());
233         assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
234
235         shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class);
236
237         reg.close();
238
239         UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage();
240         assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
241         assertSame("getListener", listener, unregListener.getListener());
242
243         service.close();
244     }
245
246     @Test
247     public void testGetOwnershipState() throws Exception {
248         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
249         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
250             @Override
251             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
252                 return shardPropsCreator;
253             }
254         };
255
256         service.start();
257
258         ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
259         shardPropsCreator.setDataTree(shardDataTree.getDataTree());
260
261         Entity entity1 = new Entity(ENTITY_TYPE, "one");
262         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
263                 entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
264         verifyGetOwnershipState(service, entity1, true, true);
265
266         writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
267                 shardDataTree);
268         verifyGetOwnershipState(service, entity1, false, true);
269
270         writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""),
271                 shardDataTree);
272         verifyGetOwnershipState(service, entity1, false, false);
273
274         Entity entity2 = new Entity(ENTITY_TYPE, "two");
275         Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
276         assertEquals("getOwnershipState present", false, state.isPresent());
277
278         writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
279                 ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
280         verifyGetOwnershipState(service, entity2, false, false);
281
282         service.close();
283     }
284
285     @Test
286     public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException {
287         final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
288         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
289             @Override
290             protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
291                 return shardPropsCreator;
292             }
293         };
294
295         service.start();
296
297         final Entity test = new Entity("test-type", "test");
298
299         assertFalse(service.isCandidateRegistered(test));
300
301         service.registerCandidate(test);
302
303         assertTrue(service.isCandidateRegistered(test));
304     }
305
306     private void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
307             boolean isOwner, boolean hasOwner) {
308         Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
309         assertEquals("getOwnershipState present", true, state.isPresent());
310         assertEquals("isOwner", isOwner, state.get().isOwner());
311         assertEquals("hasOwner", hasOwner, state.get().hasOwner());
312     }
313
314     private void verifyEntityCandidate(ActorRef entityOwnershipShard, String entityType,
315             YangInstanceIdentifier entityId, String candidateName) {
316         verifyEntityCandidate(entityType, entityId, candidateName,
317                 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
318                     @Override
319                     public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
320                         try {
321                             return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
322                         } catch (Exception e) {
323                             return null;
324                         }
325                     }
326                 });
327     }
328
329     private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity) {
330         RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
331         assertEquals("getEntity", entity, regCandidate.getEntity());
332     }
333
334     private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
335         assertNotNull("EntityOwnershipCandidateRegistration null", reg);
336         assertEquals("getInstance", entity, reg.getInstance());
337     }
338
339     static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
340         TestShardPropsCreator() {
341             super("member-1", EntityOwnerSelectionStrategyConfig.newBuilder().build());
342         }
343
344         private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
345         private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
346         private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
347         private final AtomicReference<DataTree> dataTree = new AtomicReference<>();
348
349         @Override
350         public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
351                 DatastoreContext datastoreContext, SchemaContext schemaContext) {
352             return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
353                     schemaContext, "member-1", messageClass, messageReceived, receivedMessage, dataTree);
354         }
355
356         @SuppressWarnings("unchecked")
357         <T> T waitForShardMessage() {
358             assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
359                     Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
360             assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
361             return (T) receivedMessage.get();
362         }
363
364         void expectShardMessage(Class<?> ofType) {
365             messageReceived.set(new CountDownLatch(1));
366             receivedMessage.set(null);
367             messageClass.set(ofType);
368         }
369
370         void setDataTree(DataTree tree) {
371             this.dataTree.set(tree);
372         }
373     }
374
375     static class TestEntityOwnershipShard extends EntityOwnershipShard {
376         private final AtomicReference<CountDownLatch> messageReceived;
377         private final AtomicReference<Object> receivedMessage;
378         private final AtomicReference<Class<?>> messageClass;
379         private final AtomicReference<DataTree> dataTree;
380
381         protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
382                 DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
383                 AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
384                 AtomicReference<Object> receivedMessage, AtomicReference<DataTree> dataTree) {
385             super(name, peerAddresses, datastoreContext, schemaContext, localMemberName, EntityOwnerSelectionStrategyConfig.newBuilder().build());
386             this.messageClass = messageClass;
387             this.messageReceived = messageReceived;
388             this.receivedMessage = receivedMessage;
389             this.dataTree = dataTree;
390         }
391
392         @Override
393         public void onReceiveCommand(final Object message) throws Exception {
394             try {
395                 if(dataTree.get() != null && message instanceof GetShardDataTree) {
396                     sender().tell(dataTree.get(), self());
397                 } else {
398                     super.onReceiveCommand(message);
399                 }
400             } finally {
401                 Class<?> expMsgClass = messageClass.get();
402                 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
403                     receivedMessage.set(message);
404                     messageReceived.get().countDown();
405                 }
406             }
407         }
408     }
409 }