blob: fc082142eb3863af8d1e02c13e5bb37f3ad7b379 [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
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080046 // Constructor for serialization
47 private BandwidthConstraint() {
48 this.bandwidth = null;
49 }
50
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080051 @Override
52 public boolean isValid(Link link, LinkResourceService resourceService) {
53 for (ResourceRequest request : resourceService.getAvailableResources(link)) {
54 if (request.type() == ResourceType.BANDWIDTH) {
55 BandwidthResourceRequest brr = (BandwidthResourceRequest) request;
56 if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) {
57 return true;
58 }
59 }
60 }
61 return false;
62 }
63
64 /**
65 * Returns the bandwidth required by this constraint.
66 *
67 * @return required bandwidth
68 */
69 public Bandwidth bandwidth() {
70 return bandwidth;
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hash(bandwidth);
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83 if (obj == null || getClass() != obj.getClass()) {
84 return false;
85 }
86 final BandwidthConstraint other = (BandwidthConstraint) obj;
87 return Objects.equals(this.bandwidth, other.bandwidth);
88 }
89
90 @Override
91 public String toString() {
92 return toStringHelper(this).add("bandwidth", bandwidth).toString();
93 }
94}