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