Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright Big Switch Networks 2012 |
| 3 | */ |
| 4 | |
| 5 | package net.floodlightcontroller.util; |
| 6 | |
| 7 | import java.io.IOException; |
| 8 | import java.util.EnumSet; |
| 9 | import java.util.Set; |
| 10 | |
| 11 | import net.floodlightcontroller.core.FloodlightContext; |
| 12 | import net.floodlightcontroller.core.IOFSwitch; |
| 13 | |
| 14 | import org.openflow.protocol.OFMessage; |
| 15 | import org.openflow.protocol.OFType; |
| 16 | |
| 17 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | * Dampens OFMessages sent to an OF switch. A message is only written to |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | * a switch if the same message (as defined by .equals()) has not been written |
| 20 | * in the last n milliseconds. Timer granularity is based on TimedCache |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | * @author gregor |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 23 | */ |
| 24 | public class OFMessageDamper { |
| 25 | /** |
| 26 | * An entry in the TimedCache. A cache entry consists of the sent message |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | * as well as the switch to which the message was sent. |
| 28 | * <p/> |
| 29 | * NOTE: We currently use the full OFMessage object. To save space, we |
| 30 | * could use a cryptographic hash (e.g., SHA-1). However, this would |
| 31 | * obviously be more time-consuming.... |
| 32 | * <p/> |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 33 | * We also store a reference to the actual IOFSwitch object and /not/ |
| 34 | * the switch DPID. This way we are guarnteed to not dampen messages if |
| 35 | * a switch disconnects and then reconnects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 37 | * @author gregor |
| 38 | */ |
| 39 | protected static class DamperEntry { |
| 40 | OFMessage msg; |
| 41 | IOFSwitch sw; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | public DamperEntry(OFMessage msg, IOFSwitch sw) { |
| 44 | super(); |
| 45 | this.msg = msg; |
| 46 | this.sw = sw; |
| 47 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 49 | /* (non-Javadoc) |
| 50 | * @see java.lang.Object#hashCode() |
| 51 | */ |
| 52 | @Override |
| 53 | public int hashCode() { |
| 54 | final int prime = 31; |
| 55 | int result = 1; |
| 56 | result = prime * result + ((msg == null) ? 0 : msg.hashCode()); |
| 57 | result = prime * result + ((sw == null) ? 0 : sw.hashCode()); |
| 58 | return result; |
| 59 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 61 | /* (non-Javadoc) |
| 62 | * @see java.lang.Object#equals(java.lang.Object) |
| 63 | */ |
| 64 | @Override |
| 65 | public boolean equals(Object obj) { |
| 66 | if (this == obj) return true; |
| 67 | if (obj == null) return false; |
| 68 | if (getClass() != obj.getClass()) return false; |
| 69 | DamperEntry other = (DamperEntry) obj; |
| 70 | if (msg == null) { |
| 71 | if (other.msg != null) return false; |
| 72 | } else if (!msg.equals(other.msg)) return false; |
| 73 | if (sw == null) { |
| 74 | if (other.sw != null) return false; |
| 75 | } else if (!sw.equals(other.sw)) return false; |
| 76 | return true; |
| 77 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | |
| 79 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 80 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 82 | TimedCache<DamperEntry> cache; |
| 83 | EnumSet<OFType> msgTypesToCache; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 85 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | * @param capacity the maximum number of messages that should be |
| 87 | * kept |
| 88 | * @param typesToDampen The set of OFMessageTypes that should be |
| 89 | * dampened by this instance. Other types will be passed through |
| 90 | * @param timeout The dampening timeout. A message will only be |
| 91 | * written if the last write for the an equal message more than |
| 92 | * timeout ms ago. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 93 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | public OFMessageDamper(int capacity, |
| 95 | Set<OFType> typesToDampen, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 96 | int timeout) { |
| 97 | cache = new TimedCache<DamperEntry>(capacity, timeout); |
| 98 | msgTypesToCache = EnumSet.copyOf(typesToDampen); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 101 | /** |
| 102 | * write the messag to the switch according to our dampening settings |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | * @param sw |
| 105 | * @param msg |
| 106 | * @param cntx |
| 107 | * @return true if the message was written to the switch, false if |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | * the message was dampened. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | * @throws IOException |
| 110 | */ |
| 111 | public boolean write(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | throws IOException { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 113 | return write(sw, msg, cntx, false); |
| 114 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 116 | /** |
| 117 | * write the messag to the switch according to our dampening settings |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | * @param sw |
| 120 | * @param msg |
| 121 | * @param cntx |
| 122 | * @param flush true to flush the packet immidiately |
| 123 | * @return true if the message was written to the switch, false if |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | * the message was dampened. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 125 | * @throws IOException |
| 126 | */ |
| 127 | public boolean write(IOFSwitch sw, OFMessage msg, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 128 | FloodlightContext cntx, boolean flush) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 129 | throws IOException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | if (!msgTypesToCache.contains(msg.getType())) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | sw.write(msg, cntx); |
| 132 | if (flush) { |
| 133 | sw.flush(); |
| 134 | } |
| 135 | return true; |
| 136 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 138 | DamperEntry entry = new DamperEntry(msg, sw); |
| 139 | if (cache.update(entry)) { |
| 140 | // entry exists in cache. Dampening. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | return false; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 142 | } else { |
| 143 | sw.write(msg, cntx); |
| 144 | if (flush) { |
| 145 | sw.flush(); |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | } |