blob: f5439efd6a373bd05d74bb00e759ba7c78d61ff3 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080019import com.google.common.base.MoreObjects;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.Link;
Brian O'Connor6de2e202015-05-21 14:30:41 -070021import org.onosproject.net.resource.link.LinkResourceService;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080022
23import java.util.Objects;
24
Brian O'Connorabafb502014-12-02 22:26:20 -080025import static org.onosproject.net.AnnotationKeys.getAnnotatedValue;
Sho SHIMIZU97a64cd2014-11-11 16:31:21 -080026
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080027/**
28 * Constraint that evaluates an arbitrary link annotated value is under the specified threshold.
29 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040030@Beta
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080031public class AnnotationConstraint extends BooleanConstraint {
32
33 private final String key;
34 private final double threshold;
35
36 /**
37 * Creates a new constraint to keep the value for the specified key
38 * of link annotation under the threshold.
39 *
40 * @param key key of link annotation
41 * @param threshold threshold value of the specified link annotation
42 */
43 public AnnotationConstraint(String key, double threshold) {
44 this.key = key;
45 this.threshold = threshold;
46 }
47
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080048 // Constructor for serialization
49 private AnnotationConstraint() {
50 this.key = "";
51 this.threshold = 0;
52 }
53
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080054 /**
55 * Returns the key of link annotation this constraint designates.
56 * @return key of link annotation
57 */
58 public String key() {
59 return key;
60 }
61
62 /**
63 * Returns the threshold this constraint ensures as link annotated value.
64 *
65 * @return threshold as link annotated value
66 */
67 public double threshold() {
68 return threshold;
69 }
70
71 @Override
72 public boolean isValid(Link link, LinkResourceService resourceService) {
73 double value = getAnnotatedValue(link, key);
74
75 return value <= threshold;
76 }
77
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080078 @Override
79 public double cost(Link link, LinkResourceService resourceService) {
80 if (isValid(link, resourceService)) {
81 return getAnnotatedValue(link, key);
82 } else {
83 return -1;
84 }
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hash(key, threshold);
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj) {
95 return true;
96 }
97
98 if (!(obj instanceof AnnotationConstraint)) {
99 return false;
100 }
101
102 final AnnotationConstraint other = (AnnotationConstraint) obj;
103 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
104 }
105
106 @Override
107 public String toString() {
108 return MoreObjects.toStringHelper(this)
109 .add("key", key)
110 .add("threshold", threshold)
111 .toString();
112 }
113}