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 | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
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; |
| 46 | import java.util.List; |
| 47 | import java.util.Set; |
| 48 | import java.util.stream.Collectors; |
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? |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 91 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 92 | |
| 93 | for (Link link : intent.links()) { |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 94 | outputPorts.put(link.src().deviceId(), link.src().port()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 95 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 96 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 97 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 98 | outputPorts.put(egressPoint.deviceId(), egressPoint.port()); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 101 | //FIXME change to new api |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 102 | /* Fear of streams */ |
| 103 | /* |
| 104 | Set<FlowRuleBatchEntry> rules = Sets.newHashSet(); |
| 105 | for (DeviceId deviceId : outputPorts.keys()) { |
| 106 | rules.add(createBatchEntry(operation, |
| 107 | intent, deviceId, |
| 108 | outputPorts.get(deviceId))); |
| 109 | } |
| 110 | */ |
| 111 | |
| 112 | Set<FlowRuleOperation> rules = |
| 113 | outputPorts |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 114 | .keys() |
| 115 | .stream() |
| 116 | .map(deviceId -> createBatchEntry(operation, |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 117 | intent, deviceId, |
| 118 | outputPorts.get(deviceId))) |
| 119 | .collect(Collectors.toSet()); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 120 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 121 | return Lists.newArrayList(ImmutableSet.of(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 122 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 123 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 124 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 125 | public List<Collection<FlowRuleOperation>> replace(LinkCollectionIntent oldIntent, |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 126 | LinkCollectionIntent newIntent) { |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 127 | // FIXME: implement this in a more intelligent/less brute force way |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame] | 128 | List<Collection<FlowRuleOperation>> batches = Lists.newArrayList(); |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 129 | batches.addAll(uninstall(oldIntent)); |
| 130 | batches.addAll(install(newIntent)); |
| 131 | return batches; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Creates a FlowRuleBatchEntry based on the provided parameters. |
| 136 | * |
| 137 | * @param operation the FlowRuleOperation to use |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 138 | * @param intent the link collection intent |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 139 | * @param deviceId the device ID for the flow rule |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 140 | * @param outPorts the set of output ports for the flow rule |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 141 | * @return the new flow rule batch entry |
| 142 | */ |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 143 | private FlowRuleOperation createBatchEntry(FlowRuleOperation.Type operation, |
| 144 | LinkCollectionIntent intent, |
| 145 | DeviceId deviceId, |
| 146 | Set<PortNumber> outPorts) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 147 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 148 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment |
| 149 | .builder(intent.treatment()); |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 150 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 151 | for (PortNumber outPort : outPorts) { |
| 152 | treatmentBuilder.setOutput(outPort); |
| 153 | } |
| 154 | TrafficTreatment treatment = treatmentBuilder.build(); |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 155 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 156 | TrafficSelector selector = DefaultTrafficSelector |
| 157 | .builder(intent.selector()).build(); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 158 | |
| 159 | FlowRule rule = new DefaultFlowRule(deviceId, |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 160 | selector, treatment, 123, appId, |
| 161 | new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), |
| 162 | 0, true); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 163 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 164 | return new FlowRuleOperation(rule, operation); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 165 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 166 | } |