blob: fb0e4060f4d31aa08020d19c1e7471b15c51bef9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.openflow.controller.impl;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070017
18import java.io.IOException;
19import java.util.List;
20
21import org.jboss.netty.channel.Channel;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.openflow.controller.RoleState;
Saurav Dasfa2fa932015-03-03 11:29:48 -080026import org.onosproject.openflow.controller.OpenFlowSwitch.TableType;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.openflow.controller.driver.OpenFlowAgent;
28import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
29import org.onosproject.openflow.controller.driver.RoleHandler;
30import org.onosproject.openflow.controller.driver.RoleRecvStatus;
31import org.onosproject.openflow.controller.driver.RoleReplyInfo;
32import org.onosproject.openflow.controller.driver.SwitchStateException;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070033import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
34import org.projectfloodlight.openflow.protocol.OFErrorMsg;
35import org.projectfloodlight.openflow.protocol.OFFactories;
36import org.projectfloodlight.openflow.protocol.OFFactory;
37import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
38import org.projectfloodlight.openflow.protocol.OFMessage;
39import org.projectfloodlight.openflow.protocol.OFPortDesc;
40import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply;
41import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Dasfa2fa932015-03-03 11:29:48 -080042import org.projectfloodlight.openflow.types.TableId;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070043import org.projectfloodlight.openflow.types.U64;
44
45import static org.junit.Assert.assertEquals;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.openflow.controller.RoleState.*;
47import static org.onosproject.openflow.controller.driver.RoleRecvStatus.*;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070048
49public class RoleManagerTest {
50
51 private static final U64 GID = U64.of(10L);
52 private static final long XID = 1L;
53
54 private OpenFlowSwitchDriver sw;
55 private RoleManager manager;
56
57 @Before
58 public void setUp() {
59 sw = new TestSwitchDriver();
60 manager = new RoleManager(sw);
61 }
62
63 @After
64 public void tearDown() {
65 manager = null;
66 sw = null;
67 }
68
69 @Test
70 public void deliverRoleReply() {
71 RoleRecvStatus status;
72
73 RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID);
74 RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID);
75
76 try {
77 //call without sendRoleReq() for requestPending = false
78 //first, sw.role == null
79 status = manager.deliverRoleReply(asserted);
80 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
81
82 sw.setRole(MASTER);
83 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
84 sw.setRole(SLAVE);
85
86 //match to pendingRole = MASTER, requestPending = true
87 manager.sendRoleRequest(MASTER, MATCHED_CURRENT_ROLE);
88 status = manager.deliverRoleReply(asserted);
89 assertEquals("expectation wrong", MATCHED_CURRENT_ROLE, status);
90
91 //requestPending never gets reset -- this might be a bug.
92 status = manager.deliverRoleReply(unasserted);
93 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
94 assertEquals("pending role mismatch", MASTER, ((TestSwitchDriver) sw).failed);
95
96 } catch (IOException | SwitchStateException e) {
97 assertEquals("unexpected error thrown",
98 SwitchStateException.class, e.getClass());
99 }
100 }
101
102 private class TestSwitchDriver implements OpenFlowSwitchDriver {
103
104 RoleState failed = null;
105 RoleState current = null;
106
107 @Override
108 public void sendMsg(OFMessage msg) {
109 }
110
111 @Override
112 public void sendMsg(List<OFMessage> msgs) {
113 }
114
115 @Override
Saurav Dasfa2fa932015-03-03 11:29:48 -0800116 public void transformAndSendMsg(OFMessage msg, TableType tableType) {
sangho11c30ac2015-01-22 14:30:55 -0800117 }
118
119 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700120 public void handleMessage(OFMessage fromSwitch) {
121 }
122
123 @Override
124 public void setRole(RoleState role) {
125 current = role;
126 }
127
128 @Override
129 public RoleState getRole() {
130 return current;
131 }
132
133 @Override
134 public List<OFPortDesc> getPorts() {
135 return null;
136 }
137
138 @Override
139 public OFFactory factory() {
140 // return what-ever triggers requestPending = true
141 return OFFactories.getFactory(OFVersion.OF_10);
142 }
143
144 @Override
145 public String getStringId() {
146 return "100";
147 }
148
149 @Override
150 public long getId() {
151 return 0;
152 }
153
154 @Override
Ray Milkeyd3edd032015-01-16 11:38:58 -0800155 public String manufacturerDescription() {
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700156 return null;
157 }
158
159 @Override
160 public String datapathDescription() {
161 return null;
162 }
163
164 @Override
165 public String hardwareDescription() {
166 return null;
167 }
168
169 @Override
170 public String softwareDescription() {
171 return null;
172 }
173
174 @Override
175 public String serialNumber() {
176 return null;
177 }
178
179 @Override
180 public void disconnectSwitch() {
181 }
182
183 @Override
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700184 public boolean isOptical() {
185 return false;
186 }
187
188 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700189 public void setAgent(OpenFlowAgent agent) {
190 }
191
192 @Override
193 public void setRoleHandler(RoleHandler roleHandler) {
194 }
195
196 @Override
197 public void reassertRole() {
198 }
199
200 @Override
201 public boolean handleRoleError(OFErrorMsg error) {
202 return false;
203 }
204
205 @Override
206 public void handleNiciraRole(OFMessage m) throws SwitchStateException {
207 }
208
209 @Override
210 public void handleRole(OFMessage m) throws SwitchStateException {
211 }
212
213 @Override
214 public void startDriverHandshake() {
215 }
216
217 @Override
218 public boolean isDriverHandshakeComplete() {
219 return false;
220 }
221
222 @Override
223 public void processDriverHandshakeMessage(OFMessage m) {
224 }
225
226 @Override
227 public boolean connectSwitch() {
228 return false;
229 }
230
231 @Override
232 public boolean activateMasterSwitch() {
233 return false;
234 }
235
236 @Override
237 public boolean activateEqualSwitch() {
238 return false;
239 }
240
241 @Override
242 public void transitionToEqualSwitch() {
243 }
244
245 @Override
246 public void transitionToMasterSwitch() {
247 }
248
249 @Override
250 public void removeConnectedSwitch() {
251 }
252
253 @Override
254 public void setPortDescReply(OFPortDescStatsReply portDescReply) {
255 }
256
257 @Override
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700258 public void setPortDescReplies(List<OFPortDescStatsReply> portDescReplies) {
259 }
260
261 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700262 public void setFeaturesReply(OFFeaturesReply featuresReply) {
263 }
264
265 @Override
266 public void setSwitchDescription(OFDescStatsReply desc) {
267 }
268
269 @Override
270 public int getNextTransactionId() {
271 return (int) XID;
272 }
273
274 @Override
275 public Boolean supportNxRole() {
276 return true;
277 }
278
279 @Override
280 public void setOFVersion(OFVersion ofV) {
281 }
282
283 @Override
284 public void setTableFull(boolean full) {
285 }
286
287 @Override
288 public void setChannel(Channel channel) {
289 }
290
291 @Override
292 public void setConnected(boolean connected) {
293 }
294
295 @Override
296 public boolean isConnected() {
297 return false;
298 }
299
300 @Override
301 public void write(OFMessage msg) {
302 }
303
304 @Override
305 public void write(List<OFMessage> msgs) {
306 }
307
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700308 @Override
309 public void returnRoleReply(RoleState requested, RoleState response) {
310 failed = requested;
311 }
312
Ray Milkeye53f1712015-01-16 09:17:16 -0800313 @Override
314 public String channelId() {
315 return "1.2.3.4:1";
316 }
317
Saurav Dasfa2fa932015-03-03 11:29:48 -0800318 @Override
319 public TableType getTableType(TableId tid) {
320 return TableType.NONE;
321 }
Ray Milkeye53f1712015-01-16 09:17:16 -0800322
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700323 }
324}