Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -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.incubator.net.resource.label; |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.util.Collections; |
| 21 | import java.util.Objects; |
| 22 | import java.util.Set; |
| 23 | |
Brian O'Connor | 893b922 | 2015-06-25 15:07:04 -0400 | [diff] [blame] | 24 | import com.google.common.annotations.Beta; |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | import com.google.common.collect.ImmutableSet; |
| 29 | |
| 30 | /** |
| 31 | * Abstraction of the capacity of device label resource or global label |
Sho SHIMIZU | af0454b | 2015-08-17 12:08:43 -0700 | [diff] [blame] | 32 | * resource. It's contiguous range of label resource. When a application apply |
| 33 | * some labels of some device, first catch from Set that store |
| 34 | * available labels, if the size of the Set less than the apply number, then get |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 35 | * labels by calculating with three attributes, beginLabel,endLabel and |
Sho SHIMIZU | af0454b | 2015-08-17 12:08:43 -0700 | [diff] [blame] | 36 | * currentUsedMaxLabelId. |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 37 | */ |
Brian O'Connor | 893b922 | 2015-06-25 15:07:04 -0400 | [diff] [blame] | 38 | @Beta |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 39 | public class LabelResourcePool { |
| 40 | |
| 41 | private final DeviceId deviceId; |
| 42 | private final LabelResourceId beginLabel; |
| 43 | private final LabelResourceId endLabel; |
| 44 | private final long totalNum; // capacity of label resource pool |
| 45 | private final long usedNum; // have used label number |
| 46 | private final LabelResourceId currentUsedMaxLabelId; // the maximal label |
| 47 | // number id |
| 48 | private ImmutableSet<LabelResource> releaseLabelId; // Set of released label |
| 49 | |
| 50 | /** |
| 51 | * Creates a pool by device id,begin label id,end label id. |
| 52 | * |
| 53 | * @param deviceId device identifier |
| 54 | * @param beginLabel represents for the first label id in the range of label |
| 55 | * resource pool |
| 56 | * @param endLabel represents for the last label id in the range of label |
| 57 | * resource pool |
| 58 | */ |
| 59 | public LabelResourcePool(String deviceId, long beginLabel, long endLabel) { |
| 60 | this(deviceId, beginLabel, endLabel, endLabel - beginLabel + 1, 0L, |
| 61 | beginLabel, ImmutableSet.copyOf(Collections.emptySet())); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a pool by device id,begin label id,end label id. |
Sho SHIMIZU | af0454b | 2015-08-17 12:08:43 -0700 | [diff] [blame] | 66 | * Used to update a pool in the store. |
| 67 | * |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 68 | * @param deviceId device identifier |
| 69 | * @param beginLabel represents for the first label id in the range of label |
| 70 | * resource pool |
| 71 | * @param endLabel represents for the last label id in the range of label |
| 72 | * resource pool |
| 73 | * @param totalNum capacity of label resource pool |
| 74 | * @param usedNum have used label number |
| 75 | * @param currentUsedMaxLabelId the maximal label number id |
| 76 | * @param releaseLabelId Set of released label |
| 77 | */ |
| 78 | public LabelResourcePool(String deviceId, long beginLabel, long endLabel, |
| 79 | long totalNum, long usedNum, |
| 80 | long currentUsedMaxLabelId, |
| 81 | ImmutableSet<LabelResource> releaseLabelId) { |
| 82 | checkArgument(endLabel >= beginLabel, |
| 83 | "endLabel %s must be greater than or equal to beginLabel %s", |
| 84 | endLabel, beginLabel); |
| 85 | this.deviceId = DeviceId.deviceId(deviceId); |
| 86 | this.beginLabel = LabelResourceId.labelResourceId(beginLabel); |
| 87 | this.endLabel = LabelResourceId.labelResourceId(endLabel); |
| 88 | this.totalNum = totalNum; |
| 89 | this.usedNum = usedNum; |
| 90 | this.currentUsedMaxLabelId = LabelResourceId |
| 91 | .labelResourceId(currentUsedMaxLabelId); |
| 92 | this.releaseLabelId = releaseLabelId; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns a device id. |
| 97 | * |
| 98 | * @return DeviceId |
| 99 | */ |
| 100 | public DeviceId deviceId() { |
| 101 | return deviceId; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns a begin Label id. |
| 106 | * |
| 107 | * @return begin Label id |
| 108 | */ |
| 109 | public LabelResourceId beginLabel() { |
| 110 | return beginLabel; |
| 111 | } |
| 112 | |
| 113 | /** |
Sho SHIMIZU | af0454b | 2015-08-17 12:08:43 -0700 | [diff] [blame] | 114 | * Returns an end Label id. |
jcc | fff0de9 | 2015-03-28 01:40:08 -0700 | [diff] [blame] | 115 | * |
| 116 | * @return end Label id |
| 117 | */ |
| 118 | public LabelResourceId endLabel() { |
| 119 | return endLabel; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns a begin Label id. |
| 124 | * |
| 125 | * @return current Used Maximal Label Id |
| 126 | */ |
| 127 | public LabelResourceId currentUsedMaxLabelId() { |
| 128 | return currentUsedMaxLabelId; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns total number. |
| 133 | * |
| 134 | * @return the total label number |
| 135 | */ |
| 136 | public long totalNum() { |
| 137 | return totalNum; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns used number. |
| 142 | * |
| 143 | * @return the used label number |
| 144 | */ |
| 145 | public long usedNum() { |
| 146 | return usedNum; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the Set of released label before. |
| 151 | * |
| 152 | * @return the Set of LabelResource |
| 153 | */ |
| 154 | public Set<LabelResource> releaseLabelId() { |
| 155 | return releaseLabelId; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public int hashCode() { |
| 160 | return Objects.hash(this.deviceId, this.beginLabel, this.endLabel, |
| 161 | this.totalNum, this.usedNum, |
| 162 | this.currentUsedMaxLabelId, this.releaseLabelId); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean equals(Object obj) { |
| 167 | if (obj instanceof LabelResourcePool) { |
| 168 | LabelResourcePool that = (LabelResourcePool) obj; |
| 169 | return Objects.equals(this.deviceId, that.deviceId) |
| 170 | && Objects.equals(this.beginLabel, that.beginLabel) |
| 171 | && Objects.equals(this.endLabel, that.endLabel) |
| 172 | && Objects.equals(this.totalNum, that.totalNum) |
| 173 | && Objects.equals(this.usedNum, that.usedNum) |
| 174 | && Objects.equals(this.currentUsedMaxLabelId, |
| 175 | that.currentUsedMaxLabelId) |
| 176 | && Objects.equals(this.releaseLabelId, that.releaseLabelId); |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public String toString() { |
| 183 | // TODO Auto-generated method stub |
| 184 | return MoreObjects.toStringHelper(this).add("deviceId", this.deviceId) |
| 185 | .add("beginLabel", this.beginLabel) |
| 186 | .add("endLabel", this.endLabel).add("totalNum", this.totalNum) |
| 187 | .add("usedNum", this.usedNum) |
| 188 | .add("currentUsedMaxLabelId", this.currentUsedMaxLabelId) |
| 189 | .add("releaseLabelId", this.releaseLabelId).toString(); |
| 190 | } |
| 191 | } |