blob: 015d86b81606d92e37c48791211b63f40e3cc03b [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 Casconecb0a49c2017-10-03 14:32:23 +020021import org.onlab.util.Identifier;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020022
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Identifier of a counter of a protocol-independent pipeline.
28 */
29@Beta
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020030public final class PiCounterId extends Identifier<String> {
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020031
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020032 private final String scope;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020033 private final String name;
34 private final PiCounterType type;
35
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020036 private PiCounterId(String scope, String name, PiCounterType type) {
37 super((!scope.isEmpty() ? scope + "." : "") + name);
38 this.scope = scope;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020039 this.name = name;
40 this.type = type;
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020041 }
42
43 /**
Carmelo Cascone7f75be42017-09-07 14:37:02 +020044 * Returns a counter identifier for the given name and type.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020045 *
46 * @param name counter name
Carmelo Cascone7f75be42017-09-07 14:37:02 +020047 * @param type counter type
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020048 * @return counter identifier
49 */
Carmelo Cascone7f75be42017-09-07 14:37:02 +020050 public static PiCounterId of(String name, PiCounterType type) {
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020051 checkNotNull(name);
Carmelo Cascone7f75be42017-09-07 14:37:02 +020052 checkNotNull(type);
53 checkArgument(!name.isEmpty(), "Name can't be empty");
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020054 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 Casconeb045ddc2017-09-01 01:26:35 +020081 }
82
83 /**
84 * Returns the name of the counter.
85 *
86 * @return counter name
87 */
88 public String name() {
Carmelo Cascone7f75be42017-09-07 14:37:02 +020089 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 Casconecb0a49c2017-10-03 14:32:23 +0200110 return Objects.equal(id(), that.id()) &&
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200111 type == that.type;
112 }
113
114 @Override
115 public int hashCode() {
Carmelo Casconecb0a49c2017-10-03 14:32:23 +0200116 return Objects.hashCode(id(), type);
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200117 }
118}