315abe61eaa476b48b2b1aed2d004b909468048d
[onosfw.git] /
1 package org.onosproject.incubator.net.resource.label;
2
3 import static com.google.common.base.Preconditions.checkArgument;
4
5 import java.util.Collections;
6 import java.util.Objects;
7 import java.util.Set;
8
9 import com.google.common.annotations.Beta;
10 import org.onosproject.net.DeviceId;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.ImmutableSet;
14
15 /**
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.
22  */
23 @Beta
24 public class LabelResourcePool {
25
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
32                                                         // number id
33     private ImmutableSet<LabelResource> releaseLabelId; // Set of released label
34
35     /**
36      * Creates a pool by device id,begin label id,end label id.
37      *
38      * @param deviceId device identifier
39      * @param beginLabel represents for the first label id in the range of label
40      *            resource pool
41      * @param endLabel represents for the last label id in the range of label
42      *            resource pool
43      */
44     public LabelResourcePool(String deviceId, long beginLabel, long endLabel) {
45         this(deviceId, beginLabel, endLabel, endLabel - beginLabel + 1, 0L,
46              beginLabel, ImmutableSet.copyOf(Collections.emptySet()));
47     }
48
49     /**
50      * Creates a pool by device id,begin label id,end label id.
51      * Used to update a pool in the store.
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      * @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
62      */
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;
78     }
79
80     /**
81      * Returns a device id.
82      *
83      * @return DeviceId
84      */
85     public DeviceId deviceId() {
86         return deviceId;
87     }
88
89     /**
90      * Returns a begin Label id.
91      *
92      * @return begin Label id
93      */
94     public LabelResourceId beginLabel() {
95         return beginLabel;
96     }
97
98     /**
99      * Returns an end Label id.
100      *
101      * @return end Label id
102      */
103     public LabelResourceId endLabel() {
104         return endLabel;
105     }
106
107     /**
108      * Returns a begin Label id.
109      *
110      * @return current Used Maximal Label Id
111      */
112     public LabelResourceId currentUsedMaxLabelId() {
113         return currentUsedMaxLabelId;
114     }
115
116     /**
117      * Returns total number.
118      *
119      * @return the total label number
120      */
121     public long totalNum() {
122         return totalNum;
123     }
124
125     /**
126      * Returns used number.
127      *
128      * @return the used label number
129      */
130     public long usedNum() {
131         return usedNum;
132     }
133
134     /**
135      * Returns the Set of released label before.
136      *
137      * @return the Set of LabelResource
138      */
139     public Set<LabelResource> releaseLabelId() {
140         return releaseLabelId;
141     }
142
143     @Override
144     public int hashCode() {
145         return Objects.hash(this.deviceId, this.beginLabel, this.endLabel,
146                             this.totalNum, this.usedNum,
147                             this.currentUsedMaxLabelId, this.releaseLabelId);
148     }
149
150     @Override
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);
162         }
163         return false;
164     }
165
166     @Override
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();
175     }
176 }