blob: 43b8e4b129dea985aa7be201524f4bbd219f1ff3 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.Link;
Brian O'Connor6de2e202015-05-21 14:30:41 -070020import org.onosproject.net.resource.link.BandwidthResource;
21import org.onosproject.net.resource.link.BandwidthResourceRequest;
22import org.onosproject.net.resource.link.LinkResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.resource.ResourceRequest;
24import org.onosproject.net.resource.ResourceType;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080025
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Constraint that evaluates links based on available bandwidths.
33 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040034@Beta
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080035public class BandwidthConstraint extends BooleanConstraint {
36
Sho SHIMIZU63feca72015-05-07 10:44:25 -070037 private final BandwidthResource bandwidth;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080038
39 /**
40 * Creates a new bandwidth constraint.
41 *
42 * @param bandwidth required bandwidth
43 */
Sho SHIMIZU63feca72015-05-07 10:44:25 -070044 public BandwidthConstraint(BandwidthResource bandwidth) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080045 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
46 }
47
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080048 // Constructor for serialization
49 private BandwidthConstraint() {
50 this.bandwidth = null;
51 }
52
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080053 @Override
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()) {
59 return true;
60 }
61 }
62 }
63 return false;
64 }
65
66 /**
67 * Returns the bandwidth required by this constraint.
68 *
69 * @return required bandwidth
70 */
Sho SHIMIZU63feca72015-05-07 10:44:25 -070071 public BandwidthResource bandwidth() {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080072 return bandwidth;
73 }
74
75 @Override
76 public int hashCode() {
77 return Objects.hash(bandwidth);
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj) {
83 return true;
84 }
85 if (obj == null || getClass() != obj.getClass()) {
86 return false;
87 }
88 final BandwidthConstraint other = (BandwidthConstraint) obj;
89 return Objects.equals(this.bandwidth, other.bandwidth);
90 }
91
92 @Override
93 public String toString() {
94 return toStringHelper(this).add("bandwidth", bandwidth).toString();
95 }
96}