blob: 45fe287bf17127f738d50b8aa67592ba7a29da43 [file] [log] [blame]
Rusty Eddy80f12522015-09-03 22:42:02 +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.BasePacket;
19import org.onlab.packet.Deserializer;
20import org.onlab.packet.IPacket;
21import org.onlab.packet.IpPrefix;
22
23import java.nio.ByteBuffer;
24import 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
34 private class JoinPruneGroup {
35 protected IpPrefix group;
36 protected HashMap<IpPrefix, IpPrefix> joins = new HashMap<>();
37 protected HashMap<IpPrefix, IpPrefix> prunes = new HashMap<>();
38
39 public JoinPruneGroup(IpPrefix grp) {
40 group = grp;
41 }
42 }
43 private HashMap<IpPrefix, JoinPruneGroup> joinPrunes = new HashMap<>();
44
45 /**
46 * Get the J/P hold time.
47 *
48 * @return specified in seconds.
49 */
50 public short getHoldTime() {
51 return holdTime;
52 }
53
54 /**
55 * Set the J/P holdtime in seconds.
56 *
57 * @param holdTime return the holdtime.
58 */
59 public void setHoldTime(short holdTime) {
60 this.holdTime = holdTime;
61 }
62
63 /**
64 * Get the upstreamAddr for this J/P request.
65 *
66 * @return the upstream address.
67 */
68 public PIMAddrUnicast getUpstreamAddr() {
69 return upstreamAddr;
70 }
71
72 /**
73 * Set the upstream address of this PIM J/P request.
74 *
75 * @param upstr the PIM Upstream unicast address
76 */
77 public void setUpstreamAddr(PIMAddrUnicast upstr) {
78 this.upstreamAddr = upstr;
79 }
80
81 /**
82 * 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) {
102 JoinPruneGroup jpg = joinPrunes.get(gpfx);
103 if (jpg == null) {
104 jpg = new JoinPruneGroup(gpfx);
105 joinPrunes.put(gpfx, jpg);
106 }
107
108 HashMap<IpPrefix, IpPrefix> members = (join) ? jpg.joins : jpg.prunes;
109 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
154 for (JoinPruneGroup jpg : joinPrunes.values()) {
155 PIMAddrGroup grp = new PIMAddrGroup(jpg.group);
156 bb.put(grp.serialize());
157
158 // put the number of joins and prunes
159 bb.putShort((short) jpg.joins.size());
160 bb.putShort((short) jpg.prunes.size());
161
162 // Set all of the joins
163 for (IpPrefix spfx : jpg.joins.values()) {
164 PIMAddrSource src = new PIMAddrSource(spfx);
165 bb.put(src.serialize());
166 }
167
168 // Set all of the prunes
169 for (IpPrefix spfx : jpg.prunes.values()) {
170 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
182 // TODO: I suppose I really need to implement this?
183 @Override
184 public IPacket deserialize(final byte[] data, final int offset,
185 final int length) {
186 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
187 return this;
188 }
189
190 /**
191 * Return the J/P deserializer function.
192 *
193 * @return a function that will deserialize a J/P message.
194 */
195 public static Deserializer<PIMJoinPrune> deserializer() {
196 return (data, offset, length) -> {
197
198 /*
199 * Delay buffer checks until we read enough of the packet to know how
200 * much data we will require. Each encoded address deserializer function
201 * will ensure there is enough data for that address.
202 */
203 PIMJoinPrune jp = new PIMJoinPrune();
204 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
205
206 // We must get a PIM encoded unicast address
207 PIMAddrUnicast upstream = new PIMAddrUnicast();
208 upstream.deserialize(bb);
209 jp.setUpstreamAddr(upstream);
210
211 // Use this boolean to determine the buffer space we need according to address sizes
212 boolean ipv4 = upstream.getAddr().isIp4();
213
214 // We need at minimum 4 bytes for reserved(1), ngroups(1) & holdtime(2)
215 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
216
217 // get and skip the reserved byte
218 bb.get();
219
220 // Get the number of groups.
221 int ngroups = bb.get();
222
223 // Save the holdtime.
224 jp.setHoldTime(bb.getShort());
225
226
227 for (int i = 0; i < ngroups; i++) {
228 PIMAddrGroup grp = new PIMAddrGroup();
229
230 /*
231 * grp.deserialize will ensure the buffer has enough data to read the group address.
232 */
233 grp.deserialize(bb);
234
235 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), 4);
236 int njoins = bb.getShort();
237 int nprunes = bb.getShort();
238
239 /*
240 * Now we'll verify we have enough buffer to read the next
241 * group of join and prune addresses for this group.
242 */
243 int required = (njoins + nprunes) *
244 (ipv4 ? PIMAddrSource.ENC_SOURCE_IPV4_BYTE_LENGTH : PIMAddrSource.ENC_SOURCE_IPV6_BYTE_LENGTH);
245 checkInput(bb.array(), bb.position(), bb.limit() - bb.position(), required);
246
247 // Now iterate through the joins for this group
248 for (; njoins > 0; njoins--) {
249
250 PIMAddrSource src = new PIMAddrSource();
251 src.deserialize(bb);
252
253 jp.addJoinPrune(
254 src.getAddr().toIpPrefix(),
255 grp.getAddr().toIpPrefix(), true);
256 }
257
258 // Now iterate through the prunes for this group
259 for (; nprunes > 0; nprunes--) {
260
261 PIMAddrSource src = new PIMAddrSource();
262 src.deserialize(bb);
263 jp.addJoinPrune(
264 src.getAddr().toIpPrefix(),
265 grp.getAddr().toIpPrefix(), false);
266 }
267 }
268
269 return jp;
270 };
271 }
Jian Li5fc14292015-12-04 11:30:46 -0800272
273 @Override
274 public String toString() {
275 return toStringHelper(getClass())
276 .add("upstreamAddr", upstreamAddr.toString())
277 .add("holdTime", Short.toString(holdTime))
278 .toString();
279 // TODO: need to handle joinPrunes
280 }
Rusty Eddy80f12522015-09-03 22:42:02 +0000281}