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