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 | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
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; |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Implementation of {@link LinkResourceRequest}. |
| 35 | */ |
| 36 | public 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 Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | public ResourceType type() { |
| 61 | return null; |
| 62 | } |
| 63 | |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 64 | @Override |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 65 | public IntentId intentId() { |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 66 | 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 Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 123 | * 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 Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 134 | * Adds bandwidth request with bandwidth value. |
| 135 | * |
Ray Milkey | cf590df | 2015-02-23 17:43:24 -0800 | [diff] [blame] | 136 | * @param bandwidth bandwidth value in bits per second to be requested |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 137 | * @return self |
| 138 | */ |
| 139 | @Override |
| 140 | public Builder addBandwidthRequest(double bandwidth) { |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame^] | 141 | resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth)))); |
Toshio Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 142 | return this; |
| 143 | } |
| 144 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 145 | @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 Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 156 | /** |
| 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 Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 167 | @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 Koide | 65e890f | 2014-10-23 12:02:25 -0700 | [diff] [blame] | 184 | } |