Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Collections; |
| 6 | import java.util.Comparator; |
| 7 | import java.util.HashMap; |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | |
| 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.IOFSwitch; |
| 13 | |
| 14 | import org.openflow.protocol.OFFlowMod; |
| 15 | import org.openflow.protocol.OFMessage; |
| 16 | import org.openflow.protocol.OFPort; |
| 17 | import org.openflow.util.HexString; |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | public class FlowCache { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame^] | 22 | private static final Logger log = LoggerFactory.getLogger(FlowCache.class); |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 23 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 24 | private final IFloodlightProviderService floodlightProvider; |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 25 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 26 | private final Map<Long, List<OFFlowMod>> flowCacheMap; |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 27 | |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 28 | private final Comparator<OFFlowMod> cookieComparator = |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 29 | new Comparator<OFFlowMod>() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | @Override |
| 31 | public int compare(OFFlowMod fm1, OFFlowMod fm2) { |
| 32 | long difference = fm2.getCookie() - fm1.getCookie(); |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 33 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | if (difference > 0) { |
| 35 | return 1; |
| 36 | } else if (difference < 0) { |
| 37 | return -1; |
| 38 | } else { |
| 39 | return 0; |
| 40 | } |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | public FlowCache(IFloodlightProviderService floodlightProvider) { |
| 45 | this.floodlightProvider = floodlightProvider; |
| 46 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 47 | flowCacheMap = new HashMap<Long, List<OFFlowMod>>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 50 | public void write(long dpid, OFFlowMod flowMod) { |
| 51 | synchronized (this) { |
| 52 | List<OFFlowMod> flowModList = new ArrayList<OFFlowMod>(1); |
| 53 | flowModList.add(flowMod); |
| 54 | write(dpid, flowModList); |
| 55 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 58 | public void write(long dpid, List<OFFlowMod> flowMods) { |
| 59 | synchronized (this) { |
| 60 | ensureCacheForSwitch(dpid); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 61 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 62 | List<OFFlowMod> clones = new ArrayList<OFFlowMod>(flowMods.size()); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 63 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 64 | // Somehow the OFFlowMods we get passed in will change later on. |
| 65 | // No idea how this happens, but we can just clone to prevent problems |
| 66 | try { |
| 67 | for (OFFlowMod fm : flowMods) { |
| 68 | clones.add(fm.clone()); |
| 69 | } |
| 70 | } catch (CloneNotSupportedException e) { |
| 71 | log.debug("Clone exception", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 73 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 74 | flowCacheMap.get(dpid).addAll(clones); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 75 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 76 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 77 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 78 | if (sw == null) { |
| 79 | log.debug("Switch not found when writing flow mods"); |
| 80 | return; |
| 81 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 82 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 83 | List<OFMessage> msgList = new ArrayList<OFMessage>(clones.size()); |
| 84 | msgList.addAll(clones); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 85 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 86 | try { |
| 87 | sw.write(msgList, null); |
| 88 | } catch (IOException e) { |
| 89 | log.error("Error writing to switch", e); |
| 90 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 94 | public void delete(long dpid, OFFlowMod flowMod) { |
| 95 | synchronized (this) { |
| 96 | List<OFFlowMod> flowModList = new ArrayList<OFFlowMod>(1); |
| 97 | flowModList.add(flowMod); |
| 98 | delete(dpid, flowModList); |
| 99 | } |
| 100 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 102 | public void delete(long dpid, List<OFFlowMod> flowMods) { |
| 103 | synchronized (this) { |
| 104 | ensureCacheForSwitch(dpid); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 105 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 106 | // Remove the flow mods from the cache first before we alter them |
| 107 | flowCacheMap.get(dpid).removeAll(flowMods); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 108 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 109 | // Alter the original flow mods to make them delete flow mods |
| 110 | for (OFFlowMod fm : flowMods) { |
| 111 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT) |
| 112 | .setOutPort(OFPort.OFPP_NONE) |
| 113 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 114 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 115 | fm.getActions().clear(); |
| 116 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 117 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 118 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 119 | if (sw == null) { |
| 120 | log.debug("Switch not found when writing flow mods"); |
| 121 | return; |
| 122 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 123 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 124 | List<OFMessage> msgList = new ArrayList<OFMessage>(flowMods.size()); |
| 125 | msgList.addAll(flowMods); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 126 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 127 | try { |
| 128 | sw.write(msgList, null); |
| 129 | } catch (IOException e) { |
| 130 | log.error("Error writing to switch", e); |
| 131 | } |
| 132 | } |
| 133 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 135 | public void switchConnected(IOFSwitch sw) { |
| 136 | synchronized (this) { |
| 137 | log.debug("Switch connected: {}", sw); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 138 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 139 | ensureCacheForSwitch(sw.getId()); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 140 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 141 | List<OFFlowMod> flowMods = flowCacheMap.get(sw.getId()); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 142 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 143 | Collections.sort(flowMods, cookieComparator); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 144 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 145 | sw.clearAllFlowMods(); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 146 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 147 | List<OFMessage> messages = new ArrayList<OFMessage>(flowMods.size()); |
| 148 | messages.addAll(flowMods); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 149 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 150 | try { |
| 151 | sw.write(messages, null); |
| 152 | } catch (IOException e) { |
| 153 | log.error("Failure writing flow mods to switch {}", |
| 154 | HexString.toHexString(sw.getId())); |
| 155 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | private void ensureCacheForSwitch(long dpid) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 160 | if (!flowCacheMap.containsKey(dpid)) { |
| 161 | flowCacheMap.put(dpid, new ArrayList<OFFlowMod>()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 164 | } |