2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.openflow.controller.impl;
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;
44 import java.io.IOException;
45 import java.util.List;
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;
53 public class RoleManagerTest {
55 private static final U64 GID = U64.of(10L);
56 private static final long XID = 1L;
58 private OpenFlowSwitchDriver sw;
59 private RoleManager manager;
63 sw = new TestSwitchDriver();
64 manager = new RoleManager(sw);
68 public void tearDown() {
74 public void deliverRoleReply() {
75 RoleRecvStatus status;
77 RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID);
78 RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID);
81 //call without sendRoleReq() for requestPending = false
82 //first, sw.role == null
83 status = manager.deliverRoleReply(asserted);
84 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
87 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
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);
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);
100 } catch (IOException | SwitchStateException e) {
101 assertEquals("unexpected error thrown",
102 SwitchStateException.class, e.getClass());
106 private class TestSwitchDriver implements OpenFlowSwitchDriver {
108 RoleState failed = null;
109 RoleState current = null;
112 public void sendMsg(OFMessage msg) {
116 public void sendMsg(List<OFMessage> msgs) {
121 public void handleMessage(OFMessage fromSwitch) {
125 public void setRole(RoleState role) {
130 public RoleState getRole() {
135 public List<OFPortDesc> getPorts() {
140 public OFFactory factory() {
141 // return what-ever triggers requestPending = true
142 return OFFactories.getFactory(OFVersion.OF_10);
146 public String getStringId() {
151 public long getId() {
156 public String manufacturerDescription() {
161 public String datapathDescription() {
166 public String hardwareDescription() {
171 public String softwareDescription() {
176 public String serialNumber() {
181 public void disconnectSwitch() {
185 public Device.Type deviceType() {
186 return Device.Type.SWITCH;
190 public void setAgent(OpenFlowAgent agent) {
194 public void setRoleHandler(RoleHandler roleHandler) {
198 public void reassertRole() {
202 public boolean handleRoleError(OFErrorMsg error) {
207 public void handleNiciraRole(OFMessage m) throws SwitchStateException {
211 public void handleRole(OFMessage m) throws SwitchStateException {
215 public void startDriverHandshake() {
219 public boolean isDriverHandshakeComplete() {
224 public void processDriverHandshakeMessage(OFMessage m) {
228 public void sendRoleRequest(OFMessage message) {
233 public void sendHandshakeMessage(OFMessage message) {
237 public boolean connectSwitch() {
242 public boolean activateMasterSwitch() {
247 public boolean activateEqualSwitch() {
252 public void transitionToEqualSwitch() {
256 public void transitionToMasterSwitch() {
260 public void removeConnectedSwitch() {
264 public void setPortDescReply(OFPortDescStatsReply portDescReply) {
268 public void setPortDescReplies(List<OFPortDescStatsReply> portDescReplies) {
272 public void setFeaturesReply(OFFeaturesReply featuresReply) {
276 public void setSwitchDescription(OFDescStatsReply desc) {
280 public int getNextTransactionId() {
285 public Boolean supportNxRole() {
290 public void setOFVersion(OFVersion ofV) {
294 public void setTableFull(boolean full) {
298 public void setChannel(Channel channel) {
302 public void setConnected(boolean connected) {
306 public void init(Dpid dpid, OFDescStatsReply desc, OFVersion ofv) {
311 public boolean isConnected() {
316 public void returnRoleReply(RoleState requested, RoleState response) {
321 public String channelId() {
326 public DriverHandler handler() {
331 public void setHandler(DriverHandler handler) {
336 public DriverData data() {
341 public void setData(DriverData data) {