blob: fe91de332d9072330fe3658dbd1915254c3a7947 [file] [log] [blame]
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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) {
David Glantzff631982021-09-20 16:11:15 -050079 if (link.annotations().value(key) != null) {
80 return getAnnotatedValue(link, key) <= threshold;
81 } else {
82 return false;
83 }
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080084 }
85
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080086 // doesn't use LinkResourceService
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080087 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080088 public double cost(Link link, ResourceContext context) {
89 // explicitly call a method not depending on LinkResourceService
90 return cost(link);
91 }
92
93 private double cost(Link link) {
94 if (isValid(link)) {
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080095 return getAnnotatedValue(link, key);
96 } else {
97 return -1;
98 }
99 }
100
101 @Override
102 public int hashCode() {
103 return Objects.hash(key, threshold);
104 }
105
106 @Override
107 public boolean equals(Object obj) {
108 if (this == obj) {
109 return true;
110 }
111
112 if (!(obj instanceof AnnotationConstraint)) {
113 return false;
114 }
115
116 final AnnotationConstraint other = (AnnotationConstraint) obj;
117 return Objects.equals(this.key, other.key) && Objects.equals(this.threshold, other.threshold);
118 }
119
120 @Override
121 public String toString() {
122 return MoreObjects.toStringHelper(this)
123 .add("key", key)
124 .add("threshold", threshold)
125 .toString();
126 }
127}