blob: 27f145f8ff2e7f17000604272ca6f07a7ab9d3a2 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
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'Connor6de2e202015-05-21 14:30:41 -070016package org.onosproject.incubator.net.resource.label;
jccfff0de92015-03-28 01:40:08 -070017
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.Collections;
21import java.util.Objects;
22import java.util.Set;
23
Brian O'Connor893b9222015-06-25 15:07:04 -040024import com.google.common.annotations.Beta;
jccfff0de92015-03-28 01:40:08 -070025import org.onosproject.net.DeviceId;
26
27import com.google.common.base.MoreObjects;
28import com.google.common.collect.ImmutableSet;
29
30/**
31 * Abstraction of the capacity of device label resource or global label
Sho SHIMIZUaf0454b2015-08-17 12:08:43 -070032 * 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
jccfff0de92015-03-28 01:40:08 -070035 * labels by calculating with three attributes, beginLabel,endLabel and
Sho SHIMIZUaf0454b2015-08-17 12:08:43 -070036 * currentUsedMaxLabelId.
jccfff0de92015-03-28 01:40:08 -070037 */
Brian O'Connor893b9222015-06-25 15:07:04 -040038@Beta
jccfff0de92015-03-28 01:40:08 -070039public 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 SHIMIZUaf0454b2015-08-17 12:08:43 -070066 * Used to update a pool in the store.
67 *
jccfff0de92015-03-28 01:40:08 -070068 * @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 SHIMIZUaf0454b2015-08-17 12:08:43 -0700114 * Returns an end Label id.
jccfff0de92015-03-28 01:40:08 -0700115 *
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}