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 2015-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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | import java.util.concurrent.atomic.AtomicInteger; |
| 22 | |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.mastership.MastershipEvent; |
| 28 | import org.onosproject.mastership.MastershipTerm; |
| 29 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 30 | |
| 31 | import com.google.common.collect.Sets; |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 32 | import com.google.common.util.concurrent.Futures; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 33 | |
| 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.junit.Assert.assertNull; |
| 36 | import static org.junit.Assert.assertTrue; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import static org.onosproject.mastership.MastershipEvent.Type.*; |
| 38 | import static org.onosproject.net.MastershipRole.*; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Test for the simple MastershipStore implementation. |
| 42 | */ |
| 43 | public class SimpleMastershipStoreTest { |
| 44 | |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 45 | private static final DeviceId DID1 = DeviceId.deviceId("of:01"); |
| 46 | private static final DeviceId DID2 = DeviceId.deviceId("of:02"); |
| 47 | private static final DeviceId DID3 = DeviceId.deviceId("of:03"); |
| 48 | private static final DeviceId DID4 = DeviceId.deviceId("of:04"); |
| 49 | |
| 50 | private static final NodeId N1 = new NodeId("local"); |
| 51 | private static final NodeId N2 = new NodeId("other"); |
| 52 | |
| 53 | private SimpleMastershipStore sms; |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception { |
| 57 | sms = new SimpleMastershipStore(); |
| 58 | sms.activate(); |
| 59 | } |
| 60 | |
| 61 | @After |
| 62 | public void tearDown() throws Exception { |
| 63 | sms.deactivate(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void getRole() { |
| 68 | //special case, no backup or master |
| 69 | put(DID1, N1, false, false); |
| 70 | assertEquals("wrong role", NONE, sms.getRole(N1, DID1)); |
| 71 | |
| 72 | //backup exists but we aren't mapped |
| 73 | put(DID2, N1, false, true); |
| 74 | assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2)); |
| 75 | |
| 76 | //N2 is master |
| 77 | put(DID3, N2, true, true); |
| 78 | assertEquals("wrong role", MASTER, sms.getRole(N2, DID3)); |
| 79 | |
| 80 | //N2 is master but N1 is only in backups set |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 81 | put(DID4, N1, false, true); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 82 | put(DID4, N2, true, false); |
| 83 | assertEquals("wrong role", STANDBY, sms.getRole(N1, DID4)); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void getMaster() { |
| 88 | put(DID3, N2, true, true); |
| 89 | assertEquals("wrong role", MASTER, sms.getRole(N2, DID3)); |
| 90 | assertEquals("wrong device", N2, sms.getMaster(DID3)); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void setMaster() { |
| 95 | put(DID1, N1, false, false); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 96 | assertEquals("wrong event", MASTER_CHANGED, Futures.getUnchecked(sms.setMaster(N1, DID1)).type()); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 97 | assertEquals("wrong role", MASTER, sms.getRole(N1, DID1)); |
| 98 | //set node that's already master - should be ignored |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 99 | assertNull("wrong event", Futures.getUnchecked(sms.setMaster(N1, DID1))); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 100 | |
| 101 | //set STANDBY to MASTER |
| 102 | put(DID2, N1, false, true); |
| 103 | assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2)); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 104 | assertEquals("wrong event", MASTER_CHANGED, Futures.getUnchecked(sms.setMaster(N1, DID2)).type()); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 105 | assertEquals("wrong role", MASTER, sms.getRole(N1, DID2)); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void getDevices() { |
| 110 | Set<DeviceId> d = Sets.newHashSet(DID1, DID2); |
| 111 | |
| 112 | put(DID1, N2, true, true); |
| 113 | put(DID2, N2, true, true); |
| 114 | put(DID3, N1, true, true); |
| 115 | assertTrue("wrong devices", d.equals(sms.getDevices(N2))); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void getTermFor() { |
| 120 | put(DID1, N1, true, true); |
| 121 | assertEquals("wrong term", MastershipTerm.of(N1, 0), sms.getTermFor(DID1)); |
| 122 | |
| 123 | //switch to N2 and back - 2 term switches |
| 124 | sms.setMaster(N2, DID1); |
| 125 | sms.setMaster(N1, DID1); |
| 126 | assertEquals("wrong term", MastershipTerm.of(N1, 2), sms.getTermFor(DID1)); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void requestRole() { |
| 131 | //NONE - become MASTER |
| 132 | put(DID1, N1, false, false); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 133 | assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID1))); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 135 | //was STANDBY - become MASTER |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 136 | put(DID2, N1, false, true); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 137 | assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID2))); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 138 | |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 139 | //other MASTER - stay STANDBY |
| 140 | put(DID3, N2, true, false); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 141 | assertEquals("wrong role", STANDBY, Futures.getUnchecked(sms.requestRole(DID3))); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 142 | |
| 143 | //local (N1) is MASTER - stay MASTER |
| 144 | put(DID4, N1, true, true); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 145 | assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID4))); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void unsetMaster() { |
| 150 | //NONE - record backup but take no other action |
| 151 | put(DID1, N1, false, false); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 152 | sms.setStandby(N1, DID1); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 153 | assertTrue("not backed up", sms.backups.get(DID1).contains(N1)); |
| 154 | int prev = sms.termMap.get(DID1).get(); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 155 | sms.setStandby(N1, DID1); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 156 | assertEquals("term should not change", prev, sms.termMap.get(DID1).get()); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 157 | |
| 158 | //no backup, MASTER |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 159 | put(DID1, N1, true, false); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 160 | assertNull("expect no MASTER event", Futures.getUnchecked(sms.setStandby(N1, DID1)).roleInfo().master()); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 161 | assertNull("wrong node", sms.masterMap.get(DID1)); |
| 162 | |
| 163 | //backup, switch |
| 164 | sms.masterMap.clear(); |
| 165 | put(DID1, N1, true, true); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 166 | put(DID1, N2, false, true); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 167 | put(DID2, N2, true, true); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 168 | MastershipEvent event = Futures.getUnchecked(sms.setStandby(N1, DID1)); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 169 | assertEquals("wrong event", MASTER_CHANGED, event.type()); |
| 170 | assertEquals("wrong master", N2, event.roleInfo().master()); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | //helper to populate master/backup structures |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 174 | private void put(DeviceId dev, NodeId node, boolean master, boolean backup) { |
| 175 | if (master) { |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 176 | sms.masterMap.put(dev, node); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 177 | } else if (backup) { |
| 178 | List<NodeId> stbys = sms.backups.getOrDefault(dev, new ArrayList<>()); |
| 179 | stbys.add(node); |
| 180 | sms.backups.put(dev, stbys); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 181 | } |
| 182 | sms.termMap.put(dev, new AtomicInteger()); |
| 183 | } |
| 184 | } |