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 | */ |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 18 | import java.util.HashMap; |
| 19 | import java.util.HashSet; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 20 | import java.util.List; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 21 | import java.util.Map; |
| 22 | import java.util.Map.Entry; |
| 23 | import java.util.Set; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 30 | import org.onlab.onos.core.ApplicationId; |
| 31 | import org.onlab.onos.core.CoreService; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 32 | import org.onlab.onos.net.ConnectPoint; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.DeviceId; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.Link; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 37 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.flow.FlowRule; |
| 40 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 41 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 42 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.flow.TrafficSelector; |
| 44 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 45 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 46 | import org.onlab.onos.net.intent.IntentInstaller; |
| 47 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 48 | import org.onlab.onos.net.intent.PathIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 49 | |
| 50 | import com.google.common.collect.Lists; |
| 51 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 52 | /** |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 53 | * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent} path |
| 54 | * segment intents. |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Component(immediate = true) |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 57 | public class LinkCollectionIntentInstaller |
| 58 | implements IntentInstaller<LinkCollectionIntent> { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected IntentExtensionService intentManager; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 64 | protected CoreService coreService; |
| 65 | |
| 66 | private ApplicationId appId; |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | appId = coreService.registerApplication("org.onlab.onos.net.intent"); |
| 71 | intentManager.registerInstaller(LinkCollectionIntent.class, this); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
| 76 | intentManager.unregisterInstaller(PathIntent.class); |
| 77 | } |
| 78 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 79 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 80 | public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) { |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 81 | Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 82 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 83 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 84 | for (Link link : intent.links()) { |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 85 | if (outputMap.get(link.src().deviceId()) == null) { |
| 86 | outputMap.put(link.src().deviceId(), new HashSet<PortNumber>()); |
| 87 | } |
| 88 | outputMap.get(link.src().deviceId()).add(link.src().port()); |
| 89 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 92 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 93 | if (outputMap.get(egressPoint.deviceId()) == null) { |
| 94 | outputMap |
| 95 | .put(egressPoint.deviceId(), new HashSet<PortNumber>()); |
| 96 | } |
| 97 | outputMap.get(egressPoint.deviceId()).add(egressPoint.port()); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) { |
| 102 | rules.add(createBatchEntry(FlowRuleOperation.ADD, intent, |
| 103 | entry.getKey(), entry.getValue())); |
| 104 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 105 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 106 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 110 | public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) { |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 111 | Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 112 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 113 | |
| 114 | for (Link link : intent.links()) { |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 115 | if (outputMap.get(link.src().deviceId()) == null) { |
| 116 | outputMap.put(link.src().deviceId(), new HashSet<PortNumber>()); |
| 117 | } |
| 118 | outputMap.get(link.src().deviceId()).add(link.src().port()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 119 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 120 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 121 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 122 | if (outputMap.get(egressPoint.deviceId()) == null) { |
| 123 | outputMap |
| 124 | .put(egressPoint.deviceId(), new HashSet<PortNumber>()); |
| 125 | } |
| 126 | outputMap.get(egressPoint.deviceId()).add(egressPoint.port()); |
| 127 | } |
| 128 | |
| 129 | for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) { |
| 130 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, intent, |
| 131 | entry.getKey(), entry.getValue())); |
| 132 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 133 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 134 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 135 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 136 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public List<FlowRuleBatchOperation> replace(LinkCollectionIntent intent, |
| 139 | LinkCollectionIntent newIntent) { |
| 140 | // FIXME: implement |
| 141 | return null; |
| 142 | } |
| 143 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Creates a FlowRuleBatchEntry based on the provided parameters. |
| 146 | * |
| 147 | * @param operation the FlowRuleOperation to use |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 148 | * @param intent the link collection intent |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 149 | * @param deviceId the device ID for the flow rule |
| 150 | * @param outPort the output port of the flow rule |
| 151 | * @return the new flow rule batch entry |
| 152 | */ |
| 153 | private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation, |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 154 | LinkCollectionIntent intent, |
| 155 | DeviceId deviceId, |
| 156 | Set<PortNumber> outPorts) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 157 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 158 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment |
| 159 | .builder(intent.treatment()); |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 160 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 161 | for (PortNumber outPort : outPorts) { |
| 162 | treatmentBuilder.setOutput(outPort); |
| 163 | } |
| 164 | TrafficTreatment treatment = treatmentBuilder.build(); |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 165 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 166 | TrafficSelector selector = DefaultTrafficSelector |
| 167 | .builder(intent.selector()).build(); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 168 | |
| 169 | FlowRule rule = new DefaultFlowRule(deviceId, |
Brian O'Connor | 392619e | 2014-11-20 12:06:33 -0800 | [diff] [blame] | 170 | selector, treatment, 123, |
| 171 | appId, (short) (intent.id().fingerprint() & 0xffff), 0, true); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 172 | |
| 173 | return new FlowRuleBatchEntry(operation, rule); |
| 174 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 175 | } |