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