Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 16 | package org.onosproject.net.resource.link; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Collection; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 19 | import java.util.HashMap; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 20 | import java.util.HashSet; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 21 | import java.util.Map; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 24 | import java.util.stream.Collectors; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 25 | |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 26 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableMap; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 28 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.intent.Constraint; |
| 31 | import org.onosproject.net.intent.IntentId; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 32 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 34 | import org.onosproject.net.resource.ResourceRequest; |
| 35 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 36 | |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Implementation of {@link LinkResourceRequest}. |
Sho SHIMIZU | 364cbac | 2015-10-29 15:47:35 -0700 | [diff] [blame] | 41 | * |
| 42 | * @deprecated in Emu Release |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 43 | */ |
Sho SHIMIZU | 364cbac | 2015-10-29 15:47:35 -0700 | [diff] [blame] | 44 | @Deprecated |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 45 | public final class DefaultLinkResourceRequest implements LinkResourceRequest { |
| 46 | |
| 47 | private final IntentId intentId; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 48 | protected final Map<Link, Set<ResourceRequest>> requests; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 51 | * Creates a new link resource request with the specified Intent ID, |
| 52 | * and resource requests over links. |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 53 | * |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 54 | * @param intentId intent ID associated with this request |
| 55 | * @param requests resource requests over links |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 56 | */ |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 57 | private DefaultLinkResourceRequest(IntentId intentId, Map<Link, Set<ResourceRequest>> requests) { |
| 58 | this.intentId = checkNotNull(intentId); |
| 59 | this.requests = checkNotNull(ImmutableMap.copyOf(requests)); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public ResourceType type() { |
| 64 | return null; |
| 65 | } |
| 66 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 67 | @Override |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 68 | public IntentId intentId() { |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 69 | return intentId; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Collection<Link> links() { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 74 | return requests.keySet(); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Set<ResourceRequest> resources() { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 79 | return requests.values().stream() |
| 80 | .flatMap(Collection::stream) |
| 81 | .collect(Collectors.toSet()); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Set<ResourceRequest> resources(Link link) { |
| 86 | return requests.get(link); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns builder of link resource request. |
| 91 | * |
| 92 | * @param intentId intent ID related to this request |
| 93 | * @param links a set of links for the request |
| 94 | * @return builder of link resource request |
| 95 | */ |
| 96 | public static LinkResourceRequest.Builder builder( |
| 97 | IntentId intentId, Collection<Link> links) { |
| 98 | return new Builder(intentId, links); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Builder of link resource request. |
| 103 | */ |
| 104 | public static final class Builder implements LinkResourceRequest.Builder { |
| 105 | private IntentId intentId; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 106 | private Map<Link, Set<ResourceRequest>> requests; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Creates a new link resource request. |
| 110 | * |
| 111 | * @param intentId intent ID related to this request |
| 112 | * @param links a set of links for the request |
| 113 | */ |
| 114 | private Builder(IntentId intentId, Collection<Link> links) { |
| 115 | this.intentId = intentId; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 116 | this.requests = new HashMap<>(); |
| 117 | for (Link link : links) { |
| 118 | requests.put(link, new HashSet<>()); |
| 119 | } |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Adds lambda request. |
| 124 | * |
| 125 | * @return self |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 126 | * @deprecated in Emu Release |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 127 | */ |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 128 | @Deprecated |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public Builder addLambdaRequest() { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 131 | for (Link link : requests.keySet()) { |
| 132 | requests.get(link).add(new LambdaResourceRequest()); |
| 133 | } |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 134 | return this; |
| 135 | } |
| 136 | |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 137 | @Beta |
| 138 | @Override |
| 139 | public LinkResourceRequest.Builder addLambdaRequest(LambdaResource lambda) { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 140 | for (Link link : requests.keySet()) { |
| 141 | requests.get(link).add(new LambdaResourceRequest(lambda)); |
| 142 | } |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 143 | return this; |
| 144 | } |
| 145 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 146 | /** |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 147 | * Adds Mpls request. |
| 148 | * |
| 149 | * @return self |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 150 | * @deprecated in Emu Release |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 151 | */ |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 152 | @Deprecated |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 153 | @Override |
| 154 | public Builder addMplsRequest() { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 155 | for (Link link : requests.keySet()) { |
| 156 | requests.get(link).add(new MplsLabelResourceRequest()); |
| 157 | } |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | @Beta |
| 162 | @Override |
| 163 | public Builder addMplsRequest(MplsLabel label) { |
| 164 | for (Link link : requests.keySet()) { |
| 165 | requests.get(link).add(new MplsLabelResourceRequest(label)); |
| 166 | } |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Beta |
| 171 | @Override |
| 172 | public LinkResourceRequest.Builder addMplsRequest(Map<Link, MplsLabel> labels) { |
| 173 | for (Link link : labels.keySet()) { |
| 174 | if (!requests.containsKey(link)) { |
| 175 | requests.put(link, new HashSet<>()); |
| 176 | } |
| 177 | requests.get(link).add(new MplsLabelResourceRequest(labels.get(link))); |
| 178 | } |
| 179 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 180 | return this; |
| 181 | } |
| 182 | |
| 183 | /** |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 184 | * Adds bandwidth request with bandwidth value. |
| 185 | * |
Ray Milkey | cf590df | 2015-02-23 17:43:24 -0800 | [diff] [blame] | 186 | * @param bandwidth bandwidth value in bits per second to be requested |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 187 | * @return self |
| 188 | */ |
| 189 | @Override |
| 190 | public Builder addBandwidthRequest(double bandwidth) { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 191 | for (Link link : requests.keySet()) { |
| 192 | requests.get(link).add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth)))); |
| 193 | } |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 194 | return this; |
| 195 | } |
| 196 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 197 | @Override |
| 198 | public LinkResourceRequest.Builder addConstraint(Constraint constraint) { |
Sho SHIMIZU | 4e2149e | 2016-02-22 14:42:34 -0800 | [diff] [blame^] | 199 | if (constraint instanceof BandwidthConstraint) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 200 | BandwidthConstraint bw = (BandwidthConstraint) constraint; |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 201 | return addBandwidthRequest(bw.bandwidth().bps()); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 202 | } |
| 203 | return this; |
| 204 | } |
| 205 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 206 | /** |
| 207 | * Returns link resource request. |
| 208 | * |
| 209 | * @return link resource request |
| 210 | */ |
| 211 | @Override |
| 212 | public LinkResourceRequest build() { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 213 | return new DefaultLinkResourceRequest(intentId, requests); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public int hashCode() { |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 219 | return Objects.hash(intentId, links()); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean equals(Object obj) { |
| 224 | if (this == obj) { |
| 225 | return true; |
| 226 | } |
| 227 | if (obj == null || getClass() != obj.getClass()) { |
| 228 | return false; |
| 229 | } |
| 230 | final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj; |
| 231 | return Objects.equals(this.intentId, other.intentId) |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 232 | && Objects.equals(this.links(), other.links()); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 233 | } |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 234 | } |