David Glantz | 05c6f43 | 2020-03-19 14:56:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.net.intent.constraint; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import org.onosproject.net.AnnotationKeys; |
| 20 | import org.onosproject.net.Link; |
| 21 | import org.onosproject.net.intent.ResourceContext; |
| 22 | |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 26 | |
| 27 | /** |
| 28 | * Constraint that evaluates links based on the metered flag. |
| 29 | */ |
| 30 | @Beta |
| 31 | public class MeteredConstraint extends BooleanConstraint { |
| 32 | |
| 33 | private final boolean useMetered; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new constraint for requesting connectivity using or avoiding |
| 37 | * the metered links. |
| 38 | * |
| 39 | * @param metered indicates whether a metered link can be used. |
| 40 | */ |
| 41 | public MeteredConstraint(boolean metered) { |
| 42 | this.useMetered = metered; |
| 43 | } |
| 44 | |
| 45 | // Constructor for serialization |
| 46 | private MeteredConstraint() { |
| 47 | this.useMetered = false; |
| 48 | } |
| 49 | |
| 50 | // doesn't use LinkResourceService |
| 51 | @Override |
| 52 | public boolean isValid(Link link, ResourceContext context) { |
| 53 | // explicitly call a method not depending on LinkResourceService |
| 54 | return isValid(link); |
| 55 | } |
| 56 | |
| 57 | private boolean isValid(Link link) { |
| 58 | return !isMeteredLink(link) || useMetered; |
| 59 | } |
| 60 | |
| 61 | private boolean isMeteredLink(Link link) { |
| 62 | return link.annotations().keys().contains(AnnotationKeys.METERED) |
| 63 | && Boolean.valueOf(link.annotations().value(AnnotationKeys.METERED)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Indicates if the constraint is metered or not. |
| 68 | * |
| 69 | * @return true if metered |
| 70 | */ |
| 71 | public boolean isUseMetered() { |
| 72 | return useMetered; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int hashCode() { |
| 77 | return Objects.hash(useMetered); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object obj) { |
| 82 | if (this == obj) { |
| 83 | return true; |
| 84 | } |
| 85 | if (obj == null || getClass() != obj.getClass()) { |
| 86 | return false; |
| 87 | } |
| 88 | final MeteredConstraint other = (MeteredConstraint) obj; |
| 89 | return Objects.equals(this.useMetered, other.useMetered); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String toString() { |
| 94 | return toStringHelper(this) |
| 95 | .add("metered", useMetered) |
| 96 | .toString(); |
| 97 | } |
| 98 | } |