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.BandwidthResource; |
| 21 | import org.onosproject.net.resource.link.BandwidthResourceRequest; |
| 22 | import org.onosproject.net.resource.link.LinkResourceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.resource.ResourceRequest; |
| 24 | import org.onosproject.net.resource.ResourceType; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 25 | |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Constraint that evaluates links based on available bandwidths. |
| 33 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame^] | 34 | @Beta |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 35 | public class BandwidthConstraint extends BooleanConstraint { |
| 36 | |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 37 | private final BandwidthResource bandwidth; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Creates a new bandwidth constraint. |
| 41 | * |
| 42 | * @param bandwidth required bandwidth |
| 43 | */ |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 44 | public BandwidthConstraint(BandwidthResource bandwidth) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 45 | this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null"); |
| 46 | } |
| 47 | |
Thomas Vachuska | 7d0032b | 2014-11-04 17:39:57 -0800 | [diff] [blame] | 48 | // Constructor for serialization |
| 49 | private BandwidthConstraint() { |
| 50 | this.bandwidth = null; |
| 51 | } |
| 52 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 53 | @Override |
| 54 | public boolean isValid(Link link, LinkResourceService resourceService) { |
| 55 | for (ResourceRequest request : resourceService.getAvailableResources(link)) { |
| 56 | if (request.type() == ResourceType.BANDWIDTH) { |
| 57 | BandwidthResourceRequest brr = (BandwidthResourceRequest) request; |
| 58 | if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the bandwidth required by this constraint. |
| 68 | * |
| 69 | * @return required bandwidth |
| 70 | */ |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 71 | public BandwidthResource bandwidth() { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 72 | return bandwidth; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int hashCode() { |
| 77 | return Objects.hash(bandwidth); |
| 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 BandwidthConstraint other = (BandwidthConstraint) obj; |
| 89 | return Objects.equals(this.bandwidth, other.bandwidth); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String toString() { |
| 94 | return toStringHelper(this).add("bandwidth", bandwidth).toString(); |
| 95 | } |
| 96 | } |