blob: bf1a1ff37ffd264013d6ccc400aaa1accec828af [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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070044import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Ayaka Koshibe406d0102014-09-24 16:08:12 -070045
46/**
47 * Test codifying the mastership service contracts.
48 */
49public class MastershipManagerTest {
50
51 private static final NodeId NID_LOCAL = new NodeId("local");
52 private static final NodeId NID_OTHER = new NodeId("foo");
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070053 private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
Ayaka Koshibe406d0102014-09-24 16:08:12 -070054 private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1");
55 private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2");
56
57 private MastershipManager mgr;
58 protected MastershipService service;
59
60 @Before
61 public void setUp() {
62 mgr = new MastershipManager();
63 service = mgr;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070064 injectEventDispatcher(mgr, new TestEventDispatcher());
Ayaka Koshibe406d0102014-09-24 16:08:12 -070065 mgr.clusterService = new TestClusterService();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080066 mgr.store = new TestSimpleMastershipStore(mgr.clusterService);
Ayaka Koshibe406d0102014-09-24 16:08:12 -070067 mgr.activate();
68 }
69
70 @After
71 public void tearDown() {
72 mgr.deactivate();
73 mgr.clusterService = null;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070074 injectEventDispatcher(mgr, null);
Ayaka Koshibe406d0102014-09-24 16:08:12 -070075 mgr.store = null;
76 }
77
78 @Test
79 public void setRole() {
80 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080081 assertEquals("wrong local role:", NONE, mgr.getLocalRole(DEV_MASTER));
Madan Jampanide003d92015-05-11 17:14:20 -070082 assertEquals("wrong obtained role:", STANDBY, Futures.getUnchecked(mgr.requestRoleFor(DEV_MASTER)));
Ayaka Koshibe406d0102014-09-24 16:08:12 -070083
84 //set to master
85 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
86 assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER));
87 }
88
89 @Test
90 public void relinquishMastership() {
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070091 //no backups - should just turn to NONE for device.
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070092 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
93 assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER));
94 mgr.relinquishMastership(DEV_MASTER);
95 assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER));
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070096 assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER));
Ayaka Koshibed9f693e2014-09-29 18:04:54 -070097
98 //not master, nothing should happen
Ayaka Koshibeb62aab52014-10-24 13:15:25 -070099 mgr.setRole(NID_LOCAL, DEV_OTHER, NONE);
Ayaka Koshibed9f693e2014-09-29 18:04:54 -0700100 mgr.relinquishMastership(DEV_OTHER);
101 assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER));
102
103 //provide NID_OTHER as backup and relinquish
104 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
105 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
106 mgr.setRole(NID_OTHER, DEV_MASTER, STANDBY);
107 mgr.relinquishMastership(DEV_MASTER);
108 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_MASTER));
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700109 }
110
111 @Test
112 public void requestRoleFor() {
113 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
114 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
115
116 //local should be master for one but standby for other
Madan Jampanide003d92015-05-11 17:14:20 -0700117 assertEquals("wrong role:", MASTER, Futures.getUnchecked(mgr.requestRoleFor(DEV_MASTER)));
118 assertEquals("wrong role:", STANDBY, Futures.getUnchecked(mgr.requestRoleFor(DEV_OTHER)));
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700119 }
120
121 @Test
122 public void getMasterFor() {
123 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
124 mgr.setRole(NID_OTHER, DEV_OTHER, MASTER);
125 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER));
126 assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER));
127
128 //have NID_OTHER hand over DEV_OTHER to NID_LOCAL
129 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
130 assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER));
131 }
132
133 @Test
134 public void getDevicesOf() {
135 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
136 mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY);
137 assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size());
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700138 //hand both devices to NID_LOCAL
139 mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER);
140 assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size());
141 }
142
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700143 @Test
144 public void termService() {
Yuta HIGUCHIbcac4992014-11-22 19:27:57 -0800145 MastershipTermService ts = mgr;
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700146
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700147 //term = 1 for both
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700148 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700149 assertEquals("inconsistent term: ", 1, ts.getMastershipTerm(DEV_MASTER).termNumber());
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700150
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700151 //hand devices to NID_LOCAL and back: term = 1 + 2
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700152 mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
153 mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700154 assertEquals("inconsistent terms: ", 3, ts.getMastershipTerm(DEV_MASTER).termNumber());
Ayaka Koshibe48239b02014-09-25 17:12:31 -0700155 }
156
Ray Milkeycc53abd2015-02-19 12:31:33 -0800157 private final class TestClusterService extends ClusterServiceAdapter {
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700158
159 ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
160
161 @Override
162 public ControllerNode getLocalNode() {
163 return local;
164 }
165
166 @Override
167 public Set<ControllerNode> getNodes() {
Ayaka Koshibeea5b4ce2014-10-11 14:17:17 -0700168 return Sets.newHashSet();
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700169 }
170
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700171 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800172
173 private final class TestSimpleMastershipStore extends SimpleMastershipStore
174 implements MastershipStore {
175
176 public TestSimpleMastershipStore(ClusterService clusterService) {
177 super.clusterService = clusterService;
178 }
179 }
Ayaka Koshibe406d0102014-09-24 16:08:12 -0700180}