blob: fc55932fb94ebe3ece6b229cff11fa14cf9dbef9 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -07002
3import java.util.ArrayList;
4
Pavlin Radoslavovda26a9a2013-11-22 19:31:36 -08005import org.codehaus.jackson.annotate.JsonIgnore;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -07006import org.codehaus.jackson.annotate.JsonProperty;
7
8/**
9 * The class representing multiple Flow Entry actions.
Ray Milkey269ffb92014-04-03 14:43:30 -070010 * <p/>
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070011 * A set of Flow Entry actions need to be applied to each packet.
12 */
13public class FlowEntryActions {
Ray Milkey269ffb92014-04-03 14:43:30 -070014 private ArrayList<FlowEntryAction> actions; // The Flow Entry Actions
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070015
16 /**
17 * Default constructor.
18 */
19 public FlowEntryActions() {
Ray Milkey269ffb92014-04-03 14:43:30 -070020 actions = new ArrayList<FlowEntryAction>();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070021 }
22
23 /**
24 * Constructor from a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070025 * <p/>
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070026 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070027 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070028 *
29 * @param actionsStr the set of actions as a string.
30 */
31 public FlowEntryActions(String actionsStr) {
Ray Milkey269ffb92014-04-03 14:43:30 -070032 this.fromString(actionsStr);
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070033 }
34
35 /**
36 * Copy constructor.
37 *
38 * @param other the object to copy from.
39 */
40 public FlowEntryActions(FlowEntryActions other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070041 actions = new ArrayList<FlowEntryAction>();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070042
Ray Milkey269ffb92014-04-03 14:43:30 -070043 for (FlowEntryAction action : other.actions) {
44 FlowEntryAction newAction = new FlowEntryAction(action);
45 actions.add(newAction);
46 }
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070047 }
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 Milkey269ffb92014-04-03 14:43:30 -070056 return actions;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070057 }
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 Milkey269ffb92014-04-03 14:43:30 -070066 this.actions = actions;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070067 }
68
69 /**
70 * Add a Flow Entry Action.
71 *
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070072 * @param flowEntryAction the Flow Entry Action to add.
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070073 */
74 public void addAction(FlowEntryAction flowEntryAction) {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 actions.add(flowEntryAction);
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070076 }
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 Radoslavovda26a9a2013-11-22 19:31:36 -080083 @JsonIgnore
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070084 public Boolean isEmpty() {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return actions.isEmpty();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070086 }
87
88 /**
89 * Convert the set of actions to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070090 * <p/>
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070091 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070092 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070093 *
94 * @return the set of actions as a string.
95 */
96 @Override
97 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070098 String ret = "[";
99 for (FlowEntryAction action : actions) {
100 ret += action.toString() + ";";
101 }
102 ret += "]";
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700103
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 return ret;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700105 }
106
107 /**
108 * Convert a string to a set of actions.
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 * <p/>
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700110 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700112 *
113 * @param actionsStr the set of actions as a string.
114 */
115 public void fromString(String actionsStr) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 String decode = actionsStr;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 actions = new ArrayList<FlowEntryAction>();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700119
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 if (decode.isEmpty())
121 return; // Nothing to do
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700122
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 // Remove the '[' and ']' in the beginning and the end of the string
124 if ((decode.length() > 1) && (decode.charAt(0) == '[') &&
125 (decode.charAt(decode.length() - 1) == ']')) {
126 decode = decode.substring(1, decode.length() - 1);
127 } else {
128 throw new IllegalArgumentException("Invalid action string");
129 }
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 // Split the string, and decode each action
132 String[] parts = decode.split(";");
133 for (int i = 0; i < parts.length; i++) {
134 decode = parts[i];
135 if ((decode == null) || decode.isEmpty())
136 continue;
137 FlowEntryAction flowEntryAction = null;
138 try {
139 flowEntryAction = new FlowEntryAction(decode);
140 } catch (IllegalArgumentException e) {
141 // TODO: Ignore invalid actions for now
142 continue;
143 }
144 if (flowEntryAction != null)
145 actions.add(flowEntryAction);
146 }
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700147 }
148}