blob: dcc7c022a937f635fd2e19a28107938c0492c2a9 [file] [log] [blame]
Carmelo Casconeb045ddc2017-09-01 01:26:35 +02001/*
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020020import com.google.common.base.Objects;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020021
22import static com.google.common.base.Preconditions.checkArgument;
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Identifier of a counter of a protocol-independent pipeline.
27 */
28@Beta
Carmelo Cascone7f75be42017-09-07 14:37:02 +020029public final class PiCounterId {
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020030
Carmelo Cascone7f75be42017-09-07 14:37:02 +020031 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 Casconeb045ddc2017-09-01 01:26:35 +020037 }
38
39 /**
Carmelo Cascone7f75be42017-09-07 14:37:02 +020040 * Returns a counter identifier for the given name and type.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020041 *
42 * @param name counter name
Carmelo Cascone7f75be42017-09-07 14:37:02 +020043 * @param type counter type
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020044 * @return counter identifier
45 */
Carmelo Cascone7f75be42017-09-07 14:37:02 +020046 public static PiCounterId of(String name, PiCounterType type) {
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020047 checkNotNull(name);
Carmelo Cascone7f75be42017-09-07 14:37:02 +020048 checkNotNull(type);
49 checkArgument(!name.isEmpty(), "Name can't be empty");
50 return new PiCounterId(name, type);
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020051 }
52
53 /**
54 * Returns the name of the counter.
55 *
56 * @return counter name
57 */
58 public String name() {
Carmelo Cascone7f75be42017-09-07 14:37:02 +020059 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 Casconeb045ddc2017-09-01 01:26:35 +020092 }
93}