blob: 42cbd655ebbfccf43a6311ec20345636408b21b0 [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;
Toshio Koided7d550c2014-08-21 16:08:55 -070013import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
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;
Toshio Koided7d550c2014-08-21 16:08:55 -070016import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
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;
20import net.onrc.onos.core.util.PortNumber;
Toshio Koidec79d2642014-08-19 01:09:08 -070021import net.onrc.onos.core.util.SwitchPort;
Toshio Koidea03915e2014-07-01 18:39:52 -070022
23/**
Toshio Koide7894ca02014-08-15 14:30:13 -070024 * Flow object representing a packet path.
Toshio Koidea03915e2014-07-01 18:39:52 -070025 * <p>
26 * TODO: Think this: Do we need a bandwidth constraint?
27 */
28public class PacketPathFlow extends PathFlow {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070029 private final PacketMatch match;
30 private final int hardTimeout;
31 private final int idleTimeout;
Toshio Koidea03915e2014-07-01 18:39:52 -070032
33 /**
34 * Constructor.
35 *
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070036 * @param id ID for this new Flow object
37 * @param match the Match object at the source node of the path
38 * @param ingressPort the Ingress port number at the ingress edge node
39 * @param path the Path between ingress and egress edge node
40 * @param egressActions the list of Action objects at the egress edge node
41 * @param hardTimeout the hard-timeout value in seconds, or 0 for no timeout
42 * @param idleTimeout the idle-timeout value in seconds, or 0 for no timeout
Toshio Koidea03915e2014-07-01 18:39:52 -070043 */
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070044 public PacketPathFlow(FlowId id,
45 PacketMatch match, PortNumber ingressPort, Path path,
46 List<Action> egressActions,
Toshio Koidea03915e2014-07-01 18:39:52 -070047 int hardTimeout, int idleTimeout) {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070048 super(id, ingressPort, path, egressActions);
49 this.match = checkNotNull(match);
Toshio Koidea03915e2014-07-01 18:39:52 -070050 this.hardTimeout = hardTimeout;
51 this.idleTimeout = idleTimeout;
52 }
53
54 /**
55 * Gets idle-timeout value.
56 *
57 * @return Idle-timeout value (seconds)
58 */
59 public int getIdleTimeout() {
60 return idleTimeout;
61 }
62
63 /**
64 * Gets hard-timeout value.
65 *
66 * @return Hard-timeout value (seconds)
67 */
68 public int getHardTimeout() {
69 return hardTimeout;
70 }
71
72 @Override
Toshio Koide5c5ca102014-08-19 00:49:52 -070073 public PacketMatch getMatch() {
74 return match;
75 }
76
77 @Override
Toshio Koided7d550c2014-08-21 16:08:55 -070078 public List<MatchActionOperations> compile(Operator op,
79 MatchActionIdGenerator maIdGenerator,
80 MatchActionOperationsIdGenerator maoIdGenerator) {
Toshio Koidec79d2642014-08-19 01:09:08 -070081 switch (op) {
82 case ADD:
83 return compileAddOperation(maIdGenerator, maoIdGenerator);
84 case REMOVE:
85 return compileRemoveOperation(maIdGenerator, maoIdGenerator);
86 default:
87 throw new UnsupportedOperationException("Unknown operation.");
88 }
89 }
90
91 /**
92 * Creates the next {@link MatchAction} object using iterators.
93 *
94 * @param portIterator the iterator for {@link SwitchPort} objects
95 * @param actionsIterator the iterator for the lists of {@link Action}
96 * @param maIdGenerator the ID generator of {@link MatchAction}
97 * @return {@link MatchAction} object based on the specified iterators
98 */
99 private MatchAction createNextMatchAction(Iterator<SwitchPort> portIterator,
100 Iterator<List<Action>> actionsIterator,
101 MatchActionIdGenerator maIdGenerator) {
102 if (portIterator == null || actionsIterator == null ||
103 !portIterator.hasNext() || !actionsIterator.hasNext()) {
104 return null;
105 }
106
107 // TODO: Update this after merging the new MatchAction API.
108 return new MatchAction(
109 maIdGenerator.getNewId(),
110 portIterator.next(),
111 getMatch(), actionsIterator.next());
112 }
113
114 /**
115 * Generates the list of {@link MatchActionOperations} objects with
116 * add-operation.
117 *
118 * @return the list of {@link MatchActionOperations} objects
119 */
120 private List<MatchActionOperations> compileAddOperation(
121 MatchActionIdGenerator maIdGenerator,
122 MatchActionOperationsIdGenerator maoIdGenerator) {
123 Path path = checkNotNull(getPath());
124 checkState(path.size() > 0, "Path object has no link.");
125
126 // Preparing each actions and ingress port for each switch
127 List<List<Action>> actionsList = new LinkedList<>();
128 List<SwitchPort> portList = new LinkedList<>();
129 for (FlowLink link : path) {
130 portList.add(link.getDstSwitchPort());
131 actionsList.add(Arrays.asList(
132 (Action) new OutputAction(link.getSrcPortNumber())));
133 }
134
135 // The head switch's ingress port
136 portList.add(0, new SwitchPort(path.getSrcDpid(), getIngressPortNumber()));
137
138 // The tail switch's action
139 actionsList.add(getEgressActions());
140
141 Iterator<SwitchPort> portIterator = portList.iterator();
142 Iterator<List<Action>> actionsIterator = actionsList.iterator();
143
144 // Creates the second phase operation
145 // using the head switch's match action
146 MatchAction headMatchAction = createNextMatchAction(portIterator,
147 actionsIterator, maIdGenerator);
148 if (headMatchAction == null) {
149 return null;
150 }
151 MatchActionOperations secondOp = new MatchActionOperations(
152 maoIdGenerator.getNewId());
153 secondOp.addOperation(new MatchActionOperationEntry(
154 MatchActionOperations.Operator.ADD, headMatchAction));
155
156 // Creates the first phase operation
157 // using the remaining switches' match actions
158 MatchActionOperations firstOp = new MatchActionOperations(
159 maoIdGenerator.getNewId());
160 MatchAction ma;
161 while ((ma = createNextMatchAction(portIterator, actionsIterator, maIdGenerator)) != null) {
162 firstOp.addOperation(new MatchActionOperationEntry(
163 MatchActionOperations.Operator.ADD, ma));
164 }
165
166 return Arrays.asList(firstOp, secondOp);
167 }
168
169 /**
170 * Generates the list of {@link MatchActionOperations} objects with
171 * remote-operation.
172 *
173 * @return the list of {@link MatchActionOperations} objects
174 */
175 private List<MatchActionOperations> compileRemoveOperation(
176 MatchActionIdGenerator maIdGenerator,
177 MatchActionOperationsIdGenerator maoIdGenerator) {
178 // TODO implement it
179 throw new UnsupportedOperationException(
180 "REMOVE operation is not implemented yet.");
Toshio Koidea03915e2014-07-01 18:39:52 -0700181 }
182}