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