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 * @deprecated in Emu Release
46 public final class DefaultLinkResourceRequest implements LinkResourceRequest {
48 private final IntentId intentId;
49 protected final Map<Link, Set<ResourceRequest>> requests;
52 * Creates a new link resource request with the specified Intent ID,
53 * and resource requests over links.
55 * @param intentId intent ID associated with this request
56 * @param requests resource requests over links
58 private DefaultLinkResourceRequest(IntentId intentId, Map<Link, Set<ResourceRequest>> requests) {
59 this.intentId = checkNotNull(intentId);
60 this.requests = checkNotNull(ImmutableMap.copyOf(requests));
64 public ResourceType type() {
69 public IntentId intentId() {
74 public Collection<Link> links() {
75 return requests.keySet();
79 public Set<ResourceRequest> resources() {
80 return requests.values().stream()
81 .flatMap(Collection::stream)
82 .collect(Collectors.toSet());
86 public Set<ResourceRequest> resources(Link link) {
87 return requests.get(link);
91 * Returns builder of link resource request.
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
97 public static LinkResourceRequest.Builder builder(
98 IntentId intentId, Collection<Link> links) {
99 return new Builder(intentId, links);
103 * Builder of link resource request.
105 public static final class Builder implements LinkResourceRequest.Builder {
106 private IntentId intentId;
107 private Map<Link, Set<ResourceRequest>> requests;
110 * Creates a new link resource request.
112 * @param intentId intent ID related to this request
113 * @param links a set of links for the request
115 private Builder(IntentId intentId, Collection<Link> links) {
116 this.intentId = intentId;
117 this.requests = new HashMap<>();
118 for (Link link : links) {
119 requests.put(link, new HashSet<>());
124 * Adds lambda request.
127 * @deprecated in Emu Release
131 public Builder addLambdaRequest() {
132 for (Link link : requests.keySet()) {
133 requests.get(link).add(new LambdaResourceRequest());
140 public LinkResourceRequest.Builder addLambdaRequest(LambdaResource lambda) {
141 for (Link link : requests.keySet()) {
142 requests.get(link).add(new LambdaResourceRequest(lambda));
151 * @deprecated in Emu Release
155 public Builder addMplsRequest() {
156 for (Link link : requests.keySet()) {
157 requests.get(link).add(new MplsLabelResourceRequest());
164 public Builder addMplsRequest(MplsLabel label) {
165 for (Link link : requests.keySet()) {
166 requests.get(link).add(new MplsLabelResourceRequest(label));
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<>());
178 requests.get(link).add(new MplsLabelResourceRequest(labels.get(link)));
185 * Adds bandwidth request with bandwidth value.
187 * @param bandwidth bandwidth value in bits per second to be requested
191 public Builder addBandwidthRequest(double bandwidth) {
192 for (Link link : requests.keySet()) {
193 requests.get(link).add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
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;
204 return addBandwidthRequest(bw.bandwidth().toDouble());
210 * Returns link resource request.
212 * @return link resource request
215 public LinkResourceRequest build() {
216 return new DefaultLinkResourceRequest(intentId, requests);
221 public int hashCode() {
222 return Objects.hash(intentId, links());
226 public boolean equals(Object obj) {
230 if (obj == null || getClass() != obj.getClass()) {
233 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
234 return Objects.equals(this.intentId, other.intentId)
235 && Objects.equals(this.links(), other.links());