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