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 | |
| 19 | import com.google.common.annotations.Beta; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 20 | import com.google.common.base.Objects; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 21 | |
| 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Identifier of a counter of a protocol-independent pipeline. |
| 27 | */ |
| 28 | @Beta |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 29 | public final class PiCounterId { |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 30 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 31 | private final String name; |
| 32 | private final PiCounterType type; |
| 33 | |
| 34 | private PiCounterId(String name, PiCounterType type) { |
| 35 | this.name = name; |
| 36 | this.type = type; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 40 | * Returns a counter identifier for the given name and type. |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 41 | * |
| 42 | * @param name counter name |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 43 | * @param type counter type |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 44 | * @return counter identifier |
| 45 | */ |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 46 | public static PiCounterId of(String name, PiCounterType type) { |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 47 | checkNotNull(name); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 48 | checkNotNull(type); |
| 49 | checkArgument(!name.isEmpty(), "Name can't be empty"); |
| 50 | return new PiCounterId(name, type); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the name of the counter. |
| 55 | * |
| 56 | * @return counter name |
| 57 | */ |
| 58 | public String name() { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 59 | return this.name; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the type of the counter. |
| 64 | * |
| 65 | * @return counter type |
| 66 | */ |
| 67 | public PiCounterType type() { |
| 68 | return this.type; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public boolean equals(Object o) { |
| 73 | if (this == o) { |
| 74 | return true; |
| 75 | } |
| 76 | if (!(o instanceof PiCounterId)) { |
| 77 | return false; |
| 78 | } |
| 79 | PiCounterId that = (PiCounterId) o; |
| 80 | return Objects.equal(name, that.name) && |
| 81 | type == that.type; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hashCode(name, type); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String toString() { |
| 91 | return type.name().toLowerCase() + ":" + name; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 92 | } |
| 93 | } |