blob: 62d9a65f4c4148e97a342b47d1e506e00995079c [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() {
Pavlin Radoslavov424150c2014-04-09 12:12:36 -070098 StringBuilder ret = new StringBuilder();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070099
Pavlin Radoslavov424150c2014-04-09 12:12:36 -0700100 ret.append("[");
101 for (FlowEntryAction action : actions) {
102 ret.append(action.toString() + ";");
103 }
104 ret.append("]");
105
106 return ret.toString();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700107 }
108
109 /**
110 * Convert a string to a set of actions.
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 * <p/>
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700112 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 * [[type=XXX action=XXX];[type=XXX action=XXX];...;]
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700114 *
115 * @param actionsStr the set of actions as a string.
116 */
117 public void fromString(String actionsStr) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 String decode = actionsStr;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700119
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 actions = new ArrayList<FlowEntryAction>();
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700121
Ray Milkeyb29e6262014-04-09 16:02:14 -0700122 if (decode.isEmpty()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 return; // Nothing to do
Ray Milkeyb29e6262014-04-09 16:02:14 -0700124 }
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700125
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 // 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 Radoslavov1bc2c472013-07-17 18:11:37 -0700133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 // 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 Milkeyb29e6262014-04-09 16:02:14 -0700138 if ((decode == null) || decode.isEmpty()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700140 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 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 Milkeyb29e6262014-04-09 16:02:14 -0700148 if (flowEntryAction != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 actions.add(flowEntryAction);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700150 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 }
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700152 }
153}