blob: 5377862738e2fe784bbf490d791909038db5547f [file] [log] [blame]
Toshio Koidea03915e2014-07-01 18:39:52 -07001package net.onrc.onos.api.flowmanager;
2
Toshio Koide9aa4c0f2014-08-11 16:06:44 -07003import static com.google.common.base.Preconditions.checkNotNull;
Toshio Koidec79d2642014-08-19 01:09:08 -07004import static com.google.common.base.Preconditions.checkState;
Toshio Koide9aa4c0f2014-08-11 16:06:44 -07005
Brian O'Connorab3e9e62014-09-09 23:45:55 -07006import java.util.ArrayList;
Toshio Koidec79d2642014-08-19 01:09:08 -07007import java.util.Arrays;
8import java.util.Iterator;
9import java.util.LinkedList;
Toshio Koidea03915e2014-07-01 18:39:52 -070010import java.util.List;
11
Toshio Koided7d550c2014-08-21 16:08:55 -070012import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
Toshio Koidec79d2642014-08-19 01:09:08 -070013import net.onrc.onos.core.matchaction.MatchAction;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070014import net.onrc.onos.core.matchaction.MatchActionId;
Toshio Koidec79d2642014-08-19 01:09:08 -070015import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
Ray Milkey42ae1b52014-08-15 16:37:06 -070016import net.onrc.onos.core.matchaction.MatchActionOperations;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070017import net.onrc.onos.core.matchaction.MatchActionOperationsId;
Toshio Koided8b077a2014-08-13 10:47:21 -070018import net.onrc.onos.core.matchaction.action.Action;
Toshio Koidec79d2642014-08-19 01:09:08 -070019import net.onrc.onos.core.matchaction.action.OutputAction;
Toshio Koidea03915e2014-07-01 18:39:52 -070020import net.onrc.onos.core.matchaction.match.PacketMatch;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070021import net.onrc.onos.core.util.IdGenerator;
Toshio Koidea03915e2014-07-01 18:39:52 -070022import net.onrc.onos.core.util.PortNumber;
Toshio Koidec79d2642014-08-19 01:09:08 -070023import net.onrc.onos.core.util.SwitchPort;
Toshio Koidea03915e2014-07-01 18:39:52 -070024
25/**
Toshio Koide7894ca02014-08-15 14:30:13 -070026 * Flow object representing a packet path.
Toshio Koidea03915e2014-07-01 18:39:52 -070027 * <p>
28 * TODO: Think this: Do we need a bandwidth constraint?
29 */
30public class PacketPathFlow extends PathFlow {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070031 private final PacketMatch match;
32 private final int hardTimeout;
33 private final int idleTimeout;
Toshio Koidea03915e2014-07-01 18:39:52 -070034
35 /**
Toshio Koide2c67a2d2014-08-27 11:30:56 -070036 * Default constructor for Kryo deserialization.
37 */
38 @Deprecated
39 protected PacketPathFlow() {
40 match = null;
41 hardTimeout = 0;
42 idleTimeout = 0;
43 }
44
45 /**
Toshio Koidea03915e2014-07-01 18:39:52 -070046 * Constructor.
47 *
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070048 * @param id ID for this new Flow object
49 * @param match the Match object at the source node of the path
50 * @param ingressPort the Ingress port number at the ingress edge node
51 * @param path the Path between ingress and egress edge node
52 * @param egressActions the list of Action objects at the egress edge node
53 * @param hardTimeout the hard-timeout value in seconds, or 0 for no timeout
54 * @param idleTimeout the idle-timeout value in seconds, or 0 for no timeout
Toshio Koidea03915e2014-07-01 18:39:52 -070055 */
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070056 public PacketPathFlow(FlowId id,
57 PacketMatch match, PortNumber ingressPort, Path path,
58 List<Action> egressActions,
Toshio Koidea03915e2014-07-01 18:39:52 -070059 int hardTimeout, int idleTimeout) {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070060 super(id, ingressPort, path, egressActions);
61 this.match = checkNotNull(match);
Toshio Koidea03915e2014-07-01 18:39:52 -070062 this.hardTimeout = hardTimeout;
63 this.idleTimeout = idleTimeout;
64 }
65
66 /**
67 * Gets idle-timeout value.
68 *
69 * @return Idle-timeout value (seconds)
70 */
71 public int getIdleTimeout() {
72 return idleTimeout;
73 }
74
75 /**
76 * Gets hard-timeout value.
77 *
78 * @return Hard-timeout value (seconds)
79 */
80 public int getHardTimeout() {
81 return hardTimeout;
82 }
83
84 @Override
Toshio Koide5c5ca102014-08-19 00:49:52 -070085 public PacketMatch getMatch() {
86 return match;
87 }
88
89 @Override
Toshio Koided7d550c2014-08-21 16:08:55 -070090 public List<MatchActionOperations> compile(Operator op,
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070091 IdGenerator<MatchActionId> maIdGenerator,
92 IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -070093 switch (op) {
94 case ADD:
95 return compileAddOperation(maIdGenerator, maoIdGenerator);
96 case REMOVE:
97 return compileRemoveOperation(maIdGenerator, maoIdGenerator);
98 default:
99 throw new UnsupportedOperationException("Unknown operation.");
100 }
101 }
102
103 /**
104 * Creates the next {@link MatchAction} object using iterators.
105 *
106 * @param portIterator the iterator for {@link SwitchPort} objects
107 * @param actionsIterator the iterator for the lists of {@link Action}
108 * @param maIdGenerator the ID generator of {@link MatchAction}
109 * @return {@link MatchAction} object based on the specified iterators
110 */
111 private MatchAction createNextMatchAction(Iterator<SwitchPort> portIterator,
112 Iterator<List<Action>> actionsIterator,
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700113 IdGenerator<MatchActionId> maIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -0700114 if (portIterator == null || actionsIterator == null ||
115 !portIterator.hasNext() || !actionsIterator.hasNext()) {
116 return null;
117 }
118
119 // TODO: Update this after merging the new MatchAction API.
120 return new MatchAction(
121 maIdGenerator.getNewId(),
122 portIterator.next(),
123 getMatch(), actionsIterator.next());
124 }
125
126 /**
127 * Generates the list of {@link MatchActionOperations} objects with
128 * add-operation.
129 *
130 * @return the list of {@link MatchActionOperations} objects
131 */
132 private List<MatchActionOperations> compileAddOperation(
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700133 IdGenerator<MatchActionId> maIdGenerator,
134 IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -0700135 Path path = checkNotNull(getPath());
136 checkState(path.size() > 0, "Path object has no link.");
137
138 // Preparing each actions and ingress port for each switch
139 List<List<Action>> actionsList = new LinkedList<>();
140 List<SwitchPort> portList = new LinkedList<>();
141 for (FlowLink link : path) {
142 portList.add(link.getDstSwitchPort());
Brian O'Connorab3e9e62014-09-09 23:45:55 -0700143 List<Action> l = new ArrayList<Action>();
144 l.add(new OutputAction(link.getSrcPortNumber()));
145 actionsList.add(l);
146 // Arrays.asList(
147 // (Action) new OutputAction(link.getSrcPortNumber())));
Toshio Koidec79d2642014-08-19 01:09:08 -0700148 }
149
150 // The head switch's ingress port
151 portList.add(0, new SwitchPort(path.getSrcDpid(), getIngressPortNumber()));
152
153 // The tail switch's action
154 actionsList.add(getEgressActions());
155
156 Iterator<SwitchPort> portIterator = portList.iterator();
157 Iterator<List<Action>> actionsIterator = actionsList.iterator();
158
159 // Creates the second phase operation
160 // using the head switch's match action
161 MatchAction headMatchAction = createNextMatchAction(portIterator,
162 actionsIterator, maIdGenerator);
163 if (headMatchAction == null) {
164 return null;
165 }
166 MatchActionOperations secondOp = new MatchActionOperations(
167 maoIdGenerator.getNewId());
168 secondOp.addOperation(new MatchActionOperationEntry(
169 MatchActionOperations.Operator.ADD, headMatchAction));
170
171 // Creates the first phase operation
172 // using the remaining switches' match actions
173 MatchActionOperations firstOp = new MatchActionOperations(
174 maoIdGenerator.getNewId());
175 MatchAction ma;
176 while ((ma = createNextMatchAction(portIterator, actionsIterator, maIdGenerator)) != null) {
177 firstOp.addOperation(new MatchActionOperationEntry(
178 MatchActionOperations.Operator.ADD, ma));
179 }
180
181 return Arrays.asList(firstOp, secondOp);
182 }
183
184 /**
185 * Generates the list of {@link MatchActionOperations} objects with
186 * remote-operation.
187 *
188 * @return the list of {@link MatchActionOperations} objects
189 */
190 private List<MatchActionOperations> compileRemoveOperation(
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700191 IdGenerator<MatchActionId> maIdGenerator,
192 IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -0700193 // TODO implement it
194 throw new UnsupportedOperationException(
195 "REMOVE operation is not implemented yet.");
Toshio Koidea03915e2014-07-01 18:39:52 -0700196 }
197}