54eb4ea55546858d41cf01381b20dc75a034a515
[onosfw.git] /
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  */
16 package org.onosproject.net.intent.constraint;
17
18 import com.google.common.annotations.Beta;
19 import com.google.common.base.MoreObjects;
20 import org.onosproject.net.Link;
21 import org.onosproject.net.Path;
22 import org.onosproject.net.intent.Constraint;
23 import org.onosproject.net.resource.link.LinkResourceService;
24
25 import java.time.Duration;
26 import java.time.temporal.ChronoUnit;
27 import java.util.Objects;
28
29 import static org.onosproject.net.AnnotationKeys.LATENCY;
30 import static org.onosproject.net.AnnotationKeys.getAnnotatedValue;
31
32 /**
33  * Constraint that evaluates the latency through a path.
34  */
35 @Beta
36 public class LatencyConstraint implements Constraint {
37
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
48     // Constructor for serialization
49     private LatencyConstraint() {
50         this.latency = Duration.ZERO;
51     }
52
53     public Duration latency() {
54         return latency;
55     }
56
57     @Override
58     public double cost(Link link, LinkResourceService resourceService) {
59         return getAnnotatedValue(link, LATENCY);
60     }
61
62     @Override
63     public boolean validate(Path path, LinkResourceService resourceService) {
64         double pathLatency = path.links().stream().mapToDouble(link -> cost(link, resourceService)).sum();
65         return Duration.of((long) pathLatency, ChronoUnit.MICROS).compareTo(latency) <= 0;
66     }
67
68     @Override
69     public int hashCode() {
70         return Objects.hash(latency);
71     }
72
73     @Override
74     public boolean equals(Object obj) {
75         if (this == obj) {
76             return true;
77         }
78
79         if (!(obj instanceof LatencyConstraint)) {
80             return false;
81         }
82
83         final LatencyConstraint that = (LatencyConstraint) obj;
84         return Objects.equals(this.latency, that.latency);
85     }
86
87     @Override
88     public String toString() {
89         return MoreObjects.toStringHelper(this)
90                 .add("latency", latency)
91                 .toString();
92     }
93 }