blob: 34d6776264931c199d7bc6c23f1df4a37a1a1f4f [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
Toshio Koidec79d2642014-08-19 01:09:08 -07006import java.util.Arrays;
7import java.util.Iterator;
8import java.util.LinkedList;
Toshio Koidea03915e2014-07-01 18:39:52 -07009import java.util.List;
10
Toshio Koided7d550c2014-08-21 16:08:55 -070011import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
Toshio Koidec79d2642014-08-19 01:09:08 -070012import net.onrc.onos.core.matchaction.MatchAction;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070013import net.onrc.onos.core.matchaction.MatchActionId;
Toshio Koidec79d2642014-08-19 01:09:08 -070014import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
Ray Milkey42ae1b52014-08-15 16:37:06 -070015import net.onrc.onos.core.matchaction.MatchActionOperations;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070016import net.onrc.onos.core.matchaction.MatchActionOperationsId;
Toshio Koided8b077a2014-08-13 10:47:21 -070017import net.onrc.onos.core.matchaction.action.Action;
Toshio Koidec79d2642014-08-19 01:09:08 -070018import net.onrc.onos.core.matchaction.action.OutputAction;
Toshio Koidea03915e2014-07-01 18:39:52 -070019import net.onrc.onos.core.matchaction.match.PacketMatch;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070020import net.onrc.onos.core.util.IdGenerator;
Toshio Koidea03915e2014-07-01 18:39:52 -070021import net.onrc.onos.core.util.PortNumber;
Toshio Koidec79d2642014-08-19 01:09:08 -070022import net.onrc.onos.core.util.SwitchPort;
Toshio Koidea03915e2014-07-01 18:39:52 -070023
24/**
Toshio Koide7894ca02014-08-15 14:30:13 -070025 * Flow object representing a packet path.
Toshio Koidea03915e2014-07-01 18:39:52 -070026 * <p>
27 * TODO: Think this: Do we need a bandwidth constraint?
28 */
29public class PacketPathFlow extends PathFlow {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070030 private final PacketMatch match;
31 private final int hardTimeout;
32 private final int idleTimeout;
Toshio Koidea03915e2014-07-01 18:39:52 -070033
34 /**
Toshio Koide2c67a2d2014-08-27 11:30:56 -070035 * Default constructor for Kryo deserialization.
36 */
37 @Deprecated
38 protected PacketPathFlow() {
39 match = null;
40 hardTimeout = 0;
41 idleTimeout = 0;
42 }
43
44 /**
Toshio Koidea03915e2014-07-01 18:39:52 -070045 * Constructor.
46 *
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070047 * @param id ID for this new Flow object
48 * @param match the Match object at the source node of the path
49 * @param ingressPort the Ingress port number at the ingress edge node
50 * @param path the Path between ingress and egress edge node
51 * @param egressActions the list of Action objects at the egress edge node
52 * @param hardTimeout the hard-timeout value in seconds, or 0 for no timeout
53 * @param idleTimeout the idle-timeout value in seconds, or 0 for no timeout
Toshio Koidea03915e2014-07-01 18:39:52 -070054 */
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070055 public PacketPathFlow(FlowId id,
56 PacketMatch match, PortNumber ingressPort, Path path,
57 List<Action> egressActions,
Toshio Koidea03915e2014-07-01 18:39:52 -070058 int hardTimeout, int idleTimeout) {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070059 super(id, ingressPort, path, egressActions);
60 this.match = checkNotNull(match);
Toshio Koidea03915e2014-07-01 18:39:52 -070061 this.hardTimeout = hardTimeout;
62 this.idleTimeout = idleTimeout;
63 }
64
65 /**
66 * Gets idle-timeout value.
67 *
68 * @return Idle-timeout value (seconds)
69 */
70 public int getIdleTimeout() {
71 return idleTimeout;
72 }
73
74 /**
75 * Gets hard-timeout value.
76 *
77 * @return Hard-timeout value (seconds)
78 */
79 public int getHardTimeout() {
80 return hardTimeout;
81 }
82
83 @Override
Toshio Koide5c5ca102014-08-19 00:49:52 -070084 public PacketMatch getMatch() {
85 return match;
86 }
87
88 @Override
Toshio Koided7d550c2014-08-21 16:08:55 -070089 public List<MatchActionOperations> compile(Operator op,
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070090 IdGenerator<MatchActionId> maIdGenerator,
91 IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -070092 switch (op) {
93 case ADD:
94 return compileAddOperation(maIdGenerator, maoIdGenerator);
95 case REMOVE:
96 return compileRemoveOperation(maIdGenerator, maoIdGenerator);
97 default:
98 throw new UnsupportedOperationException("Unknown operation.");
99 }
100 }
101
102 /**
103 * Creates the next {@link MatchAction} object using iterators.
104 *
105 * @param portIterator the iterator for {@link SwitchPort} objects
106 * @param actionsIterator the iterator for the lists of {@link Action}
107 * @param maIdGenerator the ID generator of {@link MatchAction}
108 * @return {@link MatchAction} object based on the specified iterators
109 */
110 private MatchAction createNextMatchAction(Iterator<SwitchPort> portIterator,
111 Iterator<List<Action>> actionsIterator,
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700112 IdGenerator<MatchActionId> maIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -0700113 if (portIterator == null || actionsIterator == null ||
114 !portIterator.hasNext() || !actionsIterator.hasNext()) {
115 return null;
116 }
117
118 // TODO: Update this after merging the new MatchAction API.
119 return new MatchAction(
120 maIdGenerator.getNewId(),
121 portIterator.next(),
122 getMatch(), actionsIterator.next());
123 }
124
125 /**
126 * Generates the list of {@link MatchActionOperations} objects with
127 * add-operation.
128 *
129 * @return the list of {@link MatchActionOperations} objects
130 */
131 private List<MatchActionOperations> compileAddOperation(
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700132 IdGenerator<MatchActionId> maIdGenerator,
133 IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -0700134 Path path = checkNotNull(getPath());
135 checkState(path.size() > 0, "Path object has no link.");
136
137 // Preparing each actions and ingress port for each switch
138 List<List<Action>> actionsList = new LinkedList<>();
139 List<SwitchPort> portList = new LinkedList<>();
140 for (FlowLink link : path) {
141 portList.add(link.getDstSwitchPort());
142 actionsList.add(Arrays.asList(
143 (Action) new OutputAction(link.getSrcPortNumber())));
144 }
145
146 // The head switch's ingress port
147 portList.add(0, new SwitchPort(path.getSrcDpid(), getIngressPortNumber()));
148
149 // The tail switch's action
150 actionsList.add(getEgressActions());
151
152 Iterator<SwitchPort> portIterator = portList.iterator();
153 Iterator<List<Action>> actionsIterator = actionsList.iterator();
154
155 // Creates the second phase operation
156 // using the head switch's match action
157 MatchAction headMatchAction = createNextMatchAction(portIterator,
158 actionsIterator, maIdGenerator);
159 if (headMatchAction == null) {
160 return null;
161 }
162 MatchActionOperations secondOp = new MatchActionOperations(
163 maoIdGenerator.getNewId());
164 secondOp.addOperation(new MatchActionOperationEntry(
165 MatchActionOperations.Operator.ADD, headMatchAction));
166
167 // Creates the first phase operation
168 // using the remaining switches' match actions
169 MatchActionOperations firstOp = new MatchActionOperations(
170 maoIdGenerator.getNewId());
171 MatchAction ma;
172 while ((ma = createNextMatchAction(portIterator, actionsIterator, maIdGenerator)) != null) {
173 firstOp.addOperation(new MatchActionOperationEntry(
174 MatchActionOperations.Operator.ADD, ma));
175 }
176
177 return Arrays.asList(firstOp, secondOp);
178 }
179
180 /**
181 * Generates the list of {@link MatchActionOperations} objects with
182 * remote-operation.
183 *
184 * @return the list of {@link MatchActionOperations} objects
185 */
186 private List<MatchActionOperations> compileRemoveOperation(
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700187 IdGenerator<MatchActionId> maIdGenerator,
188 IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -0700189 // TODO implement it
190 throw new UnsupportedOperationException(
191 "REMOVE operation is not implemented yet.");
Toshio Koidea03915e2014-07-01 18:39:52 -0700192 }
193}