blob: 1ea0ba8de312e11927db12f1fa42caee5b24d726 [file] [log] [blame]
Rusty Eddy9cbc0952015-09-14 22:29:07 +00001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onlab.packet.pim;
17
18import org.onlab.packet.DeserializationException;
19
20import java.nio.ByteBuffer;
21import java.text.MessageFormat;
22
23import static org.onlab.packet.PacketUtils.checkBufferLength;
24import static org.onlab.packet.PacketUtils.checkInput;
25
26public class PIMHelloOption {
27
28 /**
29 * PIM Option types.
30 */
31 public static final short OPT_HOLDTIME = 1;
32 public static final short OPT_PRUNEDELAY = 2;
33 public static final short OPT_PRIORITY = 19;
34 public static final short OPT_GENID = 20;
35 public static final short OPT_ADDRLIST = 24;
36
37 public static final short DEFAULT_HOLDTIME = 105;
38 public static final int DEFAULT_PRUNEDELAY = 2000; // 2,000 ms
39 public static final int DEFAULT_PRIORITY = 1;
40 public static final int DEFAULT_GENID = 0;
41
42 public static final int MINIMUM_OPTION_LEN_BYTES = 4;
43
44 // Values for this particular hello option.
45 private short optType;
46 private short optLength;
47 private byte[] optValue;
48
49 public PIMHelloOption() {
50 }
51
52 /**
53 * Set a PIM Hello option by type. The length and default value of the
54 * type will be auto filled in by default.
55 *
56 * @param type hello option type
57 */
58 public PIMHelloOption(short type) {
59 this.optType = type;
60 switch (type) {
61 case OPT_HOLDTIME:
62 this.optLength = 2;
63 this.optValue = new byte[optLength];
64 ByteBuffer.wrap(this.optValue).putShort(PIMHelloOption.DEFAULT_HOLDTIME);
65 break;
66
67 case OPT_PRUNEDELAY:
68 this.optLength = 4;
69 this.optValue = new byte[this.optLength];
70 ByteBuffer.wrap(this.optValue).putInt(PIMHelloOption.DEFAULT_PRUNEDELAY);
71 break;
72
73 case OPT_PRIORITY:
74 this.optLength = 4;
75 this.optValue = new byte[this.optLength];
76 ByteBuffer.wrap(this.optValue).putInt(PIMHelloOption.DEFAULT_PRIORITY);
77 break;
78
79 case OPT_GENID:
80 this.optLength = 4;
81 this.optValue = new byte[this.optLength];
82 ByteBuffer.wrap(this.optValue).putInt(PIMHelloOption.DEFAULT_GENID);
83 break;
84
85 case OPT_ADDRLIST:
86 this.optLength = 0; // We don't know what the length will be yet.
87 this.optValue = null;
Ray Milkey4fd3ceb2015-12-10 14:43:08 -080088 break;
Rusty Eddy9cbc0952015-09-14 22:29:07 +000089
90 default:
91 //log.error("Unkown option type: " + type + "\n" );
92 return;
93 }
94 }
95
96 public void setOptType(short type) {
97 this.optType = type;
98 }
99
100 public short getOptType() {
101 return this.optType;
102 }
103
104 public void setOptLength(short len) {
105 this.optLength = len;
106 }
107
108 public short getOptLength() {
109 return this.optLength;
110 }
111
112 public void setValue(ByteBuffer bb) throws DeserializationException {
113 this.optValue = new byte[this.optLength];
114 bb.get(this.optValue, 0, this.optLength);
115 }
116
117 public byte[] getValue() {
118 return this.optValue;
119 }
120
121 public static PIMHelloOption deserialize(ByteBuffer bb) throws DeserializationException {
122 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
123
124 PIMHelloOption opt = new PIMHelloOption();
125 opt.setOptType(bb.getShort());
126 opt.setOptLength(bb.getShort());
127
128 checkBufferLength(bb.limit(), bb.position(), opt.getOptLength());
129 opt.setValue(bb);
130
131 return opt;
132 }
133
134 public byte [] serialize() {
135 int len = 4 + this.optLength;
136 ByteBuffer bb = ByteBuffer.allocate(len);
137 bb.putShort(this.optType);
138 bb.putShort(this.optLength);
139 bb.put(this.optValue);
140 return bb.array();
141 }
142
143 public String toString() {
144 return MessageFormat.format("Type: {0}, len: {1} value: {2}", this.optType, this.optLength,
145 (this.optValue == null) ? "null" : this.optValue.toString());
146 }
147
148}