Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.Map; |
| 20 | import java.util.Optional; |
| 21 | import java.util.concurrent.CountDownLatch; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableMap; |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 30 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 31 | import org.onosproject.event.ListenerRegistry; |
| 32 | import org.onosproject.mastership.MastershipEvent; |
| 33 | import org.onosproject.mastership.MastershipEvent.Type; |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 34 | import org.onosproject.mastership.MastershipInfo; |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipListener; |
| 36 | import org.onosproject.mastership.MastershipService; |
| 37 | import org.onosproject.mastership.MastershipServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 39 | import org.onosproject.net.MastershipRole; |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 40 | import org.onosproject.store.flow.ReplicaInfo; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.store.flow.ReplicaInfoEvent; |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 42 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 43 | import org.onosproject.store.flow.ReplicaInfoService; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 44 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 45 | import static org.junit.Assert.assertEquals; |
| 46 | import static org.junit.Assert.assertTrue; |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 47 | |
| 48 | public class ReplicaInfoManagerTest { |
| 49 | |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 50 | |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 51 | 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 Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 55 | private ReplicaInfoManager mgr; |
| 56 | private ReplicaInfoService service; |
| 57 | |
| 58 | private ListenerRegistry<MastershipEvent, MastershipListener> |
| 59 | mastershipListenerRegistry; |
| 60 | private TestEventDispatcher eventDispatcher; |
| 61 | |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 62 | |
| 63 | @Before |
| 64 | public void setUp() throws Exception { |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 65 | mastershipListenerRegistry = new ListenerRegistry<>(); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 66 | |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 67 | 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 HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @After |
| 81 | public void tearDown() throws Exception { |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 82 | mgr.deactivate(); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 86 | 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 HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 99 | public void testReplicaInfoEvent() throws InterruptedException { |
| 100 | final CountDownLatch latch = new CountDownLatch(1); |
| 101 | service.addListener(new MasterNodeCheck(latch, DID1, NID1)); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 102 | |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 103 | // fake MastershipEvent |
| 104 | eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1, |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 105 | new MastershipInfo(1, Optional.of(NID1), ImmutableMap.of(NID1, MastershipRole.MASTER)))); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 106 | |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 107 | assertTrue(latch.await(1, TimeUnit.SECONDS)); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 110 | |
| 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 HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 135 | 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 HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 148 | public NodeId getMasterFor(DeviceId deviceId) { |
| 149 | return masters.get(deviceId); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 153 | public MastershipInfo getMastershipFor(DeviceId deviceId) { |
| 154 | return new MastershipInfo( |
| 155 | 1, |
| 156 | Optional.ofNullable(masters.get(deviceId)), |
| 157 | ImmutableMap.of(NID1, MastershipRole.MASTER)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 161 | public void addListener(MastershipListener listener) { |
| 162 | mastershipListenerRegistry.addListener(listener); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 166 | public void removeListener(MastershipListener listener) { |
| 167 | mastershipListenerRegistry.removeListener(listener); |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 170 | |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 171 | } |