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