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; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 26 | import org.onlab.util.Identifier; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 34 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.NextObjective; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 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; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 42 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 43 | import org.onosproject.net.resource.ResourceService; |
| 44 | import org.onosproject.net.resource.impl.LabelAllocator; |
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; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 49 | import java.util.Map; |
| 50 | import java.util.Optional; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 51 | import java.util.Set; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Compiler to produce flow objectives from link collections. |
| 55 | */ |
| 56 | @Component(immediate = true) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 57 | public class LinkCollectionIntentFlowObjectiveCompiler |
| 58 | extends LinkCollectionCompiler<Objective> |
| 59 | implements IntentCompiler<LinkCollectionIntent> { |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 62 | protected IntentConfigurableRegistrator registrator; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected CoreService coreService; |
| 66 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected FlowObjectiveService flowObjectiveService; |
| 69 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected ResourceService resourceService; |
| 72 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 73 | private ApplicationId appId; |
| 74 | |
| 75 | @Activate |
| 76 | public void activate() { |
| 77 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 78 | registrator.registerCompiler(LinkCollectionIntent.class, this, true); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 79 | if (labelAllocator == null) { |
| 80 | labelAllocator = new LabelAllocator(resourceService); |
| 81 | } |
| 82 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 87 | registrator.unregisterCompiler(LinkCollectionIntent.class, true); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 91 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 92 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 93 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 94 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 95 | Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of(); |
| 96 | |
| 97 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 98 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 99 | computePorts(intent, inputPorts, outputPorts); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 100 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 101 | if (encapConstraint.isPresent()) { |
| 102 | labels = labelAllocator.assignLabelToPorts(intent.links(), |
| 103 | intent.id(), |
| 104 | encapConstraint.get().encapType()); |
| 105 | } |
| 106 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 107 | List<Objective> objectives = new ArrayList<>(); |
| 108 | List<DeviceId> devices = new ArrayList<>(); |
| 109 | for (DeviceId deviceId: outputPorts.keys()) { |
| 110 | List<Objective> deviceObjectives = |
| 111 | createRules(intent, |
| 112 | deviceId, |
| 113 | inputPorts.get(deviceId), |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 114 | outputPorts.get(deviceId), |
| 115 | labels); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 116 | deviceObjectives.forEach(objective -> { |
| 117 | objectives.add(objective); |
| 118 | devices.add(deviceId); |
| 119 | }); |
| 120 | } |
| 121 | return Collections.singletonList( |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame^] | 122 | new FlowObjectiveIntent(appId, intent.key(), devices, |
| 123 | objectives, |
| 124 | intent.resources(), |
| 125 | intent.resourceGroup())); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 128 | @Override |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 129 | protected List<Objective> createRules(LinkCollectionIntent intent, |
| 130 | DeviceId deviceId, |
| 131 | Set<PortNumber> inPorts, |
| 132 | Set<PortNumber> outPorts, |
| 133 | Map<ConnectPoint, Identifier<?>> labels) { |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 134 | |
| 135 | List<Objective> objectives = new ArrayList<>(inPorts.size()); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * Looking for the encapsulation constraint |
| 139 | */ |
| 140 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 141 | |
| 142 | inPorts.forEach(inport -> { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 143 | |
| 144 | ForwardingInstructions instructions = this.createForwardingInstruction( |
| 145 | encapConstraint, |
| 146 | intent, |
| 147 | inport, |
| 148 | outPorts, |
| 149 | deviceId, |
| 150 | labels |
| 151 | ); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 152 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 153 | NextObjective nextObjective = DefaultNextObjective.builder() |
| 154 | .withId(flowObjectiveService.allocateNextId()) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 155 | .addTreatment(instructions.treatment()) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 156 | .withType(NextObjective.Type.SIMPLE) |
| 157 | .fromApp(appId) |
| 158 | .makePermanent().add(); |
| 159 | objectives.add(nextObjective); |
| 160 | |
| 161 | objectives.add(DefaultForwardingObjective.builder() |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 162 | .withSelector(instructions.selector()) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 163 | .nextStep(nextObjective.id()) |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 164 | .withPriority(intent.priority()) |
| 165 | .fromApp(appId) |
| 166 | .makePermanent() |
Thomas Vachuska | 8378ccf | 2016-03-02 18:02:17 -0800 | [diff] [blame] | 167 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 168 | .add()); |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 169 | } |
| 170 | ); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 171 | |
| 172 | return objectives; |
| 173 | } |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 174 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 175 | } |