blob: 44d6875e2454eb15b8abcf2301813a72e916ba27 [file] [log] [blame]
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08001/*
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 */
16package org.onlab.onos.net.intent.constraint;
17
18import org.onlab.onos.net.Link;
19import org.onlab.onos.net.resource.Bandwidth;
20import org.onlab.onos.net.resource.BandwidthResourceRequest;
21import org.onlab.onos.net.resource.LinkResourceService;
22import org.onlab.onos.net.resource.ResourceRequest;
23import org.onlab.onos.net.resource.ResourceType;
24
25import java.util.Objects;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Constraint that evaluates links based on available bandwidths.
32 */
33public class BandwidthConstraint extends BooleanConstraint {
34
35 private final Bandwidth bandwidth;
36
37 /**
38 * Creates a new bandwidth constraint.
39 *
40 * @param bandwidth required bandwidth
41 */
42 public BandwidthConstraint(Bandwidth bandwidth) {
43 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
44 }
45
46 @Override
47 public boolean isValid(Link link, LinkResourceService resourceService) {
48 for (ResourceRequest request : resourceService.getAvailableResources(link)) {
49 if (request.type() == ResourceType.BANDWIDTH) {
50 BandwidthResourceRequest brr = (BandwidthResourceRequest) request;
51 if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) {
52 return true;
53 }
54 }
55 }
56 return false;
57 }
58
59 /**
60 * Returns the bandwidth required by this constraint.
61 *
62 * @return required bandwidth
63 */
64 public Bandwidth bandwidth() {
65 return bandwidth;
66 }
67
68 @Override
69 public int hashCode() {
70 return Objects.hash(bandwidth);
71 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (this == obj) {
76 return true;
77 }
78 if (obj == null || getClass() != obj.getClass()) {
79 return false;
80 }
81 final BandwidthConstraint other = (BandwidthConstraint) obj;
82 return Objects.equals(this.bandwidth, other.bandwidth);
83 }
84
85 @Override
86 public String toString() {
87 return toStringHelper(this).add("bandwidth", bandwidth).toString();
88 }
89}