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