blob: aa23995595b0023a3152cc585612eb4b9a9a9e22 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070016package org.onlab.onos.openflow.controller.impl;
17
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;
25import org.onlab.onos.openflow.controller.RoleState;
26import org.onlab.onos.openflow.controller.driver.OpenFlowAgent;
27import org.onlab.onos.openflow.controller.driver.OpenFlowSwitchDriver;
28import org.onlab.onos.openflow.controller.driver.RoleHandler;
29import org.onlab.onos.openflow.controller.driver.RoleRecvStatus;
30import org.onlab.onos.openflow.controller.driver.RoleReplyInfo;
31import org.onlab.onos.openflow.controller.driver.SwitchStateException;
32import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
33import org.projectfloodlight.openflow.protocol.OFErrorMsg;
34import org.projectfloodlight.openflow.protocol.OFFactories;
35import org.projectfloodlight.openflow.protocol.OFFactory;
36import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
37import org.projectfloodlight.openflow.protocol.OFMessage;
38import org.projectfloodlight.openflow.protocol.OFPortDesc;
39import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply;
40import org.projectfloodlight.openflow.protocol.OFVersion;
41import org.projectfloodlight.openflow.types.U64;
42
43import static org.junit.Assert.assertEquals;
44import static org.onlab.onos.openflow.controller.RoleState.*;
45import static org.onlab.onos.openflow.controller.driver.RoleRecvStatus.*;
46
47public class RoleManagerTest {
48
49 private static final U64 GID = U64.of(10L);
50 private static final long XID = 1L;
51
52 private OpenFlowSwitchDriver sw;
53 private RoleManager manager;
54
55 @Before
56 public void setUp() {
57 sw = new TestSwitchDriver();
58 manager = new RoleManager(sw);
59 }
60
61 @After
62 public void tearDown() {
63 manager = null;
64 sw = null;
65 }
66
67 @Test
68 public void deliverRoleReply() {
69 RoleRecvStatus status;
70
71 RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID);
72 RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID);
73
74 try {
75 //call without sendRoleReq() for requestPending = false
76 //first, sw.role == null
77 status = manager.deliverRoleReply(asserted);
78 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
79
80 sw.setRole(MASTER);
81 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
82 sw.setRole(SLAVE);
83
84 //match to pendingRole = MASTER, requestPending = true
85 manager.sendRoleRequest(MASTER, MATCHED_CURRENT_ROLE);
86 status = manager.deliverRoleReply(asserted);
87 assertEquals("expectation wrong", MATCHED_CURRENT_ROLE, status);
88
89 //requestPending never gets reset -- this might be a bug.
90 status = manager.deliverRoleReply(unasserted);
91 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
92 assertEquals("pending role mismatch", MASTER, ((TestSwitchDriver) sw).failed);
93
94 } catch (IOException | SwitchStateException e) {
95 assertEquals("unexpected error thrown",
96 SwitchStateException.class, e.getClass());
97 }
98 }
99
100 private class TestSwitchDriver implements OpenFlowSwitchDriver {
101
102 RoleState failed = null;
103 RoleState current = null;
104
105 @Override
106 public void sendMsg(OFMessage msg) {
107 }
108
109 @Override
110 public void sendMsg(List<OFMessage> msgs) {
111 }
112
113 @Override
114 public void handleMessage(OFMessage fromSwitch) {
115 }
116
117 @Override
118 public void setRole(RoleState role) {
119 current = role;
120 }
121
122 @Override
123 public RoleState getRole() {
124 return current;
125 }
126
127 @Override
128 public List<OFPortDesc> getPorts() {
129 return null;
130 }
131
132 @Override
133 public OFFactory factory() {
134 // return what-ever triggers requestPending = true
135 return OFFactories.getFactory(OFVersion.OF_10);
136 }
137
138 @Override
139 public String getStringId() {
140 return "100";
141 }
142
143 @Override
144 public long getId() {
145 return 0;
146 }
147
148 @Override
149 public String manfacturerDescription() {
150 return null;
151 }
152
153 @Override
154 public String datapathDescription() {
155 return null;
156 }
157
158 @Override
159 public String hardwareDescription() {
160 return null;
161 }
162
163 @Override
164 public String softwareDescription() {
165 return null;
166 }
167
168 @Override
169 public String serialNumber() {
170 return null;
171 }
172
173 @Override
174 public void disconnectSwitch() {
175 }
176
177 @Override
178 public void returnRoleAssertFailure(RoleState role) {
179 failed = role;
180 }
181
182 @Override
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700183 public boolean isOptical() {
184 return false;
185 }
186
187 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700188 public void setAgent(OpenFlowAgent agent) {
189 }
190
191 @Override
192 public void setRoleHandler(RoleHandler roleHandler) {
193 }
194
195 @Override
196 public void reassertRole() {
197 }
198
199 @Override
200 public boolean handleRoleError(OFErrorMsg error) {
201 return false;
202 }
203
204 @Override
205 public void handleNiciraRole(OFMessage m) throws SwitchStateException {
206 }
207
208 @Override
209 public void handleRole(OFMessage m) throws SwitchStateException {
210 }
211
212 @Override
213 public void startDriverHandshake() {
214 }
215
216 @Override
217 public boolean isDriverHandshakeComplete() {
218 return false;
219 }
220
221 @Override
222 public void processDriverHandshakeMessage(OFMessage m) {
223 }
224
225 @Override
226 public boolean connectSwitch() {
227 return false;
228 }
229
230 @Override
231 public boolean activateMasterSwitch() {
232 return false;
233 }
234
235 @Override
236 public boolean activateEqualSwitch() {
237 return false;
238 }
239
240 @Override
241 public void transitionToEqualSwitch() {
242 }
243
244 @Override
245 public void transitionToMasterSwitch() {
246 }
247
248 @Override
249 public void removeConnectedSwitch() {
250 }
251
252 @Override
253 public void setPortDescReply(OFPortDescStatsReply portDescReply) {
254 }
255
256 @Override
257 public void setFeaturesReply(OFFeaturesReply featuresReply) {
258 }
259
260 @Override
261 public void setSwitchDescription(OFDescStatsReply desc) {
262 }
263
264 @Override
265 public int getNextTransactionId() {
266 return (int) XID;
267 }
268
269 @Override
270 public Boolean supportNxRole() {
271 return true;
272 }
273
274 @Override
275 public void setOFVersion(OFVersion ofV) {
276 }
277
278 @Override
279 public void setTableFull(boolean full) {
280 }
281
282 @Override
283 public void setChannel(Channel channel) {
284 }
285
286 @Override
287 public void setConnected(boolean connected) {
288 }
289
290 @Override
291 public boolean isConnected() {
292 return false;
293 }
294
295 @Override
296 public void write(OFMessage msg) {
297 }
298
299 @Override
300 public void write(List<OFMessage> msgs) {
301 }
302
303 }
304}