Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | /* |
| 29 | * Entry for RIP version 2 - RFC 2453 |
| 30 | */ |
| 31 | public class RIPV2Entry extends BasePacket { |
| 32 | public static final int ENTRY_LEN = 20; |
| 33 | public static final short AFI_IP = 2; |
| 34 | public static final byte INFINITY_METRIC = 16; |
| 35 | public static final byte NEXTHOP_METRIC = -128; // actually it is 0xFF |
| 36 | |
| 37 | private final Logger log = getLogger(getClass()); |
| 38 | protected short addressFamilyId; |
| 39 | protected short routeTag; |
| 40 | protected Ip4Address ipAddress; |
| 41 | protected Ip4Address subnetMask; |
| 42 | protected Ip4Address nextHop; |
| 43 | protected int metric; |
| 44 | |
| 45 | @Override |
| 46 | public byte[] serialize() { |
| 47 | ByteBuffer byteBuffer; |
| 48 | byteBuffer = ByteBuffer.allocate(ENTRY_LEN); |
| 49 | byteBuffer.putShort(addressFamilyId); |
| 50 | byteBuffer.putShort(routeTag); |
| 51 | byteBuffer.putInt(ipAddress.toInt()); |
| 52 | byteBuffer.putInt(subnetMask.toInt()); |
| 53 | byteBuffer.putInt(nextHop.toInt()); |
| 54 | byteBuffer.putInt(metric); |
| 55 | return byteBuffer.array(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Deserializer function for RIPv2 entry. |
| 60 | * |
| 61 | * @return deserializer function |
| 62 | */ |
| 63 | public static Deserializer<RIPV2Entry> deserializer() { |
| 64 | return (data, offset, length) -> { |
| 65 | RIPV2Entry ripEntry = new RIPV2Entry(); |
| 66 | |
| 67 | checkNotNull(data); |
| 68 | |
| 69 | if (offset < 0 || length < 0 || |
| 70 | length > data.length || offset >= data.length || |
| 71 | offset + length > data.length) { |
| 72 | throw new DeserializationException("Illegal offset or length"); |
| 73 | } |
| 74 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 75 | if (bb.remaining() < ENTRY_LEN) { |
| 76 | throw new DeserializationException( |
| 77 | "Buffer underflow while reading RIP entry"); |
| 78 | } |
| 79 | ripEntry.addressFamilyId = bb.getShort(); |
| 80 | // skip the authentication entry |
| 81 | if (ripEntry.addressFamilyId == 0xffff) { |
| 82 | return ripEntry; |
| 83 | } |
| 84 | ripEntry.routeTag = bb.getShort(); |
| 85 | ripEntry.ipAddress = Ip4Address.valueOf(bb.getInt()); |
| 86 | ripEntry.subnetMask = Ip4Address.valueOf(bb.getInt()); |
| 87 | ripEntry.nextHop = Ip4Address.valueOf(bb.getInt()); |
| 88 | ripEntry.metric = bb.getInt(); |
| 89 | return ripEntry; |
| 90 | }; |
| 91 | } |
| 92 | /* |
| 93 | * (non-Javadoc) |
| 94 | * |
| 95 | * @see java.lang.Object#hashCode() |
| 96 | */ |
| 97 | @Override |
| 98 | public int hashCode() { |
| 99 | return Objects.hash(super.hashCode(), nextHop.toInt(), subnetMask.toInt(), |
| 100 | ipAddress.toInt(), addressFamilyId, metric, routeTag); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * (non-Javadoc) |
| 105 | * |
| 106 | * @see java.lang.Object#equals(java.lang.Object) |
| 107 | */ |
| 108 | @Override |
| 109 | public boolean equals(final Object obj) { |
| 110 | if (this == obj) { |
| 111 | return true; |
| 112 | } |
| 113 | if (!(obj instanceof RIPV2Entry)) { |
| 114 | return false; |
| 115 | } |
| 116 | final RIPV2Entry that = (RIPV2Entry) obj; |
| 117 | |
| 118 | |
| 119 | return super.equals(that) && |
| 120 | Objects.equals(routeTag, that.routeTag) && |
| 121 | Objects.equals(metric, that.metric) && |
| 122 | Objects.equals(addressFamilyId, that.addressFamilyId) && |
| 123 | Objects.equals(ipAddress, that.ipAddress) && |
| 124 | Objects.equals(nextHop, that.nextHop) && |
| 125 | Objects.equals(subnetMask, that.subnetMask); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * @return the Address Family Identifier |
| 131 | */ |
| 132 | public short getAddressFamilyId() { |
| 133 | return this.addressFamilyId; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param addressFamilyIdentifier the address family identifier to set |
| 138 | * @return this |
| 139 | */ |
| 140 | public RIPV2Entry setAddressFamilyId(final short addressFamilyIdentifier) { |
| 141 | this.addressFamilyId = addressFamilyIdentifier; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return the route tag |
| 147 | */ |
| 148 | public short getRouteTag() { |
| 149 | return this.routeTag; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param routetag the route tag to set |
| 154 | * @return this |
| 155 | */ |
| 156 | public RIPV2Entry setRouteTag(final short routetag) { |
| 157 | this.routeTag = routetag; |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @return the ip address |
| 163 | */ |
| 164 | public Ip4Address getipAddress() { |
| 165 | return this.ipAddress; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param ipaddress the Ip Address to set |
| 170 | * @return this |
| 171 | */ |
| 172 | public RIPV2Entry setIpAddress(final Ip4Address ipaddress) { |
| 173 | this.ipAddress = ipaddress; |
| 174 | return this; |
| 175 | } |
| 176 | /** |
| 177 | * @return the subnet mask |
| 178 | */ |
| 179 | public Ip4Address getSubnetMask() { |
| 180 | return this.subnetMask; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param subnetmask the subnet mask to set |
| 185 | * @return this |
| 186 | */ |
| 187 | public RIPV2Entry setSubnetMask(final Ip4Address subnetmask) { |
| 188 | this.subnetMask = subnetmask; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @return the next hop |
| 194 | */ |
| 195 | public Ip4Address getNextHop() { |
| 196 | return this.nextHop; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param nexthop the ip address if the next hop to set |
| 201 | * @return this |
| 202 | */ |
| 203 | public RIPV2Entry setNextHop(final Ip4Address nexthop) { |
| 204 | this.nextHop = nexthop; |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * @return the metric |
| 211 | */ |
| 212 | public int getMetric() { |
| 213 | return this.metric; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param metric the route metric to set |
| 218 | * @return this |
| 219 | */ |
| 220 | public RIPV2Entry setMetric(final int metric) { |
| 221 | this.metric = metric; |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * (non-Javadoc) |
| 227 | * |
| 228 | * @see java.lang.Object#toString() |
| 229 | */ |
| 230 | @Override |
| 231 | public String toString() { |
| 232 | return "RIPV2Entry [address family Id=" + this.addressFamilyId + ", route tag=" + this.routeTag |
| 233 | + ", Address=" + this.ipAddress |
| 234 | + ", Subnet mask=" + this.subnetMask |
| 235 | + ", Mext hop=" + this.nextHop |
| 236 | + ", metric = " + this.metric + "]"; |
| 237 | } |
| 238 | } |