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