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