Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.controller.impl; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.jboss.netty.channel.Channel; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.openflow.controller.RoleState; |
| 26 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
| 27 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
| 28 | import org.onosproject.openflow.controller.driver.RoleHandler; |
| 29 | import org.onosproject.openflow.controller.driver.RoleRecvStatus; |
| 30 | import org.onosproject.openflow.controller.driver.RoleReplyInfo; |
| 31 | import org.onosproject.openflow.controller.driver.SwitchStateException; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 33 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 34 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 35 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 36 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
| 37 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 38 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 39 | import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply; |
| 40 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 41 | import org.projectfloodlight.openflow.types.U64; |
| 42 | |
| 43 | import static org.junit.Assert.assertEquals; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import static org.onosproject.openflow.controller.RoleState.*; |
| 45 | import static org.onosproject.openflow.controller.driver.RoleRecvStatus.*; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 46 | |
| 47 | public 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 |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 149 | public String manufacturerDescription() { |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 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 Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 178 | public boolean isOptical() { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | @Override |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 183 | 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 Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 298 | @Override |
| 299 | public void returnRoleReply(RoleState requested, RoleState response) { |
| 300 | failed = requested; |
| 301 | } |
| 302 | |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 303 | } |
| 304 | } |