Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | import java.util.Queue; |
| 6 | import java.util.Set; |
| 7 | |
| 8 | import net.floodlightcontroller.core.IOFSwitch; |
| 9 | |
| 10 | import org.openflow.protocol.OFMessage; |
| 11 | |
| 12 | /** |
| 13 | * Represents table of message queues attached to each switch. |
| 14 | * Each message should be ADD/DELETE of flow. |
| 15 | * (MODIFY of flow might be handled, but future work) |
| 16 | * @author Naoki Shiota |
| 17 | * |
| 18 | */ |
| 19 | public class FlowQueueTable { |
| 20 | |
| 21 | public enum QueueState { |
| 22 | SYNCHRONIZED, |
| 23 | SYNCHRONIZING, |
| 24 | DELETED; // not in work and to be deleted |
| 25 | } |
| 26 | |
| 27 | private class QueueInfo { |
| 28 | QueueState state; |
| 29 | |
| 30 | // Max rate of sending message (bytes/sec). 0 implies no limitation. |
| 31 | long max_rate = 0; |
| 32 | |
| 33 | // Is sending message suspended or not. |
| 34 | boolean suspended = false; |
| 35 | } |
| 36 | |
| 37 | private Map< IOFSwitch, Queue<OFMessage> > queues |
| 38 | = new HashMap< IOFSwitch, Queue<OFMessage> >(); |
| 39 | private Map<IOFSwitch, QueueInfo> queue_info |
| 40 | = new HashMap<IOFSwitch, QueueInfo>(); |
| 41 | |
| 42 | public FlowQueueTable() { |
| 43 | // TODO Auto-generated constructor stub |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add flow queue for given switch. |
| 48 | * Note queue should be given by caller so that caller can select data |
| 49 | * structure suitable for its processing. |
| 50 | * @param sw |
| 51 | * @param queue |
| 52 | */ |
| 53 | public void addSwitchQueue(IOFSwitch sw, Queue<OFMessage> queue) { |
| 54 | QueueInfo info = new QueueInfo(); |
| 55 | |
| 56 | if (queues.containsKey(sw)) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | queues.put(sw, queue); |
| 61 | queue_info.put(sw, info); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Delete flow queue for given switch. |
| 66 | * @param sw |
| 67 | */ |
| 68 | public void deleteSwitchQueue(IOFSwitch sw) { |
| 69 | if (! queues.containsKey(sw)) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | queues.remove(sw); |
| 74 | queue_info.remove(sw); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get flow queue for given switch. |
| 79 | * @param sw |
| 80 | * @return |
| 81 | */ |
| 82 | public Queue<OFMessage> getQueue(IOFSwitch sw) { |
| 83 | return queues.get(sw); |
| 84 | } |
| 85 | |
| 86 | public Set<IOFSwitch> getSwitches() { |
| 87 | return queues.keySet(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get state of flow queue for given switch. |
| 92 | * @param sw |
| 93 | */ |
| 94 | public QueueState getQueueState(IOFSwitch sw) { |
| 95 | QueueInfo info = queue_info.get(sw); |
| 96 | if (info == null) { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | return info.state; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Set state of flow queue for given switch. |
| 105 | * @param sw |
| 106 | * @param state |
| 107 | */ |
| 108 | public void setQueueState(IOFSwitch sw, QueueState state) { |
| 109 | QueueInfo info = queue_info.get(sw); |
| 110 | if (info == null) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | info.state = state; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get maximum rate for given switch. |
| 119 | * @param sw |
| 120 | */ |
| 121 | public long getQueueRate(IOFSwitch sw) { |
| 122 | QueueInfo info = queue_info.get(sw); |
| 123 | if (info == null) { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | return info.max_rate; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set maximum rate for given switch. |
| 132 | * @param sw |
| 133 | * @param rate |
| 134 | */ |
| 135 | public void setQueueRate(IOFSwitch sw, long rate) { |
| 136 | QueueInfo info = queue_info.get(sw); |
| 137 | if (info == null) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | info.max_rate = rate; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Suspend sending message of a queue for given switch. |
| 146 | * @param sw |
| 147 | */ |
| 148 | public void suspendQueue(IOFSwitch sw) { |
| 149 | setQueueSuspended(sw, true); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Resume sending message of a queue for given switch. |
| 154 | * @param sw |
| 155 | */ |
| 156 | public void resumeQueue(IOFSwitch sw) { |
| 157 | setQueueSuspended(sw, false); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check if queue is suspended or not. |
| 162 | * @param sw |
| 163 | * @return |
| 164 | */ |
| 165 | public boolean isQueueSusupended(IOFSwitch sw) { |
| 166 | QueueInfo info = queue_info.get(sw); |
| 167 | if (info == null) { |
| 168 | // TODO error handling |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | return info.suspended; |
| 173 | } |
| 174 | |
| 175 | private void setQueueSuspended(IOFSwitch sw, boolean suspended) { |
| 176 | QueueInfo info = queue_info.get(sw); |
| 177 | if (info == null) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | info.suspended =suspended; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get a lock for queue for given switch. |
| 186 | * If locked already, wait for unlock. |
| 187 | * @param sw |
| 188 | */ |
| 189 | public void lockQueue(IOFSwitch sw) { |
| 190 | // TODO not yet implement |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get a lock for queue for given switch. |
| 195 | * If locked already, return false at once. |
| 196 | * @param sw |
| 197 | * @return |
| 198 | */ |
| 199 | public boolean lockQueueIfAvailable(IOFSwitch sw) { |
| 200 | // TODO not yet implement |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | /** Release a lock for queue for given switch. |
| 205 | * @param sw |
| 206 | */ |
| 207 | public void unlockQueue(IOFSwitch sw) { |
| 208 | // TODO not yet implement |
| 209 | } |
| 210 | } |