blob: dcd8285771090fc9ea2d98c6a79ae024288facda [file] [log] [blame]
Priyanka Bb6963582016-05-20 20:21:20 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Priyanka Bb6963582016-05-20 20:21:20 +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.onosproject.net.Link;
20import org.onosproject.net.intent.ResourceContext;
21import org.onosproject.net.intent.constraint.BooleanConstraint;
Satish K2eb5d842017-04-04 16:28:37 +053022import org.onosproject.bandwidthmgr.api.BandwidthMgmtService;
Priyanka Bb6963582016-05-20 20:21:20 +053023
24import java.util.List;
25import java.util.Objects;
Priyanka Bb6963582016-05-20 20:21:20 +053026
27import static com.google.common.base.MoreObjects.toStringHelper;
28
29/**
30 * Constraint that evaluates whether links satisfies sharedbandwidth request.
31 */
32public final class SharedBandwidthConstraint extends BooleanConstraint {
33
34 private final List<Link> links;
35 private final Bandwidth sharedBwValue;
36 private final Bandwidth requestBwValue;
37 //temporary variable declared to hold changed bandwidth value
38 private Bandwidth changedBwValue;
39
40 // Constructor for serialization
41 private SharedBandwidthConstraint() {
42 links = null;
43 sharedBwValue = null;
44 requestBwValue = null;
45 }
46
47 /**
48 * Creates a new SharedBandwidth constraint.
49 *
50 * @param links shared links
51 * @param sharedBwValue shared bandwidth of the links
52 * @param requestBwValue requested bandwidth value
53 */
54 public SharedBandwidthConstraint(List<Link> links, Bandwidth sharedBwValue, Bandwidth requestBwValue) {
55 this.links = links;
56 this.sharedBwValue = sharedBwValue;
57 this.requestBwValue = requestBwValue;
58 }
59
60 /**
61 * Creates a new SharedBandwidth constraint.
62 *
63 * @param links shared links
64 * @param sharedBwValue shared bandwidth of the links
65 * @param requestBwValue requested bandwidth value
66 * @return SharedBandwidth instance
67 */
68 public static SharedBandwidthConstraint of(List<Link> links, Bandwidth sharedBwValue, Bandwidth requestBwValue) {
69 return new SharedBandwidthConstraint(links, sharedBwValue, requestBwValue);
70 }
71
72 /**
73 * Obtains shared links.
74 *
75 * @return shared links
76 */
77 public List<Link> links() {
78 return links;
79 }
80
81 /**
82 * Obtains shared bandwidth of the links.
83 *
84 * @return shared bandwidth
85 */
86 public Bandwidth sharedBwValue() {
87 return sharedBwValue;
88 }
89
90 /**
91 * Obtains requested bandwidth value.
92 *
93 * @return requested bandwidth value
94 */
95 public Bandwidth requestBwValue() {
96 return requestBwValue;
97 }
98
99 @Override
100 public boolean isValid(Link link, ResourceContext context) {
Satish K2eb5d842017-04-04 16:28:37 +0530101 return false;
102 //Do nothing instead using isValidLink needs pce service to validate link
103 }
104
105 /**
106 * Validates the link based on shared bandwidth constraint.
107 *
108 * @param link to validate shared bandwidth constraint
109 * @param bandwidthMgmtService instance of BandwidthMgmtService
110 * @return true if link satisfies shared bandwidth constraint otherwise false
111 */
112 public boolean isValidLink(Link link, BandwidthMgmtService bandwidthMgmtService) {
113 if (bandwidthMgmtService == null) {
114 return false;
115 }
Priyanka Bb6963582016-05-20 20:21:20 +0530116 changedBwValue = requestBwValue;
117 if (links.contains(link)) {
118 changedBwValue = requestBwValue.isGreaterThan(sharedBwValue) ? requestBwValue.subtract(sharedBwValue)
119 : Bandwidth.bps(0);
120 }
121
Satish K2eb5d842017-04-04 16:28:37 +0530122 return bandwidthMgmtService.isBandwidthAvailable(link, changedBwValue.bps());
Priyanka Bb6963582016-05-20 20:21:20 +0530123 }
124
125 @Override
126 public int hashCode() {
127 return Objects.hash(requestBwValue, sharedBwValue, links);
128 }
129
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
135
136 if (obj instanceof SharedBandwidthConstraint) {
137 SharedBandwidthConstraint other = (SharedBandwidthConstraint) obj;
138 return Objects.equals(this.requestBwValue, other.requestBwValue)
139 && Objects.equals(this.sharedBwValue, other.sharedBwValue)
140 && Objects.equals(this.links, other.links);
141 }
142
143 return false;
144 }
145
146 @Override
147 public String toString() {
148 return toStringHelper(this)
149 .add("requestBwValue", requestBwValue)
150 .add("sharedBwValue", sharedBwValue)
151 .add("links", links)
152 .toString();
153 }
154}