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; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Brian O'Connor | 023a1c7 | 2015-03-20 01:11:29 +0000 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Component; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 24 | import org.onlab.graph.DefaultEdgeWeigher; |
| 25 | import org.onlab.graph.ScalarWeight; |
| 26 | import org.onlab.graph.Weight; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 27 | import org.onlab.util.Bandwidth; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ElementId; |
| 32 | import org.onosproject.net.Path; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.ConnectivityIntent; |
| 35 | import org.onosproject.net.intent.Constraint; |
| 36 | import org.onosproject.net.intent.IntentCompiler; |
| 37 | import org.onosproject.net.intent.IntentExtensionService; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
jaegonkim | 7e87663 | 2017-01-25 06:01:49 +0900 | [diff] [blame] | 39 | import org.onosproject.net.intent.constraint.HashedPathSelectionConstraint; |
Yuta HIGUCHI | e37560f | 2017-02-02 19:53:26 -0800 | [diff] [blame^] | 40 | import org.onosproject.net.intent.constraint.MarkerConstraint; |
| 41 | import org.onosproject.net.intent.constraint.PathViabilityConstraint; |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.impl.PathNotFoundException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.provider.ProviderId; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 44 | import org.onosproject.net.resource.Resource; |
| 45 | import org.onosproject.net.resource.ResourceAllocation; |
| 46 | import org.onosproject.net.resource.ResourceConsumer; |
| 47 | import org.onosproject.net.resource.ResourceId; |
| 48 | import org.onosproject.net.resource.ResourceService; |
| 49 | import org.onosproject.net.resource.Resources; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 50 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.topology.PathService; |
| 52 | import org.onosproject.net.topology.TopologyEdge; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 53 | import org.onosproject.net.topology.TopologyVertex; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 56 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 57 | import java.util.Collection; |
Sho SHIMIZU | 5653151 | 2014-11-10 15:27:49 -0800 | [diff] [blame] | 58 | import java.util.Collections; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 59 | import java.util.Iterator; |
| 60 | import java.util.List; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 61 | import java.util.Optional; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 62 | import java.util.Set; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 63 | import java.util.stream.Collectors; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Base class for compilers of various |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 67 | * {@link org.onosproject.net.intent.ConnectivityIntent connectivity intents}. |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 68 | */ |
Brian O'Connor | 023a1c7 | 2015-03-20 01:11:29 +0000 | [diff] [blame] | 69 | @Component(immediate = true) |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 70 | public abstract class ConnectivityIntentCompiler<T extends ConnectivityIntent> |
| 71 | implements IntentCompiler<T> { |
| 72 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 73 | private static final ProviderId PID = new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 74 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 75 | private static final Logger log = LoggerFactory.getLogger(ConnectivityIntentCompiler.class); |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected DeviceService deviceService; |
| 79 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected IntentExtensionService intentManager; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected PathService pathService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 87 | protected ResourceService resourceService; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Returns an edge-weight capable of evaluating links on the basis of the |
| 91 | * specified constraints. |
| 92 | * |
| 93 | * @param constraints path constraints |
| 94 | * @return edge-weight function |
| 95 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 96 | protected LinkWeigher weigher(List<Constraint> constraints) { |
| 97 | return new ConstraintBasedLinkWeigher(constraints); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Validates the specified path against the given constraints. |
| 102 | * |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 103 | * @param path path to be checked |
| 104 | * @param constraints path constraints |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 105 | * @return true if the path passes all constraints |
| 106 | */ |
| 107 | protected boolean checkPath(Path path, List<Constraint> constraints) { |
| 108 | for (Constraint constraint : constraints) { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 109 | if (!constraint.validate(path, resourceService::isAvailable)) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Computes a path between two ConnectPoints. |
| 118 | * |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 119 | * @param intent intent on which behalf path is being computed |
| 120 | * @param one start of the path |
| 121 | * @param two end of the path |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 122 | * @return Path between the two |
| 123 | * @throws PathNotFoundException if a path cannot be found |
| 124 | */ |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 125 | @Deprecated |
| 126 | protected Path getPathOrException(ConnectivityIntent intent, |
| 127 | ElementId one, ElementId two) { |
| 128 | Path path = getPath(intent, one, two); |
| 129 | if (path == null) { |
| 130 | throw new PathNotFoundException(one, two); |
| 131 | } |
| 132 | // TODO: let's be more intelligent about this eventually |
| 133 | return path; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Computes a path between two ConnectPoints. |
| 138 | * |
| 139 | * @param intent intent on which behalf path is being computed |
| 140 | * @param one start of the path |
| 141 | * @param two end of the path |
| 142 | * @return Path between the two, or null if no path can be found |
| 143 | */ |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 144 | protected Path getPath(ConnectivityIntent intent, |
| 145 | ElementId one, ElementId two) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 146 | Set<Path> paths = pathService.getPaths(one, two, weigher(intent.constraints())); |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 147 | final List<Constraint> constraints = intent.constraints(); |
| 148 | ImmutableList<Path> filtered = FluentIterable.from(paths) |
Sho SHIMIZU | 8b7b3b2 | 2015-09-04 16:04:50 -0700 | [diff] [blame] | 149 | .filter(path -> checkPath(path, constraints)) |
| 150 | .toList(); |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 151 | if (filtered.isEmpty()) { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 152 | return null; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 153 | } |
jaegonkim | 7e87663 | 2017-01-25 06:01:49 +0900 | [diff] [blame] | 154 | |
| 155 | if (constraints.stream().anyMatch(c -> c instanceof HashedPathSelectionConstraint)) { |
| 156 | return filtered.get(intent.hashCode() % filtered.size()); |
| 157 | } |
| 158 | |
Sho SHIMIZU | 9909d17 | 2014-11-11 18:33:11 -0800 | [diff] [blame] | 159 | return filtered.iterator().next(); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 163 | * Computes a disjoint path between two ConnectPoints. |
| 164 | * |
| 165 | * @param intent intent on which behalf path is being computed |
| 166 | * @param one start of the path |
| 167 | * @param two end of the path |
| 168 | * @return DisjointPath between the two |
| 169 | * @throws PathNotFoundException if two paths cannot be found |
| 170 | */ |
| 171 | protected DisjointPath getDisjointPath(ConnectivityIntent intent, |
| 172 | ElementId one, ElementId two) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 173 | Set<DisjointPath> paths = pathService.getDisjointPaths(one, two, weigher(intent.constraints())); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 174 | final List<Constraint> constraints = intent.constraints(); |
| 175 | ImmutableList<DisjointPath> filtered = FluentIterable.from(paths) |
| 176 | .filter(path -> checkPath(path, constraints)) |
| 177 | .toList(); |
| 178 | if (filtered.isEmpty()) { |
| 179 | throw new PathNotFoundException(one, two); |
| 180 | } |
jaegonkim | 7e87663 | 2017-01-25 06:01:49 +0900 | [diff] [blame] | 181 | |
| 182 | if (constraints.stream().anyMatch(c -> c instanceof HashedPathSelectionConstraint)) { |
| 183 | return filtered.get(intent.hashCode() % filtered.size()); |
| 184 | } |
| 185 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 186 | return filtered.iterator().next(); |
| 187 | } |
| 188 | |
| 189 | /** |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 190 | * Allocates the bandwidth specified as intent constraint on each link |
| 191 | * composing the intent, if a bandwidth constraint is specified. |
| 192 | * |
| 193 | * @param intent the intent requesting bandwidth allocation |
| 194 | * @param connectPoints the connect points composing the intent path computed |
| 195 | */ |
| 196 | protected void allocateBandwidth(ConnectivityIntent intent, |
| 197 | List<ConnectPoint> connectPoints) { |
| 198 | // Retrieve bandwidth constraint if exists |
| 199 | List<Constraint> constraints = intent.constraints(); |
| 200 | |
| 201 | if (constraints == null) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | Optional<Constraint> constraint = |
| 206 | constraints.stream() |
| 207 | .filter(c -> c instanceof BandwidthConstraint) |
| 208 | .findAny(); |
| 209 | |
| 210 | // If there is no bandwidth constraint continue |
| 211 | if (!constraint.isPresent()) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | BandwidthConstraint bwConstraint = (BandwidthConstraint) constraint.get(); |
| 216 | |
| 217 | double bw = bwConstraint.bandwidth().bps(); |
| 218 | |
| 219 | // If a resource group is set on the intent, the resource consumer is |
| 220 | // set equal to it. Otherwise it's set to the intent key |
| 221 | ResourceConsumer newResourceConsumer = |
| 222 | intent.resourceGroup() != null ? intent.resourceGroup() : intent.key(); |
| 223 | |
| 224 | // Get the list of current resource allocations |
| 225 | Collection<ResourceAllocation> resourceAllocations = |
| 226 | resourceService.getResourceAllocations(newResourceConsumer); |
| 227 | |
| 228 | // Get the list of resources already allocated from resource allocations |
| 229 | List<Resource> resourcesAllocated = |
| 230 | resourcesFromAllocations(resourceAllocations); |
| 231 | |
| 232 | // Get the list of resource ids for resources already allocated |
| 233 | List<ResourceId> idsResourcesAllocated = resourceIds(resourcesAllocated); |
| 234 | |
| 235 | // Create the list of incoming resources requested. Exclude resources |
| 236 | // already allocated. |
| 237 | List<Resource> incomingResources = |
| 238 | resources(connectPoints, bw).stream() |
| 239 | .filter(r -> !resourcesAllocated.contains(r)) |
| 240 | .collect(Collectors.toList()); |
| 241 | |
| 242 | if (incomingResources.isEmpty()) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // Create the list of resources to be added, meaning their key is not |
| 247 | // present in the resources already allocated |
| 248 | List<Resource> resourcesToAdd = |
| 249 | incomingResources.stream() |
| 250 | .filter(r -> !idsResourcesAllocated.contains(r.id())) |
| 251 | .collect(Collectors.toList()); |
| 252 | |
| 253 | // Resources to updated are all the new valid resources except the |
| 254 | // resources to be added |
| 255 | List<Resource> resourcesToUpdate = Lists.newArrayList(incomingResources); |
| 256 | resourcesToUpdate.removeAll(resourcesToAdd); |
| 257 | |
| 258 | // If there are no resources to update skip update procedures |
| 259 | if (!resourcesToUpdate.isEmpty()) { |
| 260 | // Remove old resources that need to be updated |
| 261 | // TODO: use transaction updates when available in the resource service |
| 262 | List<ResourceAllocation> resourceAllocationsToUpdate = |
| 263 | resourceAllocations.stream() |
| 264 | .filter(rA -> resourceIds(resourcesToUpdate).contains(rA.resource().id())) |
| 265 | .collect(Collectors.toList()); |
| 266 | log.debug("Releasing bandwidth for intent {}: {} bps", newResourceConsumer, resourcesToUpdate); |
| 267 | resourceService.release(resourceAllocationsToUpdate); |
| 268 | |
| 269 | // Update resourcesToAdd with the list of both the new resources and |
| 270 | // the resources to update |
| 271 | resourcesToAdd.addAll(resourcesToUpdate); |
| 272 | } |
| 273 | |
| 274 | // Look also for resources allocated using the intent key and -if any- |
| 275 | // remove them |
| 276 | if (intent.resourceGroup() != null) { |
| 277 | // Get the list of current resource allocations made by intent key |
| 278 | Collection<ResourceAllocation> resourceAllocationsByKey = |
| 279 | resourceService.getResourceAllocations(intent.key()); |
| 280 | |
| 281 | resourceService.release(Lists.newArrayList(resourceAllocationsByKey)); |
| 282 | } |
| 283 | |
| 284 | // Allocate resources |
| 285 | log.debug("Allocating bandwidth for intent {}: {} bps", newResourceConsumer, resourcesToAdd); |
| 286 | List<ResourceAllocation> allocations = |
| 287 | resourceService.allocate(newResourceConsumer, resourcesToAdd); |
| 288 | |
| 289 | if (allocations.isEmpty()) { |
| 290 | log.debug("No resources allocated for intent {}", newResourceConsumer); |
| 291 | } |
| 292 | |
| 293 | log.debug("Done allocating bandwidth for intent {}", newResourceConsumer); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Produces a list of resources from a list of resource allocations. |
| 298 | * |
| 299 | * @param rAs the list of resource allocations |
| 300 | * @return a list of resources retrieved from the resource allocations given |
| 301 | */ |
| 302 | private static List<Resource> resourcesFromAllocations(Collection<ResourceAllocation> rAs) { |
| 303 | return rAs.stream() |
| 304 | .map(ResourceAllocation::resource) |
| 305 | .collect(Collectors.toList()); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Creates a list of continuous bandwidth resources given a list of connect |
| 310 | * points and a bandwidth. |
| 311 | * |
| 312 | * @param cps the list of connect points |
| 313 | * @param bw the bandwidth expressed as a double |
| 314 | * @return the list of resources |
| 315 | */ |
| 316 | private static List<Resource> resources(List<ConnectPoint> cps, double bw) { |
| 317 | return cps.stream() |
| 318 | // Make sure the element id is a valid device id |
| 319 | .filter(cp -> cp.elementId() instanceof DeviceId) |
| 320 | // Create a continuous resource for each CP we're going through |
| 321 | .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), |
| 322 | Bandwidth.class).resource(bw)) |
| 323 | .collect(Collectors.toList()); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Returns a list of resource ids given a list of resources. |
| 328 | * |
| 329 | * @param resources the list of resources |
| 330 | * @return the list of resource ids retrieved from the resources given |
| 331 | */ |
| 332 | private static List<ResourceId> resourceIds(List<Resource> resources) { |
| 333 | return resources.stream() |
| 334 | .map(Resource::id) |
| 335 | .collect(Collectors.toList()); |
| 336 | } |
| 337 | |
| 338 | /** |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 339 | * Edge-weight capable of evaluating link cost using a set of constraints. |
| 340 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 341 | protected class ConstraintBasedLinkWeigher extends DefaultEdgeWeigher<TopologyVertex, TopologyEdge> |
| 342 | implements LinkWeigher { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 343 | |
| 344 | private final List<Constraint> constraints; |
| 345 | |
| 346 | /** |
| 347 | * Creates a new edge-weight function capable of evaluating links |
| 348 | * on the basis of the specified constraints. |
| 349 | * |
| 350 | * @param constraints path constraints |
| 351 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 352 | ConstraintBasedLinkWeigher(List<Constraint> constraints) { |
Sho SHIMIZU | 5653151 | 2014-11-10 15:27:49 -0800 | [diff] [blame] | 353 | if (constraints == null) { |
| 354 | this.constraints = Collections.emptyList(); |
| 355 | } else { |
| 356 | this.constraints = ImmutableList.copyOf(constraints); |
| 357 | } |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 361 | public Weight weight(TopologyEdge edge) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 362 | |
| 363 | // iterate over all constraints in order and return the weight of |
| 364 | // the first one with fast fail over the first failure |
Yuta HIGUCHI | e37560f | 2017-02-02 19:53:26 -0800 | [diff] [blame^] | 365 | Iterator<Constraint> it = constraints.stream() |
| 366 | .filter(c -> !(c instanceof MarkerConstraint)) |
| 367 | .filter(c -> !(c instanceof PathViabilityConstraint)) |
| 368 | .iterator(); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 369 | |
Yuta HIGUCHI | a0b0a13 | 2016-09-16 15:47:49 -0700 | [diff] [blame] | 370 | if (!it.hasNext()) { |
Yuta HIGUCHI | fa9ee8c | 2017-02-02 20:00:14 -0800 | [diff] [blame] | 371 | return DEFAULT_HOP_WEIGHT; |
Yuta HIGUCHI | a0b0a13 | 2016-09-16 15:47:49 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 374 | double cost = it.next().cost(edge.link(), resourceService::isAvailable); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 375 | while (it.hasNext() && cost > 0) { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 376 | if (it.next().cost(edge.link(), resourceService::isAvailable) < 0) { |
Yuta HIGUCHI | e37560f | 2017-02-02 19:53:26 -0800 | [diff] [blame^] | 377 | // TODO shouldn't this be non-viable? |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 378 | cost = -1; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 379 | } |
| 380 | } |
Yuta HIGUCHI | e37560f | 2017-02-02 19:53:26 -0800 | [diff] [blame^] | 381 | return ScalarWeight.toWeight(cost); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 382 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | } |