blob: 5153aebf45d7654470f3fe02e77a00a366b1d90c [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;
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;
Brian O'Connor6de2e202015-05-21 14:30:41 -070032import org.onosproject.net.resource.ResourceRequest;
33import org.onosproject.net.resource.ResourceType;
Toshio Koide65e890f2014-10-23 12:02:25 -070034
35/**
36 * Implementation of {@link LinkResourceRequest}.
37 */
38public final class DefaultLinkResourceRequest implements LinkResourceRequest {
39
40 private final IntentId intentId;
41 private final Collection<Link> links;
42 private final Set<ResourceRequest> resources;
43
44 /**
45 * Creates a new link resource request with the given ID, links, and
46 * resource requests.
47 *
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
51 */
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);
58 }
59
Toshio Koideca0fcff2014-10-23 14:08:36 -070060
61 @Override
62 public ResourceType type() {
63 return null;
64 }
65
Toshio Koide65e890f2014-10-23 12:02:25 -070066 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070067 public IntentId intentId() {
Toshio Koide65e890f2014-10-23 12:02:25 -070068 return intentId;
69 }
70
71 @Override
72 public Collection<Link> links() {
73 return links;
74 }
75
76 @Override
77 public Set<ResourceRequest> resources() {
78 return resources;
79 }
80
81 /**
82 * Returns builder of link resource request.
83 *
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
87 */
88 public static LinkResourceRequest.Builder builder(
89 IntentId intentId, Collection<Link> links) {
90 return new Builder(intentId, links);
91 }
92
93 /**
94 * Builder of link resource request.
95 */
96 public static final class Builder implements LinkResourceRequest.Builder {
97 private IntentId intentId;
98 private Collection<Link> links;
99 private Set<ResourceRequest> resources;
100
101 /**
102 * Creates a new link resource request.
103 *
104 * @param intentId intent ID related to this request
105 * @param links a set of links for the request
106 */
107 private Builder(IntentId intentId, Collection<Link> links) {
108 this.intentId = intentId;
109 this.links = links;
110 this.resources = new HashSet<>();
111 }
112
113 /**
114 * Adds lambda request.
115 *
116 * @return self
117 */
118 @Override
119 public Builder addLambdaRequest() {
120 resources.add(new LambdaResourceRequest());
121 return this;
122 }
123
124 /**
Michele Santuari4b6019e2014-12-19 11:31:45 +0100125 * Adds Mpls request.
126 *
127 * @return self
128 */
129 @Override
130 public Builder addMplsRequest() {
131 resources.add(new MplsLabelResourceRequest());
132 return this;
133 }
134
135 /**
Toshio Koide65e890f2014-10-23 12:02:25 -0700136 * Adds bandwidth request with bandwidth value.
137 *
Ray Milkeycf590df2015-02-23 17:43:24 -0800138 * @param bandwidth bandwidth value in bits per second to be requested
Toshio Koide65e890f2014-10-23 12:02:25 -0700139 * @return self
140 */
141 @Override
142 public Builder addBandwidthRequest(double bandwidth) {
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700143 resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
Toshio Koide65e890f2014-10-23 12:02:25 -0700144 return this;
145 }
146
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800147 @Override
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());
154 }
155 return this;
156 }
157
Toshio Koide65e890f2014-10-23 12:02:25 -0700158 /**
159 * Returns link resource request.
160 *
161 * @return link resource request
162 */
163 @Override
164 public LinkResourceRequest build() {
165 return new DefaultLinkResourceRequest(intentId, links, resources);
166 }
167 }
168
Ayaka Koshibee114f042015-05-01 11:43:00 -0700169 @Override
170 public int hashCode() {
171 return Objects.hash(intentId, links);
172 }
173
174 @Override
175 public boolean equals(Object obj) {
176 if (this == obj) {
177 return true;
178 }
179 if (obj == null || getClass() != obj.getClass()) {
180 return false;
181 }
182 final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
183 return Objects.equals(this.intentId, other.intentId)
184 && Objects.equals(this.links, other.links);
185 }
Toshio Koide65e890f2014-10-23 12:02:25 -0700186}