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