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.Objects; |
| 21 | import org.onosproject.net.meter.MeterCellId; |
| 22 | import org.onosproject.net.pi.model.PiMeterId; |
| 23 | import org.onosproject.net.pi.model.PiMeterType; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Identifier of a meter cell in a protocol-independent pipeline. |
| 30 | */ |
| 31 | @Beta |
| 32 | public final class PiMeterCellId implements MeterCellId { |
| 33 | |
| 34 | private final PiMeterId meterId; |
| 35 | private final PiMeterType meterType; |
| 36 | private final long index; |
| 37 | private final PiTableEntry tableEntry; |
| 38 | |
| 39 | private PiMeterCellId(PiMeterId meterId, PiMeterType meterType, long index, |
| 40 | PiTableEntry tableEntry) { |
| 41 | this.meterId = meterId; |
| 42 | this.meterType = meterType; |
| 43 | this.index = index; |
| 44 | this.tableEntry = tableEntry; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the identifier of the meter instance where this cell is contained. |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 49 | * Meaningful only if the meter is of type {@link PiMeterType#DIRECT}, otherwise returns null. |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 50 | * |
| 51 | * @return meter identifier |
| 52 | */ |
| 53 | public PiMeterId meterId() { |
| 54 | return meterId; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the type of the meter identified. |
| 59 | * |
| 60 | * @return meter type |
| 61 | */ |
| 62 | public PiMeterType meterType() { |
| 63 | return meterType; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the meter index to which this cell ID is associated. |
| 68 | * Meaningful only if the meter is of type {@link PiMeterType#INDIRECT}. |
| 69 | * |
| 70 | * @return meter index |
| 71 | */ |
| 72 | public long index() { |
| 73 | return index; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the table entry to which this cell ID is associated. |
| 78 | * Meaningful only if the meter is of type {@link PiMeterType#DIRECT}, otherwise returns null. |
| 79 | * |
| 80 | * @return PI table entry or null |
| 81 | */ |
| 82 | public PiTableEntry tableEntry() { |
| 83 | return tableEntry; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public MeterCellType type() { |
| 88 | return MeterCellType.PIPELINE_INDEPENDENT; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return a direct meter cell ID for the given meter ID and table entry. |
| 93 | * |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 94 | * @param tableEntry table entry |
| 95 | * @return meter cell ID |
| 96 | */ |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 97 | public static PiMeterCellId ofDirect(PiTableEntry tableEntry) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 98 | checkNotNull(tableEntry); |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 99 | return new PiMeterCellId(null, PiMeterType.DIRECT, -1, tableEntry); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Return an indirect meter cell ID for the given meter ID and index. |
| 104 | * |
| 105 | * @param meterId meter ID |
| 106 | * @param index index |
| 107 | * @return meter cell ID |
| 108 | */ |
| 109 | public static PiMeterCellId ofIndirect(PiMeterId meterId, long index) { |
| 110 | checkNotNull(meterId); |
| 111 | checkArgument(index >= 0, "Index must be a positive number"); |
| 112 | return new PiMeterCellId(meterId, PiMeterType.INDIRECT, index, null); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean equals(Object obj) { |
| 117 | if (this == obj) { |
| 118 | return true; |
| 119 | } |
| 120 | if (obj == null || getClass() != obj.getClass()) { |
| 121 | return false; |
| 122 | } |
| 123 | final PiMeterCellId other = (PiMeterCellId) obj; |
| 124 | return Objects.equal(this.meterId, other.meterId) |
| 125 | && Objects.equal(this.meterType, other.meterType) |
| 126 | && Objects.equal(this.index, other.index) |
| 127 | && Objects.equal(this.tableEntry, other.tableEntry); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int hashCode() { |
| 132 | return Objects.hashCode(meterId, meterType, index, tableEntry); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String toString() { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 137 | return meterType == PiMeterType.DIRECT |
| 138 | ? tableEntry.toString() |
| 139 | : meterId.toString() + ':' + String.valueOf(index); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 140 | } |
| 141 | } |