Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.Link; |
| 20 | import org.onosproject.net.Path; |
| 21 | import org.onosproject.net.intent.Constraint; |
| 22 | import org.onosproject.net.resource.LinkResourceService; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 23 | |
| 24 | import java.time.Duration; |
| 25 | import java.time.temporal.ChronoUnit; |
| 26 | import java.util.Objects; |
| 27 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import static org.onosproject.net.AnnotationKeys.LATENCY; |
| 29 | import static org.onosproject.net.AnnotationKeys.getAnnotatedValue; |
Sho SHIMIZU | fe129db | 2014-11-11 14:24:51 -0800 | [diff] [blame] | 30 | |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Constraint that evaluates the latency through a path. |
| 33 | */ |
| 34 | public class LatencyConstraint implements Constraint { |
| 35 | |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 36 | private final Duration latency; |
| 37 | |
| 38 | /** |
| 39 | * Creates a new constraint to keep under specified latency through a path. |
| 40 | * @param latency latency to be kept |
| 41 | */ |
| 42 | public LatencyConstraint(Duration latency) { |
| 43 | this.latency = latency; |
| 44 | } |
| 45 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 46 | // Constructor for serialization |
| 47 | private LatencyConstraint() { |
| 48 | this.latency = Duration.ZERO; |
| 49 | } |
| 50 | |
Sho SHIMIZU | 412a239 | 2014-11-10 18:11:48 -0800 | [diff] [blame] | 51 | public Duration latency() { |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 52 | return latency; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public double cost(Link link, LinkResourceService resourceService) { |
Sho SHIMIZU | 97a64cd | 2014-11-11 16:31:21 -0800 | [diff] [blame] | 57 | return getAnnotatedValue(link, LATENCY); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean validate(Path path, LinkResourceService resourceService) { |
| 62 | double pathLatency = path.links().stream().mapToDouble(link -> cost(link, resourceService)).sum(); |
| 63 | return Duration.of((long) pathLatency, ChronoUnit.MICROS).compareTo(latency) <= 0; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int hashCode() { |
| 68 | return Objects.hash(latency); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public boolean equals(Object obj) { |
| 73 | if (this == obj) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | if (!(obj instanceof LatencyConstraint)) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | final LatencyConstraint that = (LatencyConstraint) obj; |
| 82 | return Objects.equals(this.latency, that.latency); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return MoreObjects.toStringHelper(this) |
| 88 | .add("latency", latency) |
| 89 | .toString(); |
| 90 | } |
| 91 | } |