blob: c0b4d1174bd52af809c895e1826f2395ce7e6204 [file] [log] [blame]
Rusty Eddy80f12522015-09-03 22:42:02 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Rusty Eddy80f12522015-09-03 22:42:02 +00003 *
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.BasePacket;
19import org.onlab.packet.Deserializer;
20import org.onlab.packet.IPacket;
21import org.onlab.packet.IpPrefix;
22
23import java.nio.ByteBuffer;
Rusty Eddybcad55b2016-02-11 18:56:09 -080024import java.util.Collection;
Rusty Eddy80f12522015-09-03 22:42:02 +000025import java.util.HashMap;
26
Jian Li5fc14292015-12-04 11:30:46 -080027import static com.google.common.base.MoreObjects.toStringHelper;
Rusty Eddy80f12522015-09-03 22:42:02 +000028import static org.onlab.packet.PacketUtils.checkInput;
29
30public class PIMJoinPrune extends BasePacket {
31
32 private PIMAddrUnicast upstreamAddr = new PIMAddrUnicast();
33 private short holdTime = (short) 0xffff;
34
Rusty Eddybcad55b2016-02-11 18:56:09 -080035 private HashMap<IpPrefix, PIMJoinPruneGroup> joinPrunes = new HashMap<>();
Rusty Eddy80f12522015-09-03 22:42:02 +000036
37 /**
38 * Get the J/P hold time.
39 *
40 * @return specified in seconds.
41 */
42 public short getHoldTime() {
43 return holdTime;
44 }
45
46 /**
47 * Set the J/P holdtime in seconds.
48 *
49 * @param holdTime return the holdtime.
50 */
51 public void setHoldTime(short holdTime) {
52 this.holdTime = holdTime;
53 }
54
55 /**
56 * Get the upstreamAddr for this J/P request.
57 *
58 * @return the upstream address.
59 */
60 public PIMAddrUnicast getUpstreamAddr() {
61 return upstreamAddr;
62 }
63
64 /**
65 * Set the upstream address of this PIM J/P request.
66 *
67 * @param upstr the PIM Upstream unicast address
68 */
69 public void setUpstreamAddr(PIMAddrUnicast upstr) {
70 this.upstreamAddr = upstr;
71 }
72
73 /**
Rusty Eddybcad55b2016-02-11 18:56:09 -080074 * Get the JoinPrune Group with all the joins and prunes.
75 *
76 * @return the joinPruneGroup collection
77 */
78 public Collection<PIMJoinPruneGroup> getJoinPrunes() {
79 return joinPrunes.values();
80 }
81
82 /**
Rusty Eddy80f12522015-09-03 22:42:02 +000083 * Add the specified s,g to join field.
84 *
85 * @param saddr the source address of the route
86 * @param gaddr the group address of the route
87 * @param join true for a join, false for a prune.
88 */
89 public void addJoinPrune(String saddr, String gaddr, boolean join) {
90 IpPrefix gpfx = IpPrefix.valueOf(gaddr);
91 IpPrefix spfx = IpPrefix.valueOf(saddr);
92 addJoinPrune(spfx, gpfx, join);
93 }
94
95 /**
96 * Add the specified S, G to the join field.
97 *
98 * @param spfx the source prefix of the route
99 * @param gpfx the group prefix of the route
100 * @param join true for join, false for prune
101 */
102 public void addJoinPrune(IpPrefix spfx, IpPrefix gpfx, boolean join) {
Rusty Eddybcad55b2016-02-11 18:56:09 -0800103 PIMJoinPruneGroup jpg = joinPrunes.get(gpfx);
Rusty Eddy80f12522015-09-03 22:42:02 +0000104 if (jpg == null) {
Rusty Eddybcad55b2016-02-11 18:56:09 -0800105 jpg = new PIMJoinPruneGroup(gpfx);
Rusty Eddy80f12522015-09-03 22:42:02 +0000106 joinPrunes.put(gpfx, jpg);
107 }
108
Rusty Eddybcad55b2016-02-11 18:56:09 -0800109 HashMap<IpPrefix, IpPrefix> members = (join) ? jpg.getJoins() : jpg.getPrunes();
Rusty Eddy80f12522015-09-03 22:42:02 +0000110 if (members.get(spfx) == null) {
111 members.put(spfx, spfx);
112 }
113 }
114
115 /**
116 * Add a join given strings represending the source and group addresses.
117 *
118 * @param saddr source address
119 * @param gaddr group address
120 */
121 public void addJoin(String saddr, String gaddr) {
122 this.addJoinPrune(saddr, gaddr, true);
123 }
124
125 /**
126 * Add a prune given strings represending the source and group addresses.
127 *
128 * @param saddr source address
129 * @param gaddr group address
130 */
131 public void addPrune(String saddr, String gaddr) {
132 this.addJoinPrune(saddr, gaddr, false);
133 }
134
135 /**
136 * Sets all payloads parent packet if applicable, then serializes this
137 * packet and all payloads.
138 *
139 * @return a byte[] containing this packet and payloads
140 */
141 @Override
142 public byte[] serialize() {
143
144 byte[] data = new byte[8096]; // Come up with something better
145 ByteBuffer bb = ByteBuffer.wrap(data);
146
147 bb.put(upstreamAddr.serialize());
148 bb.put((byte) 0); // reserved
149
150 int ngrps = joinPrunes.size();
151 bb.put((byte) ngrps);
152 bb.putShort(this.holdTime);
153
154 // Walk the group list and input all groups
Rusty Eddybcad55b2016-02-11 18:56:09 -0800155 for (PIMJoinPruneGroup jpg : joinPrunes.values()) {
156 PIMAddrGroup grp = new PIMAddrGroup(jpg.getGroup());
Rusty Eddy80f12522015-09-03 22:42:02 +0000157 bb.put(grp.serialize());
158
159 // put the number of joins and prunes
Rusty Eddybcad55b2016-02-11 18:56:09 -0800160 bb.putShort((short) jpg.getJoins().size());
161 bb.putShort((short) jpg.getPrunes().size());
Rusty Eddy80f12522015-09-03 22:42:02 +0000162
163 // Set all of the joins
Rusty Eddybcad55b2016-02-11 18:56:09 -0800164 for (IpPrefix spfx : jpg.getJoins().values()) {
Rusty Eddy80f12522015-09-03 22:42:02 +0000165 PIMAddrSource src = new PIMAddrSource(spfx);
166 bb.put(src.serialize());
167 }
168
169 // Set all of the prunes
Rusty Eddybcad55b2016-02-11 18:56:09 -0800170 for (IpPrefix spfx : jpg.getPrunes().values()) {
Rusty Eddy80f12522015-09-03 22:42:02 +0000171 PIMAddrSource src = new PIMAddrSource(spfx);
172 bb.put(src.serialize());
173 }
174 }
175
176 int len = bb.position();
177 byte[] data2 = new byte[len];
178 bb = ByteBuffer.wrap(data2, 0, len);
179 bb.put(data, 0, len);
180 return data2;
181 }
182
183 // TODO: I suppose I really need to implement this?
184 @Override
185 public IPacket deserialize(final byte[] data, final int offset,
186 final int length) {
187 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
188 return this;
189 }
190
191 /**
192 * Return the J/P deserializer function.
193 *
194 * @return a function that will deserialize a J/P message.
195 */
196 public static Deserializer<PIMJoinPrune> deserializer() {
197 return (data, offset, length) -> {
198
199 /*
200 * Delay buffer checks until we read enough of the packet to know how
201 * much data we will require. Each encoded address deserializer function
202 * will ensure there is enough data for that address.
203 */
204 PIMJoinPrune jp = new PIMJoinPrune();
205 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
206
207 // We must get a PIM encoded unicast address
208 PIMAddrUnicast upstream = new PIMAddrUnicast();
209 upstream.deserialize(bb);
210 jp.setUpstreamAddr(upstream);
211
212 // Use this boolean to determine the buffer space we need according to address sizes
213 boolean ipv4 = upstream.getAddr().isIp4();
214
215 // We need at minimum 4 bytes for reserved(1), ngroups(1) & holdtime(2)
216 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
217
218 // get and skip the reserved byte
219 bb.get();
220
221 // Get the number of groups.
222 int ngroups = bb.get();
223
224 // Save the holdtime.
225 jp.setHoldTime(bb.getShort());
226
227
228 for (int i = 0; i < ngroups; i++) {
229 PIMAddrGroup grp = new PIMAddrGroup();
230
231 /*
232 * grp.deserialize will ensure the buffer has enough data to read the group address.
233 */
234 grp.deserialize(bb);
235
236 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
237 int njoins = bb.getShort();
238 int nprunes = bb.getShort();
239
240 /*
241 * Now we'll verify we have enough buffer to read the next
242 * group of join and prune addresses for this group.
243 */
244 int required = (njoins + nprunes) *
245 (ipv4 ? PIMAddrSource.ENC_SOURCE_IPV4_BYTE_LENGTH : PIMAddrSource.ENC_SOURCE_IPV6_BYTE_LENGTH);
246 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), required);
247
248 // Now iterate through the joins for this group
249 for (; njoins > 0; njoins--) {
250
251 PIMAddrSource src = new PIMAddrSource();
252 src.deserialize(bb);
253
254 jp.addJoinPrune(
255 src.getAddr().toIpPrefix(),
256 grp.getAddr().toIpPrefix(), true);
257 }
258
259 // Now iterate through the prunes for this group
260 for (; nprunes > 0; nprunes--) {
261
262 PIMAddrSource src = new PIMAddrSource();
263 src.deserialize(bb);
264 jp.addJoinPrune(
265 src.getAddr().toIpPrefix(),
266 grp.getAddr().toIpPrefix(), false);
267 }
268 }
269
270 return jp;
271 };
272 }
Jian Li5fc14292015-12-04 11:30:46 -0800273
274 @Override
275 public String toString() {
276 return toStringHelper(getClass())
277 .add("upstreamAddr", upstreamAddr.toString())
278 .add("holdTime", Short.toString(holdTime))
279 .toString();
280 // TODO: need to handle joinPrunes
281 }
Rusty Eddy80f12522015-09-03 22:42:02 +0000282}