a5b0fbedda9a07962abba5aad69569034f816916
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / gossip / BucketStoreTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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.remote.rpc.registry.gossip;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Address;
13 import akka.actor.Props;
14 import akka.testkit.JavaTestKit;
15 import akka.testkit.TestActorRef;
16 import com.typesafe.config.ConfigFactory;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Optional;
20 import org.junit.AfterClass;
21 import org.junit.Assert;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
25 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
26
27 public class BucketStoreTest {
28
29     /**
30      * Dummy class to eliminate rawtype warnings.
31      *
32      * @author gwu
33      *
34      */
35     private static class T implements BucketData<T> {
36         @Override
37         public T copy() {
38             return new T();
39         }
40
41         @Override
42         public Optional<ActorRef> getWatchActor() {
43             return Optional.empty();
44         }
45     }
46
47     private static ActorSystem system;
48
49     @BeforeClass
50     public static void setup() {
51         system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
52         system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
53     }
54
55     @AfterClass
56     public static void teardown() {
57         JavaTestKit.shutdownActorSystem(system);
58     }
59
60     /**
61      * Given remote buckets, should merge with local copy of remote buckets.
62      */
63     @Test
64     public void testReceiveUpdateRemoteBuckets() {
65
66         final BucketStore<T> store = createStore();
67
68         Address localAddress = system.provider().getDefaultAddress();
69         Bucket<T> localBucket = new BucketImpl<>(new T());
70
71         Address a1 = new Address("tcp", "system1");
72         Address a2 = new Address("tcp", "system2");
73         Address a3 = new Address("tcp", "system3");
74
75         Bucket<T> b1 = new BucketImpl<>(new T());
76         Bucket<T> b2 = new BucketImpl<>(new T());
77         Bucket<T> b3 = new BucketImpl<>(new T());
78
79         Map<Address, Bucket<T>> remoteBuckets = new HashMap<>(3);
80         remoteBuckets.put(a1, b1);
81         remoteBuckets.put(a2, b2);
82         remoteBuckets.put(a3, b3);
83         remoteBuckets.put(localAddress, localBucket);
84
85         //Given remote buckets
86         store.receiveUpdateRemoteBuckets(remoteBuckets);
87
88         //Should NOT contain local bucket
89         //Should contain ONLY 3 entries i.e a1, a2, a3
90         Map<Address, Bucket<T>> remoteBucketsInStore = store.getRemoteBuckets();
91         Assert.assertFalse("remote buckets contains local bucket", remoteBucketsInStore.containsKey(localAddress));
92         Assert.assertTrue(remoteBucketsInStore.size() == 3);
93
94         //Add a new remote bucket
95         Address a4 = new Address("tcp", "system4");
96         Bucket<T> b4 = new BucketImpl<>(new T());
97         remoteBuckets.clear();
98         remoteBuckets.put(a4, b4);
99         store.receiveUpdateRemoteBuckets(remoteBuckets);
100
101         //Should contain a4
102         //Should contain 4 entries now i.e a1, a2, a3, a4
103         remoteBucketsInStore = store.getRemoteBuckets();
104         Assert.assertTrue("Does not contain a4", remoteBucketsInStore.containsKey(a4));
105         Assert.assertTrue(remoteBucketsInStore.size() == 4);
106
107         //Update a bucket
108         Bucket<T> b3New = new BucketImpl<>(new T());
109         remoteBuckets.clear();
110         remoteBuckets.put(a3, b3New);
111         remoteBuckets.put(a1, null);
112         remoteBuckets.put(a2, null);
113         store.receiveUpdateRemoteBuckets(remoteBuckets);
114
115         //Should only update a3
116         remoteBucketsInStore = store.getRemoteBuckets();
117         Bucket<T> b3InStore = remoteBucketsInStore.get(a3);
118         Assert.assertEquals(b3New.getVersion(), b3InStore.getVersion());
119
120         //Should NOT update a1 and a2
121         Bucket<T> b1InStore = remoteBucketsInStore.get(a1);
122         Bucket<T> b2InStore = remoteBucketsInStore.get(a2);
123         Assert.assertEquals(b1.getVersion(), b1InStore.getVersion());
124         Assert.assertEquals(b2.getVersion(), b2InStore.getVersion());
125         Assert.assertTrue(remoteBucketsInStore.size() == 4);
126
127         //Should update versions map
128         //versions map contains versions for all remote buckets (4).
129         Map<Address, Long> versionsInStore = store.getVersions();
130         Assert.assertEquals(4, versionsInStore.size());
131         Assert.assertEquals((Long)b1.getVersion(), versionsInStore.get(a1));
132         Assert.assertEquals((Long)b2.getVersion(), versionsInStore.get(a2));
133         Assert.assertEquals((Long)b3New.getVersion(), versionsInStore.get(a3));
134         Assert.assertEquals((Long)b4.getVersion(), versionsInStore.get(a4));
135
136         //Send older version of bucket
137         remoteBuckets.clear();
138         remoteBuckets.put(a3, b3);
139         store.receiveUpdateRemoteBuckets(remoteBuckets);
140
141         //Should NOT update a3
142         remoteBucketsInStore = store.getRemoteBuckets();
143         b3InStore = remoteBucketsInStore.get(a3);
144         Assert.assertEquals(b3InStore.getVersion(), b3New.getVersion());
145
146     }
147
148     /**
149      * Create BucketStore actor and returns the underlying instance of BucketStore class.
150      *
151      * @return instance of BucketStore class
152      */
153     private static BucketStore<T> createStore() {
154         final Props props = Props.create(BucketStore.class, new RemoteRpcProviderConfig(system.settings().config()),
155             new T());
156         final TestActorRef<BucketStore<T>> testRef = TestActorRef.create(system, props, "testStore");
157         return testRef.underlyingActor();
158     }
159 }