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