blob: 94ad04d908f442b8ea49977c27a945a499812d1f [file] [log] [blame]
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080019import com.google.common.base.MoreObjects;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.Link;
21import org.onosproject.net.Path;
22import org.onosproject.net.intent.Constraint;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080023import org.onosproject.net.intent.ResourceContext;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080024
25import java.time.Duration;
26import java.time.temporal.ChronoUnit;
27import java.util.Objects;
28
Brian O'Connorabafb502014-12-02 22:26:20 -080029import static org.onosproject.net.AnnotationKeys.LATENCY;
30import static org.onosproject.net.AnnotationKeys.getAnnotatedValue;
Sho SHIMIZUfe129db2014-11-11 14:24:51 -080031
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080032/**
33 * Constraint that evaluates the latency through a path.
34 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040035@Beta
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080036public class LatencyConstraint implements Constraint {
37
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080038 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 SHIMIZUcd4bac82014-11-11 17:07:25 -080048 // Constructor for serialization
49 private LatencyConstraint() {
50 this.latency = Duration.ZERO;
51 }
52
Sho SHIMIZU412a2392014-11-10 18:11:48 -080053 public Duration latency() {
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080054 return latency;
55 }
56
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080057 // doesn't use LinkResourceService
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080058 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080059 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) {
Sho SHIMIZU97a64cd2014-11-11 16:31:21 -080065 return getAnnotatedValue(link, LATENCY);
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080066 }
67
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080068 // doesn't use LinkResourceService
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080069 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080070 public boolean validate(Path path, ResourceContext context) {
71 // explicitly call a method not depending on LinkResourceService
72 return validate(path);
73 }
74
75 private boolean validate(Path path) {
76 double pathLatency = path.links().stream().mapToDouble(this::cost).sum();
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080077 return Duration.of((long) pathLatency, ChronoUnit.MICROS).compareTo(latency) <= 0;
78 }
79
80 @Override
81 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070082 return latency.hashCode();
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080083 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90
91 if (!(obj instanceof LatencyConstraint)) {
92 return false;
93 }
94
95 final LatencyConstraint that = (LatencyConstraint) obj;
96 return Objects.equals(this.latency, that.latency);
97 }
98
99 @Override
100 public String toString() {
101 return MoreObjects.toStringHelper(this)
102 .add("latency", latency)
103 .toString();
104 }
105}