2 * Copyright 2014 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.flow.impl;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.Maps;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.onosproject.cluster.NodeId;
24 import org.onosproject.cluster.RoleInfo;
25 import org.onosproject.common.event.impl.TestEventDispatcher;
26 import org.onosproject.event.ListenerRegistry;
27 import org.onosproject.mastership.MastershipEvent;
28 import org.onosproject.mastership.MastershipEvent.Type;
29 import org.onosproject.mastership.MastershipListener;
30 import org.onosproject.mastership.MastershipService;
31 import org.onosproject.mastership.MastershipServiceAdapter;
32 import org.onosproject.net.DeviceId;
33 import org.onosproject.store.flow.ReplicaInfo;
34 import org.onosproject.store.flow.ReplicaInfoEvent;
35 import org.onosproject.store.flow.ReplicaInfoEventListener;
36 import org.onosproject.store.flow.ReplicaInfoService;
38 import java.util.Collections;
39 import java.util.LinkedList;
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.TimeUnit;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
47 public class ReplicaInfoManagerTest {
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");
54 private ReplicaInfoManager mgr;
55 private ReplicaInfoService service;
57 private ListenerRegistry<MastershipEvent, MastershipListener>
58 mastershipListenerRegistry;
59 private TestEventDispatcher eventDispatcher;
63 public void setUp() throws Exception {
64 mastershipListenerRegistry = new ListenerRegistry<>();
66 mgr = new ReplicaInfoManager();
69 eventDispatcher = new TestEventDispatcher();
70 mgr.eventDispatcher = eventDispatcher;
71 mgr.mastershipService = new TestMastershipService();
73 // register dummy mastership event source
74 mgr.eventDispatcher.addSink(MastershipEvent.class, mastershipListenerRegistry);
80 public void tearDown() throws Exception {
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());
91 ReplicaInfo info2 = service.getReplicaInfoFor(DID2);
92 assertEquals("There's no master", Optional.absent(), info2.master());
93 // backups are always empty for now
94 assertEquals(Collections.emptyList(), info2.backups());
98 public void testReplicaInfoEvent() throws InterruptedException {
99 final CountDownLatch latch = new CountDownLatch(1);
100 service.addListener(new MasterNodeCheck(latch, DID1, NID1));
102 // fake MastershipEvent
103 eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1,
104 new RoleInfo(NID1, new LinkedList<NodeId>())));
106 assertTrue(latch.await(1, TimeUnit.SECONDS));
110 private final class MasterNodeCheck implements ReplicaInfoEventListener {
111 private final CountDownLatch latch;
112 private Optional<NodeId> expectedMaster;
113 private DeviceId expectedDevice;
116 MasterNodeCheck(CountDownLatch latch, DeviceId did,
119 this.expectedMaster = Optional.fromNullable(nid);
120 this.expectedDevice = did;
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());
134 private final class TestMastershipService
135 extends MastershipServiceAdapter
136 implements MastershipService {
138 private Map<DeviceId, NodeId> masters;
140 TestMastershipService() {
141 masters = Maps.newHashMap();
142 masters.put(DID1, NID1);
143 // DID2 has no master
147 public NodeId getMasterFor(DeviceId deviceId) {
148 return masters.get(deviceId);
152 public RoleInfo getNodesFor(DeviceId deviceId) {
153 return new RoleInfo(masters.get(deviceId), Collections.emptyList());
157 public void addListener(MastershipListener listener) {
158 mastershipListenerRegistry.addListener(listener);
162 public void removeListener(MastershipListener listener) {
163 mastershipListenerRegistry.removeListener(listener);