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