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