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