blob: 34af8bf9bb7e19e4ce53ac312385302e53c6e03b [file] [log] [blame]
Frank Wangd7e3b4b2017-09-24 13:37:54 +09001/*
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;
20import com.google.common.base.Objects;
21import org.onosproject.net.meter.MeterCellId;
22import org.onosproject.net.pi.model.PiMeterId;
23import org.onosproject.net.pi.model.PiMeterType;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Identifier of a meter cell in a protocol-independent pipeline.
30 */
31@Beta
32public final class PiMeterCellId implements MeterCellId {
33
34 private final PiMeterId meterId;
35 private final PiMeterType meterType;
36 private final long index;
37 private final PiTableEntry tableEntry;
38
39 private PiMeterCellId(PiMeterId meterId, PiMeterType meterType, long index,
40 PiTableEntry tableEntry) {
41 this.meterId = meterId;
42 this.meterType = meterType;
43 this.index = index;
44 this.tableEntry = tableEntry;
45 }
46
47 /**
48 * Returns the identifier of the meter instance where this cell is contained.
Carmelo Cascone81929aa2018-04-07 01:38:55 -070049 * Meaningful only if the meter is of type {@link PiMeterType#DIRECT}, otherwise returns null.
Frank Wangd7e3b4b2017-09-24 13:37:54 +090050 *
51 * @return meter identifier
52 */
53 public PiMeterId meterId() {
54 return meterId;
55 }
56
57 /**
58 * Returns the type of the meter identified.
59 *
60 * @return meter type
61 */
62 public PiMeterType meterType() {
63 return meterType;
64 }
65
66 /**
67 * Returns the meter index to which this cell ID is associated.
68 * Meaningful only if the meter is of type {@link PiMeterType#INDIRECT}.
69 *
70 * @return meter index
71 */
72 public long index() {
73 return index;
74 }
75
76 /**
77 * Returns the table entry to which this cell ID is associated.
78 * Meaningful only if the meter is of type {@link PiMeterType#DIRECT}, otherwise returns null.
79 *
80 * @return PI table entry or null
81 */
82 public PiTableEntry tableEntry() {
83 return tableEntry;
84 }
85
86 @Override
87 public MeterCellType type() {
88 return MeterCellType.PIPELINE_INDEPENDENT;
89 }
90
91 /**
92 * Return a direct meter cell ID for the given meter ID and table entry.
93 *
Frank Wangd7e3b4b2017-09-24 13:37:54 +090094 * @param tableEntry table entry
95 * @return meter cell ID
96 */
Carmelo Cascone81929aa2018-04-07 01:38:55 -070097 public static PiMeterCellId ofDirect(PiTableEntry tableEntry) {
Frank Wangd7e3b4b2017-09-24 13:37:54 +090098 checkNotNull(tableEntry);
Carmelo Cascone81929aa2018-04-07 01:38:55 -070099 return new PiMeterCellId(null, PiMeterType.DIRECT, -1, tableEntry);
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900100 }
101
102 /**
103 * Return an indirect meter cell ID for the given meter ID and index.
104 *
105 * @param meterId meter ID
106 * @param index index
107 * @return meter cell ID
108 */
109 public static PiMeterCellId ofIndirect(PiMeterId meterId, long index) {
110 checkNotNull(meterId);
111 checkArgument(index >= 0, "Index must be a positive number");
112 return new PiMeterCellId(meterId, PiMeterType.INDIRECT, index, null);
113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj == null || getClass() != obj.getClass()) {
121 return false;
122 }
123 final PiMeterCellId other = (PiMeterCellId) obj;
124 return Objects.equal(this.meterId, other.meterId)
125 && Objects.equal(this.meterType, other.meterType)
126 && Objects.equal(this.index, other.index)
127 && Objects.equal(this.tableEntry, other.tableEntry);
128 }
129
130 @Override
131 public int hashCode() {
132 return Objects.hashCode(meterId, meterType, index, tableEntry);
133 }
134
135 @Override
136 public String toString() {
Carmelo Cascone81929aa2018-04-07 01:38:55 -0700137 return meterType == PiMeterType.DIRECT
138 ? tableEntry.toString()
139 : meterId.toString() + ':' + String.valueOf(index);
Frank Wangd7e3b4b2017-09-24 13:37:54 +0900140 }
141}