blob: 265dbd02a1116a9353928e2c49c87638bea9f62b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.flow.impl;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070017
Simon Huntff663742015-05-14 13:33:05 -070018import com.google.common.collect.Maps;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cluster.NodeId;
23import org.onosproject.cluster.RoleInfo;
Thomas Vachuska36002e62015-05-19 16:12:29 -070024import org.onosproject.common.event.impl.TestEventDispatcher;
Simon Huntff663742015-05-14 13:33:05 -070025import org.onosproject.event.ListenerRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.mastership.MastershipEvent;
27import org.onosproject.mastership.MastershipEvent.Type;
28import org.onosproject.mastership.MastershipListener;
29import org.onosproject.mastership.MastershipService;
30import org.onosproject.mastership.MastershipServiceAdapter;
31import org.onosproject.net.DeviceId;
32import org.onosproject.store.flow.ReplicaInfo;
33import org.onosproject.store.flow.ReplicaInfoEvent;
34import org.onosproject.store.flow.ReplicaInfoEventListener;
35import org.onosproject.store.flow.ReplicaInfoService;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070036
Simon Huntff663742015-05-14 13:33:05 -070037import java.util.Collections;
38import java.util.LinkedList;
39import java.util.Map;
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070040import java.util.Optional;
Simon Huntff663742015-05-14 13:33:05 -070041import java.util.concurrent.CountDownLatch;
42import java.util.concurrent.TimeUnit;
43
Simon Huntff663742015-05-14 13:33:05 -070044import static org.junit.Assert.assertEquals;
45import static org.junit.Assert.assertTrue;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070046
47public class ReplicaInfoManagerTest {
48
49
50 private static final DeviceId DID1 = DeviceId.deviceId("of:1");
51 private static final DeviceId DID2 = DeviceId.deviceId("of:2");
52 private static final NodeId NID1 = new NodeId("foo");
53
54 private ReplicaInfoManager mgr;
55 private ReplicaInfoService service;
56
Simon Huntff663742015-05-14 13:33:05 -070057 private ListenerRegistry<MastershipEvent, MastershipListener>
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070058 mastershipListenerRegistry;
59 private TestEventDispatcher eventDispatcher;
60
61
62 @Before
63 public void setUp() throws Exception {
Simon Huntff663742015-05-14 13:33:05 -070064 mastershipListenerRegistry = new ListenerRegistry<>();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070065
66 mgr = new ReplicaInfoManager();
67 service = mgr;
68
69 eventDispatcher = new TestEventDispatcher();
70 mgr.eventDispatcher = eventDispatcher;
71 mgr.mastershipService = new TestMastershipService();
72
73 // register dummy mastership event source
74 mgr.eventDispatcher.addSink(MastershipEvent.class, mastershipListenerRegistry);
75
76 mgr.activate();
77 }
78
79 @After
80 public void tearDown() throws Exception {
81 mgr.deactivate();
82 }
83
84 @Test
85 public void testGetReplicaInfoFor() {
86 ReplicaInfo info1 = service.getReplicaInfoFor(DID1);
87 assertEquals(Optional.of(NID1), info1.master());
88 // backups are always empty for now
89 assertEquals(Collections.emptyList(), info1.backups());
90
91 ReplicaInfo info2 = service.getReplicaInfoFor(DID2);
Sho SHIMIZU19af19d2016-08-12 16:08:04 -070092 assertEquals("There's no master", Optional.empty(), info2.master());
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070093 // backups are always empty for now
94 assertEquals(Collections.emptyList(), info2.backups());
95 }
96
97 @Test
98 public void testReplicaInfoEvent() throws InterruptedException {
99 final CountDownLatch latch = new CountDownLatch(1);
100 service.addListener(new MasterNodeCheck(latch, DID1, NID1));
101
102 // fake MastershipEvent
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700103 eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1,
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700104 new RoleInfo(NID1, new LinkedList<>())));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700105
106 assertTrue(latch.await(1, TimeUnit.SECONDS));
107 }
108
109
110 private final class MasterNodeCheck implements ReplicaInfoEventListener {
111 private final CountDownLatch latch;
112 private Optional<NodeId> expectedMaster;
113 private DeviceId expectedDevice;
114
115
116 MasterNodeCheck(CountDownLatch latch, DeviceId did,
Sho SHIMIZU19af19d2016-08-12 16:08:04 -0700117 NodeId nid) {
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700118 this.latch = latch;
Sho SHIMIZU19af19d2016-08-12 16:08:04 -0700119 this.expectedMaster = Optional.ofNullable(nid);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700120 this.expectedDevice = did;
121 }
122
123 @Override
124 public void event(ReplicaInfoEvent event) {
125 assertEquals(expectedDevice, event.subject());
126 assertEquals(expectedMaster, event.replicaInfo().master());
127 // backups are always empty for now
128 assertEquals(Collections.emptyList(), event.replicaInfo().backups());
129 latch.countDown();
130 }
131 }
132
133
134 private final class TestMastershipService
135 extends MastershipServiceAdapter
136 implements MastershipService {
137
138 private Map<DeviceId, NodeId> masters;
139
140 TestMastershipService() {
141 masters = Maps.newHashMap();
142 masters.put(DID1, NID1);
143 // DID2 has no master
144 }
145
146 @Override
147 public NodeId getMasterFor(DeviceId deviceId) {
148 return masters.get(deviceId);
149 }
150
151 @Override
Madan Jampani86940d92015-05-06 11:47:57 -0700152 public RoleInfo getNodesFor(DeviceId deviceId) {
153 return new RoleInfo(masters.get(deviceId), Collections.emptyList());
154 }
155
156 @Override
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700157 public void addListener(MastershipListener listener) {
158 mastershipListenerRegistry.addListener(listener);
159 }
160
161 @Override
162 public void removeListener(MastershipListener listener) {
163 mastershipListenerRegistry.removeListener(listener);
164 }
165 }
166
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700167}