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 | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -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; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -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; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.flow.DefaultFlowRule; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.FlowRule; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.FlowRuleIntent; |
| 34 | import org.onosproject.net.intent.Intent; |
| 35 | import org.onosproject.net.intent.IntentCompiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.LinkCollectionIntent; |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 41 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Set; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 44 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Compiler to produce flow rules from link collections. |
| 47 | */ |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 48 | @Component(immediate = true) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 49 | public class LinkCollectionIntentCompiler |
| 50 | extends LinkCollectionCompiler<FlowRule> |
| 51 | implements IntentCompiler<LinkCollectionIntent> { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 52 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 53 | private static Logger log = LoggerFactory.getLogger(LinkCollectionIntentCompiler.class); |
| 54 | |
| 55 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 57 | protected IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected CoreService coreService; |
| 61 | |
| 62 | private ApplicationId appId; |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 67 | registrator.registerCompiler(LinkCollectionIntent.class, this, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 72 | registrator.unregisterCompiler(LinkCollectionIntent.class, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 76 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 77 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 78 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 79 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
| 80 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 81 | computePorts(intent, inputPorts, outputPorts); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 82 | |
| 83 | List<FlowRule> rules = new ArrayList<>(); |
Yi Tseng | 0e251b3 | 2016-09-20 11:08:32 -0700 | [diff] [blame^] | 84 | for (DeviceId deviceId: outputPorts.keySet()) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 85 | rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId))); |
| 86 | } |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 87 | return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 90 | @Override |
| 91 | protected List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId, |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 92 | Set<PortNumber> inPorts, Set<PortNumber> outPorts) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 93 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 94 | Set<PortNumber> ingressPorts = Sets.newHashSet(); |
| 95 | Set<PortNumber> egressPorts = Sets.newHashSet(); |
| 96 | |
| 97 | computePorts(intent, deviceId, ingressPorts, egressPorts); |
Brian O'Connor | 406e264 | 2016-04-18 11:45:35 -0700 | [diff] [blame] | 98 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 99 | List<FlowRule> rules = new ArrayList<>(inPorts.size()); |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 100 | Set<PortNumber> copyIngressPorts = ImmutableSet.copyOf(ingressPorts); |
| 101 | Set<PortNumber> copyEgressPorts = ImmutableSet.copyOf(egressPorts); |
Nicholas Dean | 126b8af | 2016-07-18 14:43:13 -0700 | [diff] [blame] | 102 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 103 | inPorts.forEach(inport -> { |
| 104 | ForwardingInstructions instructions = this.createForwardingInstructions(intent, |
| 105 | inport, |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 106 | deviceId, |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 107 | outPorts, |
| 108 | copyIngressPorts, |
| 109 | copyEgressPorts); |
| 110 | FlowRule rule = DefaultFlowRule.builder() |
| 111 | .forDevice(deviceId) |
| 112 | .withSelector(instructions.selector()) |
| 113 | .withTreatment(instructions.treatment()) |
| 114 | .withPriority(intent.priority()) |
| 115 | .fromApp(appId) |
| 116 | .makePermanent() |
| 117 | .build(); |
| 118 | rules.add(rule); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 119 | } |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 120 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 121 | |
| 122 | return rules; |
| 123 | } |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 124 | } |