blob: 992b77b09c3377e79e5eda85fd4aa5bd166538fb [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
Jordan Halterman0a2bd452018-06-13 17:24:58 -070018import java.util.Collections;
19import java.util.Map;
20import java.util.Optional;
21import java.util.concurrent.CountDownLatch;
22import java.util.concurrent.TimeUnit;
23
24import com.google.common.collect.ImmutableMap;
Simon Huntff663742015-05-14 13:33:05 -070025import com.google.common.collect.Maps;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070026import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.cluster.NodeId;
Thomas Vachuska36002e62015-05-19 16:12:29 -070030import org.onosproject.common.event.impl.TestEventDispatcher;
Jordan Haltermane74e6292018-05-15 00:52:31 -070031import org.onosproject.event.ListenerRegistry;
32import org.onosproject.mastership.MastershipEvent;
33import org.onosproject.mastership.MastershipEvent.Type;
Jordan Halterman0a2bd452018-06-13 17:24:58 -070034import org.onosproject.mastership.MastershipInfo;
Jordan Haltermane74e6292018-05-15 00:52:31 -070035import org.onosproject.mastership.MastershipListener;
36import org.onosproject.mastership.MastershipService;
37import org.onosproject.mastership.MastershipServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.DeviceId;
Jordan Halterman0a2bd452018-06-13 17:24:58 -070039import org.onosproject.net.MastershipRole;
Jordan Haltermane74e6292018-05-15 00:52:31 -070040import org.onosproject.store.flow.ReplicaInfo;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.store.flow.ReplicaInfoEvent;
Jordan Haltermane74e6292018-05-15 00:52:31 -070042import org.onosproject.store.flow.ReplicaInfoEventListener;
43import org.onosproject.store.flow.ReplicaInfoService;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070044
Simon Huntff663742015-05-14 13:33:05 -070045import static org.junit.Assert.assertEquals;
46import static org.junit.Assert.assertTrue;
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070047
48public class ReplicaInfoManagerTest {
49
Jordan Haltermane74e6292018-05-15 00:52:31 -070050
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070051 private static final DeviceId DID1 = DeviceId.deviceId("of:1");
52 private static final DeviceId DID2 = DeviceId.deviceId("of:2");
53 private static final NodeId NID1 = new NodeId("foo");
54
Jordan Haltermane74e6292018-05-15 00:52:31 -070055 private ReplicaInfoManager mgr;
56 private ReplicaInfoService service;
57
58 private ListenerRegistry<MastershipEvent, MastershipListener>
59 mastershipListenerRegistry;
60 private TestEventDispatcher eventDispatcher;
61
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070062
63 @Before
64 public void setUp() throws Exception {
Jordan Haltermane74e6292018-05-15 00:52:31 -070065 mastershipListenerRegistry = new ListenerRegistry<>();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070066
Jordan Haltermane74e6292018-05-15 00:52:31 -070067 mgr = new ReplicaInfoManager();
68 service = mgr;
69
70 eventDispatcher = new TestEventDispatcher();
71 mgr.eventDispatcher = eventDispatcher;
72 mgr.mastershipService = new TestMastershipService();
73
74 // register dummy mastership event source
75 mgr.eventDispatcher.addSink(MastershipEvent.class, mastershipListenerRegistry);
76
77 mgr.activate();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070078 }
79
80 @After
81 public void tearDown() throws Exception {
Jordan Haltermane74e6292018-05-15 00:52:31 -070082 mgr.deactivate();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070083 }
84
85 @Test
Jordan Haltermane74e6292018-05-15 00:52:31 -070086 public void testGetReplicaInfoFor() {
87 ReplicaInfo info1 = service.getReplicaInfoFor(DID1);
88 assertEquals(Optional.of(NID1), info1.master());
89 // backups are always empty for now
90 assertEquals(Collections.emptyList(), info1.backups());
91
92 ReplicaInfo info2 = service.getReplicaInfoFor(DID2);
93 assertEquals("There's no master", Optional.empty(), info2.master());
94 // backups are always empty for now
95 assertEquals(Collections.emptyList(), info2.backups());
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070096 }
97
98 @Test
Jordan Haltermane74e6292018-05-15 00:52:31 -070099 public void testReplicaInfoEvent() throws InterruptedException {
100 final CountDownLatch latch = new CountDownLatch(1);
101 service.addListener(new MasterNodeCheck(latch, DID1, NID1));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700102
Jordan Haltermane74e6292018-05-15 00:52:31 -0700103 // fake MastershipEvent
104 eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1,
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700105 new MastershipInfo(1, Optional.of(NID1), ImmutableMap.of(NID1, MastershipRole.MASTER))));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700106
Jordan Haltermane74e6292018-05-15 00:52:31 -0700107 assertTrue(latch.await(1, TimeUnit.SECONDS));
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700108 }
109
Jordan Haltermane74e6292018-05-15 00:52:31 -0700110
111 private final class MasterNodeCheck implements ReplicaInfoEventListener {
112 private final CountDownLatch latch;
113 private Optional<NodeId> expectedMaster;
114 private DeviceId expectedDevice;
115
116
117 MasterNodeCheck(CountDownLatch latch, DeviceId did,
118 NodeId nid) {
119 this.latch = latch;
120 this.expectedMaster = Optional.ofNullable(nid);
121 this.expectedDevice = did;
122 }
123
124 @Override
125 public void event(ReplicaInfoEvent event) {
126 assertEquals(expectedDevice, event.subject());
127 assertEquals(expectedMaster, event.replicaInfo().master());
128 // backups are always empty for now
129 assertEquals(Collections.emptyList(), event.replicaInfo().backups());
130 latch.countDown();
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700131 }
132 }
133
134
Jordan Haltermane74e6292018-05-15 00:52:31 -0700135 private final class TestMastershipService
136 extends MastershipServiceAdapter
137 implements MastershipService {
138
139 private Map<DeviceId, NodeId> masters;
140
141 TestMastershipService() {
142 masters = Maps.newHashMap();
143 masters.put(DID1, NID1);
144 // DID2 has no master
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700145 }
146
147 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700148 public NodeId getMasterFor(DeviceId deviceId) {
149 return masters.get(deviceId);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700150 }
151
152 @Override
Jordan Halterman0a2bd452018-06-13 17:24:58 -0700153 public MastershipInfo getMastershipFor(DeviceId deviceId) {
154 return new MastershipInfo(
155 1,
156 Optional.ofNullable(masters.get(deviceId)),
157 ImmutableMap.of(NID1, MastershipRole.MASTER));
Madan Jampani86940d92015-05-06 11:47:57 -0700158 }
159
160 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700161 public void addListener(MastershipListener listener) {
162 mastershipListenerRegistry.addListener(listener);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700163 }
164
165 @Override
Jordan Haltermane74e6292018-05-15 00:52:31 -0700166 public void removeListener(MastershipListener listener) {
167 mastershipListenerRegistry.removeListener(listener);
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700168 }
169 }
Jordan Haltermane74e6292018-05-15 00:52:31 -0700170
Yuta HIGUCHI4d187952014-10-16 19:59:35 -0700171}