Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.Link; |
| 19 | import org.onosproject.net.resource.Lambda; |
| 20 | import org.onosproject.net.resource.LinkResourceService; |
| 21 | import org.onosproject.net.resource.ResourceRequest; |
| 22 | import org.onosproject.net.resource.ResourceType; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 23 | |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * Constraint that evaluates links based on available lambda. |
| 30 | */ |
| 31 | public class LambdaConstraint extends BooleanConstraint { |
| 32 | |
| 33 | private final Lambda lambda; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new optical lambda constraint. |
| 37 | * |
| 38 | * @param lambda optional lambda to indicate a specific lambda |
| 39 | */ |
| 40 | public LambdaConstraint(Lambda lambda) { |
| 41 | this.lambda = lambda; |
| 42 | } |
| 43 | |
Thomas Vachuska | 7d0032b | 2014-11-04 17:39:57 -0800 | [diff] [blame] | 44 | // Constructor for serialization |
| 45 | private LambdaConstraint() { |
| 46 | this.lambda = null; |
| 47 | } |
| 48 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 49 | @Override |
| 50 | public boolean isValid(Link link, LinkResourceService resourceService) { |
| 51 | for (ResourceRequest request : resourceService.getAvailableResources(link)) { |
| 52 | if (request.type() == ResourceType.LAMBDA) { |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the lambda required by this constraint. |
| 61 | * |
| 62 | * @return required lambda |
| 63 | */ |
| 64 | public Lambda lambda() { |
| 65 | return lambda; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public int hashCode() { |
| 70 | return Objects.hash(lambda); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean equals(Object obj) { |
| 75 | if (this == obj) { |
| 76 | return true; |
| 77 | } |
| 78 | if (obj == null || getClass() != obj.getClass()) { |
| 79 | return false; |
| 80 | } |
| 81 | final LambdaConstraint other = (LambdaConstraint) obj; |
| 82 | return Objects.equals(this.lambda, other.lambda); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return toStringHelper(this).add("lambda", lambda).toString(); |
| 88 | } |
| 89 | } |