Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -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 | */ |
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 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.Link; |
| 21 | import org.onosproject.net.Path; |
| 22 | import org.onosproject.net.intent.Constraint; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 23 | import org.onosproject.net.intent.ResourceContext; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 24 | |
| 25 | import java.time.Duration; |
| 26 | import java.time.temporal.ChronoUnit; |
| 27 | import java.util.Objects; |
| 28 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import static org.onosproject.net.AnnotationKeys.LATENCY; |
| 30 | import static org.onosproject.net.AnnotationKeys.getAnnotatedValue; |
Sho SHIMIZU | fe129db | 2014-11-11 14:24:51 -0800 | [diff] [blame] | 31 | |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Constraint that evaluates the latency through a path. |
| 34 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 35 | @Beta |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 36 | public class LatencyConstraint implements Constraint { |
| 37 | |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 38 | private final Duration latency; |
| 39 | |
| 40 | /** |
| 41 | * Creates a new constraint to keep under specified latency through a path. |
| 42 | * @param latency latency to be kept |
| 43 | */ |
| 44 | public LatencyConstraint(Duration latency) { |
| 45 | this.latency = latency; |
| 46 | } |
| 47 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 48 | // Constructor for serialization |
| 49 | private LatencyConstraint() { |
| 50 | this.latency = Duration.ZERO; |
| 51 | } |
| 52 | |
Sho SHIMIZU | 412a239 | 2014-11-10 18:11:48 -0800 | [diff] [blame] | 53 | public Duration latency() { |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 54 | return latency; |
| 55 | } |
| 56 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 57 | // doesn't use LinkResourceService |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 58 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 59 | public double cost(Link link, ResourceContext context) { |
| 60 | // explicitly call a method not depending on LinkResourceService |
| 61 | return cost(link); |
| 62 | } |
| 63 | |
| 64 | private double cost(Link link) { |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 65 | //Check only links, not EdgeLinks |
| 66 | if (link.type() != Link.Type.EDGE) { |
| 67 | return link.annotations().value(LATENCY) != null ? getAnnotatedValue(link, LATENCY) : 0; |
| 68 | } else { |
| 69 | return 0; |
| 70 | } |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 73 | // doesn't use LinkResourceService |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 74 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 75 | public boolean validate(Path path, ResourceContext context) { |
| 76 | // explicitly call a method not depending on LinkResourceService |
| 77 | return validate(path); |
| 78 | } |
| 79 | |
| 80 | private boolean validate(Path path) { |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 81 | //Guarantee all the latency units in ONOS is nanoseconds. |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 82 | double pathLatency = path.links().stream().mapToDouble(this::cost).sum(); |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 83 | return Duration.of((long) pathLatency, ChronoUnit.NANOS).compareTo(latency) <= 0; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 88 | return latency.hashCode(); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean equals(Object obj) { |
| 93 | if (this == obj) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | if (!(obj instanceof LatencyConstraint)) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | final LatencyConstraint that = (LatencyConstraint) obj; |
| 102 | return Objects.equals(this.latency, that.latency); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public String toString() { |
| 107 | return MoreObjects.toStringHelper(this) |
| 108 | .add("latency", latency) |
| 109 | .toString(); |
| 110 | } |
| 111 | } |