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