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 | */ |
Sho SHIMIZU | 3a7c98e | 2015-02-25 17:27:04 -0800 | [diff] [blame^] | 16 | package org.onosproject.net.intent.impl.installer; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.SetMultimap; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Yuta HIGUCHI | 467ccf7 | 2014-12-17 18:03:53 -0800 | [diff] [blame] | 29 | import org.onosproject.core.DefaultGroupId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.flow.DefaultFlowRule; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.IntentInstaller; |
| 43 | import org.onosproject.net.intent.LinkCollectionIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 44 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 45 | import java.util.Collection; |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 46 | import java.util.HashSet; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 47 | import java.util.List; |
| 48 | import java.util.Set; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 49 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 50 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 52 | * segment intents. |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 53 | */ |
| 54 | @Component(immediate = true) |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 55 | public class LinkCollectionIntentInstaller |
| 56 | implements IntentInstaller<LinkCollectionIntent> { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected IntentExtensionService intentManager; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 62 | protected CoreService coreService; |
| 63 | |
| 64 | private ApplicationId appId; |
| 65 | |
| 66 | @Activate |
| 67 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 68 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 69 | intentManager.registerInstaller(LinkCollectionIntent.class, this); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
Pavlin Radoslavov | a1e2656 | 2015-02-13 16:01:24 -0800 | [diff] [blame] | 74 | intentManager.unregisterInstaller(LinkCollectionIntent.class); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 77 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 78 | public List<Collection<FlowRuleOperation>> install(LinkCollectionIntent intent) { |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 79 | return generateBatchOperations(intent, FlowRuleOperation.Type.ADD); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 83 | public List<Collection<FlowRuleOperation>> uninstall(LinkCollectionIntent intent) { |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 84 | return generateBatchOperations(intent, FlowRuleOperation.Type.REMOVE); |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 87 | private List<Collection<FlowRuleOperation>> generateBatchOperations( |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 88 | LinkCollectionIntent intent, FlowRuleOperation.Type operation) { |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 89 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 90 | //TODO do we need a set here? |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 91 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 92 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 93 | |
| 94 | for (Link link : intent.links()) { |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 95 | inputPorts.put(link.dst().deviceId(), link.dst().port()); |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 96 | outputPorts.put(link.src().deviceId(), link.src().port()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 97 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 98 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 99 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 100 | inputPorts.put(ingressPoint.deviceId(), ingressPoint.port()); |
| 101 | } |
| 102 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 103 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 104 | outputPorts.put(egressPoint.deviceId(), egressPoint.port()); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 107 | List<FlowRuleOperation> rules = Lists.newArrayList(); |
| 108 | outputPorts.keys().stream() |
| 109 | .map(deviceId -> createBatchEntries(operation, |
| 110 | intent, deviceId, |
| 111 | inputPorts.get(deviceId), |
| 112 | outputPorts.get(deviceId))) |
| 113 | .forEach(rules::addAll); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 114 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 115 | return Lists.newArrayList(ImmutableSet.of(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 116 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 117 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 118 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 119 | public List<Collection<FlowRuleOperation>> replace(LinkCollectionIntent oldIntent, |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 120 | LinkCollectionIntent newIntent) { |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 121 | // FIXME: implement this in a more intelligent/less brute force way |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 122 | List<Collection<FlowRuleOperation>> batches = Lists.newArrayList(); |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 123 | batches.addAll(uninstall(oldIntent)); |
| 124 | batches.addAll(install(newIntent)); |
| 125 | return batches; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 128 | /** |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 129 | * Creates a collection of FlowRuleOperation based on the provided |
| 130 | * parameters. |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 131 | * |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 132 | * @param operation the FlowRuleOperation type to use |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 133 | * @param intent the link collection intent |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 134 | * @param deviceId the device ID for the flow rule |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 135 | * @param inPorts the logical input ports of the flow rule |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 136 | * @param outPorts the set of output ports for the flow rule |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 137 | * @return a collection with the new flow rule batch entries |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 138 | */ |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 139 | private Collection<FlowRuleOperation> createBatchEntries( |
| 140 | FlowRuleOperation.Type operation, |
| 141 | LinkCollectionIntent intent, |
| 142 | DeviceId deviceId, |
| 143 | Set<PortNumber> inPorts, |
| 144 | Set<PortNumber> outPorts) { |
| 145 | Collection<FlowRuleOperation> result = Lists.newLinkedList(); |
| 146 | Set<PortNumber> ingressPorts = new HashSet<PortNumber>(); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 147 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 148 | // |
| 149 | // Collect all ingress ports for this device. |
| 150 | // The intent treatment is applied only on those ports. |
| 151 | // |
| 152 | for (ConnectPoint cp : intent.ingressPoints()) { |
| 153 | if (cp.deviceId().equals(deviceId)) { |
| 154 | ingressPorts.add(cp.port()); |
| 155 | } |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 156 | } |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 157 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 158 | // |
| 159 | // Create two treatments: one for setting the output ports, |
| 160 | // and a second one that applies the intent treatment and sets the |
| 161 | // output ports. |
| 162 | // NOTE: The second one is created only if there are ingress ports. |
| 163 | // |
| 164 | TrafficTreatment.Builder defaultTreatmentBuilder = |
| 165 | DefaultTrafficTreatment.builder(); |
| 166 | for (PortNumber outPort : outPorts) { |
| 167 | defaultTreatmentBuilder.setOutput(outPort); |
| 168 | } |
| 169 | TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build(); |
| 170 | TrafficTreatment intentTreatment = null; |
| 171 | if (!ingressPorts.isEmpty()) { |
| 172 | TrafficTreatment.Builder intentTreatmentBuilder = |
| 173 | DefaultTrafficTreatment.builder(intent.treatment()); |
| 174 | for (PortNumber outPort : outPorts) { |
| 175 | intentTreatmentBuilder.setOutput(outPort); |
| 176 | } |
| 177 | intentTreatment = intentTreatmentBuilder.build(); |
| 178 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 179 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 180 | for (PortNumber inPort : inPorts) { |
| 181 | TrafficSelector selector = DefaultTrafficSelector |
| 182 | .builder(intent.selector()).matchInPort(inPort).build(); |
| 183 | TrafficTreatment treatment = defaultTreatment; |
| 184 | if (ingressPorts.contains(inPort)) { |
| 185 | // Use the intent treatment if this is ingress port |
| 186 | treatment = intentTreatment; |
| 187 | } |
| 188 | FlowRule rule = new DefaultFlowRule(deviceId, |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 189 | selector, treatment, 123, appId, |
| 190 | new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), |
| 191 | 0, true); |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 192 | result.add(new FlowRuleOperation(rule, operation)); |
| 193 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 194 | |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 195 | return result; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 196 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 197 | } |