blob: 09cf7bfbc82494eba6210380c56e4051bcc39279 [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.
49 *
50 * @return meter identifier
51 */
52 public PiMeterId meterId() {
53 return meterId;
54 }
55
56 /**
57 * Returns the type of the meter identified.
58 *
59 * @return meter type
60 */
61 public PiMeterType meterType() {
62 return meterType;
63 }
64
65 /**
66 * Returns the meter index to which this cell ID is associated.
67 * Meaningful only if the meter is of type {@link PiMeterType#INDIRECT}.
68 *
69 * @return meter index
70 */
71 public long index() {
72 return index;
73 }
74
75 /**
76 * Returns the table entry to which this cell ID is associated.
77 * Meaningful only if the meter is of type {@link PiMeterType#DIRECT}, otherwise returns null.
78 *
79 * @return PI table entry or null
80 */
81 public PiTableEntry tableEntry() {
82 return tableEntry;
83 }
84
85 @Override
86 public MeterCellType type() {
87 return MeterCellType.PIPELINE_INDEPENDENT;
88 }
89
90 /**
91 * Return a direct meter cell ID for the given meter ID and table entry.
92 *
93 * @param meterId meter ID
94 * @param tableEntry table entry
95 * @return meter cell ID
96 */
97 public static PiMeterCellId ofDirect(PiMeterId meterId, PiTableEntry tableEntry) {
98 checkNotNull(meterId);
99 checkNotNull(tableEntry);
100 return new PiMeterCellId(meterId, PiMeterType.DIRECT, -1, tableEntry);
101 }
102
103 /**
104 * Return an indirect meter cell ID for the given meter ID and index.
105 *
106 * @param meterId meter ID
107 * @param index index
108 * @return meter cell ID
109 */
110 public static PiMeterCellId ofIndirect(PiMeterId meterId, long index) {
111 checkNotNull(meterId);
112 checkArgument(index >= 0, "Index must be a positive number");
113 return new PiMeterCellId(meterId, PiMeterType.INDIRECT, index, null);
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (obj == null || getClass() != obj.getClass()) {
122 return false;
123 }
124 final PiMeterCellId other = (PiMeterCellId) obj;
125 return Objects.equal(this.meterId, other.meterId)
126 && Objects.equal(this.meterType, other.meterType)
127 && Objects.equal(this.index, other.index)
128 && Objects.equal(this.tableEntry, other.tableEntry);
129 }
130
131 @Override
132 public int hashCode() {
133 return Objects.hashCode(meterId, meterType, index, tableEntry);
134 }
135
136 @Override
137 public String toString() {
138 return meterId.toString() + ':'
139 + (meterType == PiMeterType.DIRECT ? tableEntry.toString() : String.valueOf(index));
140 }
141}