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;
19 import org.onosproject.net.Link;
20 import org.onosproject.net.resource.link.BandwidthResource;
21 import org.onosproject.net.resource.link.BandwidthResourceRequest;
22 import org.onosproject.net.resource.link.LinkResourceService;
23 import org.onosproject.net.resource.ResourceRequest;
24 import org.onosproject.net.resource.ResourceType;
26 import java.util.Objects;
28 import static com.google.common.base.MoreObjects.toStringHelper;
29 import static com.google.common.base.Preconditions.checkNotNull;
32 * Constraint that evaluates links based on available bandwidths.
35 public class BandwidthConstraint extends BooleanConstraint {
37 private final BandwidthResource bandwidth;
40 * Creates a new bandwidth constraint.
42 * @param bandwidth required bandwidth
44 public BandwidthConstraint(BandwidthResource bandwidth) {
45 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
48 // Constructor for serialization
49 private BandwidthConstraint() {
50 this.bandwidth = null;
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()) {
67 * Returns the bandwidth required by this constraint.
69 * @return required bandwidth
71 public BandwidthResource bandwidth() {
76 public int hashCode() {
77 return Objects.hash(bandwidth);
81 public boolean equals(Object obj) {
85 if (obj == null || getClass() != obj.getClass()) {
88 final BandwidthConstraint other = (BandwidthConstraint) obj;
89 return Objects.equals(this.bandwidth, other.bandwidth);
93 public String toString() {
94 return toStringHelper(this).add("bandwidth", bandwidth).toString();