Yoonseon Han | a327701 | 2017-05-22 12:26:21 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yoonseon Han | a327701 | 2017-05-22 12:26:21 -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 | */ |
| 16 | |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 17 | package org.onosproject.incubator.net.virtual.store.impl; |
Yoonseon Han | a327701 | 2017-05-22 12:26:21 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import com.google.common.util.concurrent.Futures; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 26 | import org.onosproject.mastership.MastershipEvent; |
| 27 | import org.onosproject.mastership.MastershipTerm; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | import java.util.concurrent.atomic.AtomicInteger; |
| 35 | |
| 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.junit.Assert.assertNull; |
| 38 | import static org.junit.Assert.assertTrue; |
| 39 | import static org.onosproject.mastership.MastershipEvent.Type.MASTER_CHANGED; |
| 40 | import static org.onosproject.net.MastershipRole.MASTER; |
| 41 | import static org.onosproject.net.MastershipRole.NONE; |
| 42 | import static org.onosproject.net.MastershipRole.STANDBY; |
| 43 | |
| 44 | public class SimpleVirtualMastershipStoreTest { |
| 45 | |
Yuta HIGUCHI | d73c9be | 2017-05-25 09:31:40 -0700 | [diff] [blame] | 46 | private static final NetworkId VNID1 = NetworkId.networkId(1); |
Yoonseon Han | a327701 | 2017-05-22 12:26:21 -0700 | [diff] [blame] | 47 | |
| 48 | private static final DeviceId VDID1 = DeviceId.deviceId("of:01"); |
| 49 | private static final DeviceId VDID2 = DeviceId.deviceId("of:02"); |
| 50 | private static final DeviceId VDID3 = DeviceId.deviceId("of:03"); |
| 51 | private static final DeviceId VDID4 = DeviceId.deviceId("of:04"); |
| 52 | |
| 53 | private static final NodeId N1 = new NodeId("local"); |
| 54 | private static final NodeId N2 = new NodeId("other"); |
| 55 | |
| 56 | private SimpleVirtualMastershipStore sms; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | sms = new SimpleVirtualMastershipStore(); |
| 61 | sms.activate(); |
| 62 | } |
| 63 | |
| 64 | @After |
| 65 | public void tearDown() throws Exception { |
| 66 | sms.deactivate(); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void getRole() { |
| 71 | //special case, no backup or master |
| 72 | put(VNID1, VDID1, N1, false, false); |
| 73 | assertEquals("wrong role", NONE, sms.getRole(VNID1, N1, VDID1)); |
| 74 | |
| 75 | //backup exists but we aren't mapped |
| 76 | put(VNID1, VDID2, N1, false, true); |
| 77 | assertEquals("wrong role", STANDBY, sms.getRole(VNID1, N1, VDID2)); |
| 78 | |
| 79 | //N2 is master |
| 80 | put(VNID1, VDID3, N2, true, true); |
| 81 | assertEquals("wrong role", MASTER, sms.getRole(VNID1, N2, VDID3)); |
| 82 | |
| 83 | //N2 is master but N1 is only in backups set |
| 84 | put(VNID1, VDID4, N1, false, true); |
| 85 | put(VNID1, VDID4, N2, true, false); |
| 86 | assertEquals("wrong role", STANDBY, sms.getRole(VNID1, N1, VDID4)); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void getMaster() { |
| 91 | put(VNID1, VDID3, N2, true, true); |
| 92 | assertEquals("wrong role", MASTER, sms.getRole(VNID1, N2, VDID3)); |
| 93 | assertEquals("wrong node", N2, sms.getMaster(VNID1, VDID3)); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void setMaster() { |
| 98 | put(VNID1, VDID1, N1, false, false); |
| 99 | assertEquals("wrong event", MASTER_CHANGED, |
| 100 | Futures.getUnchecked(sms.setMaster(VNID1, N1, VDID1)).type()); |
| 101 | assertEquals("wrong role", MASTER, sms.getRole(VNID1, N1, VDID1)); |
| 102 | //set node that's already master - should be ignored |
| 103 | assertNull("wrong event", |
| 104 | Futures.getUnchecked(sms.setMaster(VNID1, N1, VDID1))); |
| 105 | |
| 106 | //set STANDBY to MASTER |
| 107 | put(VNID1, VDID2, N1, false, true); |
| 108 | assertEquals("wrong role", STANDBY, sms.getRole(VNID1, N1, VDID2)); |
| 109 | assertEquals("wrong event", MASTER_CHANGED, |
| 110 | Futures.getUnchecked(sms.setMaster(VNID1, N1, VDID2)).type()); |
| 111 | assertEquals("wrong role", MASTER, sms.getRole(VNID1, N1, VDID2)); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void getDevices() { |
| 116 | Set<DeviceId> d = Sets.newHashSet(VDID1, VDID2); |
| 117 | |
| 118 | put(VNID1, VDID1, N2, true, true); |
| 119 | put(VNID1, VDID2, N2, true, true); |
| 120 | put(VNID1, VDID3, N1, true, true); |
| 121 | assertTrue("wrong devices", d.equals(sms.getDevices(VNID1, N2))); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void getTermFor() { |
| 126 | put(VNID1, VDID1, N1, true, true); |
| 127 | assertEquals("wrong term", MastershipTerm.of(N1, 0), |
| 128 | sms.getTermFor(VNID1, VDID1)); |
| 129 | |
| 130 | //switch to N2 and back - 2 term switches |
| 131 | sms.setMaster(VNID1, N2, VDID1); |
| 132 | sms.setMaster(VNID1, N1, VDID1); |
| 133 | assertEquals("wrong term", MastershipTerm.of(N1, 2), |
| 134 | sms.getTermFor(VNID1, VDID1)); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void requestRole() { |
| 139 | //NONE - become MASTER |
| 140 | put(VNID1, VDID1, N1, false, false); |
| 141 | assertEquals("wrong role", MASTER, |
| 142 | Futures.getUnchecked(sms.requestRole(VNID1, VDID1))); |
| 143 | |
| 144 | //was STANDBY - become MASTER |
| 145 | put(VNID1, VDID2, N1, false, true); |
| 146 | assertEquals("wrong role", MASTER, |
| 147 | Futures.getUnchecked(sms.requestRole(VNID1, VDID2))); |
| 148 | |
| 149 | //other MASTER - stay STANDBY |
| 150 | put(VNID1, VDID3, N2, true, false); |
| 151 | assertEquals("wrong role", STANDBY, |
| 152 | Futures.getUnchecked(sms.requestRole(VNID1, VDID3))); |
| 153 | |
| 154 | //local (N1) is MASTER - stay MASTER |
| 155 | put(VNID1, VDID4, N1, true, true); |
| 156 | assertEquals("wrong role", MASTER, |
| 157 | Futures.getUnchecked(sms.requestRole(VNID1, VDID4))); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void unsetMaster() { |
| 162 | //NONE - record backup but take no other action |
| 163 | put(VNID1, VDID1, N1, false, false); |
| 164 | sms.setStandby(VNID1, N1, VDID1); |
| 165 | assertTrue("not backed up", sms.backupsByNetwork.get(VNID1) |
| 166 | .get(VDID1).contains(N1)); |
| 167 | int prev = sms.termMapByNetwork.get(VNID1).get(VDID1).get(); |
| 168 | sms.setStandby(VNID1, N1, VDID1); |
| 169 | assertEquals("term should not change", prev, sms.termMapByNetwork.get(VNID1) |
| 170 | .get(VDID1).get()); |
| 171 | |
| 172 | //no backup, MASTER |
| 173 | put(VNID1, VDID1, N1, true, false); |
| 174 | assertNull("expect no MASTER event", |
| 175 | Futures.getUnchecked(sms.setStandby(VNID1, N1, VDID1)).roleInfo().master()); |
| 176 | assertNull("wrong node", sms.masterMapByNetwork.get(VNID1).get(VDID1)); |
| 177 | |
| 178 | //backup, switch |
| 179 | sms.masterMapByNetwork.get(VNID1).clear(); |
| 180 | put(VNID1, VDID1, N1, true, true); |
| 181 | put(VNID1, VDID1, N2, false, true); |
| 182 | put(VNID1, VDID2, N2, true, true); |
| 183 | MastershipEvent event = Futures.getUnchecked(sms.setStandby(VNID1, N1, VDID1)); |
| 184 | assertEquals("wrong event", MASTER_CHANGED, event.type()); |
| 185 | assertEquals("wrong master", N2, event.roleInfo().master()); |
| 186 | } |
| 187 | |
| 188 | //helper to populate master/backup structures |
| 189 | private void put(NetworkId networkId, DeviceId dev, NodeId node, |
| 190 | boolean master, boolean backup) { |
| 191 | if (master) { |
| 192 | sms.masterMapByNetwork |
| 193 | .computeIfAbsent(networkId, k -> new HashMap<>()) |
| 194 | .put(dev, node); |
| 195 | } else if (backup) { |
| 196 | List<NodeId> stbys = sms.backupsByNetwork |
| 197 | .computeIfAbsent(networkId, k -> new HashMap<>()) |
| 198 | .getOrDefault(dev, new ArrayList<>()); |
| 199 | stbys.add(node); |
| 200 | sms.backupsByNetwork.get(networkId).put(dev, stbys); |
| 201 | } |
| 202 | |
| 203 | sms.termMapByNetwork |
| 204 | .computeIfAbsent(networkId, k -> new HashMap<>()) |
| 205 | .put(dev, new AtomicInteger()); |
| 206 | } |
| 207 | } |