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 | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 23 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.intent.Constraint; |
| 26 | import org.onosproject.net.intent.IntentId; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 27 | |
| 28 | import com.google.common.collect.ImmutableSet; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 29 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 31 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 32 | import org.onosproject.net.resource.ResourceRequest; |
| 33 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Implementation of {@link LinkResourceRequest}. |
| 37 | */ |
| 38 | public 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 Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 60 | |
| 61 | @Override |
| 62 | public ResourceType type() { |
| 63 | return null; |
| 64 | } |
| 65 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 66 | @Override |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 67 | public IntentId intentId() { |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 68 | 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 Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 125 | * 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 Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 136 | * Adds bandwidth request with bandwidth value. |
| 137 | * |
Ray Milkey | cf590df | 2015-02-23 17:43:24 -0800 | [diff] [blame] | 138 | * @param bandwidth bandwidth value in bits per second to be requested |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 139 | * @return self |
| 140 | */ |
| 141 | @Override |
| 142 | public Builder addBandwidthRequest(double bandwidth) { |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 143 | resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth)))); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 144 | return this; |
| 145 | } |
| 146 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 147 | @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 Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 158 | /** |
| 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 Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 169 | @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 Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 186 | } |