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; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Collection; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Configuration of a meter cell of a protocol-independent pipeline. |
| 32 | */ |
| 33 | @Beta |
| 34 | public final class PiMeterCellConfig implements PiEntity { |
| 35 | |
| 36 | private final PiMeterCellId cellId; |
| 37 | private final ImmutableList<PiMeterBand> piMeterBands; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new meter cell configuration for the given cell identifier and meter bands. |
| 41 | * |
| 42 | * @param cellId meter cell identifier |
| 43 | * @param piMeterBands meter bands |
| 44 | */ |
| 45 | private PiMeterCellConfig(PiMeterCellId cellId, Collection<PiMeterBand> piMeterBands) { |
| 46 | this.cellId = cellId; |
| 47 | this.piMeterBands = ImmutableList.copyOf(piMeterBands); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the cell identifier. |
| 52 | * |
| 53 | * @return cell identifier |
| 54 | */ |
| 55 | public PiMeterCellId cellId() { |
| 56 | return cellId; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the collection of bands of this cell. |
| 61 | * |
| 62 | * @return meter bands |
| 63 | */ |
| 64 | public Collection<PiMeterBand> meterBands() { |
| 65 | return piMeterBands; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public PiEntityType piEntityType() { |
| 70 | return PiEntityType.METER_CELL_CONFIG; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean equals(Object o) { |
| 75 | if (this == o) { |
| 76 | return true; |
| 77 | } |
| 78 | if (!(o instanceof PiMeterCellConfig)) { |
| 79 | return false; |
| 80 | } |
| 81 | PiMeterCellConfig that = (PiMeterCellConfig) o; |
| 82 | |
| 83 | return piMeterBands.containsAll((that.piMeterBands)) && |
| 84 | piMeterBands.size() == that.piMeterBands.size() && |
| 85 | Objects.equal(cellId, that.cellId); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int hashCode() { |
| 90 | return Objects.hashCode(cellId, piMeterBands); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String toString() { |
| 95 | return MoreObjects.toStringHelper(this) |
| 96 | .add("cellId", cellId) |
| 97 | .add("meterBands", piMeterBands) |
| 98 | .toString(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns a meter cell configuration builder. |
| 103 | * |
| 104 | * @return a new builder |
| 105 | */ |
| 106 | public static PiMeterCellConfig.Builder builder() { |
| 107 | return new PiMeterCellConfig.Builder(); |
| 108 | } |
| 109 | |
| 110 | public static final class Builder { |
| 111 | private PiMeterCellId cellId; |
| 112 | private List<PiMeterBand> bands = new ArrayList<>(); |
| 113 | |
| 114 | |
| 115 | private Builder() { |
| 116 | // Hides constructor. |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets the meter cell identifier for this meter. |
| 121 | * |
| 122 | * @param meterCellId meter cell identifier |
| 123 | * @return this |
| 124 | */ |
| 125 | public PiMeterCellConfig.Builder withMeterCellId(PiMeterCellId meterCellId) { |
| 126 | this.cellId = meterCellId; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Sets a meter band of this meter. |
| 133 | * |
| 134 | * @param band meter band |
| 135 | * @return this |
| 136 | */ |
| 137 | public PiMeterCellConfig.Builder withMeterBand(PiMeterBand band) { |
| 138 | this.bands.add(band); |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Builds the meter cell configuration. |
| 144 | * |
| 145 | * @return a new meter cell configuration |
| 146 | */ |
| 147 | public PiMeterCellConfig build() { |
| 148 | checkNotNull(cellId); |
| 149 | return new PiMeterCellConfig(cellId, bands); |
| 150 | } |
| 151 | } |
| 152 | } |