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; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Multimap; |
| 21 | import com.google.common.collect.Sets; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame^] | 25 | import org.apache.felix.scr.annotations.Modified; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 28 | import org.onosproject.openflow.controller.Dpid; |
| 29 | import org.onosproject.openflow.controller.OpenFlowController; |
| 30 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 31 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 32 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 33 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 34 | import org.onosproject.openflow.controller.PacketListener; |
| 35 | import org.onosproject.openflow.controller.RoleState; |
| 36 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame^] | 37 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 39 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 40 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 41 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 42 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 44 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 45 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 46 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 48 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 56 | import java.util.Collection; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame^] | 57 | import java.util.Dictionary; |
| 58 | import java.util.HashMap; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 59 | import java.util.HashSet; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame^] | 60 | import java.util.Map; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 61 | import java.util.Set; |
| 62 | import java.util.concurrent.ConcurrentHashMap; |
| 63 | import java.util.concurrent.ExecutorService; |
| 64 | import java.util.concurrent.Executors; |
| 65 | import java.util.concurrent.locks.Lock; |
| 66 | import java.util.concurrent.locks.ReentrantLock; |
| 67 | |
| 68 | import static org.onlab.util.Tools.groupedThreads; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 69 | |
| 70 | @Component(immediate = true) |
| 71 | @Service |
| 72 | public class OpenFlowControllerImpl implements OpenFlowController { |
| 73 | |
| 74 | private static final Logger log = |
| 75 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 76 | |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 77 | private final ExecutorService executorMsgs = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 78 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d")); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 79 | |
| 80 | private final ExecutorService executorBarrier = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 81 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d")); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 82 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 83 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
| 84 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 85 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
| 86 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 87 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
| 88 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 89 | |
| 90 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 91 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new HashSet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 92 | |
| 93 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 94 | ArrayListMultimap.create(); |
| 95 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 96 | protected Set<OpenFlowEventListener> ofEventListener = Sets.newHashSet(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 97 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 98 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 99 | ArrayListMultimap.create(); |
| 100 | |
| 101 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 102 | ArrayListMultimap.create(); |
| 103 | |
| 104 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 105 | ArrayListMultimap.create(); |
| 106 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 107 | private final Controller ctrl = new Controller(); |
| 108 | |
| 109 | @Activate |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame^] | 110 | public void activate(ComponentContext context) { |
| 111 | Map<String, String> properties = readComponentConfiguration(context); |
| 112 | ctrl.setConfigParams(properties); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 113 | ctrl.start(agent); |
| 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | public void deactivate() { |
| 118 | ctrl.stop(); |
| 119 | } |
| 120 | |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame^] | 121 | /** |
| 122 | * Extracts properties from the component configuration context. |
| 123 | * |
| 124 | * @param context the component context |
| 125 | */ |
| 126 | private Map<String, String> readComponentConfiguration(ComponentContext context) { |
| 127 | Dictionary<?, ?> properties = context.getProperties(); |
| 128 | Map<String, String> outProperties = new HashMap<>(); |
| 129 | try { |
| 130 | String strDpid = (String) properties.get("corsaDpid"); |
| 131 | if (strDpid != null) { |
| 132 | outProperties.put("corsaDpid", strDpid); |
| 133 | } |
| 134 | } catch (ClassCastException e) { |
| 135 | return outProperties; |
| 136 | } |
| 137 | return outProperties; |
| 138 | } |
| 139 | |
| 140 | @Modified |
| 141 | public void modified(ComponentContext context) { |
| 142 | // Blank @Modified method to catch modifications to the context. |
| 143 | // If no @Modified method exists, @Activate is called again |
| 144 | // when the context is modified. |
| 145 | } |
| 146 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 149 | return connectedSwitches.values(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 154 | return activeMasterSwitches.values(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 159 | return activeEqualSwitches.values(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 164 | return connectedSwitches.get(dpid); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 169 | return activeMasterSwitches.get(dpid); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 174 | return activeEqualSwitches.get(dpid); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 179 | if (!ofSwitchListener.contains(listener)) { |
| 180 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 186 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void addPacketListener(int priority, PacketListener listener) { |
| 191 | ofPacketListener.put(priority, listener); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void removePacketListener(PacketListener listener) { |
| 196 | ofPacketListener.values().remove(listener); |
| 197 | } |
| 198 | |
| 199 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 200 | public void addEventListener(OpenFlowEventListener listener) { |
| 201 | ofEventListener.add(listener); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void removeEventListener(OpenFlowEventListener listener) { |
| 206 | ofEventListener.remove(listener); |
| 207 | } |
| 208 | |
| 209 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 210 | public void write(Dpid dpid, OFMessage msg) { |
| 211 | this.getSwitch(dpid).sendMsg(msg); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 216 | Collection<OFFlowStatsEntry> flowStats; |
| 217 | Collection<OFGroupStatsEntry> groupStats; |
| 218 | Collection<OFGroupDescStatsEntry> groupDescStats; |
| 219 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 220 | switch (msg.getType()) { |
| 221 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 222 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 223 | l.portChanged(dpid, (OFPortStatus) msg); |
| 224 | } |
| 225 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 226 | case FEATURES_REPLY: |
| 227 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 228 | l.switchChanged(dpid); |
| 229 | } |
| 230 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 231 | case PACKET_IN: |
| 232 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
| 233 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 234 | (OFPacketIn) msg); |
| 235 | for (PacketListener p : ofPacketListener.values()) { |
| 236 | p.handlePacket(pktCtx); |
| 237 | } |
| 238 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 239 | // TODO: Consider using separate threadpool for sensitive messages. |
| 240 | // ie. Back to back error could cause us to starve. |
| 241 | case FLOW_REMOVED: |
| 242 | case ERROR: |
| 243 | executorMsgs.submit(new OFMessageHandler(dpid, msg)); |
| 244 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 245 | case STATS_REPLY: |
| 246 | OFStatsReply reply = (OFStatsReply) msg; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 247 | switch (reply.getStatsType()) { |
| 248 | case PORT_DESC: |
| 249 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 250 | l.switchChanged(dpid); |
| 251 | } |
| 252 | break; |
| 253 | case FLOW: |
| 254 | flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 255 | if (flowStats != null) { |
| 256 | OFFlowStatsReply.Builder rep = |
| 257 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 258 | rep.setEntries(Lists.newLinkedList(flowStats)); |
| 259 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 260 | } |
| 261 | break; |
| 262 | case GROUP: |
| 263 | groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 264 | if (groupStats != null) { |
| 265 | OFGroupStatsReply.Builder rep = |
| 266 | OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply(); |
| 267 | rep.setEntries(Lists.newLinkedList(groupStats)); |
| 268 | rep.setXid(reply.getXid()); |
| 269 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 270 | } |
| 271 | break; |
| 272 | case GROUP_DESC: |
| 273 | groupDescStats = publishGroupDescStats(dpid, |
| 274 | (OFGroupDescStatsReply) reply); |
| 275 | if (groupDescStats != null) { |
| 276 | OFGroupDescStatsReply.Builder rep = |
| 277 | OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply(); |
| 278 | rep.setEntries(Lists.newLinkedList(groupDescStats)); |
| 279 | rep.setXid(reply.getXid()); |
| 280 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 281 | } |
| 282 | break; |
| 283 | default: |
| 284 | log.warn("Unsupported stats type : {}", reply.getStatsType()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 285 | } |
| 286 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 287 | case BARRIER_REPLY: |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 288 | executorBarrier.submit(new OFMessageHandler(dpid, msg)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 289 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 290 | case EXPERIMENTER: |
| 291 | // Handle optical port stats |
| 292 | if (((OFExperimenter) msg).getExperimenter() == 0x748771) { |
| 293 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
| 294 | OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus(); |
| 295 | OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc(); |
| 296 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 297 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 298 | .setName(circuitPortStatus.getName()) |
| 299 | .setConfig(circuitPortStatus.getConfig()) |
| 300 | .setState(circuitPortStatus.getState()); |
| 301 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 302 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 303 | l.portChanged(dpid, portStatus.build()); |
| 304 | } |
| 305 | } else { |
| 306 | log.warn("Handling experimenter type {} not yet implemented", |
| 307 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 308 | } |
| 309 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 310 | default: |
| 311 | log.warn("Handling message type {} not yet implemented {}", |
| 312 | msg.getType(), msg); |
| 313 | } |
| 314 | } |
| 315 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 316 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 317 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 318 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 319 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 320 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 321 | return fullFlowStats.removeAll(dpid); |
| 322 | } |
| 323 | return null; |
| 324 | } |
| 325 | |
| 326 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 327 | OFGroupStatsReply reply) { |
| 328 | //TODO: Get rid of synchronized |
| 329 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 330 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 331 | return fullGroupStats.removeAll(dpid); |
| 332 | } |
| 333 | return null; |
| 334 | } |
| 335 | |
| 336 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 337 | OFGroupDescStatsReply reply) { |
| 338 | //TODO: Get rid of synchronized |
| 339 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 340 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 341 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 342 | } |
| 343 | return null; |
| 344 | } |
| 345 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 346 | @Override |
| 347 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 348 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 349 | if (sw == null) { |
| 350 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 351 | return; |
| 352 | } |
| 353 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Implementation of an OpenFlow Agent which is responsible for |
| 358 | * keeping track of connected switches and the state in which |
| 359 | * they are. |
| 360 | */ |
| 361 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 362 | |
| 363 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 364 | private final Lock switchLock = new ReentrantLock(); |
| 365 | |
| 366 | @Override |
| 367 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 368 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 369 | if (connectedSwitches.get(dpid) != null) { |
| 370 | log.error("Trying to add connectedSwitch but found a previous " |
| 371 | + "value for dpid: {}", dpid); |
| 372 | return false; |
| 373 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 374 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 375 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 376 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 377 | l.switchAdded(dpid); |
| 378 | } |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public boolean validActivation(Dpid dpid) { |
| 385 | if (connectedSwitches.get(dpid) == null) { |
| 386 | log.error("Trying to activate switch but is not in " |
| 387 | + "connected switches: dpid {}. Aborting ..", |
| 388 | dpid); |
| 389 | return false; |
| 390 | } |
| 391 | if (activeMasterSwitches.get(dpid) != null || |
| 392 | activeEqualSwitches.get(dpid) != null) { |
| 393 | log.error("Trying to activate switch but it is already " |
| 394 | + "activated: dpid {}. Found in activeMaster: {} " |
| 395 | + "Found in activeEqual: {}. Aborting ..", new Object[]{ |
| 396 | dpid, |
| 397 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 398 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| 399 | return false; |
| 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | @Override |
| 406 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 407 | switchLock.lock(); |
| 408 | try { |
| 409 | if (!validActivation(dpid)) { |
| 410 | return false; |
| 411 | } |
| 412 | activeMasterSwitches.put(dpid, sw); |
| 413 | return true; |
| 414 | } finally { |
| 415 | switchLock.unlock(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 421 | switchLock.lock(); |
| 422 | try { |
| 423 | if (!validActivation(dpid)) { |
| 424 | return false; |
| 425 | } |
| 426 | activeEqualSwitches.put(dpid, sw); |
| 427 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 428 | return true; |
| 429 | } finally { |
| 430 | switchLock.unlock(); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void transitionToMasterSwitch(Dpid dpid) { |
| 436 | switchLock.lock(); |
| 437 | try { |
| 438 | if (activeMasterSwitches.containsKey(dpid)) { |
| 439 | return; |
| 440 | } |
| 441 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 442 | if (sw == null) { |
| 443 | sw = getSwitch(dpid); |
| 444 | if (sw == null) { |
| 445 | log.error("Transition to master called on sw {}, but switch " |
| 446 | + "was not found in controller-cache", dpid); |
| 447 | return; |
| 448 | } |
| 449 | } |
| 450 | log.info("Transitioned switch {} to MASTER", dpid); |
| 451 | activeMasterSwitches.put(dpid, sw); |
| 452 | } finally { |
| 453 | switchLock.unlock(); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | @Override |
| 459 | public void transitionToEqualSwitch(Dpid dpid) { |
| 460 | switchLock.lock(); |
| 461 | try { |
| 462 | if (activeEqualSwitches.containsKey(dpid)) { |
| 463 | return; |
| 464 | } |
| 465 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 466 | if (sw == null) { |
| 467 | sw = getSwitch(dpid); |
| 468 | if (sw == null) { |
| 469 | log.error("Transition to equal called on sw {}, but switch " |
| 470 | + "was not found in controller-cache", dpid); |
| 471 | return; |
| 472 | } |
| 473 | } |
| 474 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 475 | activeEqualSwitches.put(dpid, sw); |
| 476 | } finally { |
| 477 | switchLock.unlock(); |
| 478 | } |
| 479 | |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public void removeConnectedSwitch(Dpid dpid) { |
| 484 | connectedSwitches.remove(dpid); |
| 485 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 486 | if (sw == null) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 487 | log.warn("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 488 | sw = activeEqualSwitches.remove(dpid); |
| 489 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 490 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 491 | log.warn("removal for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 492 | l.switchRemoved(dpid); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public void processMessage(Dpid dpid, OFMessage m) { |
| 498 | processPacket(dpid, m); |
| 499 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 500 | |
| 501 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 502 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 503 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 504 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 507 | } |
| 508 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 509 | private final class OFMessageHandler implements Runnable { |
| 510 | |
| 511 | private final OFMessage msg; |
| 512 | private final Dpid dpid; |
| 513 | |
| 514 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 515 | this.msg = msg; |
| 516 | this.dpid = dpid; |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 521 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 522 | listener.handleMessage(dpid, msg); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | } |
| 527 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 528 | } |