blob: ecbfe079e9b8fc95994bd6501592365146e86749 [file] [log] [blame]
Frank Wangd7e3b4b2017-09-24 13:37:54 +09001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
pierventrec0914ec2021-08-27 15:25:02 +020022import com.google.common.collect.ImmutableMap;
23import com.google.common.collect.Maps;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080024import org.onosproject.net.DeviceId;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090025
Wailok Shum96642092021-08-06 16:23:36 +080026import java.util.Collections;
pierventrec0914ec2021-08-27 15:25:02 +020027import java.util.Map;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090028
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Configuration of a meter cell of a protocol-independent pipeline.
33 */
34@Beta
35public final class PiMeterCellConfig implements PiEntity {
36
37 private final PiMeterCellId cellId;
pierventrec0914ec2021-08-27 15:25:02 +020038 private final ImmutableMap<PiMeterBandType, PiMeterBand> piMeterBands;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090039
40 /**
41 * Creates a new meter cell configuration for the given cell identifier and meter bands.
42 *
43 * @param cellId meter cell identifier
44 * @param piMeterBands meter bands
45 */
pierventrec0914ec2021-08-27 15:25:02 +020046 private PiMeterCellConfig(PiMeterCellId cellId, Map<PiMeterBandType, PiMeterBand> piMeterBands) {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090047 this.cellId = cellId;
pierventrec0914ec2021-08-27 15:25:02 +020048 this.piMeterBands = ImmutableMap.copyOf(piMeterBands);
Frank Wangd7e3b4b2017-09-24 13:37:54 +090049 }
50
51 /**
52 * Returns the cell identifier.
53 *
54 * @return cell identifier
55 */
56 public PiMeterCellId cellId() {
57 return cellId;
58 }
59
60 /**
pierventrec0914ec2021-08-27 15:25:02 +020061 * Returns the map of bands of this cell.
Frank Wangd7e3b4b2017-09-24 13:37:54 +090062 *
63 * @return meter bands
64 */
pierventrec0914ec2021-08-27 15:25:02 +020065 public Map<PiMeterBandType, PiMeterBand> meterBands() {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090066 return piMeterBands;
67 }
68
Wailok Shum96642092021-08-06 16:23:36 +080069 /**
70 * Check if the config represents a modify operation.
Wailok Shum6d42cff2021-08-22 19:40:13 +080071 * Or it is a non-default config read from south bound.
Wailok Shum96642092021-08-06 16:23:36 +080072 *
73 * @return true if there are exactly 2 bands
74 */
Wailok Shum6d42cff2021-08-22 19:40:13 +080075 public boolean isModifyConfig() {
Wailok Shum96642092021-08-06 16:23:36 +080076 return piMeterBands.size() == 2;
77 }
78
79 /**
80 * Check if the config represents a reset operation.
Wailok Shum6d42cff2021-08-22 19:40:13 +080081 * Or it is a default config read from south bound.
Wailok Shum96642092021-08-06 16:23:36 +080082 *
Wailok Shum6d42cff2021-08-22 19:40:13 +080083 * @return true if there is no band
Wailok Shum96642092021-08-06 16:23:36 +080084 */
Wailok Shum6d42cff2021-08-22 19:40:13 +080085 public boolean isDefaultConfig() {
Wailok Shum96642092021-08-06 16:23:36 +080086 return piMeterBands.isEmpty();
87 }
88
89 /**
pierventrec0914ec2021-08-27 15:25:02 +020090 * Returns the committed configuration if present.
91 *
92 * @return the committed band. Null otherwise
93 */
94 public PiMeterBand committedBand() {
95 return piMeterBands.get(PiMeterBandType.COMMITTED);
96 }
97
98 /**
99 * Returns the peak configuration if present.
100 *
101 * @return the peak band. Null otherwise
102 */
103 public PiMeterBand peakBand() {
104 return piMeterBands.get(PiMeterBandType.PEAK);
105 }
106
107 /**
Wailok Shum96642092021-08-06 16:23:36 +0800108 * Returns a PiMeterCellConfig with no bands.
109 * Used to reset a PI meter cell.
110 *
111 * @param piMeterCellId the PiMeterCellId need to be reset
112 * @return a PiMeterCellConfig with no bands
113 */
114 public static PiMeterCellConfig reset(PiMeterCellId piMeterCellId) {
pierventrec0914ec2021-08-27 15:25:02 +0200115 return new PiMeterCellConfig(piMeterCellId, Collections.emptyMap());
Wailok Shum96642092021-08-06 16:23:36 +0800116 }
117
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900118 @Override
119 public PiEntityType piEntityType() {
120 return PiEntityType.METER_CELL_CONFIG;
121 }
122
123 @Override
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800124 public PiMeterCellHandle handle(DeviceId deviceId) {
125 return PiMeterCellHandle.of(deviceId, this);
126 }
127
128 @Override
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900129 public boolean equals(Object o) {
130 if (this == o) {
131 return true;
132 }
133 if (!(o instanceof PiMeterCellConfig)) {
134 return false;
135 }
136 PiMeterCellConfig that = (PiMeterCellConfig) o;
137
pierventrec0914ec2021-08-27 15:25:02 +0200138 return Objects.equal(piMeterBands, that.piMeterBands) &&
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900139 Objects.equal(cellId, that.cellId);
140 }
141
142 @Override
143 public int hashCode() {
144 return Objects.hashCode(cellId, piMeterBands);
145 }
146
147 @Override
148 public String toString() {
149 return MoreObjects.toStringHelper(this)
150 .add("cellId", cellId)
151 .add("meterBands", piMeterBands)
152 .toString();
153 }
154
155 /**
156 * Returns a meter cell configuration builder.
157 *
158 * @return a new builder
159 */
160 public static PiMeterCellConfig.Builder builder() {
161 return new PiMeterCellConfig.Builder();
162 }
163
164 public static final class Builder {
pierventrec0914ec2021-08-27 15:25:02 +0200165 private PiMeterCellId cellId;
166 private Map<PiMeterBandType, PiMeterBand> bands = Maps.newHashMap();
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900167
168
169 private Builder() {
170 // Hides constructor.
171 }
172
173 /**
174 * Sets the meter cell identifier for this meter.
175 *
176 * @param meterCellId meter cell identifier
177 * @return this
178 */
179 public PiMeterCellConfig.Builder withMeterCellId(PiMeterCellId meterCellId) {
180 this.cellId = meterCellId;
181 return this;
182 }
183
184
185 /**
186 * Sets a meter band of this meter.
187 *
188 * @param band meter band
189 * @return this
190 */
191 public PiMeterCellConfig.Builder withMeterBand(PiMeterBand band) {
pierventrec0914ec2021-08-27 15:25:02 +0200192 this.bands.put(band.type(), band);
193 return this;
194 }
195
196 /**
197 * Sets the committed band of this meter.
198 *
199 * @param rate committed rate
200 * @param burst committed burst
201 * @return this
202 */
203 public PiMeterCellConfig.Builder withCommittedBand(long rate, long burst) {
204 this.bands.put(PiMeterBandType.COMMITTED, new PiMeterBand(PiMeterBandType.COMMITTED, rate, burst));
205 return this;
206 }
207
208 /**
209 * Sets the peak band of this meter.
210 *
211 * @param rate peak rate
212 * @param burst peak burst
213 * @return this
214 */
215 public PiMeterCellConfig.Builder withPeakBand(long rate, long burst) {
216 this.bands.put(PiMeterBandType.PEAK, new PiMeterBand(PiMeterBandType.PEAK, rate, burst));
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900217 return this;
218 }
219
220 /**
221 * Builds the meter cell configuration.
222 *
223 * @return a new meter cell configuration
224 */
225 public PiMeterCellConfig build() {
226 checkNotNull(cellId);
227 return new PiMeterCellConfig(cellId, bands);
228 }
229 }
230}