blob: 583570f51be4f6bdbbe09eab09cb3970c09e5d34 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connor6de2e202015-05-21 14:30:41 -070016package org.onosproject.net.resource.link;
Toshio Koide65e890f2014-10-23 12:02:25 -070017
18import java.util.Collection;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070019import java.util.HashMap;
Toshio Koide65e890f2014-10-23 12:02:25 -070020import java.util.HashSet;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070021import java.util.Map;
Toshio Koide65e890f2014-10-23 12:02:25 -070022import java.util.Set;
Ayaka Koshibee114f042015-05-01 11:43:00 -070023import java.util.Objects;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070024import java.util.stream.Collectors;
Toshio Koide65e890f2014-10-23 12:02:25 -070025
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070026import com.google.common.annotations.Beta;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070027import com.google.common.collect.ImmutableMap;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070028import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Link;
30import org.onosproject.net.intent.Constraint;
31import org.onosproject.net.intent.IntentId;
Toshio Koide65e890f2014-10-23 12:02:25 -070032
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.constraint.BandwidthConstraint;
34import org.onosproject.net.intent.constraint.LambdaConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070035import org.onosproject.net.resource.ResourceRequest;
36import org.onosproject.net.resource.ResourceType;
Toshio Koide65e890f2014-10-23 12:02:25 -070037
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070038import static com.google.common.base.Preconditions.checkNotNull;
39
Toshio Koide65e890f2014-10-23 12:02:25 -070040/**
41 * Implementation of {@link LinkResourceRequest}.
Sho SHIMIZU364cbac2015-10-29 15:47:35 -070042 *
43 * @deprecated in Emu Release
Toshio Koide65e890f2014-10-23 12:02:25 -070044 */
Sho SHIMIZU364cbac2015-10-29 15:47:35 -070045@Deprecated
Toshio Koide65e890f2014-10-23 12:02:25 -070046public final class DefaultLinkResourceRequest implements LinkResourceRequest {
47
48 private final IntentId intentId;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070049 protected final Map<Link, Set<ResourceRequest>> requests;
Toshio Koide65e890f2014-10-23 12:02:25 -070050
51 /**
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070052 * Creates a new link resource request with the specified Intent ID,
53 * and resource requests over links.
Toshio Koide65e890f2014-10-23 12:02:25 -070054 *
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070055 * @param intentId intent ID associated with this request
56 * @param requests resource requests over links
Toshio Koide65e890f2014-10-23 12:02:25 -070057 */
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070058 private DefaultLinkResourceRequest(IntentId intentId, Map<Link, Set<ResourceRequest>> requests) {
59 this.intentId = checkNotNull(intentId);
60 this.requests = checkNotNull(ImmutableMap.copyOf(requests));
Toshio Koide65e890f2014-10-23 12:02:25 -070061 }
62
Toshio Koideca0fcff2014-10-23 14:08:36 -070063 @Override
64 public ResourceType type() {
65 return null;
66 }
67
Toshio Koide65e890f2014-10-23 12:02:25 -070068 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070069 public IntentId intentId() {
Toshio Koide65e890f2014-10-23 12:02:25 -070070 return intentId;
71 }
72
73 @Override
74 public Collection<Link> links() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070075 return requests.keySet();
Toshio Koide65e890f2014-10-23 12:02:25 -070076 }
77
78 @Override
79 public Set<ResourceRequest> resources() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070080 return requests.values().stream()
81 .flatMap(Collection::stream)
82 .collect(Collectors.toSet());
83 }
84
85 @Override
86 public Set<ResourceRequest> resources(Link link) {
87 return requests.get(link);
Toshio Koide65e890f2014-10-23 12:02:25 -070088 }
89
90 /**
91 * Returns builder of link resource request.
92 *
93 * @param intentId intent ID related to this request
94 * @param links a set of links for the request
95 * @return builder of link resource request
96 */
97 public static LinkResourceRequest.Builder builder(
98 IntentId intentId, Collection<Link> links) {
99 return new Builder(intentId, links);
100 }
101
102 /**
103 * Builder of link resource request.
104 */
105 public static final class Builder implements LinkResourceRequest.Builder {
106 private IntentId intentId;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700107 private Map<Link, Set<ResourceRequest>> requests;
Toshio Koide65e890f2014-10-23 12:02:25 -0700108
109 /**
110 * Creates a new link resource request.
111 *
112 * @param intentId intent ID related to this request
113 * @param links a set of links for the request
114 */
115 private Builder(IntentId intentId, Collection<Link> links) {
116 this.intentId = intentId;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700117 this.requests = new HashMap<>();
118 for (Link link : links) {
119 requests.put(link, new HashSet<>());
120 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700121 }
122
123 /**
124 * Adds lambda request.
125 *
126 * @return self
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700127 * @deprecated in Emu Release
Toshio Koide65e890f2014-10-23 12:02:25 -0700128 */
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700129 @Deprecated
Toshio Koide65e890f2014-10-23 12:02:25 -0700130 @Override
131 public Builder addLambdaRequest() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700132 for (Link link : requests.keySet()) {
133 requests.get(link).add(new LambdaResourceRequest());
134 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700135 return this;
136 }
137
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700138 @Beta
139 @Override
140 public LinkResourceRequest.Builder addLambdaRequest(LambdaResource lambda) {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700141 for (Link link : requests.keySet()) {
142 requests.get(link).add(new LambdaResourceRequest(lambda));
143 }
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700144 return this;
145 }
146
Toshio Koide65e890f2014-10-23 12:02:25 -0700147 /**
Michele Santuari4b6019e2014-12-19 11:31:45 +0100148 * Adds Mpls request.
149 *
150 * @return self
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700151 * @deprecated in Emu Release
Michele Santuari4b6019e2014-12-19 11:31:45 +0100152 */
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700153 @Deprecated
Michele Santuari4b6019e2014-12-19 11:31:45 +0100154 @Override
155 public Builder addMplsRequest() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700156 for (Link link : requests.keySet()) {
157 requests.get(link).add(new MplsLabelResourceRequest());
158 }
159 return this;
160 }
161
162 @Beta
163 @Override
164 public Builder addMplsRequest(MplsLabel label) {
165 for (Link link : requests.keySet()) {
166 requests.get(link).add(new MplsLabelResourceRequest(label));
167 }
168 return this;
169 }
170
171 @Beta
172 @Override
173 public LinkResourceRequest.Builder addMplsRequest(Map<Link, MplsLabel> labels) {
174 for (Link link : labels.keySet()) {
175 if (!requests.containsKey(link)) {
176 requests.put(link, new HashSet<>());
177 }
178 requests.get(link).add(new MplsLabelResourceRequest(labels.get(link)));
179 }
180
Michele Santuari4b6019e2014-12-19 11:31:45 +0100181 return this;
182 }
183
184 /**
Toshio Koide65e890f2014-10-23 12:02:25 -0700185 * Adds bandwidth request with bandwidth value.
186 *
Ray Milkeycf590df2015-02-23 17:43:24 -0800187 * @param bandwidth bandwidth value in bits per second to be requested
Toshio Koide65e890f2014-10-23 12:02:25 -0700188 * @return self
189 */
190 @Override
191 public Builder addBandwidthRequest(double bandwidth) {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700192 for (Link link : requests.keySet()) {
193 requests.get(link).add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
194 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700195 return this;
196 }
197
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800198 @Override
199 public LinkResourceRequest.Builder addConstraint(Constraint constraint) {
200 if (constraint instanceof LambdaConstraint) {
201 return addLambdaRequest();
202 } else if (constraint instanceof BandwidthConstraint) {
203 BandwidthConstraint bw = (BandwidthConstraint) constraint;
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800204 return addBandwidthRequest(bw.bandwidth().bps());
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800205 }
206 return this;
207 }
208
Toshio Koide65e890f2014-10-23 12:02:25 -0700209 /**
210 * Returns link resource request.
211 *
212 * @return link resource request
213 */
214 @Override
215 public LinkResourceRequest build() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700216 return new DefaultLinkResourceRequest(intentId, requests);
Toshio Koide65e890f2014-10-23 12:02:25 -0700217 }
218 }
219
Ayaka Koshibee114f042015-05-01 11:43:00 -0700220 @Override
221 public int hashCode() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700222 return Objects.hash(intentId, links());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700223 }
224
225 @Override
226 public boolean equals(Object obj) {
227 if (this == obj) {
228 return true;
229 }
230 if (obj == null || getClass() != obj.getClass()) {
231 return false;
232 }
233 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
234 return Objects.equals(this.intentId, other.intentId)
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700235 && Objects.equals(this.links(), other.links());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700236 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700237}