blob: ac76303d7e9605f4916841edd6cdf642cc975103 [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
24/**
25 * Constraint that evaluates an arbitrary link annotated value is under the specified threshold.
26 */
27public class AnnotationConstraint extends BooleanConstraint {
28
29 private final String key;
30 private final double threshold;
31
32 /**
33 * Creates a new constraint to keep the value for the specified key
34 * of link annotation under the threshold.
35 *
36 * @param key key of link annotation
37 * @param threshold threshold value of the specified link annotation
38 */
39 public AnnotationConstraint(String key, double threshold) {
40 this.key = key;
41 this.threshold = threshold;
42 }
43
44 /**
45 * Returns the key of link annotation this constraint designates.
46 * @return key of link annotation
47 */
48 public String key() {
49 return key;
50 }
51
52 /**
53 * Returns the threshold this constraint ensures as link annotated value.
54 *
55 * @return threshold as link annotated value
56 */
57 public double threshold() {
58 return threshold;
59 }
60
61 @Override
62 public boolean isValid(Link link, LinkResourceService resourceService) {
63 double value = getAnnotatedValue(link, key);
64
65 return value <= threshold;
66 }
67
68 /**
69 * Returns the annotated value of the specified link. The annotated value
70 * is expected to be String that can be parsed as double. If parsing fails,
71 * the returned value will be 1.0.
72 *
73 * @param link link whose annotated value is obtained
74 * @param key key of link annotation
75 * @return double value of link annotation for the specified key
76 */
77 private double getAnnotatedValue(Link link, String key) {
78 double value;
79 try {
80 value = Double.parseDouble(link.annotations().value(key));
81 } catch (NumberFormatException e) {
82 value = 1.0;
83 }
84 return value;
85 }
86
87 @Override
88 public double cost(Link link, LinkResourceService resourceService) {
89 if (isValid(link, resourceService)) {
90 return getAnnotatedValue(link, key);
91 } else {
92 return -1;
93 }
94 }
95
96 @Override
97 public int hashCode() {
98 return Objects.hash(key, threshold);
99 }
100
101 @Override
102 public boolean equals(Object obj) {
103 if (this == obj) {
104 return true;
105 }
106
107 if (!(obj instanceof AnnotationConstraint)) {
108 return false;
109 }
110
111 final AnnotationConstraint other = (AnnotationConstraint) obj;
112 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
113 }
114
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(this)
118 .add("key", key)
119 .add("threshold", threshold)
120 .toString();
121 }
122}