Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 17 | |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 18 | import com.google.common.collect.FluentIterable; |
Sho SHIMIZU | 5653151 | 2014-11-10 15:27:49 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 023a1c7 | 2015-03-20 01:11:29 +0000 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Component; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 23 | import org.onlab.graph.DefaultEdgeWeigher; |
| 24 | import org.onlab.graph.ScalarWeight; |
| 25 | import org.onlab.graph.Weight; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ElementId; |
| 28 | import org.onosproject.net.Path; |
| 29 | import org.onosproject.net.intent.ConnectivityIntent; |
| 30 | import org.onosproject.net.intent.Constraint; |
| 31 | import org.onosproject.net.intent.IntentCompiler; |
| 32 | import org.onosproject.net.intent.IntentExtensionService; |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.impl.PathNotFoundException; |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.resource.ResourceQueryService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.provider.ProviderId; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 36 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.topology.PathService; |
| 38 | import org.onosproject.net.topology.TopologyEdge; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 39 | import org.onosproject.net.topology.TopologyVertex; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 40 | |
Sho SHIMIZU | 5653151 | 2014-11-10 15:27:49 -0800 | [diff] [blame] | 41 | import java.util.Collections; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 42 | import java.util.Iterator; |
| 43 | import java.util.List; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | /** |
| 47 | * Base class for compilers of various |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | * {@link org.onosproject.net.intent.ConnectivityIntent connectivity intents}. |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 49 | */ |
Brian O'Connor | 023a1c7 | 2015-03-20 01:11:29 +0000 | [diff] [blame] | 50 | @Component(immediate = true) |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 51 | public abstract class ConnectivityIntentCompiler<T extends ConnectivityIntent> |
| 52 | implements IntentCompiler<T> { |
| 53 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | private static final ProviderId PID = new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected IntentExtensionService intentManager; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected PathService pathService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 63 | protected ResourceQueryService resourceService; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Returns an edge-weight capable of evaluating links on the basis of the |
| 67 | * specified constraints. |
| 68 | * |
| 69 | * @param constraints path constraints |
| 70 | * @return edge-weight function |
| 71 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 72 | protected LinkWeigher weigher(List<Constraint> constraints) { |
| 73 | return new ConstraintBasedLinkWeigher(constraints); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Validates the specified path against the given constraints. |
| 78 | * |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 79 | * @param path path to be checked |
| 80 | * @param constraints path constraints |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 81 | * @return true if the path passes all constraints |
| 82 | */ |
| 83 | protected boolean checkPath(Path path, List<Constraint> constraints) { |
| 84 | for (Constraint constraint : constraints) { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 85 | if (!constraint.validate(path, resourceService::isAvailable)) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Computes a path between two ConnectPoints. |
| 94 | * |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 95 | * @param intent intent on which behalf path is being computed |
| 96 | * @param one start of the path |
| 97 | * @param two end of the path |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 98 | * @return Path between the two |
| 99 | * @throws PathNotFoundException if a path cannot be found |
| 100 | */ |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 101 | @Deprecated |
| 102 | protected Path getPathOrException(ConnectivityIntent intent, |
| 103 | ElementId one, ElementId two) { |
| 104 | Path path = getPath(intent, one, two); |
| 105 | if (path == null) { |
| 106 | throw new PathNotFoundException(one, two); |
| 107 | } |
| 108 | // TODO: let's be more intelligent about this eventually |
| 109 | return path; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Computes a path between two ConnectPoints. |
| 114 | * |
| 115 | * @param intent intent on which behalf path is being computed |
| 116 | * @param one start of the path |
| 117 | * @param two end of the path |
| 118 | * @return Path between the two, or null if no path can be found |
| 119 | */ |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 120 | protected Path getPath(ConnectivityIntent intent, |
| 121 | ElementId one, ElementId two) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 122 | Set<Path> paths = pathService.getPaths(one, two, weigher(intent.constraints())); |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 123 | final List<Constraint> constraints = intent.constraints(); |
| 124 | ImmutableList<Path> filtered = FluentIterable.from(paths) |
Sho SHIMIZU | 8b7b3b2 | 2015-09-04 16:04:50 -0700 | [diff] [blame] | 125 | .filter(path -> checkPath(path, constraints)) |
| 126 | .toList(); |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 127 | if (filtered.isEmpty()) { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 128 | return null; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 129 | } |
| 130 | // TODO: let's be more intelligent about this eventually |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 131 | return filtered.iterator().next(); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 135 | * Computes a disjoint path between two ConnectPoints. |
| 136 | * |
| 137 | * @param intent intent on which behalf path is being computed |
| 138 | * @param one start of the path |
| 139 | * @param two end of the path |
| 140 | * @return DisjointPath between the two |
| 141 | * @throws PathNotFoundException if two paths cannot be found |
| 142 | */ |
| 143 | protected DisjointPath getDisjointPath(ConnectivityIntent intent, |
| 144 | ElementId one, ElementId two) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 145 | Set<DisjointPath> paths = pathService.getDisjointPaths(one, two, weigher(intent.constraints())); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 146 | final List<Constraint> constraints = intent.constraints(); |
| 147 | ImmutableList<DisjointPath> filtered = FluentIterable.from(paths) |
| 148 | .filter(path -> checkPath(path, constraints)) |
| 149 | .toList(); |
| 150 | if (filtered.isEmpty()) { |
| 151 | throw new PathNotFoundException(one, two); |
| 152 | } |
| 153 | // TODO: let's be more intelligent about this eventually |
| 154 | return filtered.iterator().next(); |
| 155 | } |
| 156 | |
| 157 | /** |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 158 | * Edge-weight capable of evaluating link cost using a set of constraints. |
| 159 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 160 | protected class ConstraintBasedLinkWeigher extends DefaultEdgeWeigher<TopologyVertex, TopologyEdge> |
| 161 | implements LinkWeigher { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 162 | |
| 163 | private final List<Constraint> constraints; |
| 164 | |
| 165 | /** |
| 166 | * Creates a new edge-weight function capable of evaluating links |
| 167 | * on the basis of the specified constraints. |
| 168 | * |
| 169 | * @param constraints path constraints |
| 170 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 171 | ConstraintBasedLinkWeigher(List<Constraint> constraints) { |
Sho SHIMIZU | 5653151 | 2014-11-10 15:27:49 -0800 | [diff] [blame] | 172 | if (constraints == null) { |
| 173 | this.constraints = Collections.emptyList(); |
| 174 | } else { |
| 175 | this.constraints = ImmutableList.copyOf(constraints); |
| 176 | } |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 180 | public Weight weight(TopologyEdge edge) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 181 | |
| 182 | // iterate over all constraints in order and return the weight of |
| 183 | // the first one with fast fail over the first failure |
| 184 | Iterator<Constraint> it = constraints.iterator(); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 185 | |
Yuta HIGUCHI | a0b0a13 | 2016-09-16 15:47:49 -0700 | [diff] [blame] | 186 | if (!it.hasNext()) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 187 | return new ScalarWeight(HOP_WEIGHT_VALUE); |
Yuta HIGUCHI | a0b0a13 | 2016-09-16 15:47:49 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 190 | double cost = it.next().cost(edge.link(), resourceService::isAvailable); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 191 | while (it.hasNext() && cost > 0) { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 192 | if (it.next().cost(edge.link(), resourceService::isAvailable) < 0) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 193 | cost = -1; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame^] | 196 | return new ScalarWeight(cost); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 197 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | } |