blob: ad8ea088d932ab2d3988f9a3789a0617a415832a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connor66630c82014-10-02 21:08:19 -070016package org.onlab.onos.net.intent.impl;
17
Brian O'Connora4cab072014-10-03 18:46:39 -070018import java.util.Iterator;
alshabib902d41b2014-10-07 16:52:05 -070019import java.util.List;
Brian O'Connora4cab072014-10-03 18:46:39 -070020
Brian O'Connor66630c82014-10-02 21:08:19 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070026import org.onlab.onos.core.ApplicationId;
27import org.onlab.onos.core.CoreService;
Brian O'Connor66630c82014-10-02 21:08:19 -070028import org.onlab.onos.net.ConnectPoint;
29import org.onlab.onos.net.Link;
30import org.onlab.onos.net.flow.DefaultFlowRule;
31import org.onlab.onos.net.flow.DefaultTrafficSelector;
Brian O'Connor66630c82014-10-02 21:08:19 -070032import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070033import org.onlab.onos.net.flow.FlowRuleBatchEntry;
34import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
35import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Brian O'Connor66630c82014-10-02 21:08:19 -070036import org.onlab.onos.net.flow.TrafficSelector;
37import org.onlab.onos.net.flow.TrafficTreatment;
Brian O'Connor66630c82014-10-02 21:08:19 -070038import org.onlab.onos.net.intent.IntentExtensionService;
39import org.onlab.onos.net.intent.IntentInstaller;
40import org.onlab.onos.net.intent.PathIntent;
Ray Milkeycaa450b2014-10-29 15:54:24 -070041import org.onlab.onos.net.resource.LinkResourceAllocations;
42import org.onlab.onos.net.resource.LinkResourceService;
alshabib8ca53902014-10-07 13:11:17 -070043import org.slf4j.Logger;
Brian O'Connor66630c82014-10-02 21:08:19 -070044
alshabib902d41b2014-10-07 16:52:05 -070045import com.google.common.collect.Lists;
46
Ray Milkeycaa450b2014-10-29 15:54:24 -070047import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
48import static org.slf4j.LoggerFactory.getLogger;
49
Brian O'Connor66630c82014-10-02 21:08:19 -070050/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070051 * Installer for {@link PathIntent packet path connectivity intents}.
Brian O'Connor66630c82014-10-02 21:08:19 -070052 */
53@Component(immediate = true)
tom9a693fd2014-10-03 11:32:19 -070054public class PathIntentInstaller implements IntentInstaller<PathIntent> {
55
alshabib8ca53902014-10-07 13:11:17 -070056 private final Logger log = getLogger(getClass());
57
Brian O'Connor66630c82014-10-02 21:08:19 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected IntentExtensionService intentManager;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib92c65ad2014-10-08 21:56:05 -070062 protected CoreService coreService;
63
Ray Milkeycaa450b2014-10-29 15:54:24 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected LinkResourceService resourceService;
66
alshabib92c65ad2014-10-08 21:56:05 -070067 private ApplicationId appId;
Brian O'Connor66630c82014-10-02 21:08:19 -070068
69 @Activate
70 public void activate() {
alshabib92c65ad2014-10-08 21:56:05 -070071 appId = coreService.registerApplication("org.onlab.onos.net.intent");
Brian O'Connor66630c82014-10-02 21:08:19 -070072 intentManager.registerInstaller(PathIntent.class, this);
73 }
74
75 @Deactivate
76 public void deactivate() {
77 intentManager.unregisterInstaller(PathIntent.class);
78 }
79
80 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070081 public List<FlowRuleBatchOperation> install(PathIntent intent) {
Ray Milkeycaa450b2014-10-29 15:54:24 -070082 LinkResourceAllocations allocations = allocateBandwidth(intent);
83 if (allocations == null) {
84 log.debug("Insufficient bandwidth available to install path intent {}", intent);
85 return null;
86 }
tom9a693fd2014-10-03 11:32:19 -070087 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -070088 DefaultTrafficSelector.builder(intent.selector());
89 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connor66630c82014-10-02 21:08:19 -070090 ConnectPoint prev = links.next().dst();
alshabib902d41b2014-10-07 16:52:05 -070091 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connorf2dbde52014-10-10 16:20:24 -070092 // TODO Generate multiple batches
Brian O'Connor66630c82014-10-02 21:08:19 -070093 while (links.hasNext()) {
94 builder.matchInport(prev.port());
95 Link link = links.next();
tomf5c9d922014-10-03 15:22:03 -070096 TrafficTreatment treatment = builder()
97 .setOutput(link.src().port()).build();
alshabib902d41b2014-10-07 16:52:05 -070098
tom9a693fd2014-10-03 11:32:19 -070099 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Brian O'Connora4cab072014-10-03 18:46:39 -0700100 builder.build(), treatment,
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700101 123, appId, 0, true);
alshabib902d41b2014-10-07 16:52:05 -0700102 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
Brian O'Connor66630c82014-10-02 21:08:19 -0700103 prev = link.dst();
104 }
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700105 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Brian O'Connor66630c82014-10-02 21:08:19 -0700106 }
107
108 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700109 public List<FlowRuleBatchOperation> uninstall(PathIntent intent) {
Brian O'Connora4cab072014-10-03 18:46:39 -0700110 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -0700111 DefaultTrafficSelector.builder(intent.selector());
112 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connora4cab072014-10-03 18:46:39 -0700113 ConnectPoint prev = links.next().dst();
alshabib902d41b2014-10-07 16:52:05 -0700114 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700115 // TODO Generate multiple batches
Brian O'Connora4cab072014-10-03 18:46:39 -0700116 while (links.hasNext()) {
117 builder.matchInport(prev.port());
118 Link link = links.next();
119 TrafficTreatment treatment = builder()
120 .setOutput(link.src().port()).build();
121 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
122 builder.build(), treatment,
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700123 123, appId, 0, true);
alshabib902d41b2014-10-07 16:52:05 -0700124 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
Brian O'Connora4cab072014-10-03 18:46:39 -0700125 prev = link.dst();
126 }
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700127 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Brian O'Connorcb900f42014-10-07 21:55:33 -0700128 }
129
Ray Milkeycaa450b2014-10-29 15:54:24 -0700130 private LinkResourceAllocations allocateBandwidth(PathIntent intent) {
131 return resourceService.requestResources(intent.resourceRequests()[0]);
132 }
133
Brian O'Connorcb900f42014-10-07 21:55:33 -0700134 // TODO refactor below this line... ----------------------------
135
136 /**
137 * Generates the series of MatchActionOperations from the
138 * {@link FlowBatchOperation}.
139 * <p>
140 * FIXME: Currently supporting PacketPathFlow and SingleDstTreeFlow only.
141 * <p>
142 * FIXME: MatchActionOperations should have dependency field to the other
143 * match action operations, and this method should use this.
144 *
145 * @param op the {@link FlowBatchOperation} object
146 * @return the list of {@link MatchActionOperations} objects
147 */
148 /*
149 private List<MatchActionOperations>
150 generateMatchActionOperationsList(FlowBatchOperation op) {
151
152 // MatchAction operations at head (ingress) switches.
153 MatchActionOperations headOps = matchActionService.createOperationsList();
154
155 // MatchAction operations at rest of the switches.
156 MatchActionOperations tailOps = matchActionService.createOperationsList();
157
158 MatchActionOperations removeOps = matchActionService.createOperationsList();
159
160 for (BatchOperationEntry<Operator, ?> e : op.getOperations()) {
161
162 if (e.getOperator() == FlowBatchOperation.Operator.ADD) {
163 generateInstallMatchActionOperations(e, tailOps, headOps);
164 } else if (e.getOperator() == FlowBatchOperation.Operator.REMOVE) {
165 generateRemoveMatchActionOperations(e, removeOps);
166 } else {
167 throw new UnsupportedOperationException(
168 "FlowManager supports ADD and REMOVE operations only.");
169 }
170
171 }
172
173 return Arrays.asList(tailOps, headOps, removeOps);
174 }
175 */
176
177 /**
178 * Generates MatchActionOperations for an INSTALL FlowBatchOperation.
179 * <p/>
180 * FIXME: Currently only supports flows that generate exactly two match
181 * action operation sets.
182 *
183 * @param e Flow BatchOperationEntry
184 * @param tailOps MatchActionOperation set that the tail
185 * MatchActionOperations will be placed in
186 * @param headOps MatchActionOperation set that the head
187 * MatchActionOperations will be placed in
188 */
189 /*
190 private void generateInstallMatchActionOperations(
191 BatchOperationEntry<Operator, ?> e,
192 MatchActionOperations tailOps,
193 MatchActionOperations headOps) {
194
195 if (!(e.getTarget() instanceof Flow)) {
196 throw new IllegalStateException(
197 "The target is not Flow object: " + e.getTarget());
198 }
199
200 // Compile flows to match-actions
201 Flow flow = (Flow) e.getTarget();
202 List<MatchActionOperations> maOps = flow.compile(
203 e.getOperator(), matchActionService);
204 verifyNotNull(maOps, "Could not compile the flow: " + flow);
205 verify(maOps.size() == 2,
206 "The flow generates unspported match-action operations.");
207
208 // Map FlowId to MatchActionIds
209 for (MatchActionOperations maOp : maOps) {
210 for (MatchActionOperationEntry entry : maOp.getOperations()) {
211 flowMatchActionsMap.put(
212 KryoFactory.serialize(flow.getId()),
213 KryoFactory.serialize(entry.getTarget()));
214 }
215 }
216
217 // Merge match-action operations
218 for (MatchActionOperationEntry mae : maOps.get(0).getOperations()) {
219 verify(mae.getOperator() == MatchActionOperations.Operator.INSTALL);
220 tailOps.addOperation(mae);
221 }
222 for (MatchActionOperationEntry mae : maOps.get(1).getOperations()) {
223 verify(mae.getOperator() == MatchActionOperations.Operator.INSTALL);
224 headOps.addOperation(mae);
alshabib902d41b2014-10-07 16:52:05 -0700225 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700226 }
Brian O'Connorcb900f42014-10-07 21:55:33 -0700227 */
228 /**
229 * Generates MatchActionOperations for a REMOVE FlowBatchOperation.
230 *
231 * @param e Flow BatchOperationEntry
232 * @param removeOps MatchActionOperation set that the remove
233 * MatchActionOperations will be placed in
234 */
235 /*
236 private void generateRemoveMatchActionOperations(
237 BatchOperationEntry<Operator, ?> e,
238 MatchActionOperations removeOps) {
239
240 if (!(e.getTarget() instanceof FlowId)) {
241 throw new IllegalStateException(
242 "The target is not a FlowId object: " + e.getTarget());
243 }
244
245 // Compile flows to match-actions
246 FlowId flowId = (FlowId) e.getTarget();
247
248 for (byte[] matchActionIdBytes :
249 flowMatchActionsMap.remove(KryoFactory.serialize(flowId))) {
250 MatchActionId matchActionId = KryoFactory.deserialize(matchActionIdBytes);
251 removeOps.addOperation(new MatchActionOperationEntry(
252 MatchActionOperations.Operator.REMOVE, matchActionId));
253 }
254 }
255 */
Brian O'Connor66630c82014-10-02 21:08:19 -0700256}