blob: 8839cebcf8e7d6226dafe443af7426c046ff0789 [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.
10 *
11 * A set of Flow Entry actions need to be applied to each packet.
12 */
13public class FlowEntryActions {
14 private ArrayList<FlowEntryAction> actions; // The Flow Entry Actions
15
16 /**
17 * Default constructor.
18 */
19 public FlowEntryActions() {
20 actions = new ArrayList<FlowEntryAction>();
21 }
22
23 /**
24 * Constructor from a string.
25 *
26 * The string has the following form:
27 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
28 *
29 * @param actionsStr the set of actions as a string.
30 */
31 public FlowEntryActions(String actionsStr) {
32 this.fromString(actionsStr);
33 }
34
35 /**
36 * Copy constructor.
37 *
38 * @param other the object to copy from.
39 */
40 public FlowEntryActions(FlowEntryActions other) {
41 actions = new ArrayList<FlowEntryAction>();
42
43 for (FlowEntryAction action : other.actions) {
44 FlowEntryAction newAction = new FlowEntryAction(action);
45 actions.add(newAction);
46 }
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() {
56 return actions;
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) {
66 this.actions = actions;
67 }
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) {
75 actions.add(flowEntryAction);
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 Radoslavovda26a9a2013-11-22 19:31:36 -080083 @JsonIgnore
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070084 public Boolean isEmpty() {
85 return actions.isEmpty();
86 }
87
88 /**
89 * Convert the set of actions to a string.
90 *
91 * The string has the following form:
92 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
93 *
94 * @return the set of actions as a string.
95 */
96 @Override
97 public String toString() {
98 String ret = "[";
99 for (FlowEntryAction action : actions) {
100 ret += action.toString() + ";";
101 }
102 ret += "]";
103
104 return ret;
105 }
106
107 /**
108 * Convert a string to a set of actions.
109 *
110 * The string has the following form:
111 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
112 *
113 * @param actionsStr the set of actions as a string.
114 */
115 public void fromString(String actionsStr) {
116 String decode = actionsStr;
117
118 actions = new ArrayList<FlowEntryAction>();
119
120 if (decode.isEmpty())
121 return; // Nothing to do
122
123 // 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 }
130
131 // 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 }
147 }
148}