blob: 63b3978203478cfa8b90c24cef3c97e32ff129f8 [file] [log] [blame]
Thomas Vachuskaedc944c2014-11-04 15:42:25 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.Link;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080023import org.onosproject.net.intent.ResourceContext;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080024import org.onosproject.net.resource.Resources;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080025
26import java.util.Objects;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080027import java.util.stream.Stream;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080028
29import static com.google.common.base.MoreObjects.toStringHelper;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Constraint that evaluates links based on available bandwidths.
34 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040035@Beta
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070036public final class BandwidthConstraint extends BooleanConstraint {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080037
Sho SHIMIZUa88db492015-11-23 13:21:04 -080038 private final Bandwidth bandwidth;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080039
40 /**
41 * Creates a new bandwidth constraint.
42 *
43 * @param bandwidth required bandwidth
44 */
Sho SHIMIZUa88db492015-11-23 13:21:04 -080045 public BandwidthConstraint(Bandwidth bandwidth) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080046 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
47 }
48
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070049 /**
50 * Creates a new bandwidth constraint.
51 *
52 * @param v required amount of bandwidth
53 * @param unit {@link DataRateUnit} of {@code v}
54 * @return {@link BandwidthConstraint} instance with given bandwidth requirement
55 */
56 public static BandwidthConstraint of(double v, DataRateUnit unit) {
Sho SHIMIZUa88db492015-11-23 13:21:04 -080057 return new BandwidthConstraint(Bandwidth.of(v, unit));
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070058 }
59
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080060 // Constructor for serialization
61 private BandwidthConstraint() {
62 this.bandwidth = null;
63 }
64
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080065 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080066 public boolean isValid(Link link, ResourceContext context) {
67 return Stream.of(link.src(), link.dst())
68 .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).resource(bandwidth.bps()))
69 .allMatch(context::isAvailable);
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080070 }
71
72 /**
73 * Returns the bandwidth required by this constraint.
74 *
75 * @return required bandwidth
76 */
Sho SHIMIZUa88db492015-11-23 13:21:04 -080077 public Bandwidth bandwidth() {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080078 return bandwidth;
79 }
80
81 @Override
82 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070083 return bandwidth.hashCode();
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080084 }
85
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) {
89 return true;
90 }
91 if (obj == null || getClass() != obj.getClass()) {
92 return false;
93 }
94 final BandwidthConstraint other = (BandwidthConstraint) obj;
95 return Objects.equals(this.bandwidth, other.bandwidth);
96 }
97
98 @Override
99 public String toString() {
100 return toStringHelper(this).add("bandwidth", bandwidth).toString();
101 }
102}