blob: f13e276af3bd38943936b8718f380361a5f0383e [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;
27import java.util.List;
28
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;
38 private final ImmutableList<PiMeterBand> piMeterBands;
39
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 */
46 private PiMeterCellConfig(PiMeterCellId cellId, Collection<PiMeterBand> piMeterBands) {
47 this.cellId = cellId;
48 this.piMeterBands = ImmutableList.copyOf(piMeterBands);
49 }
50
51 /**
52 * Returns the cell identifier.
53 *
54 * @return cell identifier
55 */
56 public PiMeterCellId cellId() {
57 return cellId;
58 }
59
60 /**
61 * Returns the collection of bands of this cell.
62 *
63 * @return meter bands
64 */
65 public Collection<PiMeterBand> meterBands() {
66 return piMeterBands;
67 }
68
69 @Override
70 public PiEntityType piEntityType() {
71 return PiEntityType.METER_CELL_CONFIG;
72 }
73
74 @Override
Carmelo Cascone4c289b72019-01-22 15:30:45 -080075 public PiMeterCellHandle handle(DeviceId deviceId) {
76 return PiMeterCellHandle.of(deviceId, this);
77 }
78
79 @Override
Frank Wangd7e3b4b2017-09-24 13:37:54 +090080 public boolean equals(Object o) {
81 if (this == o) {
82 return true;
83 }
84 if (!(o instanceof PiMeterCellConfig)) {
85 return false;
86 }
87 PiMeterCellConfig that = (PiMeterCellConfig) o;
88
89 return piMeterBands.containsAll((that.piMeterBands)) &&
90 piMeterBands.size() == that.piMeterBands.size() &&
91 Objects.equal(cellId, that.cellId);
92 }
93
94 @Override
95 public int hashCode() {
96 return Objects.hashCode(cellId, piMeterBands);
97 }
98
99 @Override
100 public String toString() {
101 return MoreObjects.toStringHelper(this)
102 .add("cellId", cellId)
103 .add("meterBands", piMeterBands)
104 .toString();
105 }
106
107 /**
108 * Returns a meter cell configuration builder.
109 *
110 * @return a new builder
111 */
112 public static PiMeterCellConfig.Builder builder() {
113 return new PiMeterCellConfig.Builder();
114 }
115
116 public static final class Builder {
117 private PiMeterCellId cellId;
118 private List<PiMeterBand> bands = new ArrayList<>();
119
120
121 private Builder() {
122 // Hides constructor.
123 }
124
125 /**
126 * Sets the meter cell identifier for this meter.
127 *
128 * @param meterCellId meter cell identifier
129 * @return this
130 */
131 public PiMeterCellConfig.Builder withMeterCellId(PiMeterCellId meterCellId) {
132 this.cellId = meterCellId;
133 return this;
134 }
135
136
137 /**
138 * Sets a meter band of this meter.
139 *
140 * @param band meter band
141 * @return this
142 */
143 public PiMeterCellConfig.Builder withMeterBand(PiMeterBand band) {
144 this.bands.add(band);
145 return this;
146 }
147
148 /**
149 * Builds the meter cell configuration.
150 *
151 * @return a new meter cell configuration
152 */
153 public PiMeterCellConfig build() {
154 checkNotNull(cellId);
155 return new PiMeterCellConfig(cellId, bands);
156 }
157 }
158}