Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 29 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Intent; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 31 | import org.onosproject.net.intent.IntentException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentExtensionService; |
| 33 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 34 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 35 | |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 36 | import java.util.Collections; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 40 | import java.util.stream.Collectors; |
| 41 | import java.util.stream.Stream; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 42 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 43 | import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure; |
| 44 | |
| 45 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 46 | /** |
| 47 | * An intent compiler for |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}. |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | public class MultiPointToSinglePointIntentCompiler |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 52 | extends ConnectivityIntentCompiler<MultiPointToSinglePointIntent> { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected IntentExtensionService intentManager; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 58 | protected DeviceService deviceService; |
| 59 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 60 | @Activate |
| 61 | public void activate() { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 62 | intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
wu | 76a3d67 | 2017-04-11 14:06:44 +0800 | [diff] [blame] | 67 | intentManager.unregisterCompiler(MultiPointToSinglePointIntent.class); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 71 | public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) { |
Jonathan Hart | f5e3580 | 2014-12-01 20:45:18 -0800 | [diff] [blame] | 72 | Map<DeviceId, Link> links = new HashMap<>(); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 73 | ConnectPoint egressPoint = intent.egressPoint(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 74 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 75 | final boolean allowMissingPaths = intentAllowsPartialFailure(intent); |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 76 | boolean hasPaths = false; |
| 77 | boolean missingSomePaths = false; |
| 78 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 79 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 80 | if (ingressPoint.deviceId().equals(egressPoint.deviceId())) { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 81 | if (deviceService.isAvailable(ingressPoint.deviceId())) { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 82 | hasPaths = true; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 83 | } else { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 84 | missingSomePaths = true; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 85 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 86 | continue; |
Jonathan Hart | f5e3580 | 2014-12-01 20:45:18 -0800 | [diff] [blame] | 87 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 88 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 89 | Path path = getPath(intent, ingressPoint.deviceId(), egressPoint.deviceId()); |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 90 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 91 | if (path != null) { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 92 | hasPaths = true; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 93 | |
| 94 | for (Link link : path.links()) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 95 | if (links.containsKey(link.dst().deviceId())) { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 96 | // We've already reached the existing tree with the first |
| 97 | // part of this path. Add the merging point with different |
| 98 | // incoming port, but don't add the remainder of the path |
| 99 | // in case it differs from the path we already have. |
| 100 | links.put(link.src().deviceId(), link); |
| 101 | break; |
| 102 | } |
| 103 | links.put(link.src().deviceId(), link); |
| 104 | } |
| 105 | } else { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 106 | missingSomePaths = true; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 110 | // Allocate bandwidth on existing paths if a bandwidth constraint is set |
| 111 | List<ConnectPoint> ingressCPs = |
| 112 | intent.filteredIngressPoints().stream() |
| 113 | .map(fcp -> fcp.connectPoint()) |
| 114 | .collect(Collectors.toList()); |
| 115 | ConnectPoint egressCP = intent.filteredEgressPoint().connectPoint(); |
| 116 | |
| 117 | List<ConnectPoint> pathCPs = |
| 118 | links.values().stream() |
| 119 | .flatMap(l -> Stream.of(l.src(), l.dst())) |
| 120 | .collect(Collectors.toList()); |
| 121 | |
| 122 | pathCPs.addAll(ingressCPs); |
| 123 | pathCPs.add(egressCP); |
| 124 | |
| 125 | allocateBandwidth(intent, pathCPs); |
| 126 | |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 127 | if (!hasPaths) { |
| 128 | throw new IntentException("Cannot find any path between ingress and egress points."); |
| 129 | } else if (!allowMissingPaths && missingSomePaths) { |
| 130 | throw new IntentException("Missing some paths between ingress and egress points."); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 133 | Intent result = LinkCollectionIntent.builder() |
| 134 | .appId(intent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 135 | .key(intent.key()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 136 | .treatment(intent.treatment()) |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 137 | .selector(intent.selector()) |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 138 | .links(Sets.newHashSet(links.values())) |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 139 | .filteredIngressPoints(intent.filteredIngressPoints()) |
| 140 | .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint())) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 141 | .priority(intent.priority()) |
Sho SHIMIZU | 1b46a97 | 2015-04-10 17:33:00 -0700 | [diff] [blame] | 142 | .constraints(intent.constraints()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 143 | .resourceGroup(intent.resourceGroup()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 144 | .build(); |
| 145 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 146 | return Collections.singletonList(result); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 147 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 148 | } |