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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.