blob: 59b5ad2247b93eb3e7c6a6268ad37071b1eccd14 [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
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080044 // Constructor for serialization
45 private AnnotationConstraint() {
46 this.key = "";
47 this.threshold = 0;
48 }
49
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080050 /**
51 * Returns the key of link annotation this constraint designates.
52 * @return key of link annotation
53 */
54 public String key() {
55 return key;
56 }
57
58 /**
59 * Returns the threshold this constraint ensures as link annotated value.
60 *
61 * @return threshold as link annotated value
62 */
63 public double threshold() {
64 return threshold;
65 }
66
67 @Override
68 public boolean isValid(Link link, LinkResourceService resourceService) {
69 double value = getAnnotatedValue(link, key);
70
71 return value <= threshold;
72 }
73
74 /**
75 * Returns the annotated value of the specified link. The annotated value
76 * is expected to be String that can be parsed as double. If parsing fails,
77 * the returned value will be 1.0.
78 *
79 * @param link link whose annotated value is obtained
80 * @param key key of link annotation
81 * @return double value of link annotation for the specified key
82 */
83 private double getAnnotatedValue(Link link, String key) {
84 double value;
85 try {
86 value = Double.parseDouble(link.annotations().value(key));
87 } catch (NumberFormatException e) {
88 value = 1.0;
89 }
90 return value;
91 }
92
93 @Override
94 public double cost(Link link, LinkResourceService resourceService) {
95 if (isValid(link, resourceService)) {
96 return getAnnotatedValue(link, key);
97 } else {
98 return -1;
99 }
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(key, threshold);
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112
113 if (!(obj instanceof AnnotationConstraint)) {
114 return false;
115 }
116
117 final AnnotationConstraint other = (AnnotationConstraint) obj;
118 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
119 }
120
121 @Override
122 public String toString() {
123 return MoreObjects.toStringHelper(this)
124 .add("key", key)
125 .add("threshold", threshold)
126 .toString();
127 }
128}