Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.collect.ImmutableList; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collection; |
Wailok Shum | 873b6b9 | 2021-08-06 16:23:36 +0800 | [diff] [blame^] | 27 | import java.util.Collections; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Configuration of a meter cell of a protocol-independent pipeline. |
| 34 | */ |
| 35 | @Beta |
| 36 | public 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 Shum | 873b6b9 | 2021-08-06 16:23:36 +0800 | [diff] [blame^] | 70 | /** |
| 71 | * Check if the config represents a modify operation. |
| 72 | * |
| 73 | * @return true if there are exactly 2 bands |
| 74 | */ |
| 75 | public boolean isModify() { |
| 76 | return piMeterBands.size() == 2; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check if the config represents a reset operation. |
| 81 | * |
| 82 | * @return true if there is no band. |
| 83 | */ |
| 84 | public boolean isReset() { |
| 85 | return piMeterBands.isEmpty(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns a PiMeterCellConfig with no bands. |
| 90 | * Used to reset a PI meter cell. |
| 91 | * |
| 92 | * @param piMeterCellId the PiMeterCellId need to be reset |
| 93 | * @return a PiMeterCellConfig with no bands |
| 94 | */ |
| 95 | public static PiMeterCellConfig reset(PiMeterCellId piMeterCellId) { |
| 96 | return new PiMeterCellConfig(piMeterCellId, Collections.emptyList()); |
| 97 | } |
| 98 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 99 | @Override |
| 100 | public PiEntityType piEntityType() { |
| 101 | return PiEntityType.METER_CELL_CONFIG; |
| 102 | } |
| 103 | |
| 104 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 105 | public PiMeterCellHandle handle(DeviceId deviceId) { |
| 106 | return PiMeterCellHandle.of(deviceId, this); |
| 107 | } |
| 108 | |
| 109 | @Override |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 110 | public boolean equals(Object o) { |
| 111 | if (this == o) { |
| 112 | return true; |
| 113 | } |
| 114 | if (!(o instanceof PiMeterCellConfig)) { |
| 115 | return false; |
| 116 | } |
| 117 | PiMeterCellConfig that = (PiMeterCellConfig) o; |
| 118 | |
| 119 | return piMeterBands.containsAll((that.piMeterBands)) && |
| 120 | piMeterBands.size() == that.piMeterBands.size() && |
| 121 | Objects.equal(cellId, that.cellId); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int hashCode() { |
| 126 | return Objects.hashCode(cellId, piMeterBands); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | return MoreObjects.toStringHelper(this) |
| 132 | .add("cellId", cellId) |
| 133 | .add("meterBands", piMeterBands) |
| 134 | .toString(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns a meter cell configuration builder. |
| 139 | * |
| 140 | * @return a new builder |
| 141 | */ |
| 142 | public static PiMeterCellConfig.Builder builder() { |
| 143 | return new PiMeterCellConfig.Builder(); |
| 144 | } |
| 145 | |
| 146 | public static final class Builder { |
| 147 | private PiMeterCellId cellId; |
| 148 | private List<PiMeterBand> bands = new ArrayList<>(); |
| 149 | |
| 150 | |
| 151 | private Builder() { |
| 152 | // Hides constructor. |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Sets the meter cell identifier for this meter. |
| 157 | * |
| 158 | * @param meterCellId meter cell identifier |
| 159 | * @return this |
| 160 | */ |
| 161 | public PiMeterCellConfig.Builder withMeterCellId(PiMeterCellId meterCellId) { |
| 162 | this.cellId = meterCellId; |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Sets a meter band of this meter. |
| 169 | * |
| 170 | * @param band meter band |
| 171 | * @return this |
| 172 | */ |
| 173 | public PiMeterCellConfig.Builder withMeterBand(PiMeterBand band) { |
| 174 | this.bands.add(band); |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Builds the meter cell configuration. |
| 180 | * |
| 181 | * @return a new meter cell configuration |
| 182 | */ |
| 183 | public PiMeterCellConfig build() { |
| 184 | checkNotNull(cellId); |
| 185 | return new PiMeterCellConfig(cellId, bands); |
| 186 | } |
| 187 | } |
| 188 | } |