Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 19 | import com.google.common.collect.SetMultimap; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.DefaultGroupId; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 30 | import org.onosproject.net.EdgeLink; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Link; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.DefaultFlowRule; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.intent.FlowRuleIntent; |
| 40 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.IntentCompiler; |
| 42 | import org.onosproject.net.intent.IntentExtensionService; |
| 43 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 44 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 45 | |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.Arrays; |
| 48 | import java.util.List; |
| 49 | import java.util.Set; |
| 50 | import java.util.stream.Collectors; |
| 51 | |
| 52 | @Component(immediate = true) |
| 53 | public class LinkCollectionIntentCompiler implements IntentCompiler<LinkCollectionIntent> { |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected IntentExtensionService intentManager; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | private ApplicationId appId; |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 66 | intentManager.registerCompiler(LinkCollectionIntent.class, this); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | intentManager.unregisterCompiler(LinkCollectionIntent.class); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable, |
| 76 | Set<LinkResourceAllocations> resources) { |
| 77 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 78 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
| 79 | |
| 80 | for (Link link : intent.links()) { |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 81 | DeviceId srcDeviceId; |
| 82 | DeviceId dstDeviceId; |
| 83 | |
| 84 | if (link instanceof EdgeLink) { |
| 85 | EdgeLink edgeLink = (EdgeLink) link; |
| 86 | dstDeviceId = edgeLink.hostLocation().deviceId(); |
| 87 | srcDeviceId = dstDeviceId; |
| 88 | } else { |
| 89 | inputPorts.put(link.dst().deviceId(), link.dst().port()); |
| 90 | srcDeviceId = link.src().deviceId(); |
| 91 | } |
| 92 | outputPorts.put(srcDeviceId, link.src().port()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 96 | inputPorts.put(ingressPoint.deviceId(), ingressPoint.port()); |
| 97 | } |
| 98 | |
| 99 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
| 100 | outputPorts.put(egressPoint.deviceId(), egressPoint.port()); |
| 101 | } |
| 102 | |
| 103 | List<FlowRule> rules = new ArrayList<>(); |
| 104 | for (DeviceId deviceId: outputPorts.keys()) { |
| 105 | rules.addAll(createRules(intent, deviceId, inputPorts.get(deviceId), outputPorts.get(deviceId))); |
| 106 | } |
Sho SHIMIZU | 0c3c076 | 2015-04-25 08:37:34 +0900 | [diff] [blame] | 107 | return Arrays.asList(new FlowRuleIntent(appId, rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private List<FlowRule> createRules(LinkCollectionIntent intent, DeviceId deviceId, |
| 111 | Set<PortNumber> inPorts, Set<PortNumber> outPorts) { |
| 112 | Set<PortNumber> ingressPorts = intent.ingressPoints().stream() |
| 113 | .filter(point -> point.deviceId().equals(deviceId)) |
| 114 | .map(ConnectPoint::port) |
| 115 | .collect(Collectors.toSet()); |
| 116 | |
| 117 | TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder(); |
| 118 | outPorts.stream() |
| 119 | .forEach(defaultTreatmentBuilder::setOutput); |
| 120 | TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build(); |
| 121 | |
| 122 | TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment()); |
| 123 | outPorts.stream() |
| 124 | .forEach(ingressTreatmentBuilder::setOutput); |
| 125 | TrafficTreatment ingressTreatment = ingressTreatmentBuilder.build(); |
| 126 | |
| 127 | List<FlowRule> rules = new ArrayList<>(inPorts.size()); |
| 128 | for (PortNumber inPort: inPorts) { |
| 129 | TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector()).matchInPort(inPort).build(); |
| 130 | TrafficTreatment treatment; |
| 131 | if (ingressPorts.contains(inPort)) { |
| 132 | treatment = ingressTreatment; |
| 133 | } else { |
| 134 | treatment = defaultTreatment; |
| 135 | } |
| 136 | |
| 137 | DefaultFlowRule rule = new DefaultFlowRule(deviceId, selector, treatment, 123, appId, |
| 138 | new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true); |
| 139 | |
| 140 | rules.add(rule); |
| 141 | } |
| 142 | |
| 143 | return rules; |
| 144 | } |
| 145 | } |