blob: d3da3407fa79cb60f1c6d3500ead0ce8c1471806 [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;
22import com.google.common.collect.ImmutableList;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080023import org.onosproject.net.DeviceId;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090024
25import java.util.ArrayList;
26import java.util.Collection;
Wailok Shum96642092021-08-06 16:23:36 +080027import java.util.Collections;
Frank Wangd7e3b4b2017-09-24 13:37:54 +090028import java.util.List;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Configuration of a meter cell of a protocol-independent pipeline.
34 */
35@Beta
36public final class PiMeterCellConfig implements PiEntity {
37
38 private final PiMeterCellId cellId;
39 private final ImmutableList<PiMeterBand> piMeterBands;
40
41 /**
42 * Creates a new meter cell configuration for the given cell identifier and meter bands.
43 *
44 * @param cellId meter cell identifier
45 * @param piMeterBands meter bands
46 */
47 private PiMeterCellConfig(PiMeterCellId cellId, Collection<PiMeterBand> piMeterBands) {
48 this.cellId = cellId;
49 this.piMeterBands = ImmutableList.copyOf(piMeterBands);
50 }
51
52 /**
53 * Returns the cell identifier.
54 *
55 * @return cell identifier
56 */
57 public PiMeterCellId cellId() {
58 return cellId;
59 }
60
61 /**
62 * Returns the collection of bands of this cell.
63 *
64 * @return meter bands
65 */
66 public Collection<PiMeterBand> meterBands() {
67 return piMeterBands;
68 }
69
Wailok Shum96642092021-08-06 16:23:36 +080070 /**
71 * Check if the config represents a modify operation.
Wailok Shum6d42cff2021-08-22 19:40:13 +080072 * Or it is a non-default config read from south bound.
Wailok Shum96642092021-08-06 16:23:36 +080073 *
74 * @return true if there are exactly 2 bands
75 */
Wailok Shum6d42cff2021-08-22 19:40:13 +080076 public boolean isModifyConfig() {
Wailok Shum96642092021-08-06 16:23:36 +080077 return piMeterBands.size() == 2;
78 }
79
80 /**
81 * Check if the config represents a reset operation.
Wailok Shum6d42cff2021-08-22 19:40:13 +080082 * Or it is a default config read from south bound.
Wailok Shum96642092021-08-06 16:23:36 +080083 *
Wailok Shum6d42cff2021-08-22 19:40:13 +080084 * @return true if there is no band
Wailok Shum96642092021-08-06 16:23:36 +080085 */
Wailok Shum6d42cff2021-08-22 19:40:13 +080086 public boolean isDefaultConfig() {
Wailok Shum96642092021-08-06 16:23:36 +080087 return piMeterBands.isEmpty();
88 }
89
90 /**
91 * Returns a PiMeterCellConfig with no bands.
92 * Used to reset a PI meter cell.
93 *
94 * @param piMeterCellId the PiMeterCellId need to be reset
95 * @return a PiMeterCellConfig with no bands
96 */
97 public static PiMeterCellConfig reset(PiMeterCellId piMeterCellId) {
98 return new PiMeterCellConfig(piMeterCellId, Collections.emptyList());
99 }
100
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900101 @Override
102 public PiEntityType piEntityType() {
103 return PiEntityType.METER_CELL_CONFIG;
104 }
105
106 @Override
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800107 public PiMeterCellHandle handle(DeviceId deviceId) {
108 return PiMeterCellHandle.of(deviceId, this);
109 }
110
111 @Override
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900112 public boolean equals(Object o) {
113 if (this == o) {
114 return true;
115 }
116 if (!(o instanceof PiMeterCellConfig)) {
117 return false;
118 }
119 PiMeterCellConfig that = (PiMeterCellConfig) o;
120
121 return piMeterBands.containsAll((that.piMeterBands)) &&
122 piMeterBands.size() == that.piMeterBands.size() &&
123 Objects.equal(cellId, that.cellId);
124 }
125
126 @Override
127 public int hashCode() {
128 return Objects.hashCode(cellId, piMeterBands);
129 }
130
131 @Override
132 public String toString() {
133 return MoreObjects.toStringHelper(this)
134 .add("cellId", cellId)
135 .add("meterBands", piMeterBands)
136 .toString();
137 }
138
139 /**
140 * Returns a meter cell configuration builder.
141 *
142 * @return a new builder
143 */
144 public static PiMeterCellConfig.Builder builder() {
145 return new PiMeterCellConfig.Builder();
146 }
147
148 public static final class Builder {
149 private PiMeterCellId cellId;
150 private List<PiMeterBand> bands = new ArrayList<>();
151
152
153 private Builder() {
154 // Hides constructor.
155 }
156
157 /**
158 * Sets the meter cell identifier for this meter.
159 *
160 * @param meterCellId meter cell identifier
161 * @return this
162 */
163 public PiMeterCellConfig.Builder withMeterCellId(PiMeterCellId meterCellId) {
164 this.cellId = meterCellId;
165 return this;
166 }
167
168
169 /**
170 * Sets a meter band of this meter.
171 *
172 * @param band meter band
173 * @return this
174 */
175 public PiMeterCellConfig.Builder withMeterBand(PiMeterBand band) {
176 this.bands.add(band);
177 return this;
178 }
179
180 /**
181 * Builds the meter cell configuration.
182 *
183 * @return a new meter cell configuration
184 */
185 public PiMeterCellConfig build() {
186 checkNotNull(cellId);
187 return new PiMeterCellConfig(cellId, bands);
188 }
189 }
190}