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