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