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