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