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) { |
David Glantz | 0b3d1ab | 2021-09-20 16:11:15 -0500 | [diff] [blame^] | 79 | if (link.annotations().value(key) != null) { |
| 80 | return getAnnotatedValue(link, key) <= threshold; |
| 81 | } else { |
| 82 | return false; |
| 83 | } |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 86 | // doesn't use LinkResourceService |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 87 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 88 | 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 SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 95 | 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 | } |