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