blob: a2a25b6827afd79804638f259e23ddbb24a75a62 [file] [log] [blame]
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08001/*
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 */
16package org.onlab.onos.net.intent.constraint;
17
18import com.google.common.base.MoreObjects;
19import org.onlab.onos.net.Link;
20import org.onlab.onos.net.Path;
21import org.onlab.onos.net.intent.Constraint;
22import org.onlab.onos.net.resource.LinkResourceService;
23
24import java.time.Duration;
25import java.time.temporal.ChronoUnit;
26import java.util.Objects;
27
Sho SHIMIZUfe129db2014-11-11 14:24:51 -080028import static org.onlab.onos.net.AnnotationKeys.LATENCY;
29
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080030/**
31 * Constraint that evaluates the latency through a path.
32 */
33public class LatencyConstraint implements Constraint {
34
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080035 private final Duration latency;
36
37 /**
38 * Creates a new constraint to keep under specified latency through a path.
39 * @param latency latency to be kept
40 */
41 public LatencyConstraint(Duration latency) {
42 this.latency = latency;
43 }
44
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080045 // Constructor for serialization
46 private LatencyConstraint() {
47 this.latency = Duration.ZERO;
48 }
49
Sho SHIMIZU412a2392014-11-10 18:11:48 -080050 public Duration latency() {
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080051 return latency;
52 }
53
54 @Override
55 public double cost(Link link, LinkResourceService resourceService) {
Sho SHIMIZUfe129db2014-11-11 14:24:51 -080056 String value = link.annotations().value(LATENCY);
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080057
58 double latencyInMicroSec;
59 try {
60 latencyInMicroSec = Double.parseDouble(value);
61 } catch (NumberFormatException e) {
62 latencyInMicroSec = 1.0;
63 }
64
65 return latencyInMicroSec;
66 }
67
68 @Override
69 public boolean validate(Path path, LinkResourceService resourceService) {
70 double pathLatency = path.links().stream().mapToDouble(link -> cost(link, resourceService)).sum();
71 return Duration.of((long) pathLatency, ChronoUnit.MICROS).compareTo(latency) <= 0;
72 }
73
74 @Override
75 public int hashCode() {
76 return Objects.hash(latency);
77 }
78
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj) {
82 return true;
83 }
84
85 if (!(obj instanceof LatencyConstraint)) {
86 return false;
87 }
88
89 final LatencyConstraint that = (LatencyConstraint) obj;
90 return Objects.equals(this.latency, that.latency);
91 }
92
93 @Override
94 public String toString() {
95 return MoreObjects.toStringHelper(this)
96 .add("latency", latency)
97 .toString();
98 }
99}