blob: 52ee04fc04e467e950db09b20ecdfe6ef53703d5 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cluster.impl;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070017
18import java.util.Set;
19
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Ray Milkeycc53abd2015-02-19 12:31:33 -080023import org.onlab.packet.IpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cluster.ClusterService;
Ray Milkeycc53abd2015-02-19 12:31:33 -080025import org.onosproject.cluster.ClusterServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cluster.DefaultControllerNode;
28import org.onosproject.cluster.NodeId;
29import org.onosproject.event.impl.TestEventDispatcher;
30import org.onosproject.mastership.MastershipService;
31import org.onosproject.mastership.MastershipStore;
32import org.onosproject.mastership.MastershipTermService;
33import org.onosproject.net.DeviceId;
34import org.onosproject.store.trivial.impl.SimpleMastershipStore;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070035
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070036import com.google.common.collect.Sets;
37
Ayaka Koshibe406d0102014-09-24 16:08:12 -070038import static org.junit.Assert.assertEquals;
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070039import static org.junit.Assert.assertNull;
Ray Milkeycc53abd2015-02-19 12:31:33 -080040import static org.onosproject.net.MastershipRole.MASTER;
41import static org.onosproject.net.MastershipRole.NONE;
42import static org.onosproject.net.MastershipRole.STANDBY;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070043
44/**
45 * Test codifying the mastership service contracts.
46 */
47public class MastershipManagerTest {
48
49 private static final NodeId NID_LOCAL = new NodeId("local");
50 private static final NodeId NID_OTHER = new NodeId("foo");
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070051 private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
Ayaka Koshibe406d0102014-09-24 16:08:12 -070052 private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1");
53 private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2");
54
55 private MastershipManager mgr;
56 protected MastershipService service;
57
58 @Before
59 public void setUp() {
60 mgr = new MastershipManager();
61 service = mgr;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070062 mgr.eventDispatcher = new TestEventDispatcher();
63 mgr.clusterService = new TestClusterService();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080064 mgr.store = new TestSimpleMastershipStore(mgr.clusterService);
Ayaka Koshibe406d0102014-09-24 16:08:12 -070065 mgr.activate();
66 }
67
68 @After
69 public void tearDown() {
70 mgr.deactivate();
71 mgr.clusterService = null;
72 mgr.eventDispatcher = null;
73 mgr.store = null;
74 }
75
76 @Test
77 public void setRole() {
78 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080079 assertEquals("wrong local role:", NONE, mgr.getLocalRole(DEV_MASTER));
80 assertEquals("wrong obtained role:", STANDBY, mgr.requestRoleFor(DEV_MASTER));
Ayaka Koshibe406d0102014-09-24 16:08:12 -070081
82 //set to master
83 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
84 assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER));
85 }
86
87 @Test
88 public void relinquishMastership() {
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070089 //no backups - should just turn to NONE for device.
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070090 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
91 assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER));
92 mgr.relinquishMastership(DEV_MASTER);
93 assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER));
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070094 assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER));
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070095
96 //not master, nothing should happen
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070097 mgr.setRole(NID_LOCAL, DEV_OTHER, NONE);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070098 mgr.relinquishMastership(DEV_OTHER);
99 assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER));
100
101 //provide NID_OTHER as backup and relinquish
102 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
103 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
104 mgr.setRole(NID_OTHER, DEV_MASTER, STANDBY);
105 mgr.relinquishMastership(DEV_MASTER);
106 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_MASTER));
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700107 }
108
109 @Test
110 public void requestRoleFor() {
111 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
112 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
113
114 //local should be master for one but standby for other
115 assertEquals("wrong role:", MASTER, mgr.requestRoleFor(DEV_MASTER));
116 assertEquals("wrong role:", STANDBY, mgr.requestRoleFor(DEV_OTHER));
117 }
118
119 @Test
120 public void getMasterFor() {
121 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
122 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
123 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
124 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER));
125
126 //have NID_OTHER hand over DEV_OTHER to NID_LOCAL
127 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
128 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER));
129 }
130
131 @Test
132 public void getDevicesOf() {
133 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
134 mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY);
135 assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size());
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700136 //hand both devices to NID_LOCAL
137 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
138 assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size());
139 }
140
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700141 @Test
142 public void termService() {
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800143 MastershipTermService ts = mgr;
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700144
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700145 //term = 1 for both
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700146 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700147 assertEquals("inconsistent term: ", 1, ts.getMastershipTerm(DEV_MASTER).termNumber());
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700148
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700149 //hand devices to NID_LOCAL and back: term = 1 + 2
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700150 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
151 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700152 assertEquals("inconsistent terms: ", 3, ts.getMastershipTerm(DEV_MASTER).termNumber());
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700153 }
154
Ray Milkeycc53abd2015-02-19 12:31:33 -0800155 private final class TestClusterService extends ClusterServiceAdapter {
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700156
157 ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
158
159 @Override
160 public ControllerNode getLocalNode() {
161 return local;
162 }
163
164 @Override
165 public Set<ControllerNode> getNodes() {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700166 return Sets.newHashSet();
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700167 }
168
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700169 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800170
171 private final class TestSimpleMastershipStore extends SimpleMastershipStore
172 implements MastershipStore {
173
174 public TestSimpleMastershipStore(ClusterService clusterService) {
175 super.clusterService = clusterService;
176 }
177 }
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700178}