blob: f5a9870c8450c0883c359346b46d0968da2138d3 [file] [log] [blame]
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -08003 *
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;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080021import org.onosproject.net.intent.ResourceContext;
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
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080071 // doesn't use LinkResourceService
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080072 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080073 public boolean isValid(Link link, ResourceContext context) {
74 // explicitly call a method not depending on LinkResourceService
75 return isValid(link);
76 }
77
78 private boolean isValid(Link link) {
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080079 double value = getAnnotatedValue(link, key);
80
81 return value <= threshold;
82 }
83
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080084 // doesn't use LinkResourceService
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080085 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080086 public double cost(Link link, ResourceContext context) {
87 // explicitly call a method not depending on LinkResourceService
88 return cost(link);
89 }
90
91 private double cost(Link link) {
92 if (isValid(link)) {
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080093 return getAnnotatedValue(link, key);
94 } else {
95 return -1;
96 }
97 }
98
99 @Override
100 public int hashCode() {
101 return Objects.hash(key, threshold);
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109
110 if (!(obj instanceof AnnotationConstraint)) {
111 return false;
112 }
113
114 final AnnotationConstraint other = (AnnotationConstraint) obj;
115 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
116 }
117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(this)
121 .add("key", key)
122 .add("threshold", threshold)
123 .toString();
124 }
125}