blob: 3e47bfad18c0aa767b1f24ad8a6a210cb7227eec [file] [log] [blame]
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08003 *
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;
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070019
Sho SHIMIZUa88db492015-11-23 13:21:04 -080020import org.onlab.util.Bandwidth;
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070021import org.onlab.util.DataRateUnit;
Kavitha Alagesanb0c3a1a2016-12-06 10:47:09 +053022import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.Link;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080024import org.onosproject.net.intent.ResourceContext;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080025import org.onosproject.net.resource.Resources;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080026
27import java.util.Objects;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080028import java.util.stream.Stream;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080029
30import static com.google.common.base.MoreObjects.toStringHelper;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Constraint that evaluates links based on available bandwidths.
35 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040036@Beta
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070037public final class BandwidthConstraint extends BooleanConstraint {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080038
Sho SHIMIZUa88db492015-11-23 13:21:04 -080039 private final Bandwidth bandwidth;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080040
41 /**
42 * Creates a new bandwidth constraint.
43 *
44 * @param bandwidth required bandwidth
45 */
Sho SHIMIZUa88db492015-11-23 13:21:04 -080046 public BandwidthConstraint(Bandwidth bandwidth) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080047 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
48 }
49
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070050 /**
51 * Creates a new bandwidth constraint.
52 *
53 * @param v required amount of bandwidth
54 * @param unit {@link DataRateUnit} of {@code v}
55 * @return {@link BandwidthConstraint} instance with given bandwidth requirement
56 */
57 public static BandwidthConstraint of(double v, DataRateUnit unit) {
Sho SHIMIZUa88db492015-11-23 13:21:04 -080058 return new BandwidthConstraint(Bandwidth.of(v, unit));
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070059 }
60
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080061 // Constructor for serialization
62 private BandwidthConstraint() {
63 this.bandwidth = null;
64 }
65
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080066 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080067 public boolean isValid(Link link, ResourceContext context) {
68 return Stream.of(link.src(), link.dst())
Kavitha Alagesanb0c3a1a2016-12-06 10:47:09 +053069 .filter(cp -> cp.elementId() instanceof DeviceId)
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080070 .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).resource(bandwidth.bps()))
71 .allMatch(context::isAvailable);
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080072 }
73
74 /**
75 * Returns the bandwidth required by this constraint.
76 *
77 * @return required bandwidth
78 */
Sho SHIMIZUa88db492015-11-23 13:21:04 -080079 public Bandwidth bandwidth() {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080080 return bandwidth;
81 }
82
83 @Override
84 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070085 return bandwidth.hashCode();
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080086 }
87
88 @Override
89 public boolean equals(Object obj) {
90 if (this == obj) {
91 return true;
92 }
93 if (obj == null || getClass() != obj.getClass()) {
94 return false;
95 }
96 final BandwidthConstraint other = (BandwidthConstraint) obj;
97 return Objects.equals(this.bandwidth, other.bandwidth);
98 }
99
100 @Override
101 public String toString() {
102 return toStringHelper(this).add("bandwidth", bandwidth).toString();
103 }
104}