Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
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; |
| 31 | import org.onosproject.net.intent.IntentCompiler; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.IntentExtensionService; |
| 34 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 35 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 36 | import org.onosproject.net.intent.PointToPointIntent; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 37 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.topology.PathService; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 40 | import java.util.Collections; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.List; |
| 43 | import java.util.Map; |
| 44 | import java.util.Set; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 45 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 46 | import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure; |
| 47 | |
| 48 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 49 | /** |
| 50 | * An intent compiler for |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}. |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public class MultiPointToSinglePointIntentCompiler |
| 55 | implements IntentCompiler<MultiPointToSinglePointIntent> { |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected IntentExtensionService intentManager; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected PathService pathService; |
| 62 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected DeviceService deviceService; |
| 65 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 66 | @Activate |
| 67 | public void activate() { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 68 | intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 74 | } |
| 75 | |
| 76 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 77 | public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable, |
| 78 | Set<LinkResourceAllocations> resources) { |
Jonathan Hart | f5e3580 | 2014-12-01 20:45:18 -0800 | [diff] [blame] | 79 | Map<DeviceId, Link> links = new HashMap<>(); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 80 | ConnectPoint egressPoint = intent.egressPoint(); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 81 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 82 | final boolean allowMissingPaths = intentAllowsPartialFailure(intent); |
| 83 | boolean partialTree = false; |
| 84 | boolean anyMissingPaths = false; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 85 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 86 | if (ingressPoint.deviceId().equals(egressPoint.deviceId())) { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 87 | if (deviceService.isAvailable(ingressPoint.deviceId())) { |
| 88 | partialTree = true; |
| 89 | } else { |
| 90 | anyMissingPaths = true; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 91 | } |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 92 | |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 93 | continue; |
Jonathan Hart | f5e3580 | 2014-12-01 20:45:18 -0800 | [diff] [blame] | 94 | } |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 95 | |
| 96 | Path path = getPath(ingressPoint, intent.egressPoint()); |
| 97 | if (path != null) { |
| 98 | partialTree = true; |
| 99 | |
| 100 | for (Link link : path.links()) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 101 | if (links.containsKey(link.dst().deviceId())) { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 102 | // We've already reached the existing tree with the first |
| 103 | // part of this path. Add the merging point with different |
| 104 | // incoming port, but don't add the remainder of the path |
| 105 | // in case it differs from the path we already have. |
| 106 | links.put(link.src().deviceId(), link); |
| 107 | break; |
| 108 | } |
| 109 | links.put(link.src().deviceId(), link); |
| 110 | } |
| 111 | } else { |
| 112 | anyMissingPaths = true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (!partialTree) { |
| 117 | throw new IntentException("Could not find any paths between ingress and egress points."); |
| 118 | } else if (!allowMissingPaths && anyMissingPaths) { |
| 119 | throw new IntentException("Missing some paths between ingress and egress ports."); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 122 | Intent result = LinkCollectionIntent.builder() |
| 123 | .appId(intent.appId()) |
| 124 | .selector(intent.selector()) |
| 125 | .treatment(intent.treatment()) |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 126 | .links(Sets.newHashSet(links.values())) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 127 | .ingressPoints(intent.ingressPoints()) |
| 128 | .egressPoints(ImmutableSet.of(intent.egressPoint())) |
| 129 | .priority(intent.priority()) |
Sho SHIMIZU | 1b46a97 | 2015-04-10 17:33:00 -0700 | [diff] [blame] | 130 | .constraints(intent.constraints()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 131 | .build(); |
| 132 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 133 | return Collections.singletonList(result); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Computes a path between two ConnectPoints. |
| 138 | * |
| 139 | * @param one start of the path |
| 140 | * @param two end of the path |
| 141 | * @return Path between the two |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 142 | */ |
| 143 | private Path getPath(ConnectPoint one, ConnectPoint two) { |
| 144 | Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId()); |
| 145 | if (paths.isEmpty()) { |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 146 | return null; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 147 | } |
| 148 | // TODO: let's be more intelligent about this eventually |
| 149 | return paths.iterator().next(); |
| 150 | } |
| 151 | } |