Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.Link; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.ResourceContext; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 22 | |
| 23 | import java.util.Objects; |
| 24 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import static org.onosproject.net.AnnotationKeys.getAnnotatedValue; |
Sho SHIMIZU | 97a64cd | 2014-11-11 16:31:21 -0800 | [diff] [blame] | 26 | |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Constraint that evaluates an arbitrary link annotated value is under the specified threshold. |
| 29 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 30 | @Beta |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 31 | public 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 SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 48 | // Constructor for serialization |
| 49 | private AnnotationConstraint() { |
| 50 | this.key = ""; |
| 51 | this.threshold = 0; |
| 52 | } |
| 53 | |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 54 | /** |
| 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 71 | // doesn't use LinkResourceService |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 72 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 73 | 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 SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 79 | double value = getAnnotatedValue(link, key); |
| 80 | |
| 81 | return value <= threshold; |
| 82 | } |
| 83 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 84 | // doesn't use LinkResourceService |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 85 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 86 | 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 SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 93 | 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 | } |