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