blob: da8a8822d43af10e8512bc056071b805cd4c5bc8 [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
Carmelo Cascone87892e22017-11-13 16:01:29 -080019import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import org.onosproject.net.pi.model.PiCounterId;
22import org.onosproject.net.pi.model.PiCounterType;
23
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020027/**
Carmelo Cascone7f75be42017-09-07 14:37:02 +020028 * Identifier of a counter cell in a protocol-independent pipeline.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020029 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080030@Beta
31public final class PiCounterCellId {
32
33 private final PiCounterId counterId;
34 private final PiCounterType counterType;
35 private final long index;
36 private final PiTableEntry tableEntry;
37
38 private PiCounterCellId(PiCounterId counterId, PiCounterType counterType, long index,
39 PiTableEntry tableEntry) {
40 this.counterId = counterId;
41 this.counterType = counterType;
42 this.index = index;
43 this.tableEntry = tableEntry;
44 }
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020045
46 /**
Carmelo Cascone7f75be42017-09-07 14:37:02 +020047 * Returns the identifier of the counter instance where this cell is contained.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020048 *
49 * @return counter identifier
50 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080051 public PiCounterId counterId() {
52 return counterId;
53 }
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020054
55 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -080056 * Returns the type of the counter identified.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020057 *
Carmelo Cascone7f75be42017-09-07 14:37:02 +020058 * @return counter type
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020059 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080060 public PiCounterType counterType() {
61 return counterType;
62 }
63
64 /**
65 * Returns the counter index to which this cell ID is associated. Meaningful only if the counter is of type {@link
66 * PiCounterType#INDIRECT}.
67 *
68 * @return counter index
69 */
70 public long index() {
71 return index;
72 }
73
74 /**
75 * Returns the table entry to which this cell ID is associated. Meaningful only if the counter is of type {@link
76 * PiCounterType#DIRECT}, otherwise returns null.
77 *
78 * @return PI table entry or null
79 */
80 public PiTableEntry tableEntry() {
81 return tableEntry;
82 }
83
84 /**
85 * Return a direct counter cell ID for the given counter ID and table entry.
86 *
87 * @param counterId counter ID
88 * @param tableEntry table entry
89 * @return counter cell ID
90 */
91 public static PiCounterCellId ofDirect(PiCounterId counterId, PiTableEntry tableEntry) {
92 checkNotNull(counterId);
93 checkNotNull(tableEntry);
94 return new PiCounterCellId(counterId, PiCounterType.DIRECT, -1, tableEntry);
95 }
96
97 /**
98 * Return an indirect counter cell ID for the given counter ID and index.
99 *
100 * @param counterId counter ID
101 * @param index index
102 * @return counter cell ID
103 */
104 public static PiCounterCellId ofIndirect(PiCounterId counterId, long index) {
105 checkNotNull(counterId);
106 checkArgument(index >= 0, "Index must be a positive number");
107 return new PiCounterCellId(counterId, PiCounterType.INDIRECT, index, null);
108 }
109
110 @Override
111 public boolean equals(Object obj) {
112 if (this == obj) {
113 return true;
114 }
115 if (obj == null || getClass() != obj.getClass()) {
116 return false;
117 }
118 final PiCounterCellId other = (PiCounterCellId) obj;
119 return Objects.equal(this.counterId, other.counterId)
120 && Objects.equal(this.counterType, other.counterType)
121 && Objects.equal(this.index, other.index)
122 && Objects.equal(this.tableEntry, other.tableEntry);
123 }
124
125 @Override
126 public int hashCode() {
127 return Objects.hashCode(counterId, counterType, index, tableEntry);
128 }
129
130 @Override
131 public String toString() {
132 return counterId.toString() + ':'
133 + (counterType == PiCounterType.DIRECT ? tableEntry.toString() : String.valueOf(index));
134 }
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200135}