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