blob: 58d35066ab4c53466111be908c0b5ae646cbe754 [file] [log] [blame]
Yoonseon Hana3277012017-05-22 12:26:21 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yoonseon Hana3277012017-05-22 12:26:21 -07003 *
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 Vachuska52f2cd12018-11-08 21:20:04 -080017package org.onosproject.incubator.net.virtual.store.impl;
Yoonseon Hana3277012017-05-22 12:26:21 -070018
19import com.google.common.collect.Sets;
20import com.google.common.util.concurrent.Futures;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.incubator.net.virtual.NetworkId;
26import org.onosproject.mastership.MastershipEvent;
27import org.onosproject.mastership.MastershipTerm;
28import org.onosproject.net.DeviceId;
29
30import java.util.ArrayList;
31import java.util.HashMap;
32import java.util.List;
33import java.util.Set;
34import java.util.concurrent.atomic.AtomicInteger;
35
36import static org.junit.Assert.assertEquals;
37import static org.junit.Assert.assertNull;
38import static org.junit.Assert.assertTrue;
39import static org.onosproject.mastership.MastershipEvent.Type.MASTER_CHANGED;
40import static org.onosproject.net.MastershipRole.MASTER;
41import static org.onosproject.net.MastershipRole.NONE;
42import static org.onosproject.net.MastershipRole.STANDBY;
43
44public class SimpleVirtualMastershipStoreTest {
45
Yuta HIGUCHId73c9be2017-05-25 09:31:40 -070046 private static final NetworkId VNID1 = NetworkId.networkId(1);
Yoonseon Hana3277012017-05-22 12:26:21 -070047
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}