blob: cfe3b62cfd1b257b1d8afd2778f9c54636adc56e [file] [log] [blame]
Kalhee Kim6222dbe2017-10-26 15:44:37 +00001/*
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.onlab.packet;
18
19import org.slf4j.Logger;
20
21import java.nio.ByteBuffer;
22import java.util.Arrays;
23import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Default DHCP option.
31 */
32public 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 Kim475fb2c2018-01-16 21:25:53 +000036 public static final int NEXTHOP_METRIC = 255; // actually it is 0xFF
Kalhee Kim6222dbe2017-10-26 15:44:37 +000037
38 private final Logger log = getLogger(getClass());
39 protected byte[] prefix; // 16 bytes
40 protected short routeTag;
Kalhee Kim475fb2c2018-01-16 21:25:53 +000041 protected int prefixLen;
42 protected int metric;
Kalhee Kim6222dbe2017-10-26 15:44:37 +000043
44 @Override
45 public byte[] serialize() {
46 ByteBuffer byteBuffer;
47 byteBuffer = ByteBuffer.allocate(ENTRY_LEN);
48 byteBuffer.put(prefix);
49 byteBuffer.putShort(routeTag);
Kalhee Kim475fb2c2018-01-16 21:25:53 +000050 byteBuffer.put((byte) prefixLen);
51 byteBuffer.put((byte) metric);
Kalhee Kim6222dbe2017-10-26 15:44:37 +000052 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 Kim475fb2c2018-01-16 21:25:53 +000079 ripngEntry.prefixLen = 0xFF & bb.get();
80 ripngEntry.metric = 0xFF & bb.get();
Kalhee Kim6222dbe2017-10-26 15:44:37 +000081 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 Milkeyb04f2652017-12-05 11:40:20 -0800110 Objects.equals(metric, that.metric) &&
Kalhee Kim6222dbe2017-10-26 15:44:37 +0000111 Objects.equals(routeTag, that.routeTag) &&
112 Objects.equals(prefixLen, that.prefixLen) &&
113 Arrays.equals(prefix, that.prefix) &&
114 Objects.equals(routeTag, that.routeTag);
115 }
116 /**
117 * @return the IPv6 prefix
118 */
119 public byte[] getPrefix() {
120 return this.prefix;
121 }
122
123 /**
124 * @param prefix the IPv6 prefix to set
125 * @return this
126 */
127 public RIPngEntry setPrefix(final byte[] prefix) {
128 this.prefix = prefix;
129 return this;
130 }
131
132 /**
133 * @return the route tag
134 */
135 public short getRouteTag() {
136 return this.routeTag;
137 }
138
139 /**
140 * @param routetag the route tag to set
141 * @return this
142 */
143 public RIPngEntry setRouteTag(final short routetag) {
144 this.routeTag = routetag;
145 return this;
146 }
147
148 /**
149 * @return the prefix length
150 */
Kalhee Kim475fb2c2018-01-16 21:25:53 +0000151 public int getPrefixLen() {
Kalhee Kim6222dbe2017-10-26 15:44:37 +0000152 return this.prefixLen;
153 }
154
155 /**
156 * @param prefixlen the prefix length to set
157 * @return this
158 */
Kalhee Kim475fb2c2018-01-16 21:25:53 +0000159 public RIPngEntry setPrefixLen(final int prefixlen) {
Kalhee Kim6222dbe2017-10-26 15:44:37 +0000160 this.prefixLen = prefixlen;
161 return this;
162 }
163
164 /**
165 * @return the metric
166 */
Kalhee Kim475fb2c2018-01-16 21:25:53 +0000167 public int getMetric() {
Kalhee Kim6222dbe2017-10-26 15:44:37 +0000168 return this.metric;
169 }
170
171 /**
172 * @param metric the route metric to set
173 * @return this
174 */
Kalhee Kim475fb2c2018-01-16 21:25:53 +0000175 public RIPngEntry setMetric(final int metric) {
Kalhee Kim6222dbe2017-10-26 15:44:37 +0000176 this.metric = metric;
177 return this;
178 }
179
180 /*
181 * (non-Javadoc)
182 *
183 * @see java.lang.Object#toString()
184 */
185 @Override
186 public String toString() {
Kalhee Kim475fb2c2018-01-16 21:25:53 +0000187 return "RIPngEntry [prefix=" + Arrays.toString(this.prefix) + ", route tag=" + this.routeTag
Kalhee Kim6222dbe2017-10-26 15:44:37 +0000188 + ", prefix length=" + this.prefixLen
189 + ", metric = " + this.metric + "]";
190 }
191}