Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.flow.impl; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 17 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 18 | import com.google.common.base.Optional; |
| 19 | import com.google.common.collect.Maps; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.cluster.RoleInfo; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame^] | 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 26 | import org.onosproject.event.ListenerRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 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; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 37 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 38 | import java.util.Collections; |
| 39 | import java.util.LinkedList; |
| 40 | import java.util.Map; |
| 41 | import java.util.concurrent.CountDownLatch; |
| 42 | import java.util.concurrent.TimeUnit; |
| 43 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 44 | import static org.junit.Assert.assertEquals; |
| 45 | import static org.junit.Assert.assertTrue; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 46 | |
| 47 | public 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 Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 57 | private ListenerRegistry<MastershipEvent, MastershipListener> |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 58 | mastershipListenerRegistry; |
| 59 | private TestEventDispatcher eventDispatcher; |
| 60 | |
| 61 | |
| 62 | @Before |
| 63 | public void setUp() throws Exception { |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 64 | mastershipListenerRegistry = new ListenerRegistry<>(); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 65 | |
| 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); |
| 92 | assertEquals("There's no master", Optional.absent(), info2.master()); |
| 93 | // 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 Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 103 | eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1, |
| 104 | new RoleInfo(NID1, new LinkedList<NodeId>()))); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 105 | |
| 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, |
| 117 | NodeId nid) { |
| 118 | this.latch = latch; |
| 119 | this.expectedMaster = Optional.fromNullable(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(); |
| 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 Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 152 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 153 | return new RoleInfo(masters.get(deviceId), Collections.emptyList()); |
| 154 | } |
| 155 | |
| 156 | @Override |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 157 | 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 HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 167 | } |