blob: 2985f24a7693cb15a649ee4ba21a9942734e62bb [file] [log] [blame]
Priyanka Bb6963582016-05-20 20:21:20 +05301/*
2 * Copyright 2016-present 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 */
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;
22import org.onosproject.net.resource.Resources;
23
24import java.util.List;
25import java.util.Objects;
26import java.util.stream.Stream;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29
30/**
31 * Constraint that evaluates whether links satisfies sharedbandwidth request.
32 */
33public final class SharedBandwidthConstraint extends BooleanConstraint {
34
35 private final List<Link> links;
36 private final Bandwidth sharedBwValue;
37 private final Bandwidth requestBwValue;
38 //temporary variable declared to hold changed bandwidth value
39 private Bandwidth changedBwValue;
40
41 // Constructor for serialization
42 private SharedBandwidthConstraint() {
43 links = null;
44 sharedBwValue = null;
45 requestBwValue = null;
46 }
47
48 /**
49 * Creates a new SharedBandwidth constraint.
50 *
51 * @param links shared links
52 * @param sharedBwValue shared bandwidth of the links
53 * @param requestBwValue requested bandwidth value
54 */
55 public SharedBandwidthConstraint(List<Link> links, Bandwidth sharedBwValue, Bandwidth requestBwValue) {
56 this.links = links;
57 this.sharedBwValue = sharedBwValue;
58 this.requestBwValue = requestBwValue;
59 }
60
61 /**
62 * Creates a new SharedBandwidth constraint.
63 *
64 * @param links shared links
65 * @param sharedBwValue shared bandwidth of the links
66 * @param requestBwValue requested bandwidth value
67 * @return SharedBandwidth instance
68 */
69 public static SharedBandwidthConstraint of(List<Link> links, Bandwidth sharedBwValue, Bandwidth requestBwValue) {
70 return new SharedBandwidthConstraint(links, sharedBwValue, requestBwValue);
71 }
72
73 /**
74 * Obtains shared links.
75 *
76 * @return shared links
77 */
78 public List<Link> links() {
79 return links;
80 }
81
82 /**
83 * Obtains shared bandwidth of the links.
84 *
85 * @return shared bandwidth
86 */
87 public Bandwidth sharedBwValue() {
88 return sharedBwValue;
89 }
90
91 /**
92 * Obtains requested bandwidth value.
93 *
94 * @return requested bandwidth value
95 */
96 public Bandwidth requestBwValue() {
97 return requestBwValue;
98 }
99
100 @Override
101 public boolean isValid(Link link, ResourceContext context) {
102 changedBwValue = requestBwValue;
103 if (links.contains(link)) {
104 changedBwValue = requestBwValue.isGreaterThan(sharedBwValue) ? requestBwValue.subtract(sharedBwValue)
105 : Bandwidth.bps(0);
106 }
107
108 return Stream
109 .of(link.src(), link.dst())
110 .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).resource(
111 changedBwValue.bps())).allMatch(context::isAvailable);
112 }
113
114 @Override
115 public int hashCode() {
116 return Objects.hash(requestBwValue, sharedBwValue, links);
117 }
118
119 @Override
120 public boolean equals(Object obj) {
121 if (this == obj) {
122 return true;
123 }
124
125 if (obj instanceof SharedBandwidthConstraint) {
126 SharedBandwidthConstraint other = (SharedBandwidthConstraint) obj;
127 return Objects.equals(this.requestBwValue, other.requestBwValue)
128 && Objects.equals(this.sharedBwValue, other.sharedBwValue)
129 && Objects.equals(this.links, other.links);
130 }
131
132 return false;
133 }
134
135 @Override
136 public String toString() {
137 return toStringHelper(this)
138 .add("requestBwValue", requestBwValue)
139 .add("sharedBwValue", sharedBwValue)
140 .add("links", links)
141 .toString();
142 }
143}