blob: 7260dddddda6e51a2318d7ed49c30251c124ce05 [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
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700178 public boolean isOptical() {
179 return false;
180 }
181
182 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700183 public void setAgent(OpenFlowAgent agent) {
184 }
185
186 @Override
187 public void setRoleHandler(RoleHandler roleHandler) {
188 }
189
190 @Override
191 public void reassertRole() {
192 }
193
194 @Override
195 public boolean handleRoleError(OFErrorMsg error) {
196 return false;
197 }
198
199 @Override
200 public void handleNiciraRole(OFMessage m) throws SwitchStateException {
201 }
202
203 @Override
204 public void handleRole(OFMessage m) throws SwitchStateException {
205 }
206
207 @Override
208 public void startDriverHandshake() {
209 }
210
211 @Override
212 public boolean isDriverHandshakeComplete() {
213 return false;
214 }
215
216 @Override
217 public void processDriverHandshakeMessage(OFMessage m) {
218 }
219
220 @Override
221 public boolean connectSwitch() {
222 return false;
223 }
224
225 @Override
226 public boolean activateMasterSwitch() {
227 return false;
228 }
229
230 @Override
231 public boolean activateEqualSwitch() {
232 return false;
233 }
234
235 @Override
236 public void transitionToEqualSwitch() {
237 }
238
239 @Override
240 public void transitionToMasterSwitch() {
241 }
242
243 @Override
244 public void removeConnectedSwitch() {
245 }
246
247 @Override
248 public void setPortDescReply(OFPortDescStatsReply portDescReply) {
249 }
250
251 @Override
252 public void setFeaturesReply(OFFeaturesReply featuresReply) {
253 }
254
255 @Override
256 public void setSwitchDescription(OFDescStatsReply desc) {
257 }
258
259 @Override
260 public int getNextTransactionId() {
261 return (int) XID;
262 }
263
264 @Override
265 public Boolean supportNxRole() {
266 return true;
267 }
268
269 @Override
270 public void setOFVersion(OFVersion ofV) {
271 }
272
273 @Override
274 public void setTableFull(boolean full) {
275 }
276
277 @Override
278 public void setChannel(Channel channel) {
279 }
280
281 @Override
282 public void setConnected(boolean connected) {
283 }
284
285 @Override
286 public boolean isConnected() {
287 return false;
288 }
289
290 @Override
291 public void write(OFMessage msg) {
292 }
293
294 @Override
295 public void write(List<OFMessage> msgs) {
296 }
297
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700298 @Override
299 public void returnRoleReply(RoleState requested, RoleState response) {
300 failed = requested;
301 }
302
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700303 }
304}