Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 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.onlab.packet.pim; |
| 17 | |
| 18 | import org.onlab.packet.DeserializationException; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.text.MessageFormat; |
kdarapu | 229bc45 | 2017-04-23 00:29:12 +0530 | [diff] [blame] | 22 | import java.util.Arrays; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 23 | |
| 24 | import static org.onlab.packet.PacketUtils.checkBufferLength; |
| 25 | import static org.onlab.packet.PacketUtils.checkInput; |
| 26 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 27 | /** |
| 28 | * PIM HELLO option. |
| 29 | */ |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 30 | public class PIMHelloOption { |
| 31 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 32 | /* |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 33 | * PIM Option types. |
| 34 | */ |
| 35 | public static final short OPT_HOLDTIME = 1; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 36 | public static final short HOLDTIME_LENGTH = 2; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 37 | public static final short DEFAULT_HOLDTIME = 105; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 38 | |
| 39 | public static final short OPT_PRUNEDELAY = 2; |
| 40 | public static final short PRUNEDELAY_LENGTH = 4; |
| 41 | public static final short DEFAULT_PRUNEDELAY = 500; // 500 ms |
| 42 | public static final short DEFAULT_OVERRIDEINTERVAL = 2500; // 2500 ms |
| 43 | |
| 44 | public static final short OPT_PRIORITY = 19; |
| 45 | public static final short PRIORITY_LENGTH = 4; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 46 | public static final int DEFAULT_PRIORITY = 1; |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 47 | |
| 48 | public static final short OPT_GENID = 20; |
| 49 | public static final short GENID_LENGTH = 4; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 50 | public static final int DEFAULT_GENID = 0; |
| 51 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 52 | public static final short OPT_ADDRLIST = 24; |
| 53 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 54 | public static final int MINIMUM_OPTION_LEN_BYTES = 4; |
| 55 | |
| 56 | // Values for this particular hello option. |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 57 | private short optType = 0; |
| 58 | private short optLength = 0; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 59 | private byte[] optValue; |
| 60 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Constructs a new hello option with no fields set. |
| 63 | */ |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 64 | public PIMHelloOption() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Set a PIM Hello option by type. The length and default value of the |
| 69 | * type will be auto filled in by default. |
| 70 | * |
| 71 | * @param type hello option type |
| 72 | */ |
| 73 | public PIMHelloOption(short type) { |
| 74 | this.optType = type; |
| 75 | switch (type) { |
| 76 | case OPT_HOLDTIME: |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 77 | this.optLength = HOLDTIME_LENGTH; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 78 | this.optValue = new byte[optLength]; |
| 79 | ByteBuffer.wrap(this.optValue).putShort(PIMHelloOption.DEFAULT_HOLDTIME); |
| 80 | break; |
| 81 | |
| 82 | case OPT_PRUNEDELAY: |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 83 | this.optLength = PRUNEDELAY_LENGTH; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 84 | this.optValue = new byte[this.optLength]; |
| 85 | ByteBuffer.wrap(this.optValue).putInt(PIMHelloOption.DEFAULT_PRUNEDELAY); |
| 86 | break; |
| 87 | |
| 88 | case OPT_PRIORITY: |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 89 | this.optLength = PRIORITY_LENGTH; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 90 | this.optValue = new byte[this.optLength]; |
| 91 | ByteBuffer.wrap(this.optValue).putInt(PIMHelloOption.DEFAULT_PRIORITY); |
| 92 | break; |
| 93 | |
| 94 | case OPT_GENID: |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 95 | this.optLength = GENID_LENGTH; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 96 | this.optValue = new byte[this.optLength]; |
| 97 | ByteBuffer.wrap(this.optValue).putInt(PIMHelloOption.DEFAULT_GENID); |
| 98 | break; |
| 99 | |
| 100 | case OPT_ADDRLIST: |
| 101 | this.optLength = 0; // We don't know what the length will be yet. |
| 102 | this.optValue = null; |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 103 | break; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 104 | |
| 105 | default: |
| 106 | //log.error("Unkown option type: " + type + "\n" ); |
| 107 | return; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public void setOptType(short type) { |
| 112 | this.optType = type; |
| 113 | } |
| 114 | |
| 115 | public short getOptType() { |
| 116 | return this.optType; |
| 117 | } |
| 118 | |
| 119 | public void setOptLength(short len) { |
| 120 | this.optLength = len; |
| 121 | } |
| 122 | |
| 123 | public short getOptLength() { |
| 124 | return this.optLength; |
| 125 | } |
| 126 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 127 | public void setValue(ByteBuffer bb) { |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 128 | this.optValue = new byte[this.optLength]; |
| 129 | bb.get(this.optValue, 0, this.optLength); |
| 130 | } |
| 131 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 132 | public void setValue(byte[] value) { |
| 133 | this.optValue = value; |
| 134 | } |
| 135 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 136 | public byte[] getValue() { |
| 137 | return this.optValue; |
| 138 | } |
| 139 | |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Creates a new PIM Hello option with the specified values. |
| 142 | * |
| 143 | * @param type hello option type |
| 144 | * @param length option length |
| 145 | * @param value option value |
| 146 | * @return new PIM Hello option |
| 147 | */ |
| 148 | public static PIMHelloOption create(short type, short length, ByteBuffer value) { |
| 149 | PIMHelloOption option = new PIMHelloOption(); |
| 150 | option.setOptType(type); |
| 151 | option.setOptLength(length); |
| 152 | value.rewind(); |
| 153 | option.setValue(value); |
| 154 | return option; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Creates a new priority option. |
| 159 | * |
| 160 | * @param priority priority |
| 161 | * @return priority option |
| 162 | */ |
| 163 | public static PIMHelloOption createPriority(int priority) { |
| 164 | return create(OPT_PRIORITY, PRIORITY_LENGTH, |
| 165 | ByteBuffer.allocate(PRIORITY_LENGTH).putInt(priority)); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Creates a new hold time option. |
| 170 | * |
| 171 | * @param holdTime hold time |
| 172 | * @return hold time option |
| 173 | */ |
| 174 | public static PIMHelloOption createHoldTime(short holdTime) { |
| 175 | return create(OPT_HOLDTIME, HOLDTIME_LENGTH, |
| 176 | ByteBuffer.allocate(HOLDTIME_LENGTH).putShort(holdTime)); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Creates a new generation ID option with a particular generation ID. |
| 181 | * |
| 182 | * @param genId generation ID value |
| 183 | * @return generation ID option |
| 184 | */ |
| 185 | public static PIMHelloOption createGenID(int genId) { |
| 186 | return create(OPT_GENID, GENID_LENGTH, |
| 187 | ByteBuffer.allocate(GENID_LENGTH).putInt(genId)); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Creates a new LAN Prune Delay option. |
| 192 | * |
| 193 | * @param propagationDelay prune delay |
| 194 | * @param overrideInterval override interval |
| 195 | * @return prune delay option |
| 196 | */ |
| 197 | public static PIMHelloOption createPruneDelay(short propagationDelay, short overrideInterval) { |
| 198 | return create(OPT_PRUNEDELAY, PRUNEDELAY_LENGTH, |
| 199 | ByteBuffer.allocate(PRUNEDELAY_LENGTH) |
| 200 | .putShort(propagationDelay) |
| 201 | .putShort(overrideInterval)); |
| 202 | } |
| 203 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 204 | public static PIMHelloOption deserialize(ByteBuffer bb) throws DeserializationException { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 205 | checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), MINIMUM_OPTION_LEN_BYTES); |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 206 | |
| 207 | PIMHelloOption opt = new PIMHelloOption(); |
| 208 | opt.setOptType(bb.getShort()); |
| 209 | opt.setOptLength(bb.getShort()); |
| 210 | |
| 211 | checkBufferLength(bb.limit(), bb.position(), opt.getOptLength()); |
| 212 | opt.setValue(bb); |
| 213 | |
| 214 | return opt; |
| 215 | } |
| 216 | |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 217 | public byte[] serialize() { |
Jonathan Hart | 5af5f14 | 2016-01-28 18:45:27 -0800 | [diff] [blame] | 218 | int len = MINIMUM_OPTION_LEN_BYTES + this.optLength; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 219 | ByteBuffer bb = ByteBuffer.allocate(len); |
| 220 | bb.putShort(this.optType); |
| 221 | bb.putShort(this.optLength); |
| 222 | bb.put(this.optValue); |
| 223 | return bb.array(); |
| 224 | } |
| 225 | |
| 226 | public String toString() { |
| 227 | return MessageFormat.format("Type: {0}, len: {1} value: {2}", this.optType, this.optLength, |
kdarapu | 229bc45 | 2017-04-23 00:29:12 +0530 | [diff] [blame] | 228 | Arrays.toString(this.optValue)); |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } |