HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 5 | import org.codehaus.jackson.annotate.JsonProperty; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 6 | |
| 7 | /** |
Pavlin Radoslavov | d5b21db | 2013-07-29 17:09:53 -0700 | [diff] [blame] | 8 | * The data forwarding path state from a source to a destination. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 9 | */ |
| 10 | public class DataPath { |
| 11 | private SwitchPort srcPort; // The source port |
| 12 | private SwitchPort dstPort; // The destination port |
| 13 | private ArrayList<FlowEntry> flowEntries; // The Flow Entries |
| 14 | |
| 15 | /** |
| 16 | * Default constructor. |
| 17 | */ |
| 18 | public DataPath() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 19 | srcPort = new SwitchPort(); |
| 20 | dstPort = new SwitchPort(); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 21 | flowEntries = new ArrayList<FlowEntry>(); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Get the data path source port. |
| 26 | * |
| 27 | * @return the data path source port. |
| 28 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 29 | @JsonProperty("srcPort") |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 30 | public SwitchPort srcPort() { return srcPort; } |
| 31 | |
| 32 | /** |
| 33 | * Set the data path source port. |
| 34 | * |
| 35 | * @param srcPort the data path source port to set. |
| 36 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 37 | @JsonProperty("srcPort") |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 38 | public void setSrcPort(SwitchPort srcPort) { |
| 39 | this.srcPort = srcPort; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the data path destination port. |
| 44 | * |
| 45 | * @return the data path destination port. |
| 46 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 47 | @JsonProperty("dstPort") |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 48 | public SwitchPort dstPort() { return dstPort; } |
| 49 | |
| 50 | /** |
| 51 | * Set the data path destination port. |
| 52 | * |
| 53 | * @param dstPort the data path destination port to set. |
| 54 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 55 | @JsonProperty("dstPort") |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 56 | public void setDstPort(SwitchPort dstPort) { |
| 57 | this.dstPort = dstPort; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the data path flow entries. |
| 62 | * |
| 63 | * @return the data path flow entries. |
| 64 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 65 | @JsonProperty("flowEntries") |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 66 | public ArrayList<FlowEntry> flowEntries() { return flowEntries; } |
| 67 | |
| 68 | /** |
| 69 | * Set the data path flow entries. |
| 70 | * |
| 71 | * @param flowEntries the data path flow entries to set. |
| 72 | */ |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 73 | @JsonProperty("flowEntries") |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 74 | public void setFlowEntries(ArrayList<FlowEntry> flowEntries) { |
| 75 | this.flowEntries = flowEntries; |
| 76 | } |
| 77 | |
| 78 | /** |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 79 | * Apply Flow Path Flags to the pre-computed Data Path. |
| 80 | * |
| 81 | * @param flowPathFlags the Flow Path Flags to apply. |
| 82 | */ |
| 83 | public void applyFlowPathFlags(FlowPathFlags flowPathFlags) { |
| 84 | if (flowPathFlags == null) |
| 85 | return; // Nothing to do |
| 86 | |
| 87 | // Discard the first Flow Entry |
| 88 | if (flowPathFlags.isDiscardFirstHopEntry()) { |
| 89 | if (flowEntries.size() > 0) |
| 90 | flowEntries.remove(0); |
| 91 | } |
| 92 | |
| 93 | // Keep only the first Flow Entry |
| 94 | if (flowPathFlags.isKeepOnlyFirstHopEntry()) { |
| 95 | if (flowEntries.size() > 1) { |
| 96 | FlowEntry flowEntry = flowEntries.get(0); |
| 97 | flowEntries.clear(); |
| 98 | flowEntries.add(flowEntry); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
Pavlin Radoslavov | 31a15c3 | 2013-11-06 17:51:12 -0800 | [diff] [blame] | 104 | * Remove Flow Entries that were deleted. |
| 105 | */ |
| 106 | public void removeDeletedFlowEntries() { |
| 107 | // |
| 108 | // NOTE: We create a new ArrayList, and add only the Flow Entries |
| 109 | // that are NOT FE_USER_DELETE. |
| 110 | // This is sub-optimal: if it adds notable processing cost, |
| 111 | // the Flow Entries container should be changed to LinkedList |
| 112 | // or some other container that has O(1) cost of removing an entry. |
| 113 | // |
| 114 | |
| 115 | // Test first whether any Flow Entry was deleted |
| 116 | boolean foundDeletedFlowEntry = false; |
| 117 | for (FlowEntry flowEntry : this.flowEntries) { |
| 118 | if (flowEntry.flowEntryUserState() == |
| 119 | FlowEntryUserState.FE_USER_DELETE) { |
| 120 | foundDeletedFlowEntry = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (! foundDeletedFlowEntry) |
| 125 | return; // Nothing to do |
| 126 | |
| 127 | // Create a new collection and exclude the deleted flow entries |
| 128 | ArrayList<FlowEntry> newFlowEntries = new ArrayList<FlowEntry>(); |
| 129 | for (FlowEntry flowEntry : this.flowEntries()) { |
| 130 | if (flowEntry.flowEntryUserState() != |
| 131 | FlowEntryUserState.FE_USER_DELETE) { |
| 132 | newFlowEntries.add(flowEntry); |
| 133 | } |
| 134 | } |
| 135 | setFlowEntries(newFlowEntries); |
| 136 | } |
| 137 | |
| 138 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 139 | * Get a string with the summary of the shortest-path data path |
| 140 | * computation. |
| 141 | * |
| 142 | * NOTE: This method assumes the DataPath was created by |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 143 | * using the TopologyManager shortest path computation, so the inPort |
| 144 | * and outPort of the Flow Entries are set. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 145 | * NOTE: This method is a temporary solution and will be removed |
| 146 | * in the future. |
| 147 | * |
| 148 | * @return a string with the summary of the shortest-path |
| 149 | * data path computation if valid, otherwise the string "X". |
| 150 | * If the shortest-path was valid, The string has the following form: |
| 151 | * inPort/dpid/outPort;inPort/dpid/outPort;... |
| 152 | */ |
| 153 | public String dataPathSummary() { |
Yuta HIGUCHI | 8e8900f | 2013-10-14 15:56:22 -0700 | [diff] [blame] | 154 | StringBuilder resultStr = new StringBuilder(5+1+20+1+5+1); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 155 | if (this.flowEntries != null) { |
| 156 | for (FlowEntry flowEntry : this.flowEntries) { |
| 157 | // The data path summary string |
Yuta HIGUCHI | 8e8900f | 2013-10-14 15:56:22 -0700 | [diff] [blame] | 158 | resultStr.append(flowEntry.inPort().toString()).append('/') |
| 159 | .append(flowEntry.dpid().toString()).append('/') |
| 160 | .append(flowEntry.outPort().toString()).append(';'); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Yuta HIGUCHI | 8e8900f | 2013-10-14 15:56:22 -0700 | [diff] [blame] | 163 | if (resultStr.length() == 0) |
| 164 | resultStr.append("X"); // Invalid shortest-path |
| 165 | return resultStr.toString(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 169 | * Convert the data path to a string. |
| 170 | * |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 171 | * The string has the following form: |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 172 | * [src=01:01:01:01:01:01:01:01/1111 flowEntry=<entry1> flowEntry=<entry2> flowEntry=<entry3> dst=02:02:02:02:02:02:02:02/2222] |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 173 | * |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 174 | * @return the data path as a string. |
| 175 | */ |
| 176 | @Override |
| 177 | public String toString() { |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 178 | String ret = "[src=" + this.srcPort.toString(); |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 179 | |
| 180 | for (FlowEntry fe : flowEntries) { |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 181 | ret += " flowEntry=" + fe.toString(); |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 182 | } |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 183 | ret += " dst=" + this.dstPort.toString() + "]"; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 184 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 185 | return ret; |
| 186 | } |
| 187 | } |