blob: 0998e0ae1d6f530596ff6ae7a1121296c71b9942 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
tomea961ff2014-10-01 12:45:15 -070016package org.onlab.onos.store.trivial.impl;
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;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070026import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080027import org.onlab.onos.mastership.MastershipEvent;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070028import org.onlab.onos.mastership.MastershipTerm;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070029import org.onlab.onos.net.DeviceId;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070030
31import com.google.common.collect.Sets;
32
33import static org.junit.Assert.assertEquals;
34import static org.junit.Assert.assertNull;
35import static org.junit.Assert.assertTrue;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070036import static org.onlab.onos.mastership.MastershipEvent.Type.*;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070037import static org.onlab.onos.net.MastershipRole.*;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070038
39/**
40 * Test for the simple MastershipStore implementation.
41 */
42public class SimpleMastershipStoreTest {
43
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070044 private static final DeviceId DID1 = DeviceId.deviceId("of:01");
45 private static final DeviceId DID2 = DeviceId.deviceId("of:02");
46 private static final DeviceId DID3 = DeviceId.deviceId("of:03");
47 private static final DeviceId DID4 = DeviceId.deviceId("of:04");
48
49 private static final NodeId N1 = new NodeId("local");
50 private static final NodeId N2 = new NodeId("other");
51
52 private SimpleMastershipStore sms;
53
54 @Before
55 public void setUp() throws Exception {
56 sms = new SimpleMastershipStore();
57 sms.activate();
58 }
59
60 @After
61 public void tearDown() throws Exception {
62 sms.deactivate();
63 }
64
65 @Test
66 public void getRole() {
67 //special case, no backup or master
68 put(DID1, N1, false, false);
69 assertEquals("wrong role", NONE, sms.getRole(N1, DID1));
70
71 //backup exists but we aren't mapped
72 put(DID2, N1, false, true);
73 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
74
75 //N2 is master
76 put(DID3, N2, true, true);
77 assertEquals("wrong role", MASTER, sms.getRole(N2, DID3));
78
79 //N2 is master but N1 is only in backups set
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080080 put(DID4, N1, false, true);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070081 put(DID4, N2, true, false);
82 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID4));
83 }
84
85 @Test
86 public void getMaster() {
87 put(DID3, N2, true, true);
88 assertEquals("wrong role", MASTER, sms.getRole(N2, DID3));
89 assertEquals("wrong device", N2, sms.getMaster(DID3));
90 }
91
92 @Test
93 public void setMaster() {
94 put(DID1, N1, false, false);
95 assertEquals("wrong event", MASTER_CHANGED, sms.setMaster(N1, DID1).type());
96 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
97 //set node that's already master - should be ignored
98 assertNull("wrong event", sms.setMaster(N1, DID1));
99
100 //set STANDBY to MASTER
101 put(DID2, N1, false, true);
102 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
103 assertEquals("wrong event", MASTER_CHANGED, sms.setMaster(N1, DID2).type());
104 assertEquals("wrong role", MASTER, sms.getRole(N1, DID2));
105 }
106
107 @Test
108 public void getDevices() {
109 Set<DeviceId> d = Sets.newHashSet(DID1, DID2);
110
111 put(DID1, N2, true, true);
112 put(DID2, N2, true, true);
113 put(DID3, N1, true, true);
114 assertTrue("wrong devices", d.equals(sms.getDevices(N2)));
115 }
116
117 @Test
118 public void getTermFor() {
119 put(DID1, N1, true, true);
120 assertEquals("wrong term", MastershipTerm.of(N1, 0), sms.getTermFor(DID1));
121
122 //switch to N2 and back - 2 term switches
123 sms.setMaster(N2, DID1);
124 sms.setMaster(N1, DID1);
125 assertEquals("wrong term", MastershipTerm.of(N1, 2), sms.getTermFor(DID1));
126 }
127
128 @Test
129 public void requestRole() {
130 //NONE - become MASTER
131 put(DID1, N1, false, false);
132 assertEquals("wrong role", MASTER, sms.requestRole(DID1));
133
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800134 //was STANDBY - become MASTER
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700135 put(DID2, N1, false, true);
136 assertEquals("wrong role", MASTER, sms.requestRole(DID2));
137
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800138 //other MASTER - stay STANDBY
139 put(DID3, N2, true, false);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700140 assertEquals("wrong role", STANDBY, sms.requestRole(DID3));
141
142 //local (N1) is MASTER - stay MASTER
143 put(DID4, N1, true, true);
144 assertEquals("wrong role", MASTER, sms.requestRole(DID4));
145 }
146
147 @Test
148 public void unsetMaster() {
149 //NONE - record backup but take no other action
150 put(DID1, N1, false, false);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700151 sms.setStandby(N1, DID1);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800152 assertTrue("not backed up", sms.backups.get(DID1).contains(N1));
153 int prev = sms.termMap.get(DID1).get();
Ayaka Koshibec4047702014-10-07 14:43:52 -0700154 sms.setStandby(N1, DID1);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800155 assertEquals("term should not change", prev, sms.termMap.get(DID1).get());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700156
157 //no backup, MASTER
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800158 put(DID1, N1, true, false);
159 assertNull("expect no MASTER event", sms.setStandby(N1, DID1).roleInfo().master());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700160 assertNull("wrong node", sms.masterMap.get(DID1));
161
162 //backup, switch
163 sms.masterMap.clear();
164 put(DID1, N1, true, true);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800165 put(DID1, N2, false, true);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700166 put(DID2, N2, true, true);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800167 MastershipEvent event = sms.setStandby(N1, DID1);
168 assertEquals("wrong event", MASTER_CHANGED, event.type());
169 assertEquals("wrong master", N2, event.roleInfo().master());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700170 }
171
172 //helper to populate master/backup structures
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800173 private void put(DeviceId dev, NodeId node, boolean master, boolean backup) {
174 if (master) {
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700175 sms.masterMap.put(dev, node);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800176 } else if (backup) {
177 List<NodeId> stbys = sms.backups.getOrDefault(dev, new ArrayList<>());
178 stbys.add(node);
179 sms.backups.put(dev, stbys);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700180 }
181 sms.termMap.put(dev, new AtomicInteger());
182 }
183}