blob: 477c7c4da5babdacf9f34803ddd894148428e5fc [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 */
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;
Thomas Vachuska36002e62015-05-19 16:12:29 -070029import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.mastership.MastershipService;
31import org.onosproject.mastership.MastershipStore;
32import org.onosproject.mastership.MastershipTermService;
33import org.onosproject.net.DeviceId;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070034import org.onosproject.store.trivial.SimpleMastershipStore;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070035
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070036import com.google.common.collect.Sets;
Madan Jampanide003d92015-05-11 17:14:20 -070037import com.google.common.util.concurrent.Futures;
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -070038
Ayaka Koshibe406d0102014-09-24 16:08:12 -070039import static org.junit.Assert.assertEquals;
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070040import static org.junit.Assert.assertNull;
Ray Milkeycc53abd2015-02-19 12:31:33 -080041import static org.onosproject.net.MastershipRole.MASTER;
42import static org.onosproject.net.MastershipRole.NONE;
43import static org.onosproject.net.MastershipRole.STANDBY;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070044
45/**
46 * Test codifying the mastership service contracts.
47 */
48public class MastershipManagerTest {
49
50 private static final NodeId NID_LOCAL = new NodeId("local");
51 private static final NodeId NID_OTHER = new NodeId("foo");
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070052 private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
Ayaka Koshibe406d0102014-09-24 16:08:12 -070053 private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1");
54 private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2");
55
56 private MastershipManager mgr;
57 protected MastershipService service;
58
59 @Before
60 public void setUp() {
61 mgr = new MastershipManager();
62 service = mgr;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070063 mgr.eventDispatcher = new TestEventDispatcher();
64 mgr.clusterService = new TestClusterService();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080065 mgr.store = new TestSimpleMastershipStore(mgr.clusterService);
Ayaka Koshibe406d0102014-09-24 16:08:12 -070066 mgr.activate();
67 }
68
69 @After
70 public void tearDown() {
71 mgr.deactivate();
72 mgr.clusterService = null;
73 mgr.eventDispatcher = null;
74 mgr.store = null;
75 }
76
77 @Test
78 public void setRole() {
79 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080080 assertEquals("wrong local role:", NONE, mgr.getLocalRole(DEV_MASTER));
Madan Jampanide003d92015-05-11 17:14:20 -070081 assertEquals("wrong obtained role:", STANDBY, Futures.getUnchecked(mgr.requestRoleFor(DEV_MASTER)));
Ayaka Koshibe406d0102014-09-24 16:08:12 -070082
83 //set to master
84 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
85 assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER));
86 }
87
88 @Test
89 public void relinquishMastership() {
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070090 //no backups - should just turn to NONE for device.
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070091 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
92 assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER));
93 mgr.relinquishMastership(DEV_MASTER);
94 assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER));
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070095 assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER));
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070096
97 //not master, nothing should happen
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070098 mgr.setRole(NID_LOCAL, DEV_OTHER, NONE);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070099 mgr.relinquishMastership(DEV_OTHER);
100 assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER));
101
102 //provide NID_OTHER as backup and relinquish
103 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
104 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
105 mgr.setRole(NID_OTHER, DEV_MASTER, STANDBY);
106 mgr.relinquishMastership(DEV_MASTER);
107 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_MASTER));
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700108 }
109
110 @Test
111 public void requestRoleFor() {
112 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
113 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
114
115 //local should be master for one but standby for other
Madan Jampanide003d92015-05-11 17:14:20 -0700116 assertEquals("wrong role:", MASTER, Futures.getUnchecked(mgr.requestRoleFor(DEV_MASTER)));
117 assertEquals("wrong role:", STANDBY, Futures.getUnchecked(mgr.requestRoleFor(DEV_OTHER)));
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700118 }
119
120 @Test
121 public void getMasterFor() {
122 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
123 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
124 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
125 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER));
126
127 //have NID_OTHER hand over DEV_OTHER to NID_LOCAL
128 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
129 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER));
130 }
131
132 @Test
133 public void getDevicesOf() {
134 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
135 mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY);
136 assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size());
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700137 //hand both devices to NID_LOCAL
138 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
139 assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size());
140 }
141
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700142 @Test
143 public void termService() {
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800144 MastershipTermService ts = mgr;
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700145
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700146 //term = 1 for both
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700147 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700148 assertEquals("inconsistent term: ", 1, ts.getMastershipTerm(DEV_MASTER).termNumber());
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700149
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700150 //hand devices to NID_LOCAL and back: term = 1 + 2
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700151 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
152 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700153 assertEquals("inconsistent terms: ", 3, ts.getMastershipTerm(DEV_MASTER).termNumber());
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700154 }
155
Ray Milkeycc53abd2015-02-19 12:31:33 -0800156 private final class TestClusterService extends ClusterServiceAdapter {
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700157
158 ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
159
160 @Override
161 public ControllerNode getLocalNode() {
162 return local;
163 }
164
165 @Override
166 public Set<ControllerNode> getNodes() {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700167 return Sets.newHashSet();
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700168 }
169
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700170 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800171
172 private final class TestSimpleMastershipStore extends SimpleMastershipStore
173 implements MastershipStore {
174
175 public TestSimpleMastershipStore(ClusterService clusterService) {
176 super.clusterService = clusterService;
177 }
178 }
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700179}