blob: 1767a41b9387d726e6afabe59c6c0cd85ead1aad [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
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080046 // Constructor for serialization
47 private AnnotationConstraint() {
48 this.key = "";
49 this.threshold = 0;
50 }
51
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080052 /**
53 * Returns the key of link annotation this constraint designates.
54 * @return key of link annotation
55 */
56 public String key() {
57 return key;
58 }
59
60 /**
61 * Returns the threshold this constraint ensures as link annotated value.
62 *
63 * @return threshold as link annotated value
64 */
65 public double threshold() {
66 return threshold;
67 }
68
69 @Override
70 public boolean isValid(Link link, LinkResourceService resourceService) {
71 double value = getAnnotatedValue(link, key);
72
73 return value <= threshold;
74 }
75
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080076 @Override
77 public double cost(Link link, LinkResourceService resourceService) {
78 if (isValid(link, resourceService)) {
79 return getAnnotatedValue(link, key);
80 } else {
81 return -1;
82 }
83 }
84
85 @Override
86 public int hashCode() {
87 return Objects.hash(key, threshold);
88 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) {
93 return true;
94 }
95
96 if (!(obj instanceof AnnotationConstraint)) {
97 return false;
98 }
99
100 final AnnotationConstraint other = (AnnotationConstraint) obj;
101 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
102 }
103
104 @Override
105 public String toString() {
106 return MoreObjects.toStringHelper(this)
107 .add("key", key)
108 .add("threshold", threshold)
109 .toString();
110 }
111}