Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | |
Pavlin Radoslavov | da26a9a | 2013-11-22 19:31:36 -0800 | [diff] [blame] | 5 | import org.codehaus.jackson.annotate.JsonIgnore; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 6 | import org.codehaus.jackson.annotate.JsonProperty; |
| 7 | |
| 8 | /** |
| 9 | * The class representing multiple Flow Entry actions. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 10 | * <p/> |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 11 | * A set of Flow Entry actions need to be applied to each packet. |
| 12 | */ |
| 13 | public class FlowEntryActions { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 14 | private ArrayList<FlowEntryAction> actions; // The Flow Entry Actions |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Default constructor. |
| 18 | */ |
| 19 | public FlowEntryActions() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 20 | actions = new ArrayList<FlowEntryAction>(); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Constructor from a string. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | * <p/> |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 26 | * The string has the following form: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | * [[type=XXX action=XXX];[type=XXX action=XXX];...;] |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 28 | * |
| 29 | * @param actionsStr the set of actions as a string. |
| 30 | */ |
| 31 | public FlowEntryActions(String actionsStr) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | this.fromString(actionsStr); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Copy constructor. |
| 37 | * |
| 38 | * @param other the object to copy from. |
| 39 | */ |
| 40 | public FlowEntryActions(FlowEntryActions other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | actions = new ArrayList<FlowEntryAction>(); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 42 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | for (FlowEntryAction action : other.actions) { |
| 44 | FlowEntryAction newAction = new FlowEntryAction(action); |
| 45 | actions.add(newAction); |
| 46 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the Flow Entry Actions. |
| 51 | * |
| 52 | * @return the Flow Entry Actions. |
| 53 | */ |
| 54 | @JsonProperty("actions") |
| 55 | public ArrayList<FlowEntryAction> actions() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | return actions; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Set the Flow Entry Actions. |
| 61 | * |
| 62 | * @param actions the Flow Entry Actions to set. |
| 63 | */ |
| 64 | @JsonProperty("actions") |
| 65 | public void setActions(ArrayList<FlowEntryAction> actions) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | this.actions = actions; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Add a Flow Entry Action. |
| 71 | * |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 72 | * @param flowEntryAction the Flow Entry Action to add. |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 73 | */ |
| 74 | public void addAction(FlowEntryAction flowEntryAction) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | actions.add(flowEntryAction); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Test whether the set of actions is empty. |
| 80 | * |
| 81 | * @return true if the set of actions is empty, otherwise false. |
| 82 | */ |
Pavlin Radoslavov | da26a9a | 2013-11-22 19:31:36 -0800 | [diff] [blame] | 83 | @JsonIgnore |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 84 | public Boolean isEmpty() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | return actions.isEmpty(); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Convert the set of actions to a string. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | * <p/> |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 91 | * The string has the following form: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | * [[type=XXX action=XXX];[type=XXX action=XXX];...;] |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 93 | * |
| 94 | * @return the set of actions as a string. |
| 95 | */ |
| 96 | @Override |
| 97 | public String toString() { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 98 | StringBuilder ret = new StringBuilder(); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 99 | |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 100 | ret.append("["); |
| 101 | for (FlowEntryAction action : actions) { |
| 102 | ret.append(action.toString() + ";"); |
| 103 | } |
| 104 | ret.append("]"); |
| 105 | |
| 106 | return ret.toString(); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Convert a string to a set of actions. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | * <p/> |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 112 | * The string has the following form: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | * [[type=XXX action=XXX];[type=XXX action=XXX];...;] |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 114 | * |
| 115 | * @param actionsStr the set of actions as a string. |
| 116 | */ |
| 117 | public void fromString(String actionsStr) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | String decode = actionsStr; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 119 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | actions = new ArrayList<FlowEntryAction>(); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 121 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 122 | if (decode.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | return; // Nothing to do |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 124 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 125 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 126 | // Remove the '[' and ']' in the beginning and the end of the string |
| 127 | if ((decode.length() > 1) && (decode.charAt(0) == '[') && |
| 128 | (decode.charAt(decode.length() - 1) == ']')) { |
| 129 | decode = decode.substring(1, decode.length() - 1); |
| 130 | } else { |
| 131 | throw new IllegalArgumentException("Invalid action string"); |
| 132 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 133 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | // Split the string, and decode each action |
| 135 | String[] parts = decode.split(";"); |
| 136 | for (int i = 0; i < parts.length; i++) { |
| 137 | decode = parts[i]; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 138 | if ((decode == null) || decode.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 140 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | FlowEntryAction flowEntryAction = null; |
| 142 | try { |
| 143 | flowEntryAction = new FlowEntryAction(decode); |
| 144 | } catch (IllegalArgumentException e) { |
| 145 | // TODO: Ignore invalid actions for now |
| 146 | continue; |
| 147 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 148 | if (flowEntryAction != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | actions.add(flowEntryAction); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 150 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 151 | } |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 152 | } |
| 153 | } |