Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.SetMultimap; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 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; |
| 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.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 36 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 37 | import org.onosproject.net.flowobjective.Objective; |
| 38 | import org.onosproject.net.intent.FlowObjectiveIntent; |
| 39 | import org.onosproject.net.intent.Intent; |
| 40 | import org.onosproject.net.intent.IntentCompiler; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.LinkCollectionIntent; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 42 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | import java.util.Collections; |
| 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 47 | import java.util.stream.Collectors; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Compiler to produce flow objectives from link collections. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | public class LinkCollectionIntentFlowObjectivesCompiler implements IntentCompiler<LinkCollectionIntent> { |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 56 | protected IntentConfigurableRegistrator registrator; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | private ApplicationId appId; |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 66 | registrator.registerCompiler(LinkCollectionIntent.class, this, true); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 71 | registrator.unregisterCompiler(LinkCollectionIntent.class, true); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 75 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 76 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 77 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
| 78 | |
| 79 | for (Link link : intent.links()) { |
| 80 | inputPorts.put(link.dst().deviceId(), link.dst().port()); |
| 81 | outputPorts.put(link.src().deviceId(), link.src().port()); |
| 82 | } |
| 83 | |
| 84 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 85 | inputPorts.put(ingressPoint.deviceId(), ingressPoint.port()); |
| 86 | } |
| 87 | |
| 88 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 89 | outputPorts.put(egressPoint.deviceId(), egressPoint.port()); |
| 90 | } |
| 91 | |
| 92 | List<Objective> objectives = new ArrayList<>(); |
| 93 | List<DeviceId> devices = new ArrayList<>(); |
| 94 | for (DeviceId deviceId: outputPorts.keys()) { |
| 95 | List<Objective> deviceObjectives = |
| 96 | createRules(intent, |
| 97 | deviceId, |
| 98 | inputPorts.get(deviceId), |
| 99 | outputPorts.get(deviceId)); |
| 100 | deviceObjectives.forEach(objective -> { |
| 101 | objectives.add(objective); |
| 102 | devices.add(deviceId); |
| 103 | }); |
| 104 | } |
| 105 | return Collections.singletonList( |
| 106 | new FlowObjectiveIntent(appId, devices, objectives, intent.resources())); |
| 107 | } |
| 108 | |
| 109 | private List<Objective> createRules(LinkCollectionIntent intent, DeviceId deviceId, |
| 110 | Set<PortNumber> inPorts, Set<PortNumber> outPorts) { |
| 111 | Set<PortNumber> ingressPorts = intent.ingressPoints().stream() |
| 112 | .filter(point -> point.deviceId().equals(deviceId)) |
| 113 | .map(ConnectPoint::port) |
| 114 | .collect(Collectors.toSet()); |
| 115 | |
| 116 | TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder(); |
| 117 | outPorts.stream() |
| 118 | .forEach(defaultTreatmentBuilder::setOutput); |
| 119 | TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build(); |
| 120 | |
| 121 | TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment()); |
| 122 | outPorts.stream() |
| 123 | .forEach(ingressTreatmentBuilder::setOutput); |
| 124 | TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build(); |
| 125 | |
| 126 | List<Objective> objectives = new ArrayList<>(inPorts.size()); |
| 127 | for (PortNumber inPort: inPorts) { |
| 128 | TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build(); |
| 129 | TrafficTreatment treatment; |
| 130 | if (ingressPorts.contains(inPort)) { |
| 131 | treatment = ingressTreatment; |
| 132 | } else { |
| 133 | treatment = defaultTreatment; |
| 134 | } |
| 135 | |
| 136 | Objective objective = DefaultForwardingObjective.builder() |
| 137 | .withSelector(selector) |
| 138 | .withTreatment(treatment) |
| 139 | .withPriority(intent.priority()) |
| 140 | .fromApp(appId) |
| 141 | .makePermanent() |
Thomas Vachuska | 8378ccf | 2016-03-02 18:02:17 -0800 | [diff] [blame] | 142 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 143 | .add(); |
| 144 | |
| 145 | objectives.add(objective); |
| 146 | } |
| 147 | |
| 148 | return objectives; |
| 149 | } |
| 150 | } |