Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -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 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -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 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.driver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 20 | import org.jboss.netty.channel.Channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.openflow.controller.Dpid; |
| 25 | import org.onosproject.openflow.controller.RoleState; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 27 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 28 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
| 29 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 30 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 31 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
| 32 | import org.projectfloodlight.openflow.protocol.OFMessage; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFNiciraControllerRoleRequest; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 35 | import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply; |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.OFRoleReply; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFRoleRequest; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 43 | import java.io.IOException; |
| 44 | import java.net.InetSocketAddress; |
| 45 | import java.net.SocketAddress; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.Collections; |
| 48 | import java.util.List; |
| 49 | import java.util.concurrent.atomic.AtomicInteger; |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 50 | import java.util.concurrent.atomic.AtomicReference; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 51 | import java.util.stream.Collectors; |
| 52 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 53 | /** |
| 54 | * An abstract representation of an OpenFlow switch. Can be extended by others |
| 55 | * to serve as a base for their vendor specific representation of a switch. |
| 56 | */ |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 57 | public abstract class AbstractOpenFlowSwitch extends AbstractHandlerBehaviour |
| 58 | implements OpenFlowSwitchDriver { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 59 | |
Yuta HIGUCHI | f5416d8 | 2014-10-24 21:17:40 -0700 | [diff] [blame] | 60 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 61 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 62 | private Channel channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 63 | protected String channelId; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 64 | |
| 65 | private boolean connected; |
| 66 | protected boolean startDriverHandshakeCalled = false; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 67 | private Dpid dpid; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 68 | private OpenFlowAgent agent; |
| 69 | private final AtomicInteger xidCounter = new AtomicInteger(0); |
| 70 | |
| 71 | private OFVersion ofVersion; |
| 72 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 73 | protected List<OFPortDescStatsReply> ports = new ArrayList<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 74 | |
| 75 | protected boolean tableFull; |
| 76 | |
| 77 | private RoleHandler roleMan; |
| 78 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 79 | // TODO this is accessed from multiple threads, but volatile may have performance implications |
| 80 | protected volatile RoleState role; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 81 | |
| 82 | protected OFFeaturesReply features; |
| 83 | protected OFDescStatsReply desc; |
| 84 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 85 | private final AtomicReference<List<OFMessage>> messagesPendingMastership |
| 86 | = new AtomicReference<>(); |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 87 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public void init(Dpid dpid, OFDescStatsReply desc, OFVersion ofv) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 90 | this.dpid = dpid; |
| 91 | this.desc = desc; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 92 | this.ofVersion = ofv; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | //************************ |
| 96 | // Channel related |
| 97 | //************************ |
| 98 | |
| 99 | @Override |
| 100 | public final void disconnectSwitch() { |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame^] | 101 | setConnected(false); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 102 | this.channel.close(); |
| 103 | } |
| 104 | |
| 105 | @Override |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 106 | public void sendMsg(OFMessage msg) { |
| 107 | this.sendMsg(Collections.singletonList(msg)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public final void sendMsg(List<OFMessage> msgs) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 112 | /* |
| 113 | It is possible that in this block, we transition to SLAVE/EQUAL. |
| 114 | If this is the case, the supplied messages will race with the |
| 115 | RoleRequest message, and they could be rejected by the switch. |
| 116 | In the interest of performance, we will not protect this block with |
| 117 | a synchronization primitive, because the message would have just been |
| 118 | dropped anyway. |
| 119 | */ |
| 120 | if (role == RoleState.MASTER) { |
| 121 | // fast path send when we are master |
| 122 | |
| 123 | sendMsgsOnChannel(msgs); |
| 124 | return; |
| 125 | } |
| 126 | // check to see if mastership transition is in progress |
| 127 | synchronized (messagesPendingMastership) { |
| 128 | /* |
| 129 | messagesPendingMastership is used as synchronization variable for |
| 130 | all mastership related changes. In this block, mastership (including |
| 131 | role update) will have either occurred or not. |
| 132 | */ |
| 133 | if (role == RoleState.MASTER) { |
| 134 | // transition to MASTER complete, send messages |
| 135 | sendMsgsOnChannel(msgs); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | List<OFMessage> messages = messagesPendingMastership.get(); |
| 140 | if (messages != null) { |
| 141 | // we are transitioning to MASTER, so add messages to queue |
| 142 | messages.addAll(msgs); |
| 143 | log.debug("Enqueue message for switch {}. queue size after is {}", |
| 144 | dpid, messages.size()); |
| 145 | } else { |
| 146 | // not transitioning to MASTER |
| 147 | log.warn("Dropping message for switch {} (role: {}, connected: {}): {}", |
| 148 | dpid, role, channel.isConnected(), msgs); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private void sendMsgsOnChannel(List<OFMessage> msgs) { |
| 154 | if (channel.isConnected()) { |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 155 | channel.write(msgs); |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 156 | } else { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 157 | log.warn("Dropping messages for switch {} because channel is not connected: {}", |
| 158 | dpid, msgs); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 159 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 163 | public final void sendRoleRequest(OFMessage msg) { |
| 164 | if (msg instanceof OFRoleRequest || |
| 165 | msg instanceof OFNiciraControllerRoleRequest) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 166 | sendMsgsOnChannel(Collections.singletonList(msg)); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | throw new IllegalArgumentException("Someone is trying to send " + |
| 170 | "a non role request message"); |
| 171 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 172 | |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 173 | @Override |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 174 | public final void sendHandshakeMessage(OFMessage message) { |
| 175 | if (!this.isDriverHandshakeComplete()) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 176 | sendMsgsOnChannel(Collections.singletonList(message)); |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public final boolean isConnected() { |
| 182 | return this.connected; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public final void setConnected(boolean connected) { |
| 187 | this.connected = connected; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 188 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 189 | |
| 190 | @Override |
| 191 | public final void setChannel(Channel channel) { |
| 192 | this.channel = channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 193 | final SocketAddress address = channel.getRemoteAddress(); |
| 194 | if (address instanceof InetSocketAddress) { |
| 195 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 196 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 197 | if (ipAddress.isIp4()) { |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 198 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 199 | } else { |
| 200 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 201 | } |
| 202 | } |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 203 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 204 | |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 205 | @Override |
| 206 | public String channelId() { |
| 207 | return channelId; |
| 208 | } |
| 209 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 210 | //************************ |
| 211 | // Switch features related |
| 212 | //************************ |
| 213 | |
| 214 | @Override |
| 215 | public final long getId() { |
| 216 | return this.dpid.value(); |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 217 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public final String getStringId() { |
| 221 | return this.dpid.toString(); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public final void setOFVersion(OFVersion ofV) { |
| 226 | this.ofVersion = ofV; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void setTableFull(boolean full) { |
| 231 | this.tableFull = full; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void setFeaturesReply(OFFeaturesReply featuresReply) { |
| 236 | this.features = featuresReply; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public abstract Boolean supportNxRole(); |
| 241 | |
| 242 | //************************ |
| 243 | // Message handling |
| 244 | //************************ |
| 245 | /** |
| 246 | * Handle the message coming from the dataplane. |
| 247 | * |
| 248 | * @param m the actual message |
| 249 | */ |
| 250 | @Override |
| 251 | public final void handleMessage(OFMessage m) { |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 252 | if (this.role == RoleState.MASTER || m instanceof OFPortStatus) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 253 | this.agent.processMessage(dpid, m); |
| 254 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public RoleState getRole() { |
| 259 | return role; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 260 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 261 | |
| 262 | @Override |
| 263 | public final boolean connectSwitch() { |
| 264 | return this.agent.addConnectedSwitch(dpid, this); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public final boolean activateMasterSwitch() { |
| 269 | return this.agent.addActivatedMasterSwitch(dpid, this); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public final boolean activateEqualSwitch() { |
| 274 | return this.agent.addActivatedEqualSwitch(dpid, this); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public final void transitionToEqualSwitch() { |
| 279 | this.agent.transitionToEqualSwitch(dpid); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public final void transitionToMasterSwitch() { |
| 284 | this.agent.transitionToMasterSwitch(dpid); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 285 | synchronized (messagesPendingMastership) { |
| 286 | List<OFMessage> messages = messagesPendingMastership.get(); |
| 287 | if (messages != null) { |
| 288 | this.sendMsg(messages); |
| 289 | log.debug("Sending {} pending messages to switch {}", |
| 290 | messages.size(), dpid); |
| 291 | messagesPendingMastership.set(null); |
| 292 | } |
| 293 | // perform role transition after clearing messages queue |
| 294 | this.role = RoleState.MASTER; |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 295 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public final void removeConnectedSwitch() { |
| 300 | this.agent.removeConnectedSwitch(dpid); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public OFFactory factory() { |
| 305 | return OFFactories.getFactory(ofVersion); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void setPortDescReply(OFPortDescStatsReply portDescReply) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 310 | this.ports.add(portDescReply); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void setPortDescReplies(List<OFPortDescStatsReply> portDescReplies) { |
| 315 | this.ports.addAll(portDescReplies); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 319 | public void returnRoleReply(RoleState requested, RoleState response) { |
| 320 | this.agent.returnRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 324 | public abstract void startDriverHandshake(); |
| 325 | |
| 326 | @Override |
| 327 | public abstract boolean isDriverHandshakeComplete(); |
| 328 | |
| 329 | @Override |
| 330 | public abstract void processDriverHandshakeMessage(OFMessage m); |
| 331 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 332 | |
| 333 | // Role Handling |
| 334 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public void setRole(RoleState role) { |
| 337 | try { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 338 | if (role == RoleState.SLAVE || role == RoleState.EQUAL) { |
| 339 | // perform role transition to SLAVE/EQUAL before sending role request |
| 340 | this.role = role; |
| 341 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 342 | if (this.roleMan.sendRoleRequest(role, RoleRecvStatus.MATCHED_SET_ROLE)) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 343 | log.debug("Sending role {} to switch {}", role, getStringId()); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 344 | if (role == RoleState.MASTER) { |
| 345 | synchronized (messagesPendingMastership) { |
| 346 | if (messagesPendingMastership.get() == null) { |
| 347 | log.debug("Initializing new message queue for switch {}", dpid); |
| 348 | /* |
| 349 | The presence of messagesPendingMastership indicates that |
| 350 | a switch is currently transitioning to MASTER, but |
| 351 | is still awaiting role reply from switch. |
| 352 | */ |
| 353 | messagesPendingMastership.set(Lists.newArrayList()); |
| 354 | } |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 355 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 356 | } |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 357 | } else if (role == RoleState.MASTER) { |
| 358 | // role request not support; transition switch to MASTER |
alshabib | 7814e9f | 2014-09-30 11:52:12 -0700 | [diff] [blame] | 359 | this.role = role; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 360 | } |
| 361 | } catch (IOException e) { |
| 362 | log.error("Unable to write to switch {}.", this.dpid); |
| 363 | } |
| 364 | } |
| 365 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 366 | @Override |
| 367 | public void reassertRole() { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 368 | // TODO should messages be sent directly or queue during reassertion? |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 369 | if (this.getRole() == RoleState.MASTER) { |
| 370 | log.warn("Received permission error from switch {} while " + |
| 371 | "being master. Reasserting master role.", |
| 372 | this.getStringId()); |
| 373 | this.setRole(RoleState.MASTER); |
| 374 | } |
| 375 | } |
| 376 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 377 | @Override |
| 378 | public void handleRole(OFMessage m) throws SwitchStateException { |
| 379 | RoleReplyInfo rri = roleMan.extractOFRoleReply((OFRoleReply) m); |
| 380 | RoleRecvStatus rrs = roleMan.deliverRoleReply(rri); |
| 381 | if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { |
| 382 | if (rri.getRole() == RoleState.MASTER) { |
| 383 | this.transitionToMasterSwitch(); |
| 384 | } else if (rri.getRole() == RoleState.EQUAL || |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 385 | rri.getRole() == RoleState.SLAVE) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 386 | this.transitionToEqualSwitch(); |
| 387 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 388 | } else { |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 389 | log.warn("Failed to set role for {}", this.getStringId()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void handleNiciraRole(OFMessage m) throws SwitchStateException { |
| 395 | RoleState r = this.roleMan.extractNiciraRoleReply((OFExperimenter) m); |
| 396 | if (r == null) { |
| 397 | // The message wasn't really a Nicira role reply. We just |
| 398 | // dispatch it to the OFMessage listeners in this case. |
| 399 | this.handleMessage(m); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 400 | return; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | RoleRecvStatus rrs = this.roleMan.deliverRoleReply( |
| 404 | new RoleReplyInfo(r, null, m.getXid())); |
| 405 | if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { |
| 406 | if (r == RoleState.MASTER) { |
| 407 | this.transitionToMasterSwitch(); |
| 408 | } else if (r == RoleState.EQUAL || |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 409 | r == RoleState.SLAVE) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 410 | this.transitionToEqualSwitch(); |
| 411 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 412 | } else { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 413 | this.disconnectSwitch(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public boolean handleRoleError(OFErrorMsg error) { |
| 419 | try { |
| 420 | return RoleRecvStatus.OTHER_EXPECTATION != this.roleMan.deliverError(error); |
| 421 | } catch (SwitchStateException e) { |
| 422 | this.disconnectSwitch(); |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 427 | @Override |
| 428 | public final void setAgent(OpenFlowAgent ag) { |
| 429 | if (this.agent == null) { |
| 430 | this.agent = ag; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public final void setRoleHandler(RoleHandler roleHandler) { |
| 436 | if (this.roleMan == null) { |
| 437 | this.roleMan = roleHandler; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public void setSwitchDescription(OFDescStatsReply d) { |
| 443 | this.desc = d; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public int getNextTransactionId() { |
| 448 | return this.xidCounter.getAndIncrement(); |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public List<OFPortDesc> getPorts() { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 453 | return this.ports.stream() |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 454 | .flatMap(portReply -> portReply.getEntries().stream()) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 455 | .collect(Collectors.toList()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | @Override |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 459 | public String manufacturerDescription() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 460 | return this.desc.getMfrDesc(); |
| 461 | } |
| 462 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 463 | @Override |
| 464 | public String datapathDescription() { |
| 465 | return this.desc.getDpDesc(); |
| 466 | } |
| 467 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public String hardwareDescription() { |
| 470 | return this.desc.getHwDesc(); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public String softwareDescription() { |
| 475 | return this.desc.getSwDesc(); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public String serialNumber() { |
| 480 | return this.desc.getSerialNum(); |
| 481 | } |
| 482 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 483 | @Override |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 484 | public Device.Type deviceType() { |
| 485 | return Device.Type.SWITCH; |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 486 | } |
| 487 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 488 | @Override |
| 489 | public String toString() { |
| 490 | return this.getClass().getName() + " [" + ((channel != null) |
| 491 | ? channel.getRemoteAddress() : "?") |
| 492 | + " DPID[" + ((getStringId() != null) ? getStringId() : "?") + "]]"; |
| 493 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 494 | } |