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