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