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.HashSet;
21 import java.util.Objects;
23 import org.onlab.util.Bandwidth;
24 import org.onosproject.net.Link;
25 import org.onosproject.net.intent.Constraint;
26 import org.onosproject.net.intent.IntentId;
28 import com.google.common.collect.ImmutableSet;
30 import org.onosproject.net.intent.constraint.BandwidthConstraint;
31 import org.onosproject.net.intent.constraint.LambdaConstraint;
32 import org.onosproject.net.resource.ResourceRequest;
33 import org.onosproject.net.resource.ResourceType;
36 * Implementation of {@link LinkResourceRequest}.
38 public final class DefaultLinkResourceRequest implements LinkResourceRequest {
40 private final IntentId intentId;
41 private final Collection<Link> links;
42 private final Set<ResourceRequest> resources;
45 * Creates a new link resource request with the given ID, links, and
48 * @param intentId intent ID related to this request
49 * @param links a set of links for the request
50 * @param resources a set of resources to be requested
52 private DefaultLinkResourceRequest(IntentId intentId,
53 Collection<Link> links,
54 Set<ResourceRequest> resources) {
55 this.intentId = intentId;
56 this.links = ImmutableSet.copyOf(links);
57 this.resources = ImmutableSet.copyOf(resources);
62 public ResourceType type() {
67 public IntentId intentId() {
72 public Collection<Link> links() {
77 public Set<ResourceRequest> resources() {
82 * Returns builder of link resource request.
84 * @param intentId intent ID related to this request
85 * @param links a set of links for the request
86 * @return builder of link resource request
88 public static LinkResourceRequest.Builder builder(
89 IntentId intentId, Collection<Link> links) {
90 return new Builder(intentId, links);
94 * Builder of link resource request.
96 public static final class Builder implements LinkResourceRequest.Builder {
97 private IntentId intentId;
98 private Collection<Link> links;
99 private Set<ResourceRequest> resources;
102 * Creates a new link resource request.
104 * @param intentId intent ID related to this request
105 * @param links a set of links for the request
107 private Builder(IntentId intentId, Collection<Link> links) {
108 this.intentId = intentId;
110 this.resources = new HashSet<>();
114 * Adds lambda request.
119 public Builder addLambdaRequest() {
120 resources.add(new LambdaResourceRequest());
130 public Builder addMplsRequest() {
131 resources.add(new MplsLabelResourceRequest());
136 * Adds bandwidth request with bandwidth value.
138 * @param bandwidth bandwidth value in bits per second to be requested
142 public Builder addBandwidthRequest(double bandwidth) {
143 resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
148 public LinkResourceRequest.Builder addConstraint(Constraint constraint) {
149 if (constraint instanceof LambdaConstraint) {
150 return addLambdaRequest();
151 } else if (constraint instanceof BandwidthConstraint) {
152 BandwidthConstraint bw = (BandwidthConstraint) constraint;
153 return addBandwidthRequest(bw.bandwidth().toDouble());
159 * Returns link resource request.
161 * @return link resource request
164 public LinkResourceRequest build() {
165 return new DefaultLinkResourceRequest(intentId, links, resources);
170 public int hashCode() {
171 return Objects.hash(intentId, links);
175 public boolean equals(Object obj) {
179 if (obj == null || getClass() != obj.getClass()) {
182 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
183 return Objects.equals(this.intentId, other.intentId)
184 && Objects.equals(this.links, other.links);