Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +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 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onlab.util.Identifier; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | import static org.onosproject.net.pi.runtime.PiCounterType.INDIRECT; |
| 25 | |
| 26 | /** |
| 27 | * Identifier of an indirect counter cell in a protocol-independent pipeline. |
| 28 | */ |
| 29 | @Beta |
| 30 | public final class PiIndirectCounterCellId extends Identifier<String> implements PiCounterCellId { |
| 31 | |
| 32 | private final PiCounterId counterId; |
| 33 | private final long index; |
| 34 | |
| 35 | private PiIndirectCounterCellId(PiCounterId counterId, long index) { |
| 36 | super(counterId.toString() + "[" + index + "]"); |
| 37 | this.counterId = counterId; |
| 38 | this.index = index; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns a counter cell identifier for the given counter identifier and index. |
| 43 | * |
| 44 | * @param counterId counter identifier |
| 45 | * @param index index |
| 46 | * @return counter cell identifier |
| 47 | */ |
| 48 | public static PiIndirectCounterCellId of(PiCounterId counterId, long index) { |
| 49 | checkNotNull(counterId); |
| 50 | checkArgument(counterId.type() == INDIRECT, "Counter ID must be of type INDIRECT"); |
| 51 | checkArgument(index >= 0, "Index must be a positive integer"); |
| 52 | return new PiIndirectCounterCellId(counterId, index); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the index of this cell. |
| 57 | * |
| 58 | * @return cell index |
| 59 | */ |
| 60 | public long index() { |
| 61 | return index; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public PiCounterId counterId() { |
| 66 | return counterId; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public PiCounterType type() { |
| 71 | return INDIRECT; |
| 72 | } |
| 73 | } |