1 package org.onosproject.incubator.net.resource.label;
3 import static com.google.common.base.Preconditions.checkArgument;
5 import java.util.Collections;
6 import java.util.Objects;
9 import com.google.common.annotations.Beta;
10 import org.onosproject.net.DeviceId;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.ImmutableSet;
16 * Abstraction of the capacity of device label resource or global label
17 * resource. It's contiguous range of label resource. When a application apply
18 * some labels of some device, first catch from Set that store
19 * available labels, if the size of the Set less than the apply number, then get
20 * labels by calculating with three attributes, beginLabel,endLabel and
21 * currentUsedMaxLabelId.
24 public class LabelResourcePool {
26 private final DeviceId deviceId;
27 private final LabelResourceId beginLabel;
28 private final LabelResourceId endLabel;
29 private final long totalNum; // capacity of label resource pool
30 private final long usedNum; // have used label number
31 private final LabelResourceId currentUsedMaxLabelId; // the maximal label
33 private ImmutableSet<LabelResource> releaseLabelId; // Set of released label
36 * Creates a pool by device id,begin label id,end label id.
38 * @param deviceId device identifier
39 * @param beginLabel represents for the first label id in the range of label
41 * @param endLabel represents for the last label id in the range of label
44 public LabelResourcePool(String deviceId, long beginLabel, long endLabel) {
45 this(deviceId, beginLabel, endLabel, endLabel - beginLabel + 1, 0L,
46 beginLabel, ImmutableSet.copyOf(Collections.emptySet()));
50 * Creates a pool by device id,begin label id,end label id.
51 * Used to update a pool in the store.
53 * @param deviceId device identifier
54 * @param beginLabel represents for the first label id in the range of label
56 * @param endLabel represents for the last label id in the range of label
58 * @param totalNum capacity of label resource pool
59 * @param usedNum have used label number
60 * @param currentUsedMaxLabelId the maximal label number id
61 * @param releaseLabelId Set of released label
63 public LabelResourcePool(String deviceId, long beginLabel, long endLabel,
64 long totalNum, long usedNum,
65 long currentUsedMaxLabelId,
66 ImmutableSet<LabelResource> releaseLabelId) {
67 checkArgument(endLabel >= beginLabel,
68 "endLabel %s must be greater than or equal to beginLabel %s",
69 endLabel, beginLabel);
70 this.deviceId = DeviceId.deviceId(deviceId);
71 this.beginLabel = LabelResourceId.labelResourceId(beginLabel);
72 this.endLabel = LabelResourceId.labelResourceId(endLabel);
73 this.totalNum = totalNum;
74 this.usedNum = usedNum;
75 this.currentUsedMaxLabelId = LabelResourceId
76 .labelResourceId(currentUsedMaxLabelId);
77 this.releaseLabelId = releaseLabelId;
81 * Returns a device id.
85 public DeviceId deviceId() {
90 * Returns a begin Label id.
92 * @return begin Label id
94 public LabelResourceId beginLabel() {
99 * Returns an end Label id.
101 * @return end Label id
103 public LabelResourceId endLabel() {
108 * Returns a begin Label id.
110 * @return current Used Maximal Label Id
112 public LabelResourceId currentUsedMaxLabelId() {
113 return currentUsedMaxLabelId;
117 * Returns total number.
119 * @return the total label number
121 public long totalNum() {
126 * Returns used number.
128 * @return the used label number
130 public long usedNum() {
135 * Returns the Set of released label before.
137 * @return the Set of LabelResource
139 public Set<LabelResource> releaseLabelId() {
140 return releaseLabelId;
144 public int hashCode() {
145 return Objects.hash(this.deviceId, this.beginLabel, this.endLabel,
146 this.totalNum, this.usedNum,
147 this.currentUsedMaxLabelId, this.releaseLabelId);
151 public boolean equals(Object obj) {
152 if (obj instanceof LabelResourcePool) {
153 LabelResourcePool that = (LabelResourcePool) obj;
154 return Objects.equals(this.deviceId, that.deviceId)
155 && Objects.equals(this.beginLabel, that.beginLabel)
156 && Objects.equals(this.endLabel, that.endLabel)
157 && Objects.equals(this.totalNum, that.totalNum)
158 && Objects.equals(this.usedNum, that.usedNum)
159 && Objects.equals(this.currentUsedMaxLabelId,
160 that.currentUsedMaxLabelId)
161 && Objects.equals(this.releaseLabelId, that.releaseLabelId);
167 public String toString() {
168 // TODO Auto-generated method stub
169 return MoreObjects.toStringHelper(this).add("deviceId", this.deviceId)
170 .add("beginLabel", this.beginLabel)
171 .add("endLabel", this.endLabel).add("totalNum", this.totalNum)
172 .add("usedNum", this.usedNum)
173 .add("currentUsedMaxLabelId", this.currentUsedMaxLabelId)
174 .add("releaseLabelId", this.releaseLabelId).toString();