0a71a40d4694b78213fbc0576f7451d002e92454
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
4  * 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
7  *
8  *     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.
15  */
16 package org.onosproject.openflow.controller.impl;
17
18 import org.jboss.netty.channel.Channel;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.onosproject.net.Device;
23 import org.onosproject.net.driver.DriverData;
24 import org.onosproject.net.driver.DriverHandler;
25 import org.onosproject.openflow.controller.Dpid;
26 import org.onosproject.openflow.controller.RoleState;
27 import org.onosproject.openflow.controller.driver.OpenFlowAgent;
28 import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
29 import org.onosproject.openflow.controller.driver.RoleHandler;
30 import org.onosproject.openflow.controller.driver.RoleRecvStatus;
31 import org.onosproject.openflow.controller.driver.RoleReplyInfo;
32 import org.onosproject.openflow.controller.driver.SwitchStateException;
33 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
34 import org.projectfloodlight.openflow.protocol.OFErrorMsg;
35 import org.projectfloodlight.openflow.protocol.OFFactories;
36 import org.projectfloodlight.openflow.protocol.OFFactory;
37 import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
38 import org.projectfloodlight.openflow.protocol.OFMessage;
39 import org.projectfloodlight.openflow.protocol.OFPortDesc;
40 import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply;
41 import org.projectfloodlight.openflow.protocol.OFVersion;
42 import org.projectfloodlight.openflow.types.U64;
43
44 import java.io.IOException;
45 import java.util.List;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.onosproject.openflow.controller.RoleState.MASTER;
49 import static org.onosproject.openflow.controller.RoleState.SLAVE;
50 import static org.onosproject.openflow.controller.driver.RoleRecvStatus.MATCHED_CURRENT_ROLE;
51 import static org.onosproject.openflow.controller.driver.RoleRecvStatus.OTHER_EXPECTATION;
52
53 public class RoleManagerTest {
54
55     private static final U64 GID = U64.of(10L);
56     private static final long XID = 1L;
57
58     private OpenFlowSwitchDriver sw;
59     private RoleManager manager;
60
61     @Before
62     public void setUp() {
63         sw = new TestSwitchDriver();
64         manager = new RoleManager(sw);
65     }
66
67     @After
68     public void tearDown() {
69         manager = null;
70         sw = null;
71     }
72
73     @Test
74     public void deliverRoleReply() {
75         RoleRecvStatus status;
76
77         RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID);
78         RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID);
79
80         try {
81             //call without sendRoleReq() for requestPending = false
82             //first, sw.role == null
83             status = manager.deliverRoleReply(asserted);
84             assertEquals("expectation wrong", OTHER_EXPECTATION, status);
85
86             sw.setRole(MASTER);
87             assertEquals("expectation wrong", OTHER_EXPECTATION, status);
88             sw.setRole(SLAVE);
89
90             //match to pendingRole = MASTER, requestPending = true
91             manager.sendRoleRequest(MASTER, MATCHED_CURRENT_ROLE);
92             status = manager.deliverRoleReply(asserted);
93             assertEquals("expectation wrong", MATCHED_CURRENT_ROLE, status);
94
95             //requestPending never gets reset -- this might be a bug.
96             status = manager.deliverRoleReply(unasserted);
97             assertEquals("expectation wrong", OTHER_EXPECTATION, status);
98             assertEquals("pending role mismatch", MASTER, ((TestSwitchDriver) sw).failed);
99
100         } catch (IOException | SwitchStateException e) {
101             assertEquals("unexpected error thrown",
102                     SwitchStateException.class, e.getClass());
103         }
104     }
105
106     private class TestSwitchDriver implements OpenFlowSwitchDriver {
107
108         RoleState failed = null;
109         RoleState current = null;
110
111         @Override
112         public void sendMsg(OFMessage msg) {
113         }
114
115         @Override
116         public void sendMsg(List<OFMessage> msgs) {
117         }
118
119
120         @Override
121         public void handleMessage(OFMessage fromSwitch) {
122         }
123
124         @Override
125         public void setRole(RoleState role) {
126             current = role;
127         }
128
129         @Override
130         public RoleState getRole() {
131             return current;
132         }
133
134         @Override
135         public List<OFPortDesc> getPorts() {
136             return null;
137         }
138
139         @Override
140         public OFFactory factory() {
141             // return what-ever triggers requestPending = true
142             return OFFactories.getFactory(OFVersion.OF_10);
143         }
144
145         @Override
146         public String getStringId() {
147             return "100";
148         }
149
150         @Override
151         public long getId() {
152             return 0;
153         }
154
155         @Override
156         public String manufacturerDescription() {
157             return null;
158         }
159
160         @Override
161         public String datapathDescription() {
162             return null;
163         }
164
165         @Override
166         public String hardwareDescription() {
167             return null;
168         }
169
170         @Override
171         public String softwareDescription() {
172             return null;
173         }
174
175         @Override
176         public String serialNumber() {
177             return null;
178         }
179
180         @Override
181         public void disconnectSwitch() {
182         }
183
184         @Override
185         public Device.Type deviceType() {
186             return Device.Type.SWITCH;
187         }
188
189         @Override
190         public void setAgent(OpenFlowAgent agent) {
191         }
192
193         @Override
194         public void setRoleHandler(RoleHandler roleHandler) {
195         }
196
197         @Override
198         public void reassertRole() {
199         }
200
201         @Override
202         public boolean handleRoleError(OFErrorMsg error) {
203             return false;
204         }
205
206         @Override
207         public void handleNiciraRole(OFMessage m) throws SwitchStateException {
208         }
209
210         @Override
211         public void handleRole(OFMessage m) throws SwitchStateException {
212         }
213
214         @Override
215         public void startDriverHandshake() {
216         }
217
218         @Override
219         public boolean isDriverHandshakeComplete() {
220             return false;
221         }
222
223         @Override
224         public void processDriverHandshakeMessage(OFMessage m) {
225         }
226
227         @Override
228         public void sendRoleRequest(OFMessage message) {
229
230         }
231
232         @Override
233         public void sendHandshakeMessage(OFMessage message) {
234         }
235
236         @Override
237         public boolean connectSwitch() {
238             return false;
239         }
240
241         @Override
242         public boolean activateMasterSwitch() {
243             return false;
244         }
245
246         @Override
247         public boolean activateEqualSwitch() {
248             return false;
249         }
250
251         @Override
252         public void transitionToEqualSwitch() {
253         }
254
255         @Override
256         public void transitionToMasterSwitch() {
257         }
258
259         @Override
260         public void removeConnectedSwitch() {
261         }
262
263         @Override
264         public void setPortDescReply(OFPortDescStatsReply portDescReply) {
265         }
266
267         @Override
268         public void setPortDescReplies(List<OFPortDescStatsReply> portDescReplies) {
269         }
270
271         @Override
272         public void setFeaturesReply(OFFeaturesReply featuresReply) {
273         }
274
275         @Override
276         public void setSwitchDescription(OFDescStatsReply desc) {
277         }
278
279         @Override
280         public int getNextTransactionId() {
281             return (int) XID;
282         }
283
284         @Override
285         public Boolean supportNxRole() {
286             return true;
287         }
288
289         @Override
290         public void setOFVersion(OFVersion ofV) {
291         }
292
293         @Override
294         public void setTableFull(boolean full) {
295         }
296
297         @Override
298         public void setChannel(Channel channel) {
299         }
300
301         @Override
302         public void setConnected(boolean connected) {
303         }
304
305         @Override
306         public void init(Dpid dpid, OFDescStatsReply desc, OFVersion ofv) {
307
308         }
309
310         @Override
311         public boolean isConnected() {
312             return false;
313         }
314
315         @Override
316         public void returnRoleReply(RoleState requested, RoleState response) {
317             failed = requested;
318         }
319
320         @Override
321         public String channelId() {
322             return "1.2.3.4:1";
323         }
324
325         @Override
326         public DriverHandler handler() {
327             return null;
328         }
329
330         @Override
331         public void setHandler(DriverHandler handler) {
332
333         }
334
335         @Override
336         public DriverData data() {
337             return null;
338         }
339
340         @Override
341         public void setData(DriverData data) {
342
343         }
344     }
345 }