2 * Copyright 2014 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.intent.constraint;
18 import com.google.common.annotations.Beta;
20 import org.onlab.util.DataRateUnit;
21 import org.onosproject.net.Link;
22 import org.onosproject.net.resource.link.BandwidthResource;
23 import org.onosproject.net.resource.link.BandwidthResourceRequest;
24 import org.onosproject.net.resource.link.LinkResourceService;
25 import org.onosproject.net.resource.ResourceRequest;
26 import org.onosproject.net.resource.ResourceType;
28 import java.util.Objects;
30 import static com.google.common.base.MoreObjects.toStringHelper;
31 import static com.google.common.base.Preconditions.checkNotNull;
34 * Constraint that evaluates links based on available bandwidths.
37 public final class BandwidthConstraint extends BooleanConstraint {
39 private final BandwidthResource bandwidth;
42 * Creates a new bandwidth constraint.
44 * @param bandwidth required bandwidth
46 public BandwidthConstraint(BandwidthResource bandwidth) {
47 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
51 * Creates a new bandwidth constraint.
53 * @param v required amount of bandwidth
54 * @param unit {@link DataRateUnit} of {@code v}
55 * @return {@link BandwidthConstraint} instance with given bandwidth requirement
57 public static BandwidthConstraint of(double v, DataRateUnit unit) {
58 return new BandwidthConstraint(BandwidthResource.of(v, unit));
61 // Constructor for serialization
62 private BandwidthConstraint() {
63 this.bandwidth = null;
67 public boolean isValid(Link link, LinkResourceService resourceService) {
68 for (ResourceRequest request : resourceService.getAvailableResources(link)) {
69 if (request.type() == ResourceType.BANDWIDTH) {
70 BandwidthResourceRequest brr = (BandwidthResourceRequest) request;
71 if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) {
80 * Returns the bandwidth required by this constraint.
82 * @return required bandwidth
84 public BandwidthResource bandwidth() {
89 public int hashCode() {
90 return Objects.hash(bandwidth);
94 public boolean equals(Object obj) {
98 if (obj == null || getClass() != obj.getClass()) {
101 final BandwidthConstraint other = (BandwidthConstraint) obj;
102 return Objects.equals(this.bandwidth, other.bandwidth);
106 public String toString() {
107 return toStringHelper(this).add("bandwidth", bandwidth).toString();