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