Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
| 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.SetMultimap; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.flow.DefaultFlowRule; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.FlowRule; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.intent.FlowRuleIntent; |
| 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.IntentCompiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.LinkCollectionIntent; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 41 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 42 | |
| 43 | import java.util.ArrayList; |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 44 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 47 | import java.util.stream.Collectors; |
| 48 | |
| 49 | @Component(immediate = true) |
| 50 | public class LinkCollectionIntentCompiler implements IntentCompiler<LinkCollectionIntent> { |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 53 | protected IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected CoreService coreService; |
| 57 | |
| 58 | private ApplicationId appId; |
| 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 63 | registrator.registerCompiler(LinkCollectionIntent.class, this, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Deactivate |
| 67 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 68 | registrator.unregisterCompiler(LinkCollectionIntent.class, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable, |
| 73 | Set<LinkResourceAllocations> resources) { |
| 74 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 75 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
| 76 | |
| 77 | for (Link link : intent.links()) { |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 78 | inputPorts.put(link.dst().deviceId(), link.dst().port()); |
| 79 | outputPorts.put(link.src().deviceId(), link.src().port()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 83 | inputPorts.put(ingressPoint.deviceId(), ingressPoint.port()); |
| 84 | } |
| 85 | |
| 86 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 87 | outputPorts.put(egressPoint.deviceId(), egressPoint.port()); |
| 88 | } |
| 89 | |
| 90 | List<FlowRule> rules = new ArrayList<>(); |
| 91 | for (DeviceId deviceId: outputPorts.keys()) { |
| 92 | rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId))); |
| 93 | } |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 94 | return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId, |
| 98 | Set<PortNumber> inPorts, Set<PortNumber> outPorts) { |
| 99 | Set<PortNumber> ingressPorts = intent.ingressPoints().stream() |
| 100 | .filter(point -> point.deviceId().equals(deviceId)) |
| 101 | .map(ConnectPoint::port) |
| 102 | .collect(Collectors.toSet()); |
| 103 | |
| 104 | TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder(); |
| 105 | outPorts.stream() |
| 106 | .forEach(defaultTreatmentBuilder::setOutput); |
| 107 | TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build(); |
| 108 | |
| 109 | TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment()); |
| 110 | outPorts.stream() |
| 111 | .forEach(ingressTreatmentBuilder::setOutput); |
| 112 | TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build(); |
| 113 | |
| 114 | List<FlowRule> rules = new ArrayList<>(inPorts.size()); |
| 115 | for (PortNumber inPort: inPorts) { |
| 116 | TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build(); |
| 117 | TrafficTreatment treatment; |
| 118 | if (ingressPorts.contains(inPort)) { |
| 119 | treatment = ingressTreatment; |
| 120 | } else { |
| 121 | treatment = defaultTreatment; |
| 122 | } |
| 123 | |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 124 | FlowRule rule = DefaultFlowRule.builder() |
| 125 | .forDevice(deviceId) |
| 126 | .withSelector(selector) |
| 127 | .withTreatment(treatment) |
| 128 | .withPriority(intent.priority()) |
| 129 | .fromApp(appId) |
| 130 | .makePermanent() |
| 131 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 132 | rules.add(rule); |
| 133 | } |
| 134 | |
| 135 | return rules; |
| 136 | } |
| 137 | } |