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.Arrays; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Default DHCP option. |
| 31 | */ |
| 32 | public class RIPngEntry extends BasePacket { |
| 33 | public static final int OPT_CODE_LEN = 1; |
| 34 | public static final int ENTRY_LEN = 20; |
| 35 | public static final byte INFINITY_METRIC = 16; |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 36 | public static final int NEXTHOP_METRIC = 255; // actually it is 0xFF |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 37 | |
| 38 | private final Logger log = getLogger(getClass()); |
| 39 | protected byte[] prefix; // 16 bytes |
| 40 | protected short routeTag; |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 41 | protected int prefixLen; |
| 42 | protected int metric; |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 43 | |
| 44 | @Override |
| 45 | public byte[] serialize() { |
| 46 | ByteBuffer byteBuffer; |
| 47 | byteBuffer = ByteBuffer.allocate(ENTRY_LEN); |
| 48 | byteBuffer.put(prefix); |
| 49 | byteBuffer.putShort(routeTag); |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 50 | byteBuffer.put((byte) prefixLen); |
| 51 | byteBuffer.put((byte) metric); |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 52 | return byteBuffer.array(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Deserializer function for RIPng entry. |
| 57 | * |
| 58 | * @return deserializer function |
| 59 | */ |
| 60 | public static Deserializer<RIPngEntry> deserializer() { |
| 61 | return (data, offset, length) -> { |
| 62 | RIPngEntry ripngEntry = new RIPngEntry(); |
| 63 | |
| 64 | checkNotNull(data); |
| 65 | |
| 66 | if (offset < 0 || length < 0 || |
| 67 | length > data.length || offset >= data.length || |
| 68 | offset + length > data.length) { |
| 69 | throw new DeserializationException("Illegal offset or length"); |
| 70 | } |
| 71 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 72 | if (bb.remaining() < ENTRY_LEN) { |
| 73 | throw new DeserializationException( |
| 74 | "Buffer underflow while reading RIPng entry"); |
| 75 | } |
| 76 | ripngEntry.prefix = new byte[IpAddress.INET6_BYTE_LENGTH]; |
| 77 | bb.get(ripngEntry.prefix); |
| 78 | ripngEntry.routeTag = bb.getShort(); |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 79 | ripngEntry.prefixLen = 0xFF & bb.get(); |
| 80 | ripngEntry.metric = 0xFF & bb.get(); |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 81 | return ripngEntry; |
| 82 | }; |
| 83 | } |
| 84 | /* |
| 85 | * (non-Javadoc) |
| 86 | * |
| 87 | * @see java.lang.Object#hashCode() |
| 88 | */ |
| 89 | @Override |
| 90 | public int hashCode() { |
| 91 | return Objects.hash(super.hashCode(), metric, prefixLen, Arrays.hashCode(prefix), routeTag); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * (non-Javadoc) |
| 96 | * |
| 97 | * @see java.lang.Object#equals(java.lang.Object) |
| 98 | */ |
| 99 | @Override |
| 100 | public boolean equals(final Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (!(obj instanceof RIPngEntry)) { |
| 105 | return false; |
| 106 | } |
| 107 | final RIPngEntry that = (RIPngEntry) obj; |
| 108 | |
| 109 | return super.equals(that) && |
Ray Milkey | b04f265 | 2017-12-05 11:40:20 -0800 | [diff] [blame] | 110 | Objects.equals(metric, that.metric) && |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 111 | Objects.equals(prefixLen, that.prefixLen) && |
| 112 | Arrays.equals(prefix, that.prefix) && |
| 113 | Objects.equals(routeTag, that.routeTag); |
| 114 | } |
| 115 | /** |
| 116 | * @return the IPv6 prefix |
| 117 | */ |
| 118 | public byte[] getPrefix() { |
| 119 | return this.prefix; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param prefix the IPv6 prefix to set |
| 124 | * @return this |
| 125 | */ |
| 126 | public RIPngEntry setPrefix(final byte[] prefix) { |
| 127 | this.prefix = prefix; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return the route tag |
| 133 | */ |
| 134 | public short getRouteTag() { |
| 135 | return this.routeTag; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @param routetag the route tag to set |
| 140 | * @return this |
| 141 | */ |
| 142 | public RIPngEntry setRouteTag(final short routetag) { |
| 143 | this.routeTag = routetag; |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @return the prefix length |
| 149 | */ |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 150 | public int getPrefixLen() { |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 151 | return this.prefixLen; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param prefixlen the prefix length to set |
| 156 | * @return this |
| 157 | */ |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 158 | public RIPngEntry setPrefixLen(final int prefixlen) { |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 159 | this.prefixLen = prefixlen; |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @return the metric |
| 165 | */ |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 166 | public int getMetric() { |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 167 | return this.metric; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @param metric the route metric to set |
| 172 | * @return this |
| 173 | */ |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 174 | public RIPngEntry setMetric(final int metric) { |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 175 | this.metric = metric; |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * (non-Javadoc) |
| 181 | * |
| 182 | * @see java.lang.Object#toString() |
| 183 | */ |
| 184 | @Override |
| 185 | public String toString() { |
Kalhee Kim | 475fb2c | 2018-01-16 21:25:53 +0000 | [diff] [blame] | 186 | return "RIPngEntry [prefix=" + Arrays.toString(this.prefix) + ", route tag=" + this.routeTag |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 187 | + ", prefix length=" + this.prefixLen |
| 188 | + ", metric = " + this.metric + "]"; |
| 189 | } |
| 190 | } |