blob: 285635a7b070cb02d36219b63e815bfcaa38da0f [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
168 public void setAgent(OpenFlowAgent agent) {
169 }
170
171 @Override
172 public void setRoleHandler(RoleHandler roleHandler) {
173 }
174
175 @Override
176 public void reassertRole() {
177 }
178
179 @Override
180 public boolean handleRoleError(OFErrorMsg error) {
181 return false;
182 }
183
184 @Override
185 public void handleNiciraRole(OFMessage m) throws SwitchStateException {
186 }
187
188 @Override
189 public void handleRole(OFMessage m) throws SwitchStateException {
190 }
191
192 @Override
193 public void startDriverHandshake() {
194 }
195
196 @Override
197 public boolean isDriverHandshakeComplete() {
198 return false;
199 }
200
201 @Override
202 public void processDriverHandshakeMessage(OFMessage m) {
203 }
204
205 @Override
206 public boolean connectSwitch() {
207 return false;
208 }
209
210 @Override
211 public boolean activateMasterSwitch() {
212 return false;
213 }
214
215 @Override
216 public boolean activateEqualSwitch() {
217 return false;
218 }
219
220 @Override
221 public void transitionToEqualSwitch() {
222 }
223
224 @Override
225 public void transitionToMasterSwitch() {
226 }
227
228 @Override
229 public void removeConnectedSwitch() {
230 }
231
232 @Override
233 public void setPortDescReply(OFPortDescStatsReply portDescReply) {
234 }
235
236 @Override
237 public void setFeaturesReply(OFFeaturesReply featuresReply) {
238 }
239
240 @Override
241 public void setSwitchDescription(OFDescStatsReply desc) {
242 }
243
244 @Override
245 public int getNextTransactionId() {
246 return (int) XID;
247 }
248
249 @Override
250 public Boolean supportNxRole() {
251 return true;
252 }
253
254 @Override
255 public void setOFVersion(OFVersion ofV) {
256 }
257
258 @Override
259 public void setTableFull(boolean full) {
260 }
261
262 @Override
263 public void setChannel(Channel channel) {
264 }
265
266 @Override
267 public void setConnected(boolean connected) {
268 }
269
270 @Override
271 public boolean isConnected() {
272 return false;
273 }
274
275 @Override
276 public void write(OFMessage msg) {
277 }
278
279 @Override
280 public void write(List<OFMessage> msgs) {
281 }
282
283 }
284}