blob: 4b9a70e9ddc31fae906488d5ea63e19b152c65a8 [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
Carmelo Cascone81929aa2018-04-07 01:38:55 -070038 private PiCounterCellId(PiCounterId counterId, PiCounterType counterType,
39 long index, PiTableEntry tableEntry) {
Carmelo Cascone87892e22017-11-13 16:01:29 -080040 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 Cascone81929aa2018-04-07 01:38:55 -070047 * Returns the identifier of the counter instance where this cell is
48 * contained. Meaningful only if the counter is of type {@link
49 * PiCounterType#INDIRECT}.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020050 *
51 * @return counter identifier
52 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080053 public PiCounterId counterId() {
54 return counterId;
55 }
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020056
57 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -080058 * Returns the type of the counter identified.
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020059 *
Carmelo Cascone7f75be42017-09-07 14:37:02 +020060 * @return counter type
Carmelo Casconeb045ddc2017-09-01 01:26:35 +020061 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080062 public PiCounterType counterType() {
63 return counterType;
64 }
65
66 /**
Carmelo Cascone81929aa2018-04-07 01:38:55 -070067 * Returns the counter index to which this cell ID is associated. Meaningful
68 * only if the counter is of type {@link PiCounterType#INDIRECT}.
Carmelo Cascone87892e22017-11-13 16:01:29 -080069 *
70 * @return counter index
71 */
72 public long index() {
73 return index;
74 }
75
76 /**
Carmelo Cascone81929aa2018-04-07 01:38:55 -070077 * Returns the table entry to which this cell ID is associated. Meaningful
78 * only if the counter is of type {@link PiCounterType#DIRECT}, otherwise
79 * returns null.
Carmelo Cascone87892e22017-11-13 16:01:29 -080080 *
81 * @return PI table entry or null
82 */
83 public PiTableEntry tableEntry() {
84 return tableEntry;
85 }
86
87 /**
Carmelo Cascone81929aa2018-04-07 01:38:55 -070088 * Return a direct counter cell ID for the given counter ID and table
89 * entry.
Carmelo Cascone87892e22017-11-13 16:01:29 -080090 *
Carmelo Cascone87892e22017-11-13 16:01:29 -080091 * @param tableEntry table entry
92 * @return counter cell ID
93 */
Carmelo Cascone81929aa2018-04-07 01:38:55 -070094 public static PiCounterCellId ofDirect(PiTableEntry tableEntry) {
Carmelo Cascone87892e22017-11-13 16:01:29 -080095 checkNotNull(tableEntry);
Carmelo Cascone81929aa2018-04-07 01:38:55 -070096 return new PiCounterCellId(null, PiCounterType.DIRECT, -1, tableEntry);
Carmelo Cascone87892e22017-11-13 16:01:29 -080097 }
98
99 /**
100 * Return an indirect counter cell ID for the given counter ID and index.
101 *
102 * @param counterId counter ID
103 * @param index index
104 * @return counter cell ID
105 */
106 public static PiCounterCellId ofIndirect(PiCounterId counterId, long index) {
107 checkNotNull(counterId);
108 checkArgument(index >= 0, "Index must be a positive number");
109 return new PiCounterCellId(counterId, PiCounterType.INDIRECT, index, null);
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 }
117 if (obj == null || getClass() != obj.getClass()) {
118 return false;
119 }
120 final PiCounterCellId other = (PiCounterCellId) obj;
121 return Objects.equal(this.counterId, other.counterId)
122 && Objects.equal(this.counterType, other.counterType)
123 && Objects.equal(this.index, other.index)
124 && Objects.equal(this.tableEntry, other.tableEntry);
125 }
126
127 @Override
128 public int hashCode() {
129 return Objects.hashCode(counterId, counterType, index, tableEntry);
130 }
131
132 @Override
133 public String toString() {
Carmelo Cascone81929aa2018-04-07 01:38:55 -0700134 return counterType == PiCounterType.DIRECT
135 ? tableEntry.toString()
136 : counterId.toString() + ':' + String.valueOf(index);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800137 }
Carmelo Casconeb045ddc2017-09-01 01:26:35 +0200138}