blob: f8e143a41cd881930a1fd3342aab87180952b34e [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}.
42 */
43public final class DefaultLinkResourceRequest implements LinkResourceRequest {
44
45 private final IntentId intentId;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070046 protected final Map<Link, Set<ResourceRequest>> requests;
Toshio Koide65e890f2014-10-23 12:02:25 -070047
48 /**
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070049 * Creates a new link resource request with the specified Intent ID,
50 * and resource requests over links.
Toshio Koide65e890f2014-10-23 12:02:25 -070051 *
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070052 * @param intentId intent ID associated with this request
53 * @param requests resource requests over links
Toshio Koide65e890f2014-10-23 12:02:25 -070054 */
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070055 private DefaultLinkResourceRequest(IntentId intentId, Map<Link, Set<ResourceRequest>> requests) {
56 this.intentId = checkNotNull(intentId);
57 this.requests = checkNotNull(ImmutableMap.copyOf(requests));
Toshio Koide65e890f2014-10-23 12:02:25 -070058 }
59
Toshio Koideca0fcff2014-10-23 14:08:36 -070060 @Override
61 public ResourceType type() {
62 return null;
63 }
64
Toshio Koide65e890f2014-10-23 12:02:25 -070065 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070066 public IntentId intentId() {
Toshio Koide65e890f2014-10-23 12:02:25 -070067 return intentId;
68 }
69
70 @Override
71 public Collection<Link> links() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070072 return requests.keySet();
Toshio Koide65e890f2014-10-23 12:02:25 -070073 }
74
75 @Override
76 public Set<ResourceRequest> resources() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070077 return requests.values().stream()
78 .flatMap(Collection::stream)
79 .collect(Collectors.toSet());
80 }
81
82 @Override
83 public Set<ResourceRequest> resources(Link link) {
84 return requests.get(link);
Toshio Koide65e890f2014-10-23 12:02:25 -070085 }
86
87 /**
88 * Returns builder of link resource request.
89 *
90 * @param intentId intent ID related to this request
91 * @param links a set of links for the request
92 * @return builder of link resource request
93 */
94 public static LinkResourceRequest.Builder builder(
95 IntentId intentId, Collection<Link> links) {
96 return new Builder(intentId, links);
97 }
98
99 /**
100 * Builder of link resource request.
101 */
102 public static final class Builder implements LinkResourceRequest.Builder {
103 private IntentId intentId;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700104 private Map<Link, Set<ResourceRequest>> requests;
Toshio Koide65e890f2014-10-23 12:02:25 -0700105
106 /**
107 * Creates a new link resource request.
108 *
109 * @param intentId intent ID related to this request
110 * @param links a set of links for the request
111 */
112 private Builder(IntentId intentId, Collection<Link> links) {
113 this.intentId = intentId;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700114 this.requests = new HashMap<>();
115 for (Link link : links) {
116 requests.put(link, new HashSet<>());
117 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700118 }
119
120 /**
121 * Adds lambda request.
122 *
123 * @return self
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700124 * @deprecated in Emu Release
Toshio Koide65e890f2014-10-23 12:02:25 -0700125 */
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700126 @Deprecated
Toshio Koide65e890f2014-10-23 12:02:25 -0700127 @Override
128 public Builder addLambdaRequest() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700129 for (Link link : requests.keySet()) {
130 requests.get(link).add(new LambdaResourceRequest());
131 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700132 return this;
133 }
134
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700135 @Beta
136 @Override
137 public LinkResourceRequest.Builder addLambdaRequest(LambdaResource lambda) {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700138 for (Link link : requests.keySet()) {
139 requests.get(link).add(new LambdaResourceRequest(lambda));
140 }
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700141 return this;
142 }
143
Toshio Koide65e890f2014-10-23 12:02:25 -0700144 /**
Michele Santuari4b6019e2014-12-19 11:31:45 +0100145 * Adds Mpls request.
146 *
147 * @return self
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700148 * @deprecated in Emu Release
Michele Santuari4b6019e2014-12-19 11:31:45 +0100149 */
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700150 @Deprecated
Michele Santuari4b6019e2014-12-19 11:31:45 +0100151 @Override
152 public Builder addMplsRequest() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700153 for (Link link : requests.keySet()) {
154 requests.get(link).add(new MplsLabelResourceRequest());
155 }
156 return this;
157 }
158
159 @Beta
160 @Override
161 public Builder addMplsRequest(MplsLabel label) {
162 for (Link link : requests.keySet()) {
163 requests.get(link).add(new MplsLabelResourceRequest(label));
164 }
165 return this;
166 }
167
168 @Beta
169 @Override
170 public LinkResourceRequest.Builder addMplsRequest(Map<Link, MplsLabel> labels) {
171 for (Link link : labels.keySet()) {
172 if (!requests.containsKey(link)) {
173 requests.put(link, new HashSet<>());
174 }
175 requests.get(link).add(new MplsLabelResourceRequest(labels.get(link)));
176 }
177
Michele Santuari4b6019e2014-12-19 11:31:45 +0100178 return this;
179 }
180
181 /**
Toshio Koide65e890f2014-10-23 12:02:25 -0700182 * Adds bandwidth request with bandwidth value.
183 *
Ray Milkeycf590df2015-02-23 17:43:24 -0800184 * @param bandwidth bandwidth value in bits per second to be requested
Toshio Koide65e890f2014-10-23 12:02:25 -0700185 * @return self
186 */
187 @Override
188 public Builder addBandwidthRequest(double bandwidth) {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700189 for (Link link : requests.keySet()) {
190 requests.get(link).add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
191 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700192 return this;
193 }
194
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800195 @Override
196 public LinkResourceRequest.Builder addConstraint(Constraint constraint) {
197 if (constraint instanceof LambdaConstraint) {
198 return addLambdaRequest();
199 } else if (constraint instanceof BandwidthConstraint) {
200 BandwidthConstraint bw = (BandwidthConstraint) constraint;
201 return addBandwidthRequest(bw.bandwidth().toDouble());
202 }
203 return this;
204 }
205
Toshio Koide65e890f2014-10-23 12:02:25 -0700206 /**
207 * Returns link resource request.
208 *
209 * @return link resource request
210 */
211 @Override
212 public LinkResourceRequest build() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700213 return new DefaultLinkResourceRequest(intentId, requests);
Toshio Koide65e890f2014-10-23 12:02:25 -0700214 }
215 }
216
Ayaka Koshibee114f042015-05-01 11:43:00 -0700217 @Override
218 public int hashCode() {
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700219 return Objects.hash(intentId, links());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700220 }
221
222 @Override
223 public boolean equals(Object obj) {
224 if (this == obj) {
225 return true;
226 }
227 if (obj == null || getClass() != obj.getClass()) {
228 return false;
229 }
230 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
231 return Objects.equals(this.intentId, other.intentId)
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700232 && Objects.equals(this.links(), other.links());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700233 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700234}