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