karthik1977 | 42c0a0e | 2024-03-02 18:55:06 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024-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 | package org.onosproject.sflow; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
karthik1977 | f106597 | 2024-03-16 15:21:57 +0530 | [diff] [blame^] | 19 | import java.nio.ByteBuffer; |
| 20 | import org.onlab.packet.BasePacket; |
| 21 | import org.onlab.packet.Deserializer; |
| 22 | |
| 23 | import java.util.function.BiPredicate; |
karthik1977 | 42c0a0e | 2024-03-02 18:55:06 +0530 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Represents VLAN counters for network interfaces. |
| 27 | */ |
karthik1977 | f106597 | 2024-03-16 15:21:57 +0530 | [diff] [blame^] | 28 | public final class VlanCounter extends BasePacket { |
| 29 | |
| 30 | public static final int VLAN_COUNTER_LENGTH = 28; |
| 31 | |
karthik1977 | 42c0a0e | 2024-03-02 18:55:06 +0530 | [diff] [blame] | 32 | private int vlanId; |
| 33 | private long octets; |
| 34 | private int ucastPkts; |
| 35 | private int multicastPkts; |
| 36 | private int broadcastPkts; |
| 37 | private int discards; |
| 38 | |
| 39 | private VlanCounter(Builder builder) { |
| 40 | this.vlanId = builder.vlanId; |
| 41 | this.octets = builder.octets; |
| 42 | this.ucastPkts = builder.ucastPkts; |
| 43 | this.multicastPkts = builder.multicastPkts; |
| 44 | this.broadcastPkts = builder.broadcastPkts; |
| 45 | this.discards = builder.discards; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Gets the VLAN ID. |
| 51 | * |
| 52 | * @return the VLAN ID |
| 53 | */ |
| 54 | public int getVlanId() { |
| 55 | return vlanId; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets the count of octets. |
| 60 | * |
| 61 | * @return the count of octets |
| 62 | */ |
| 63 | public long getOctets() { |
| 64 | return octets; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the count of unicast packets. |
| 69 | * |
| 70 | * @return the count of unicast packets |
| 71 | */ |
| 72 | public int getUcastPkts() { |
| 73 | return ucastPkts; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the count of multicast packets. |
| 78 | * |
| 79 | * @return the count of multicast packets |
| 80 | */ |
| 81 | public int getMulticastPkts() { |
| 82 | return multicastPkts; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the count of broadcast packets. |
| 87 | * |
| 88 | * @return the count of broadcast packets |
| 89 | */ |
| 90 | public int getBroadcastPkts() { |
| 91 | return broadcastPkts; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets the count of discards. |
| 96 | * |
| 97 | * @return the count of discards |
| 98 | */ |
| 99 | public int getDiscards() { |
| 100 | return discards; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() { |
| 105 | return MoreObjects.toStringHelper(getClass()) |
| 106 | .add("vlanId", vlanId) |
| 107 | .add("octets", octets) |
| 108 | .add("ucastPkts", ucastPkts) |
| 109 | .add("multicastPkts", multicastPkts) |
| 110 | .add("broadcastPkts", broadcastPkts) |
| 111 | .add("discards", discards) |
| 112 | .toString(); |
| 113 | } |
| 114 | |
| 115 | /** |
karthik1977 | f106597 | 2024-03-16 15:21:57 +0530 | [diff] [blame^] | 116 | * Deserializer function for sFlow interface vlan counter. |
| 117 | * |
| 118 | * @return deserializer function |
| 119 | */ |
| 120 | public static Deserializer<VlanCounter> deserializer() { |
| 121 | return (data, offset, length) -> { |
| 122 | |
| 123 | BiPredicate<ByteBuffer, Integer> isValidBuffer = (b, l) |
| 124 | -> b.hasRemaining() && b.remaining() >= l; |
| 125 | |
| 126 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 127 | if (!isValidBuffer.test(bb, VLAN_COUNTER_LENGTH)) { |
| 128 | throw new IllegalStateException("Invalid interface vlan counter buffer size."); |
| 129 | } |
| 130 | |
| 131 | Builder builder = new Builder(); |
| 132 | return builder.vlanId(bb.getInt()) |
| 133 | .octets(bb.getLong()) |
| 134 | .ucastPkts(bb.getInt()) |
| 135 | .multicastPkts(bb.getInt()) |
| 136 | .broadcastPkts(bb.getInt()) |
| 137 | .discards(bb.getInt()) |
| 138 | .build(); |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public byte[] serialize() { |
| 144 | throw new UnsupportedOperationException("Not supported yet."); |
| 145 | } |
| 146 | |
| 147 | /** |
karthik1977 | 42c0a0e | 2024-03-02 18:55:06 +0530 | [diff] [blame] | 148 | * Builder pattern to create an instance of VlanCounter. |
| 149 | */ |
| 150 | public static class Builder { |
| 151 | private int vlanId; |
| 152 | private long octets; |
| 153 | private int ucastPkts; |
| 154 | private int multicastPkts; |
| 155 | private int broadcastPkts; |
| 156 | private int discards; |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Sets the VLAN ID. |
| 161 | * |
| 162 | * @param vlanId the VLAN ID to set |
| 163 | * @return this builder instance |
| 164 | */ |
| 165 | public Builder vlanId(int vlanId) { |
| 166 | this.vlanId = vlanId; |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Sets the count of octets. |
| 172 | * |
| 173 | * @param octets the count of octets to set |
| 174 | * @return this builder instance |
| 175 | */ |
| 176 | public Builder octets(long octets) { |
| 177 | this.octets = octets; |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Sets the count of unicast packets. |
| 183 | * |
| 184 | * @param ucastPkts the count of unicast packets |
| 185 | * @return this builder instance |
| 186 | */ |
| 187 | public Builder ucastPkts(int ucastPkts) { |
| 188 | this.ucastPkts = ucastPkts; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Sets the count of multicast packets. |
| 194 | * |
| 195 | * @param multicastPkts the count of multicast packets |
| 196 | * @return this builder instance |
| 197 | */ |
| 198 | public Builder multicastPkts(int multicastPkts) { |
| 199 | this.multicastPkts = multicastPkts; |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Sets the count of broadcast packets. |
| 205 | * |
| 206 | * @param broadcastPkts the count of broadcast packets |
| 207 | * @return this builder instance |
| 208 | */ |
| 209 | public Builder broadcastPkts(int broadcastPkts) { |
| 210 | this.broadcastPkts = broadcastPkts; |
| 211 | return this; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Sets the count of discards. |
| 216 | * |
| 217 | * @param discards the count of discards |
| 218 | * @return this builder instance |
| 219 | */ |
| 220 | public Builder discards(int discards) { |
| 221 | this.discards = discards; |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Builds an instance of VlanCounter based on the configured parameters. |
| 227 | * |
| 228 | * @return an instance of VlanCounter |
| 229 | */ |
| 230 | public VlanCounter build() { |
| 231 | return new VlanCounter(this); |
| 232 | } |
| 233 | } |
| 234 | } |