Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -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 | |
| 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; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
| 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; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -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; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.flow.DefaultFlowRule; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRule; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.FlowRuleIntent; |
| 35 | import org.onosproject.net.intent.Intent; |
| 36 | import org.onosproject.net.intent.IntentCompiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.LinkCollectionIntent; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 38 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 39 | import org.onosproject.net.resource.ResourceService; |
| 40 | import org.onosproject.net.resource.impl.LabelAllocator; |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 43 | |
| 44 | import java.util.ArrayList; |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 45 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 46 | import java.util.List; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 47 | import java.util.Map; |
| 48 | import java.util.Optional; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 49 | import java.util.Set; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 50 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Compiler to produce flow rules from link collections. |
| 53 | */ |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 54 | @Component(immediate = true) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 55 | public class LinkCollectionIntentCompiler |
| 56 | extends LinkCollectionCompiler<FlowRule> |
| 57 | implements IntentCompiler<LinkCollectionIntent> { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 58 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 59 | private static Logger log = LoggerFactory.getLogger(LinkCollectionIntentCompiler.class); |
| 60 | |
| 61 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 63 | protected IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected CoreService coreService; |
| 67 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected ResourceService resourceService; |
| 70 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 71 | private ApplicationId appId; |
| 72 | |
| 73 | @Activate |
| 74 | public void activate() { |
| 75 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 76 | registrator.registerCompiler(LinkCollectionIntent.class, this, false); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 77 | if (labelAllocator == null) { |
| 78 | labelAllocator = new LabelAllocator(resourceService); |
| 79 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 84 | registrator.unregisterCompiler(LinkCollectionIntent.class, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 88 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 89 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 90 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 91 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 92 | Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of(); |
| 93 | |
| 94 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 95 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 96 | computePorts(intent, inputPorts, outputPorts); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 97 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 98 | if (encapConstraint.isPresent()) { |
| 99 | labels = labelAllocator.assignLabelToPorts(intent.links(), |
| 100 | intent.id(), |
| 101 | encapConstraint.get().encapType()); |
| 102 | } |
| 103 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 104 | List<FlowRule> rules = new ArrayList<>(); |
Yi Tseng | 155370e | 2016-09-20 11:08:32 -0700 | [diff] [blame] | 105 | for (DeviceId deviceId: outputPorts.keySet()) { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 106 | rules.addAll(createRules( |
| 107 | intent, |
| 108 | deviceId, |
| 109 | inputPorts.get(deviceId), |
| 110 | outputPorts.get(deviceId), |
| 111 | labels) |
| 112 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 113 | } |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 114 | return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 117 | @Override |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 118 | protected List<FlowRule> createRules(LinkCollectionIntent intent, |
| 119 | DeviceId deviceId, |
| 120 | Set<PortNumber> inPorts, |
| 121 | Set<PortNumber> outPorts, |
| 122 | Map<ConnectPoint, Identifier<?>> labels) { |
Brian O'Connor | 406e264 | 2016-04-18 11:45:35 -0700 | [diff] [blame] | 123 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 124 | List<FlowRule> rules = new ArrayList<>(inPorts.size()); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 125 | /* |
| 126 | * Looking for the encapsulation constraint |
| 127 | */ |
| 128 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Nicholas Dean | 126b8af | 2016-07-18 14:43:13 -0700 | [diff] [blame] | 129 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 130 | inPorts.forEach(inport -> { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame^] | 131 | |
| 132 | ForwardingInstructions instructions = this.createForwardingInstruction( |
| 133 | encapConstraint, |
| 134 | intent, |
| 135 | inport, |
| 136 | outPorts, |
| 137 | deviceId, |
| 138 | labels |
| 139 | ); |
| 140 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 141 | FlowRule rule = DefaultFlowRule.builder() |
| 142 | .forDevice(deviceId) |
| 143 | .withSelector(instructions.selector()) |
| 144 | .withTreatment(instructions.treatment()) |
| 145 | .withPriority(intent.priority()) |
| 146 | .fromApp(appId) |
| 147 | .makePermanent() |
| 148 | .build(); |
| 149 | rules.add(rule); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 150 | } |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 151 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 152 | |
| 153 | return rules; |
| 154 | } |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 155 | } |