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 | */ |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cluster.impl; |
| 17 | |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.onos.cluster.ClusterEventListener; |
| 24 | import org.onlab.onos.cluster.ClusterService; |
| 25 | import org.onlab.onos.cluster.ControllerNode; |
| 26 | import org.onlab.onos.cluster.ControllerNode.State; |
| 27 | import org.onlab.onos.cluster.DefaultControllerNode; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 28 | import org.onlab.onos.cluster.NodeId; |
| 29 | import org.onlab.onos.event.impl.TestEventDispatcher; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 30 | import org.onlab.onos.mastership.MastershipService; |
| 31 | import org.onlab.onos.mastership.MastershipTermService; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.DeviceId; |
tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 33 | import org.onlab.onos.store.trivial.impl.SimpleMastershipStore; |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 35 | |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 36 | import com.google.common.collect.Sets; |
| 37 | |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 38 | import static org.junit.Assert.assertEquals; |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 39 | import static org.junit.Assert.assertNull; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 40 | import static org.onlab.onos.net.MastershipRole.*; |
| 41 | |
| 42 | /** |
| 43 | * Test codifying the mastership service contracts. |
| 44 | */ |
| 45 | public class MastershipManagerTest { |
| 46 | |
| 47 | private static final NodeId NID_LOCAL = new NodeId("local"); |
| 48 | private static final NodeId NID_OTHER = new NodeId("foo"); |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 49 | private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1"); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 50 | private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1"); |
| 51 | private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2"); |
| 52 | |
| 53 | private MastershipManager mgr; |
| 54 | protected MastershipService service; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | mgr = new MastershipManager(); |
| 59 | service = mgr; |
| 60 | mgr.store = new SimpleMastershipStore(); |
| 61 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 62 | mgr.clusterService = new TestClusterService(); |
| 63 | mgr.activate(); |
| 64 | } |
| 65 | |
| 66 | @After |
| 67 | public void tearDown() { |
| 68 | mgr.deactivate(); |
| 69 | mgr.clusterService = null; |
| 70 | mgr.eventDispatcher = null; |
| 71 | mgr.store = null; |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void setRole() { |
| 76 | mgr.setRole(NID_OTHER, DEV_MASTER, MASTER); |
| 77 | assertEquals("wrong local role:", STANDBY, mgr.getLocalRole(DEV_MASTER)); |
| 78 | |
| 79 | //set to master |
| 80 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 81 | assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void relinquishMastership() { |
Ayaka Koshibe | b62aab5 | 2014-10-24 13:15:25 -0700 | [diff] [blame] | 86 | //no backups - should just turn to NONE for device. |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 87 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 88 | assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER)); |
| 89 | mgr.relinquishMastership(DEV_MASTER); |
| 90 | assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER)); |
Ayaka Koshibe | b62aab5 | 2014-10-24 13:15:25 -0700 | [diff] [blame] | 91 | assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER)); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 92 | |
| 93 | //not master, nothing should happen |
Ayaka Koshibe | b62aab5 | 2014-10-24 13:15:25 -0700 | [diff] [blame] | 94 | mgr.setRole(NID_LOCAL, DEV_OTHER, NONE); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 95 | mgr.relinquishMastership(DEV_OTHER); |
| 96 | assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER)); |
| 97 | |
| 98 | //provide NID_OTHER as backup and relinquish |
| 99 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 100 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER)); |
| 101 | mgr.setRole(NID_OTHER, DEV_MASTER, STANDBY); |
| 102 | mgr.relinquishMastership(DEV_MASTER); |
| 103 | assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_MASTER)); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void requestRoleFor() { |
| 108 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 109 | mgr.setRole(NID_OTHER, DEV_OTHER, MASTER); |
| 110 | |
| 111 | //local should be master for one but standby for other |
| 112 | assertEquals("wrong role:", MASTER, mgr.requestRoleFor(DEV_MASTER)); |
| 113 | assertEquals("wrong role:", STANDBY, mgr.requestRoleFor(DEV_OTHER)); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void getMasterFor() { |
| 118 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 119 | mgr.setRole(NID_OTHER, DEV_OTHER, MASTER); |
| 120 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER)); |
| 121 | assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER)); |
| 122 | |
| 123 | //have NID_OTHER hand over DEV_OTHER to NID_LOCAL |
| 124 | mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER); |
| 125 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER)); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void getDevicesOf() { |
| 130 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 131 | mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY); |
| 132 | assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size()); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 133 | //hand both devices to NID_LOCAL |
| 134 | mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER); |
| 135 | assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size()); |
| 136 | } |
| 137 | |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 138 | @Test |
| 139 | public void termService() { |
| 140 | MastershipTermService ts = mgr.requestTermService(); |
| 141 | |
| 142 | //term = 0 for both |
| 143 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 144 | assertEquals("inconsistent term: ", 0, ts.getMastershipTerm(DEV_MASTER).termNumber()); |
| 145 | |
| 146 | //hand devices to NID_LOCAL and back: term = 2 |
| 147 | mgr.setRole(NID_OTHER, DEV_MASTER, MASTER); |
| 148 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 149 | assertEquals("inconsistent terms: ", 2, ts.getMastershipTerm(DEV_MASTER).termNumber()); |
| 150 | } |
| 151 | |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 152 | private final class TestClusterService implements ClusterService { |
| 153 | |
| 154 | ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST); |
| 155 | |
| 156 | @Override |
| 157 | public ControllerNode getLocalNode() { |
| 158 | return local; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Set<ControllerNode> getNodes() { |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 163 | return Sets.newHashSet(); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public ControllerNode getNode(NodeId nodeId) { |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public State getState(NodeId nodeId) { |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void addListener(ClusterEventListener listener) { |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void removeListener(ClusterEventListener listener) { |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | } |