blob: cff097379c7bef1bb70be7bf55f4fe727d3393f9 [file] [log] [blame]
Satish K2eb5d842017-04-04 16:28:37 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Satish K2eb5d842017-04-04 16:28:37 +05303 *
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.onosproject.pce.pceservice.constraint;
17
18import org.onlab.util.Bandwidth;
19import org.onlab.util.DataRateUnit;
20import org.onosproject.net.Link;
21import org.onosproject.net.intent.ResourceContext;
22import org.onosproject.net.intent.constraint.BooleanConstraint;
23import org.onosproject.bandwidthmgr.api.BandwidthMgmtService;
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 pce bandwidths.
32 */
33public final class PceBandwidthConstraint extends BooleanConstraint {
34
35 private final Bandwidth bandwidth;
36
37 /**
38 * Creates a new pce bandwidth constraint.
39 *
40 * @param bandwidth required bandwidth
41 */
42 public PceBandwidthConstraint(Bandwidth bandwidth) {
43 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
44 }
45
46 /**
47 * Creates a new pce bandwidth constraint.
48 *
49 * @param v required amount of bandwidth
50 * @param unit {@link DataRateUnit} of {@code v}
51 * @return {@link PceBandwidthConstraint} instance with given bandwidth requirement
52 */
53 public static PceBandwidthConstraint of(double v, DataRateUnit unit) {
54 return new PceBandwidthConstraint(Bandwidth.of(v, unit));
55 }
56
57 // Constructor for serialization
58 private PceBandwidthConstraint() {
59 this.bandwidth = null;
60 }
61
62 @Override
63 public boolean isValid(Link link, ResourceContext context) {
64 return false;
65 //Do nothing instead using isValidLink needs bandwidthMgmtService to validate link
66 }
67
68 /**
69 * Validates the link based on pce bandwidth constraint.
70 *
71 * @param link to validate pce bandwidth constraint
72 * @param bandwidthMgmtService instance of BandwidthMgmtService
73 * @return true if link satisfies pce bandwidth constraint otherwise false
74 */
75 public boolean isValidLink(Link link, BandwidthMgmtService bandwidthMgmtService) {
76 if (bandwidthMgmtService == null) {
77 return false;
78 }
79
80 return bandwidthMgmtService.isBandwidthAvailable(link, bandwidth.bps());
81
82 }
83
84 /**
85 * Returns the bandwidth required by this constraint.
86 *
87 * @return required bandwidth
88 */
89 public Bandwidth bandwidth() {
90 return bandwidth;
91 }
92
93 @Override
94 public int hashCode() {
95 return bandwidth.hashCode();
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
103 if (obj == null || getClass() != obj.getClass()) {
104 return false;
105 }
106 final PceBandwidthConstraint other = (PceBandwidthConstraint) obj;
107 return Objects.equals(this.bandwidth, other.bandwidth);
108 }
109
110 @Override
111 public String toString() {
112 return toStringHelper(this).add("bandwidth", bandwidth).toString();
113 }
114}