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