2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.resource.link;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
23 import java.util.Objects;
24 import java.util.stream.Collectors;
26 import com.google.common.annotations.Beta;
27 import com.google.common.collect.ImmutableMap;
28 import org.onlab.util.Bandwidth;
29 import org.onosproject.net.Link;
30 import org.onosproject.net.intent.Constraint;
31 import org.onosproject.net.intent.IntentId;
33 import org.onosproject.net.intent.constraint.BandwidthConstraint;
34 import org.onosproject.net.intent.constraint.LambdaConstraint;
35 import org.onosproject.net.resource.ResourceRequest;
36 import org.onosproject.net.resource.ResourceType;
38 import static com.google.common.base.Preconditions.checkNotNull;
41 * Implementation of {@link LinkResourceRequest}.
43 public final class DefaultLinkResourceRequest implements LinkResourceRequest {
45 private final IntentId intentId;
46 protected final Map<Link, Set<ResourceRequest>> requests;
49 * Creates a new link resource request with the specified Intent ID,
50 * and resource requests over links.
52 * @param intentId intent ID associated with this request
53 * @param requests resource requests over links
55 private DefaultLinkResourceRequest(IntentId intentId, Map<Link, Set<ResourceRequest>> requests) {
56 this.intentId = checkNotNull(intentId);
57 this.requests = checkNotNull(ImmutableMap.copyOf(requests));
61 public ResourceType type() {
66 public IntentId intentId() {
71 public Collection<Link> links() {
72 return requests.keySet();
76 public Set<ResourceRequest> resources() {
77 return requests.values().stream()
78 .flatMap(Collection::stream)
79 .collect(Collectors.toSet());
83 public Set<ResourceRequest> resources(Link link) {
84 return requests.get(link);
88 * Returns builder of link resource request.
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
94 public static LinkResourceRequest.Builder builder(
95 IntentId intentId, Collection<Link> links) {
96 return new Builder(intentId, links);
100 * Builder of link resource request.
102 public static final class Builder implements LinkResourceRequest.Builder {
103 private IntentId intentId;
104 private Map<Link, Set<ResourceRequest>> requests;
107 * Creates a new link resource request.
109 * @param intentId intent ID related to this request
110 * @param links a set of links for the request
112 private Builder(IntentId intentId, Collection<Link> links) {
113 this.intentId = intentId;
114 this.requests = new HashMap<>();
115 for (Link link : links) {
116 requests.put(link, new HashSet<>());
121 * Adds lambda request.
124 * @deprecated in Emu Release
128 public Builder addLambdaRequest() {
129 for (Link link : requests.keySet()) {
130 requests.get(link).add(new LambdaResourceRequest());
137 public LinkResourceRequest.Builder addLambdaRequest(LambdaResource lambda) {
138 for (Link link : requests.keySet()) {
139 requests.get(link).add(new LambdaResourceRequest(lambda));
148 * @deprecated in Emu Release
152 public Builder addMplsRequest() {
153 for (Link link : requests.keySet()) {
154 requests.get(link).add(new MplsLabelResourceRequest());
161 public Builder addMplsRequest(MplsLabel label) {
162 for (Link link : requests.keySet()) {
163 requests.get(link).add(new MplsLabelResourceRequest(label));
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<>());
175 requests.get(link).add(new MplsLabelResourceRequest(labels.get(link)));
182 * Adds bandwidth request with bandwidth value.
184 * @param bandwidth bandwidth value in bits per second to be requested
188 public Builder addBandwidthRequest(double bandwidth) {
189 for (Link link : requests.keySet()) {
190 requests.get(link).add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
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());
207 * Returns link resource request.
209 * @return link resource request
212 public LinkResourceRequest build() {
213 return new DefaultLinkResourceRequest(intentId, requests);
218 public int hashCode() {
219 return Objects.hash(intentId, links());
223 public boolean equals(Object obj) {
227 if (obj == null || getClass() != obj.getClass()) {
230 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
231 return Objects.equals(this.intentId, other.intentId)
232 && Objects.equals(this.links(), other.links());