blob: 9709643354b3cde585d71407f37b8983b84e6905 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Jordan Haltermane74e6292018-05-15 00:52:31 -070023import org.onosproject.cluster.RoleInfo;
Thomas Vachuska36002e62015-05-19 16:12:29 -070024import org.onosproject.common.event.impl.TestEventDispatcher;
Jordan Haltermane74e6292018-05-15 00:52:31 -070025import org.onosproject.event.ListenerRegistry;
26import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.DeviceId;
Jordan Haltermane74e6292018-05-15 00:52:31 -070032import org.onosproject.store.flow.ReplicaInfo;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.store.flow.ReplicaInfoEvent;
Jordan Haltermane74e6292018-05-15 00:52:31 -070034import org.onosproject.store.flow.ReplicaInfoEventListener;
35import org.onosproject.store.flow.ReplicaInfoService;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070036
Jordan Haltermane74e6292018-05-15 00:52:31 -070037import java.util.Collections;
38import java.util.LinkedList;
39import java.util.Map;
40import java.util.Optional;
41import 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
Jordan Haltermane74e6292018-05-15 00:52:31 -070049
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070050 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
Jordan Haltermane74e6292018-05-15 00:52:31 -070054 private ReplicaInfoManager mgr;
55 private ReplicaInfoService service;
56
57 private ListenerRegistry<MastershipEvent, MastershipListener>
58 mastershipListenerRegistry;
59 private TestEventDispatcher eventDispatcher;
60
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070061
62 @Before
63 public void setUp() throws Exception {
Jordan Haltermane74e6292018-05-15 00:52:31 -070064 mastershipListenerRegistry = new ListenerRegistry<>();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070065
Jordan Haltermane74e6292018-05-15 00:52:31 -070066 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();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070077 }
78
79 @After
80 public void tearDown() throws Exception {
Jordan Haltermane74e6292018-05-15 00:52:31 -070081 mgr.deactivate();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070082 }
83
84 @Test
Jordan Haltermane74e6292018-05-15 00:52:31 -070085 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);
92 assertEquals("There's no master", Optional.empty(), info2.master());
93 // backups are always empty for now
94 assertEquals(Collections.emptyList(), info2.backups());
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070095 }
96
97 @Test
Jordan Haltermane74e6292018-05-15 00:52:31 -070098 public void testReplicaInfoEvent() throws InterruptedException {
99 final CountDownLatch latch = new CountDownLatch(1);
100 service.addListener(new MasterNodeCheck(latch, DID1, NID1));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700101
Jordan Haltermane74e6292018-05-15 00:52:31 -0700102 // fake MastershipEvent
103 eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1,
104 new RoleInfo(NID1, new LinkedList<>())));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700105
Jordan Haltermane74e6292018-05-15 00:52:31 -0700106 assertTrue(latch.await(1, TimeUnit.SECONDS));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700107 }
108
Jordan Haltermane74e6292018-05-15 00:52:31 -0700109
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,
117 NodeId nid) {
118 this.latch = latch;
119 this.expectedMaster = Optional.ofNullable(nid);
120 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();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700130 }
131 }
132
133
Jordan Haltermane74e6292018-05-15 00:52:31 -0700134 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
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700144 }
145
146 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700147 public NodeId getMasterFor(DeviceId deviceId) {
148 return masters.get(deviceId);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700149 }
150
151 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700152 public RoleInfo getNodesFor(DeviceId deviceId) {
153 return new RoleInfo(masters.get(deviceId), Collections.emptyList());
Madan Jampani86940d92015-05-06 11:47:57 -0700154 }
155
156 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700157 public void addListener(MastershipListener listener) {
158 mastershipListenerRegistry.addListener(listener);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700159 }
160
161 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700162 public void removeListener(MastershipListener listener) {
163 mastershipListenerRegistry.removeListener(listener);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700164 }
165 }
Jordan Haltermane74e6292018-05-15 00:52:31 -0700166
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700167}