Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 18 | import java.util.Iterator; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 19 | import java.util.List; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 20 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 26 | import org.onlab.onos.core.ApplicationId; |
| 27 | import org.onlab.onos.core.CoreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.ConnectPoint; |
| 29 | import org.onlab.onos.net.Link; |
| 30 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 31 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 34 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 35 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.TrafficSelector; |
| 37 | import org.onlab.onos.net.flow.TrafficTreatment; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 38 | import org.onlab.onos.net.intent.Constraint; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 40 | import org.onlab.onos.net.intent.IntentInstaller; |
| 41 | import org.onlab.onos.net.intent.PathIntent; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 42 | import org.onlab.onos.net.resource.DefaultLinkResourceRequest; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 44 | import org.onlab.onos.net.resource.LinkResourceRequest; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.resource.LinkResourceService; |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 47 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 48 | import com.google.common.collect.Lists; |
| 49 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 50 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 53 | /** |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 54 | * Installer for {@link PathIntent packet path connectivity intents}. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Component(immediate = true) |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 57 | public class PathIntentInstaller implements IntentInstaller<PathIntent> { |
| 58 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected IntentExtensionService intentManager; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 65 | protected CoreService coreService; |
| 66 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected LinkResourceService resourceService; |
| 69 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 70 | protected ApplicationId appId; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 74 | appId = coreService.registerApplication("org.onlab.onos.net.intent"); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 75 | intentManager.registerInstaller(PathIntent.class, this); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
| 80 | intentManager.unregisterInstaller(PathIntent.class); |
| 81 | } |
| 82 | |
| 83 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 84 | public List<FlowRuleBatchOperation> install(PathIntent intent) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 85 | LinkResourceAllocations allocations = allocateResources(intent); |
Thomas Vachuska | e3784c9 | 2014-10-29 21:09:10 -0700 | [diff] [blame] | 86 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 87 | TrafficSelector.Builder builder = |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 88 | DefaultTrafficSelector.builder(intent.selector()); |
| 89 | Iterator<Link> links = intent.path().links().iterator(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 90 | ConnectPoint prev = links.next().dst(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 91 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 92 | // TODO Generate multiple batches |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 93 | while (links.hasNext()) { |
| 94 | builder.matchInport(prev.port()); |
| 95 | Link link = links.next(); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 96 | TrafficTreatment treatment = builder() |
| 97 | .setOutput(link.src().port()).build(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 98 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 99 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 100 | builder.build(), treatment, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 101 | 123, appId, 0, true); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 102 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 103 | prev = link.dst(); |
| 104 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 105 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 109 | public List<FlowRuleBatchOperation> uninstall(PathIntent intent) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 110 | LinkResourceAllocations allocatedResources = resourceService.getAllocations(intent.id()); |
| 111 | if (allocatedResources != null) { |
| 112 | resourceService.releaseResources(allocatedResources); |
| 113 | } |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 114 | TrafficSelector.Builder builder = |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 115 | DefaultTrafficSelector.builder(intent.selector()); |
| 116 | Iterator<Link> links = intent.path().links().iterator(); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 117 | ConnectPoint prev = links.next().dst(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 118 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 119 | // TODO Generate multiple batches |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 120 | while (links.hasNext()) { |
| 121 | builder.matchInport(prev.port()); |
| 122 | Link link = links.next(); |
| 123 | TrafficTreatment treatment = builder() |
| 124 | .setOutput(link.src().port()).build(); |
| 125 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
| 126 | builder.build(), treatment, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 127 | 123, appId, 0, true); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 128 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 129 | prev = link.dst(); |
| 130 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 131 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Allocate resources required for an intent. |
| 136 | * |
| 137 | * @param intent intent to allocate resource for |
| 138 | * @return allocated resources if any are required, null otherwise |
| 139 | */ |
| 140 | private LinkResourceAllocations allocateResources(PathIntent intent) { |
| 141 | if (intent.constraints() == null) { |
| 142 | return null; |
| 143 | } |
| 144 | LinkResourceRequest.Builder builder = |
| 145 | DefaultLinkResourceRequest.builder(intent.id(), intent.path().links()); |
| 146 | for (Constraint constraint : intent.constraints()) { |
| 147 | builder.addConstraint(constraint); |
| 148 | } |
| 149 | LinkResourceRequest request = builder.build(); |
| 150 | return request.resources().isEmpty() ? null : resourceService.requestResources(request); |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 153 | // TODO refactor below this line... ---------------------------- |
| 154 | |
| 155 | /** |
| 156 | * Generates the series of MatchActionOperations from the |
| 157 | * {@link FlowBatchOperation}. |
| 158 | * <p> |
| 159 | * FIXME: Currently supporting PacketPathFlow and SingleDstTreeFlow only. |
| 160 | * <p> |
| 161 | * FIXME: MatchActionOperations should have dependency field to the other |
| 162 | * match action operations, and this method should use this. |
| 163 | * |
| 164 | * @param op the {@link FlowBatchOperation} object |
| 165 | * @return the list of {@link MatchActionOperations} objects |
| 166 | */ |
| 167 | /* |
| 168 | private List<MatchActionOperations> |
| 169 | generateMatchActionOperationsList(FlowBatchOperation op) { |
| 170 | |
| 171 | // MatchAction operations at head (ingress) switches. |
| 172 | MatchActionOperations headOps = matchActionService.createOperationsList(); |
| 173 | |
| 174 | // MatchAction operations at rest of the switches. |
| 175 | MatchActionOperations tailOps = matchActionService.createOperationsList(); |
| 176 | |
| 177 | MatchActionOperations removeOps = matchActionService.createOperationsList(); |
| 178 | |
| 179 | for (BatchOperationEntry<Operator, ?> e : op.getOperations()) { |
| 180 | |
| 181 | if (e.getOperator() == FlowBatchOperation.Operator.ADD) { |
| 182 | generateInstallMatchActionOperations(e, tailOps, headOps); |
| 183 | } else if (e.getOperator() == FlowBatchOperation.Operator.REMOVE) { |
| 184 | generateRemoveMatchActionOperations(e, removeOps); |
| 185 | } else { |
| 186 | throw new UnsupportedOperationException( |
| 187 | "FlowManager supports ADD and REMOVE operations only."); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | return Arrays.asList(tailOps, headOps, removeOps); |
| 193 | } |
| 194 | */ |
| 195 | |
| 196 | /** |
| 197 | * Generates MatchActionOperations for an INSTALL FlowBatchOperation. |
| 198 | * <p/> |
| 199 | * FIXME: Currently only supports flows that generate exactly two match |
| 200 | * action operation sets. |
| 201 | * |
| 202 | * @param e Flow BatchOperationEntry |
| 203 | * @param tailOps MatchActionOperation set that the tail |
| 204 | * MatchActionOperations will be placed in |
| 205 | * @param headOps MatchActionOperation set that the head |
| 206 | * MatchActionOperations will be placed in |
| 207 | */ |
| 208 | /* |
| 209 | private void generateInstallMatchActionOperations( |
| 210 | BatchOperationEntry<Operator, ?> e, |
| 211 | MatchActionOperations tailOps, |
| 212 | MatchActionOperations headOps) { |
| 213 | |
| 214 | if (!(e.getTarget() instanceof Flow)) { |
| 215 | throw new IllegalStateException( |
| 216 | "The target is not Flow object: " + e.getTarget()); |
| 217 | } |
| 218 | |
| 219 | // Compile flows to match-actions |
| 220 | Flow flow = (Flow) e.getTarget(); |
| 221 | List<MatchActionOperations> maOps = flow.compile( |
| 222 | e.getOperator(), matchActionService); |
| 223 | verifyNotNull(maOps, "Could not compile the flow: " + flow); |
| 224 | verify(maOps.size() == 2, |
| 225 | "The flow generates unspported match-action operations."); |
| 226 | |
| 227 | // Map FlowId to MatchActionIds |
| 228 | for (MatchActionOperations maOp : maOps) { |
| 229 | for (MatchActionOperationEntry entry : maOp.getOperations()) { |
| 230 | flowMatchActionsMap.put( |
| 231 | KryoFactory.serialize(flow.getId()), |
| 232 | KryoFactory.serialize(entry.getTarget())); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Merge match-action operations |
| 237 | for (MatchActionOperationEntry mae : maOps.get(0).getOperations()) { |
| 238 | verify(mae.getOperator() == MatchActionOperations.Operator.INSTALL); |
| 239 | tailOps.addOperation(mae); |
| 240 | } |
| 241 | for (MatchActionOperationEntry mae : maOps.get(1).getOperations()) { |
| 242 | verify(mae.getOperator() == MatchActionOperations.Operator.INSTALL); |
| 243 | headOps.addOperation(mae); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 244 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 245 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 246 | */ |
| 247 | /** |
| 248 | * Generates MatchActionOperations for a REMOVE FlowBatchOperation. |
| 249 | * |
| 250 | * @param e Flow BatchOperationEntry |
| 251 | * @param removeOps MatchActionOperation set that the remove |
| 252 | * MatchActionOperations will be placed in |
| 253 | */ |
| 254 | /* |
| 255 | private void generateRemoveMatchActionOperations( |
| 256 | BatchOperationEntry<Operator, ?> e, |
| 257 | MatchActionOperations removeOps) { |
| 258 | |
| 259 | if (!(e.getTarget() instanceof FlowId)) { |
| 260 | throw new IllegalStateException( |
| 261 | "The target is not a FlowId object: " + e.getTarget()); |
| 262 | } |
| 263 | |
| 264 | // Compile flows to match-actions |
| 265 | FlowId flowId = (FlowId) e.getTarget(); |
| 266 | |
| 267 | for (byte[] matchActionIdBytes : |
| 268 | flowMatchActionsMap.remove(KryoFactory.serialize(flowId))) { |
| 269 | MatchActionId matchActionId = KryoFactory.deserialize(matchActionIdBytes); |
| 270 | removeOps.addOperation(new MatchActionOperationEntry( |
| 271 | MatchActionOperations.Operator.REMOVE, matchActionId)); |
| 272 | } |
| 273 | } |
| 274 | */ |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 275 | } |