Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -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; |
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; |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 19 | |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 20 | import org.onlab.util.Bandwidth; |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 21 | import org.onlab.util.DataRateUnit; |
Kavitha Alagesan | b0c3a1a | 2016-12-06 10:47:09 +0530 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.Link; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.ResourceContext; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 25 | import org.onosproject.net.resource.Resources; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 26 | |
| 27 | import java.util.Objects; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 28 | import java.util.stream.Stream; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 29 | |
| 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Constraint that evaluates links based on available bandwidths. |
| 35 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 36 | @Beta |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 37 | public final class BandwidthConstraint extends BooleanConstraint { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 38 | |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 39 | private final Bandwidth bandwidth; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Creates a new bandwidth constraint. |
| 43 | * |
| 44 | * @param bandwidth required bandwidth |
| 45 | */ |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 46 | public BandwidthConstraint(Bandwidth bandwidth) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 47 | this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null"); |
| 48 | } |
| 49 | |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Creates a new bandwidth constraint. |
| 52 | * |
| 53 | * @param v required amount of bandwidth |
| 54 | * @param unit {@link DataRateUnit} of {@code v} |
| 55 | * @return {@link BandwidthConstraint} instance with given bandwidth requirement |
| 56 | */ |
| 57 | public static BandwidthConstraint of(double v, DataRateUnit unit) { |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 58 | return new BandwidthConstraint(Bandwidth.of(v, unit)); |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Thomas Vachuska | 7d0032b | 2014-11-04 17:39:57 -0800 | [diff] [blame] | 61 | // Constructor for serialization |
| 62 | private BandwidthConstraint() { |
| 63 | this.bandwidth = null; |
| 64 | } |
| 65 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 66 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 67 | public boolean isValid(Link link, ResourceContext context) { |
| 68 | return Stream.of(link.src(), link.dst()) |
Kavitha Alagesan | b0c3a1a | 2016-12-06 10:47:09 +0530 | [diff] [blame] | 69 | .filter(cp -> cp.elementId() instanceof DeviceId) |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 70 | .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).resource(bandwidth.bps())) |
| 71 | .allMatch(context::isAvailable); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the bandwidth required by this constraint. |
| 76 | * |
| 77 | * @return required bandwidth |
| 78 | */ |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 79 | public Bandwidth bandwidth() { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 80 | return bandwidth; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 85 | return bandwidth.hashCode(); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object obj) { |
| 90 | if (this == obj) { |
| 91 | return true; |
| 92 | } |
| 93 | if (obj == null || getClass() != obj.getClass()) { |
| 94 | return false; |
| 95 | } |
| 96 | final BandwidthConstraint other = (BandwidthConstraint) obj; |
| 97 | return Objects.equals(this.bandwidth, other.bandwidth); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String toString() { |
| 102 | return toStringHelper(this).add("bandwidth", bandwidth).toString(); |
| 103 | } |
| 104 | } |