blob: 8d08e3bd8f828a62039b7957194ced5cd36ac8f0 [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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.resource;
Toshio Koide65e890f2014-10-23 12:02:25 -070017
18import java.util.Collection;
19import java.util.HashSet;
20import java.util.Set;
Ayaka Koshibee114f042015-05-01 11:43:00 -070021import java.util.Objects;
Toshio Koide65e890f2014-10-23 12:02:25 -070022
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070023import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.Link;
25import org.onosproject.net.intent.Constraint;
26import org.onosproject.net.intent.IntentId;
Toshio Koide65e890f2014-10-23 12:02:25 -070027
28import com.google.common.collect.ImmutableSet;
Ayaka Koshibee114f042015-05-01 11:43:00 -070029
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.constraint.BandwidthConstraint;
31import org.onosproject.net.intent.constraint.LambdaConstraint;
Toshio Koide65e890f2014-10-23 12:02:25 -070032
33/**
34 * Implementation of {@link LinkResourceRequest}.
35 */
36public final class DefaultLinkResourceRequest implements LinkResourceRequest {
37
38 private final IntentId intentId;
39 private final Collection<Link> links;
40 private final Set<ResourceRequest> resources;
41
42 /**
43 * Creates a new link resource request with the given ID, links, and
44 * resource requests.
45 *
46 * @param intentId intent ID related to this request
47 * @param links a set of links for the request
48 * @param resources a set of resources to be requested
49 */
50 private DefaultLinkResourceRequest(IntentId intentId,
51 Collection<Link> links,
52 Set<ResourceRequest> resources) {
53 this.intentId = intentId;
54 this.links = ImmutableSet.copyOf(links);
55 this.resources = ImmutableSet.copyOf(resources);
56 }
57
Toshio Koideca0fcff2014-10-23 14:08:36 -070058
59 @Override
60 public ResourceType type() {
61 return null;
62 }
63
Toshio Koide65e890f2014-10-23 12:02:25 -070064 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070065 public IntentId intentId() {
Toshio Koide65e890f2014-10-23 12:02:25 -070066 return intentId;
67 }
68
69 @Override
70 public Collection<Link> links() {
71 return links;
72 }
73
74 @Override
75 public Set<ResourceRequest> resources() {
76 return resources;
77 }
78
79 /**
80 * Returns builder of link resource request.
81 *
82 * @param intentId intent ID related to this request
83 * @param links a set of links for the request
84 * @return builder of link resource request
85 */
86 public static LinkResourceRequest.Builder builder(
87 IntentId intentId, Collection<Link> links) {
88 return new Builder(intentId, links);
89 }
90
91 /**
92 * Builder of link resource request.
93 */
94 public static final class Builder implements LinkResourceRequest.Builder {
95 private IntentId intentId;
96 private Collection<Link> links;
97 private Set<ResourceRequest> resources;
98
99 /**
100 * Creates a new link resource request.
101 *
102 * @param intentId intent ID related to this request
103 * @param links a set of links for the request
104 */
105 private Builder(IntentId intentId, Collection<Link> links) {
106 this.intentId = intentId;
107 this.links = links;
108 this.resources = new HashSet<>();
109 }
110
111 /**
112 * Adds lambda request.
113 *
114 * @return self
115 */
116 @Override
117 public Builder addLambdaRequest() {
118 resources.add(new LambdaResourceRequest());
119 return this;
120 }
121
122 /**
Michele Santuari4b6019e2014-12-19 11:31:45 +0100123 * Adds Mpls request.
124 *
125 * @return self
126 */
127 @Override
128 public Builder addMplsRequest() {
129 resources.add(new MplsLabelResourceRequest());
130 return this;
131 }
132
133 /**
Toshio Koide65e890f2014-10-23 12:02:25 -0700134 * Adds bandwidth request with bandwidth value.
135 *
Ray Milkeycf590df2015-02-23 17:43:24 -0800136 * @param bandwidth bandwidth value in bits per second to be requested
Toshio Koide65e890f2014-10-23 12:02:25 -0700137 * @return self
138 */
139 @Override
140 public Builder addBandwidthRequest(double bandwidth) {
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700141 resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
Toshio Koide65e890f2014-10-23 12:02:25 -0700142 return this;
143 }
144
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800145 @Override
146 public LinkResourceRequest.Builder addConstraint(Constraint constraint) {
147 if (constraint instanceof LambdaConstraint) {
148 return addLambdaRequest();
149 } else if (constraint instanceof BandwidthConstraint) {
150 BandwidthConstraint bw = (BandwidthConstraint) constraint;
151 return addBandwidthRequest(bw.bandwidth().toDouble());
152 }
153 return this;
154 }
155
Toshio Koide65e890f2014-10-23 12:02:25 -0700156 /**
157 * Returns link resource request.
158 *
159 * @return link resource request
160 */
161 @Override
162 public LinkResourceRequest build() {
163 return new DefaultLinkResourceRequest(intentId, links, resources);
164 }
165 }
166
Ayaka Koshibee114f042015-05-01 11:43:00 -0700167 @Override
168 public int hashCode() {
169 return Objects.hash(intentId, links);
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (this == obj) {
175 return true;
176 }
177 if (obj == null || getClass() != obj.getClass()) {
178 return false;
179 }
180 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
181 return Objects.equals(this.intentId, other.intentId)
182 && Objects.equals(this.links, other.links);
183 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700184}