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