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 | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 33 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.NextObjective; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.Objective; |
| 37 | import org.onosproject.net.intent.FlowObjectiveIntent; |
| 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.IntentCompiler; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.LinkCollectionIntent; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 41 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 42 | import java.util.ArrayList; |
| 43 | import java.util.Collections; |
| 44 | import java.util.List; |
| 45 | import java.util.Set; |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Compiler to produce flow objectives from link collections. |
| 49 | */ |
| 50 | @Component(immediate = true) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 51 | public class LinkCollectionIntentFlowObjectiveCompiler |
| 52 | extends LinkCollectionCompiler<Objective> |
| 53 | implements IntentCompiler<LinkCollectionIntent> { |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 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 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected FlowObjectiveService flowObjectiveService; |
| 63 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 64 | private ApplicationId appId; |
| 65 | |
| 66 | @Activate |
| 67 | public void activate() { |
| 68 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 69 | registrator.registerCompiler(LinkCollectionIntent.class, this, true); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 74 | registrator.unregisterCompiler(LinkCollectionIntent.class, true); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 78 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 79 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 80 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 81 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
| 82 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 83 | computePorts(intent, inputPorts, outputPorts); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 84 | |
| 85 | List<Objective> objectives = new ArrayList<>(); |
| 86 | List<DeviceId> devices = new ArrayList<>(); |
| 87 | for (DeviceId deviceId: outputPorts.keys()) { |
| 88 | List<Objective> deviceObjectives = |
| 89 | createRules(intent, |
| 90 | deviceId, |
| 91 | inputPorts.get(deviceId), |
| 92 | outputPorts.get(deviceId)); |
| 93 | deviceObjectives.forEach(objective -> { |
| 94 | objectives.add(objective); |
| 95 | devices.add(deviceId); |
| 96 | }); |
| 97 | } |
| 98 | return Collections.singletonList( |
| 99 | new FlowObjectiveIntent(appId, devices, objectives, intent.resources())); |
| 100 | } |
| 101 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 102 | @Override |
| 103 | protected List<Objective> createRules(LinkCollectionIntent intent, DeviceId deviceId, |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 104 | Set<PortNumber> inPorts, Set<PortNumber> outPorts) { |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 105 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 106 | Set<PortNumber> ingressPorts = Sets.newHashSet(); |
| 107 | Set<PortNumber> egressPorts = Sets.newHashSet(); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 108 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 109 | computePorts(intent, deviceId, ingressPorts, egressPorts); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 110 | |
| 111 | List<Objective> objectives = new ArrayList<>(inPorts.size()); |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 112 | Set<PortNumber> copyIngressPorts = ImmutableSet.copyOf(ingressPorts); |
| 113 | Set<PortNumber> copyEgressPorts = ImmutableSet.copyOf(egressPorts); |
| 114 | |
| 115 | inPorts.forEach(inport -> { |
| 116 | ForwardingInstructions instructions = this.createForwardingInstructions(intent, |
| 117 | inport, |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 118 | deviceId, |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 119 | outPorts, |
| 120 | copyIngressPorts, |
| 121 | copyEgressPorts); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 122 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 123 | NextObjective nextObjective = DefaultNextObjective.builder() |
| 124 | .withId(flowObjectiveService.allocateNextId()) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 125 | .addTreatment(instructions.treatment()) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 126 | .withType(NextObjective.Type.SIMPLE) |
| 127 | .fromApp(appId) |
| 128 | .makePermanent().add(); |
| 129 | objectives.add(nextObjective); |
| 130 | |
| 131 | objectives.add(DefaultForwardingObjective.builder() |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 132 | .withSelector(instructions.selector()) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 133 | .nextStep(nextObjective.id()) |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 134 | .withPriority(intent.priority()) |
| 135 | .fromApp(appId) |
| 136 | .makePermanent() |
Thomas Vachuska | 8378ccf | 2016-03-02 18:02:17 -0800 | [diff] [blame] | 137 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 138 | .add()); |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 139 | } |
| 140 | ); |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 141 | |
| 142 | return objectives; |
| 143 | } |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 144 | |
Ray Milkey | db74ec7 | 2016-03-01 10:35:24 -0800 | [diff] [blame] | 145 | } |