blob: 711e3669d84f05013e63cebdcef14008c23e75ae [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
18import java.util.Set;
19import java.util.concurrent.atomic.AtomicInteger;
20
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070024import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070025import org.onlab.onos.mastership.MastershipTerm;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070026import org.onlab.onos.net.DeviceId;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070027
28import com.google.common.collect.Sets;
29
30import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertNull;
32import static org.junit.Assert.assertTrue;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070033import static org.onlab.onos.mastership.MastershipEvent.Type.*;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070034import static org.onlab.onos.net.MastershipRole.*;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070035
36/**
37 * Test for the simple MastershipStore implementation.
38 */
39public class SimpleMastershipStoreTest {
40
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070041 private static final DeviceId DID1 = DeviceId.deviceId("of:01");
42 private static final DeviceId DID2 = DeviceId.deviceId("of:02");
43 private static final DeviceId DID3 = DeviceId.deviceId("of:03");
44 private static final DeviceId DID4 = DeviceId.deviceId("of:04");
45
46 private static final NodeId N1 = new NodeId("local");
47 private static final NodeId N2 = new NodeId("other");
48
49 private SimpleMastershipStore sms;
50
51 @Before
52 public void setUp() throws Exception {
53 sms = new SimpleMastershipStore();
54 sms.activate();
55 }
56
57 @After
58 public void tearDown() throws Exception {
59 sms.deactivate();
60 }
61
62 @Test
63 public void getRole() {
64 //special case, no backup or master
65 put(DID1, N1, false, false);
66 assertEquals("wrong role", NONE, sms.getRole(N1, DID1));
67
68 //backup exists but we aren't mapped
69 put(DID2, N1, false, true);
70 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
71
72 //N2 is master
73 put(DID3, N2, true, true);
74 assertEquals("wrong role", MASTER, sms.getRole(N2, DID3));
75
76 //N2 is master but N1 is only in backups set
77 put(DID4, N2, true, false);
78 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID4));
79 }
80
81 @Test
82 public void getMaster() {
83 put(DID3, N2, true, true);
84 assertEquals("wrong role", MASTER, sms.getRole(N2, DID3));
85 assertEquals("wrong device", N2, sms.getMaster(DID3));
86 }
87
88 @Test
89 public void setMaster() {
90 put(DID1, N1, false, false);
91 assertEquals("wrong event", MASTER_CHANGED, sms.setMaster(N1, DID1).type());
92 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
93 //set node that's already master - should be ignored
94 assertNull("wrong event", sms.setMaster(N1, DID1));
95
96 //set STANDBY to MASTER
97 put(DID2, N1, false, true);
98 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
99 assertEquals("wrong event", MASTER_CHANGED, sms.setMaster(N1, DID2).type());
100 assertEquals("wrong role", MASTER, sms.getRole(N1, DID2));
101 }
102
103 @Test
104 public void getDevices() {
105 Set<DeviceId> d = Sets.newHashSet(DID1, DID2);
106
107 put(DID1, N2, true, true);
108 put(DID2, N2, true, true);
109 put(DID3, N1, true, true);
110 assertTrue("wrong devices", d.equals(sms.getDevices(N2)));
111 }
112
113 @Test
114 public void getTermFor() {
115 put(DID1, N1, true, true);
116 assertEquals("wrong term", MastershipTerm.of(N1, 0), sms.getTermFor(DID1));
117
118 //switch to N2 and back - 2 term switches
119 sms.setMaster(N2, DID1);
120 sms.setMaster(N1, DID1);
121 assertEquals("wrong term", MastershipTerm.of(N1, 2), sms.getTermFor(DID1));
122 }
123
124 @Test
125 public void requestRole() {
126 //NONE - become MASTER
127 put(DID1, N1, false, false);
128 assertEquals("wrong role", MASTER, sms.requestRole(DID1));
129
130 //STANDBY without backup - become MASTER
131 put(DID2, N1, false, true);
132 assertEquals("wrong role", MASTER, sms.requestRole(DID2));
133
134 //STANDBY with backup - stay STANDBY
135 put(DID3, N2, false, true);
136 assertEquals("wrong role", STANDBY, sms.requestRole(DID3));
137
138 //local (N1) is MASTER - stay MASTER
139 put(DID4, N1, true, true);
140 assertEquals("wrong role", MASTER, sms.requestRole(DID4));
141 }
142
143 @Test
144 public void unsetMaster() {
145 //NONE - record backup but take no other action
146 put(DID1, N1, false, false);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700147 sms.setStandby(N1, DID1);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700148 assertTrue("not backed up", sms.backups.contains(N1));
149 sms.termMap.clear();
Ayaka Koshibec4047702014-10-07 14:43:52 -0700150 sms.setStandby(N1, DID1);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700151 assertTrue("term not set", sms.termMap.containsKey(DID1));
152
153 //no backup, MASTER
154 put(DID1, N1, true, true);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700155 assertNull("wrong event", sms.setStandby(N1, DID1));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700156 assertNull("wrong node", sms.masterMap.get(DID1));
157
158 //backup, switch
159 sms.masterMap.clear();
160 put(DID1, N1, true, true);
161 put(DID2, N2, true, true);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700162 assertEquals("wrong event", MASTER_CHANGED, sms.setStandby(N1, DID1).type());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700163 }
164
165 //helper to populate master/backup structures
166 private void put(DeviceId dev, NodeId node, boolean store, boolean backup) {
167 if (store) {
168 sms.masterMap.put(dev, node);
169 }
170 if (backup) {
171 sms.backups.add(node);
172 }
173 sms.termMap.put(dev, new AtomicInteger());
174 }
175}