blob: 35a77627a867630de99d19994226e83d21fce7be [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070017
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080018import java.util.ArrayList;
19import java.util.List;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070020import java.util.Set;
21import java.util.concurrent.atomic.AtomicInteger;
22
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cluster.NodeId;
27import org.onosproject.mastership.MastershipEvent;
28import org.onosproject.mastership.MastershipTerm;
29import org.onosproject.net.DeviceId;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070030
31import com.google.common.collect.Sets;
Madan Jampanif7536ab2015-05-07 23:23:23 -070032import com.google.common.util.concurrent.Futures;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070033
34import static org.junit.Assert.assertEquals;
35import static org.junit.Assert.assertNull;
36import static org.junit.Assert.assertTrue;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import static org.onosproject.mastership.MastershipEvent.Type.*;
38import static org.onosproject.net.MastershipRole.*;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070039
40/**
41 * Test for the simple MastershipStore implementation.
42 */
43public class SimpleMastershipStoreTest {
44
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070045 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 HIGUCHI0c6e1842014-11-05 22:34:23 -080081 put(DID4, N1, false, true);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070082 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 Jampanif7536ab2015-05-07 23:23:23 -070096 assertEquals("wrong event", MASTER_CHANGED, Futures.getUnchecked(sms.setMaster(N1, DID1)).type());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070097 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
98 //set node that's already master - should be ignored
Madan Jampanif7536ab2015-05-07 23:23:23 -070099 assertNull("wrong event", Futures.getUnchecked(sms.setMaster(N1, DID1)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700100
101 //set STANDBY to MASTER
102 put(DID2, N1, false, true);
103 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
Madan Jampanif7536ab2015-05-07 23:23:23 -0700104 assertEquals("wrong event", MASTER_CHANGED, Futures.getUnchecked(sms.setMaster(N1, DID2)).type());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700105 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 Jampanide003d92015-05-11 17:14:20 -0700133 assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID1)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700134
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800135 //was STANDBY - become MASTER
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700136 put(DID2, N1, false, true);
Madan Jampanide003d92015-05-11 17:14:20 -0700137 assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID2)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700138
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800139 //other MASTER - stay STANDBY
140 put(DID3, N2, true, false);
Madan Jampanide003d92015-05-11 17:14:20 -0700141 assertEquals("wrong role", STANDBY, Futures.getUnchecked(sms.requestRole(DID3)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700142
143 //local (N1) is MASTER - stay MASTER
144 put(DID4, N1, true, true);
Madan Jampanide003d92015-05-11 17:14:20 -0700145 assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID4)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700146 }
147
148 @Test
149 public void unsetMaster() {
150 //NONE - record backup but take no other action
151 put(DID1, N1, false, false);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700152 sms.setStandby(N1, DID1);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800153 assertTrue("not backed up", sms.backups.get(DID1).contains(N1));
154 int prev = sms.termMap.get(DID1).get();
Ayaka Koshibec4047702014-10-07 14:43:52 -0700155 sms.setStandby(N1, DID1);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800156 assertEquals("term should not change", prev, sms.termMap.get(DID1).get());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700157
158 //no backup, MASTER
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800159 put(DID1, N1, true, false);
Madan Jampanif7536ab2015-05-07 23:23:23 -0700160 assertNull("expect no MASTER event", Futures.getUnchecked(sms.setStandby(N1, DID1)).roleInfo().master());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700161 assertNull("wrong node", sms.masterMap.get(DID1));
162
163 //backup, switch
164 sms.masterMap.clear();
165 put(DID1, N1, true, true);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800166 put(DID1, N2, false, true);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700167 put(DID2, N2, true, true);
Madan Jampanif7536ab2015-05-07 23:23:23 -0700168 MastershipEvent event = Futures.getUnchecked(sms.setStandby(N1, DID1));
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800169 assertEquals("wrong event", MASTER_CHANGED, event.type());
170 assertEquals("wrong master", N2, event.roleInfo().master());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700171 }
172
173 //helper to populate master/backup structures
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800174 private void put(DeviceId dev, NodeId node, boolean master, boolean backup) {
175 if (master) {
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700176 sms.masterMap.put(dev, node);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800177 } else if (backup) {
178 List<NodeId> stbys = sms.backups.getOrDefault(dev, new ArrayList<>());
179 stbys.add(node);
180 sms.backups.put(dev, stbys);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700181 }
182 sms.termMap.put(dev, new AtomicInteger());
183 }
184}