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