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