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; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Set; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 22 | |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 23 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 24 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.intent.Constraint; |
| 27 | import org.onosproject.net.intent.IntentId; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 28 | |
| 29 | import com.google.common.collect.ImmutableSet; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 30 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 32 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 33 | import org.onosproject.net.resource.ResourceRequest; |
| 34 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Implementation of {@link LinkResourceRequest}. |
| 38 | */ |
| 39 | public final class DefaultLinkResourceRequest implements LinkResourceRequest { |
| 40 | |
| 41 | private final IntentId intentId; |
| 42 | private final Collection<Link> links; |
| 43 | private final Set<ResourceRequest> resources; |
| 44 | |
| 45 | /** |
| 46 | * Creates a new link resource request with the given ID, links, and |
| 47 | * resource requests. |
| 48 | * |
| 49 | * @param intentId intent ID related to this request |
| 50 | * @param links a set of links for the request |
| 51 | * @param resources a set of resources to be requested |
| 52 | */ |
| 53 | private DefaultLinkResourceRequest(IntentId intentId, |
| 54 | Collection<Link> links, |
| 55 | Set<ResourceRequest> resources) { |
| 56 | this.intentId = intentId; |
| 57 | this.links = ImmutableSet.copyOf(links); |
| 58 | this.resources = ImmutableSet.copyOf(resources); |
| 59 | } |
| 60 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 61 | |
| 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() { |
| 74 | return links; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Set<ResourceRequest> resources() { |
| 79 | return resources; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns builder of link resource request. |
| 84 | * |
| 85 | * @param intentId intent ID related to this request |
| 86 | * @param links a set of links for the request |
| 87 | * @return builder of link resource request |
| 88 | */ |
| 89 | public static LinkResourceRequest.Builder builder( |
| 90 | IntentId intentId, Collection<Link> links) { |
| 91 | return new Builder(intentId, links); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Builder of link resource request. |
| 96 | */ |
| 97 | public static final class Builder implements LinkResourceRequest.Builder { |
| 98 | private IntentId intentId; |
| 99 | private Collection<Link> links; |
| 100 | private Set<ResourceRequest> resources; |
| 101 | |
| 102 | /** |
| 103 | * Creates a new link resource request. |
| 104 | * |
| 105 | * @param intentId intent ID related to this request |
| 106 | * @param links a set of links for the request |
| 107 | */ |
| 108 | private Builder(IntentId intentId, Collection<Link> links) { |
| 109 | this.intentId = intentId; |
| 110 | this.links = links; |
| 111 | this.resources = new HashSet<>(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Adds lambda request. |
| 116 | * |
| 117 | * @return self |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 118 | * @deprecated in Emu Release |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 119 | */ |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 120 | @Deprecated |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public Builder addLambdaRequest() { |
| 123 | resources.add(new LambdaResourceRequest()); |
| 124 | return this; |
| 125 | } |
| 126 | |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 127 | @Beta |
| 128 | @Override |
| 129 | public LinkResourceRequest.Builder addLambdaRequest(LambdaResource lambda) { |
| 130 | resources.add(new LambdaResourceRequest(lambda)); |
| 131 | return this; |
| 132 | } |
| 133 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 134 | /** |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 135 | * Adds Mpls request. |
| 136 | * |
| 137 | * @return self |
| 138 | */ |
| 139 | @Override |
| 140 | public Builder addMplsRequest() { |
| 141 | resources.add(new MplsLabelResourceRequest()); |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 146 | * Adds bandwidth request with bandwidth value. |
| 147 | * |
Ray Milkey | cf590df | 2015-02-23 17:43:24 -0800 | [diff] [blame] | 148 | * @param bandwidth bandwidth value in bits per second to be requested |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 149 | * @return self |
| 150 | */ |
| 151 | @Override |
| 152 | public Builder addBandwidthRequest(double bandwidth) { |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 153 | resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth)))); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 154 | return this; |
| 155 | } |
| 156 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 157 | @Override |
| 158 | public LinkResourceRequest.Builder addConstraint(Constraint constraint) { |
| 159 | if (constraint instanceof LambdaConstraint) { |
| 160 | return addLambdaRequest(); |
| 161 | } else if (constraint instanceof BandwidthConstraint) { |
| 162 | BandwidthConstraint bw = (BandwidthConstraint) constraint; |
| 163 | return addBandwidthRequest(bw.bandwidth().toDouble()); |
| 164 | } |
| 165 | return this; |
| 166 | } |
| 167 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Returns link resource request. |
| 170 | * |
| 171 | * @return link resource request |
| 172 | */ |
| 173 | @Override |
| 174 | public LinkResourceRequest build() { |
| 175 | return new DefaultLinkResourceRequest(intentId, links, resources); |
| 176 | } |
| 177 | } |
| 178 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public int hashCode() { |
| 181 | return Objects.hash(intentId, links); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean equals(Object obj) { |
| 186 | if (this == obj) { |
| 187 | return true; |
| 188 | } |
| 189 | if (obj == null || getClass() != obj.getClass()) { |
| 190 | return false; |
| 191 | } |
| 192 | final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj; |
| 193 | return Objects.equals(this.intentId, other.intentId) |
| 194 | && Objects.equals(this.links, other.links); |
| 195 | } |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 196 | } |