blob: a093be9e31b58d7665ee61b6d3ccaadfaf4851d3 [file] [log] [blame]
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -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.resource.LinkResourceService;
21
22import java.util.Objects;
23
Sho SHIMIZU97a64cd2014-11-11 16:31:21 -080024import static org.onlab.onos.net.AnnotationKeys.getAnnotatedValue;
25
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080026/**
27 * Constraint that evaluates an arbitrary link annotated value is under the specified threshold.
28 */
29public class AnnotationConstraint extends BooleanConstraint {
30
31 private final String key;
32 private final double threshold;
33
34 /**
35 * Creates a new constraint to keep the value for the specified key
36 * of link annotation under the threshold.
37 *
38 * @param key key of link annotation
39 * @param threshold threshold value of the specified link annotation
40 */
41 public AnnotationConstraint(String key, double threshold) {
42 this.key = key;
43 this.threshold = threshold;
44 }
45
46 /**
47 * Returns the key of link annotation this constraint designates.
48 * @return key of link annotation
49 */
50 public String key() {
51 return key;
52 }
53
54 /**
55 * Returns the threshold this constraint ensures as link annotated value.
56 *
57 * @return threshold as link annotated value
58 */
59 public double threshold() {
60 return threshold;
61 }
62
63 @Override
64 public boolean isValid(Link link, LinkResourceService resourceService) {
65 double value = getAnnotatedValue(link, key);
66
67 return value <= threshold;
68 }
69
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080070 @Override
71 public double cost(Link link, LinkResourceService resourceService) {
72 if (isValid(link, resourceService)) {
73 return getAnnotatedValue(link, key);
74 } else {
75 return -1;
76 }
77 }
78
79 @Override
80 public int hashCode() {
81 return Objects.hash(key, threshold);
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 }
89
90 if (!(obj instanceof AnnotationConstraint)) {
91 return false;
92 }
93
94 final AnnotationConstraint other = (AnnotationConstraint) obj;
95 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
96 }
97
98 @Override
99 public String toString() {
100 return MoreObjects.toStringHelper(this)
101 .add("key", key)
102 .add("threshold", threshold)
103 .toString();
104 }
105}