Mani
Committed by Gerrit Code Review

ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding

Change-Id: I33b48593ec38d28bdff215ce3a608a6d085a9077
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.subtype;
17 +
18 +import com.google.common.base.MoreObjects;
19 +
20 +/**
21 + * Representation of an LS Request packet and fields and access methods to access it.
22 + */
23 +public class LsRequestPacket {
24 +
25 + private int lsType;
26 + private String linkStateId;
27 + private String ownRouterId;
28 +
29 + /**
30 + * Gets the LSA type.
31 + *
32 + * @return LSA type
33 + */
34 + public int lsType() {
35 + return lsType;
36 + }
37 +
38 + /**
39 + * Sets the LSA type.
40 + *
41 + * @param lsType LSA type
42 + */
43 + public void setLsType(int lsType) {
44 + this.lsType = lsType;
45 + }
46 +
47 + /**
48 + * Gets the link state id.
49 + *
50 + * @return link state id
51 + */
52 + public String linkStateId() {
53 + return linkStateId;
54 + }
55 +
56 + /**
57 + * Sets link state id.
58 + *
59 + * @param linkStateId state id
60 + */
61 + public void setLinkStateId(String linkStateId) {
62 + this.linkStateId = linkStateId;
63 + }
64 +
65 + /**
66 + * Gets the router id.
67 + *
68 + * @return router id
69 + */
70 + public String ownRouterId() {
71 + return ownRouterId;
72 + }
73 +
74 + /**
75 + * Sets the router id.
76 + *
77 + * @param ownRouterId router id
78 + */
79 + public void setOwnRouterId(String ownRouterId) {
80 + this.ownRouterId = ownRouterId;
81 + }
82 +
83 + @Override
84 + public String toString() {
85 + return MoreObjects.toStringHelper(getClass())
86 + .omitNullValues()
87 + .add("lsType", lsType)
88 + .add("linkStateId", linkStateId)
89 + .add("ownRouterId", ownRouterId)
90 + .toString();
91 + }
92 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +
17 +/**
18 + * Implementation of the OSPF packet sub types which is used in OSPF packets..
19 + */
20 +package org.onosproject.ospf.protocol.ospfpacket.subtype;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.ospf.exceptions.OspfErrorType;
22 +import org.onosproject.ospf.exceptions.OspfParseException;
23 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
24 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
25 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
26 +import org.onosproject.ospf.protocol.util.OspfPacketType;
27 +import org.onosproject.ospf.protocol.util.OspfParameters;
28 +import org.onosproject.ospf.protocol.util.OspfUtil;
29 +import org.slf4j.Logger;
30 +import org.slf4j.LoggerFactory;
31 +
32 +import java.util.ArrayList;
33 +import java.util.List;
34 +
35 +/**
36 + * Representation of an OSPF Database Description packet.
37 + * Database Description packets are OSPF packet type 2.
38 + * These packets are exchanged when an adjacency is being initialized.
39 + * They describe the contents of the link-state database.
40 + */
41 +public class DdPacket extends OspfPacketHeader {
42 +
43 + /*
44 + 0 1 2 3
45 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
46 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 + | Version # | 2 | Packet length |
48 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 + | Router ID |
50 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 + | Area ID |
52 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 + | Checksum | AuType |
54 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 + | Authentication |
56 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 + | Authentication |
58 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 + | Interface MTU | Options |0|0|0|0|0|I|M|MS
60 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 + | DD sequence number |
62 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 + | |
64 + +- -+
65 + | |
66 + +- An LSA Header -+
67 + | |
68 + +- -+
69 + | |
70 + +- -+
71 + | |
72 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 + | ... |
74 + */
75 + private static final Logger log = LoggerFactory.getLogger(DdPacket.class);
76 + private int imtu;
77 + private int options;
78 + private int ims; // initialize , more but / master slave bit
79 + private int isMaster;
80 + private int isInitialize;
81 + private int isMore;
82 + private long sequenceNo;
83 + private boolean isOpaqueCapable;
84 + private List<LsaHeader> lsaHeaderList = new ArrayList<>();
85 +
86 + /**
87 + * Creates an instance of DD packet.
88 + */
89 + public DdPacket() {
90 + }
91 +
92 + /**
93 + * Creates an instance of DD packet.
94 + *
95 + * @param ospfHeader OSPF header instance
96 + */
97 + public DdPacket(OspfPacketHeader ospfHeader) {
98 + populateHeader(ospfHeader);
99 + }
100 +
101 + /**
102 + * Gets is opaque capable or not.
103 + *
104 + * @return true if opaque capable else false
105 + */
106 + public boolean isOpaqueCapable() {
107 + return isOpaqueCapable;
108 + }
109 +
110 + /**
111 + * Sets is opaque capable or not.
112 + *
113 + * @param isOpaqueCapable true or false
114 + */
115 + public void setIsOpaqueCapable(boolean isOpaqueCapable) {
116 + this.isOpaqueCapable = isOpaqueCapable;
117 + }
118 +
119 + /**
120 + * Gets IMS value.
121 + *
122 + * @return IMS bits as an int value
123 + */
124 + public int ims() {
125 + return ims;
126 + }
127 +
128 + /**
129 + * Sets IMS value.
130 + *
131 + * @param ims IMS value
132 + */
133 + public void setIms(int ims) {
134 + this.ims = ims;
135 + }
136 +
137 + /**
138 + * Gets master bit value.
139 + *
140 + * @return 1 if master else 0
141 + */
142 + public int isMaster() {
143 + return isMaster;
144 + }
145 +
146 + /**
147 + * Sets master value.
148 + *
149 + * @param isMaster 1 represents master
150 + */
151 + public void setIsMaster(int isMaster) {
152 + this.isMaster = isMaster;
153 + }
154 +
155 + /**
156 + * Gets Initialize bit value.
157 + *
158 + * @return 1 if initialize else 0
159 + */
160 + public int isInitialize() {
161 + return isInitialize;
162 + }
163 +
164 + /**
165 + * Sets initialize value.
166 + *
167 + * @param isInitialize 1 is initialize else 0
168 + */
169 + public void setIsInitialize(int isInitialize) {
170 + this.isInitialize = isInitialize;
171 + }
172 +
173 + /**
174 + * Gets is more bit set or not.
175 + *
176 + * @return 1 if more set else 0
177 + */
178 + public int isMore() {
179 + return isMore;
180 + }
181 +
182 + /**
183 + * Sets more bit value to 0 or 1.
184 + *
185 + * @param isMore 1 if more set else 0
186 + */
187 + public void setIsMore(int isMore) {
188 + this.isMore = isMore;
189 + }
190 +
191 +
192 + /**
193 + * Gets IMTU value.
194 + *
195 + * @return IMTU value
196 + */
197 + public int imtu() {
198 + return imtu;
199 + }
200 +
201 + /**
202 + * Sets IMTU value.
203 + *
204 + * @param imtu value
205 + */
206 + public void setImtu(int imtu) {
207 + this.imtu = imtu;
208 + }
209 +
210 + /**
211 + * Gets options value.
212 + *
213 + * @return options
214 + */
215 + public int options() {
216 + return options;
217 + }
218 +
219 + /**
220 + * Sets options value.
221 + *
222 + * @param options options value
223 + */
224 + public void setOptions(int options) {
225 + this.options = options;
226 + }
227 +
228 + /**
229 + * Gets sequence number.
230 + *
231 + * @return sequenceNo
232 + */
233 + public long sequenceNo() {
234 + return sequenceNo;
235 + }
236 +
237 + /**
238 + * Sets Sequence number.
239 + *
240 + * @param sequenceNo sequence number
241 + */
242 + public void setSequenceNo(long sequenceNo) {
243 + this.sequenceNo = sequenceNo;
244 + }
245 +
246 + /**
247 + * Gets LSA header list.
248 + *
249 + * @return LSA header
250 + */
251 + public List<LsaHeader> getLsaHeaderList() {
252 + return lsaHeaderList;
253 + }
254 +
255 + /**
256 + * Adds LSA header to header list.
257 + *
258 + * @param lsaHeader lsa header instance
259 + */
260 + public void addLsaHeader(LsaHeader lsaHeader) {
261 +
262 + if (!lsaHeaderList.contains(lsaHeader)) {
263 + lsaHeaderList.add(lsaHeader);
264 + }
265 + }
266 +
267 + @Override
268 + public OspfPacketType ospfMessageType() {
269 + return OspfPacketType.DD;
270 + }
271 +
272 + @Override
273 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
274 +
275 + try {
276 + this.setImtu(channelBuffer.readShort());
277 +
278 + int options = channelBuffer.readByte();
279 + String obit = Integer.toHexString(options);
280 + if (obit.length() == 1) {
281 + obit = "0" + obit;
282 + }
283 + String toBinary = Integer.toBinaryString(Integer.parseInt(new Character(obit.charAt(0)).toString()));
284 + if (toBinary.length() == 1) {
285 + toBinary = "000" + toBinary;
286 + } else if (toBinary.length() == 2) {
287 + toBinary = "00" + toBinary;
288 + } else if (toBinary.length() == 3) {
289 + toBinary = "0" + toBinary;
290 + }
291 + if (Integer.parseInt(new Character(toBinary.charAt(1)).toString()) == 1) {
292 + this.setIsOpaqueCapable(true);
293 + }
294 + this.setOptions(options);
295 + this.setIms(channelBuffer.readByte());
296 + //Convert the byte to ims bits
297 + String strIms = Integer.toBinaryString(this.ims());
298 + if (strIms.length() == 3) {
299 + this.setIsInitialize(Integer.parseInt(Character.toString(strIms.charAt(0))));
300 + this.setIsMore(Integer.parseInt(Character.toString(strIms.charAt(1))));
301 + this.setIsMaster(Integer.parseInt(Character.toString(strIms.charAt(2))));
302 + } else if (strIms.length() == 2) {
303 + this.setIsInitialize(0);
304 + this.setIsMore(Integer.parseInt(Character.toString(strIms.charAt(0))));
305 + this.setIsMaster(Integer.parseInt(Character.toString(strIms.charAt(1))));
306 + } else if (strIms.length() == 1) {
307 + this.setIsInitialize(0);
308 + this.setIsMore(0);
309 + this.setIsMaster(Integer.parseInt(Character.toString(strIms.charAt(0))));
310 + }
311 + this.setSequenceNo(channelBuffer.readInt());
312 +
313 + //add all the LSA Headers - header is of 20 bytes
314 + while (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) {
315 + LsaHeader header = OspfUtil.readLsaHeader(channelBuffer.readBytes(OspfUtil.LSA_HEADER_LENGTH));
316 + //add the LSAHeader to DDPacket
317 + addLsaHeader(header);
318 + }
319 +
320 + } catch (Exception e) {
321 + log.debug("Error::DdPacket:: {}", e.getMessage());
322 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
323 + }
324 + }
325 +
326 + @Override
327 + public byte[] asBytes() {
328 +
329 + byte[] ddMessage = null;
330 +
331 + byte[] ddHeader = getDdHeaderAsByteArray();
332 + byte[] ddBody = getDdBodyAsByteArray();
333 + ddMessage = Bytes.concat(ddHeader, ddBody);
334 +
335 + return ddMessage;
336 + }
337 +
338 + /**
339 + * Gets DD Header as byte array.
340 + *
341 + * @return dd header as byte array.
342 + */
343 + public byte[] getDdHeaderAsByteArray() {
344 + List<Byte> headerLst = new ArrayList<>();
345 +
346 + try {
347 + headerLst.add((byte) this.ospfVersion());
348 + headerLst.add((byte) this.ospfType());
349 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
350 + headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
351 + headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
352 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
353 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
354 + //Authentication is 0 always. Total 8 bytes consist of zero
355 + byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
356 + headerLst.addAll(Bytes.asList(auth));
357 +
358 + } catch (Exception e) {
359 + log.debug("Error");
360 +
361 + }
362 +
363 + return Bytes.toArray(headerLst);
364 + }
365 +
366 +
367 + /**
368 + * Gets DD body as byte array.
369 + *
370 + * @return DD body
371 + */
372 + public byte[] getDdBodyAsByteArray() {
373 + List<Byte> bodyLst = new ArrayList<>();
374 +
375 + try {
376 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.imtu())));
377 + bodyLst.add((byte) this.options());
378 +
379 + StringBuilder sb = new StringBuilder();
380 + sb.append(this.isInitialize());
381 + sb.append(this.isMore());
382 + sb.append(this.isMaster());
383 +
384 + bodyLst.add((byte) Integer.parseInt(sb.toString(), 2));
385 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.sequenceNo()))); // passing long value
386 +
387 + for (LsaHeader lsaHeader : lsaHeaderList) {
388 + if (lsaHeader.lsType() == OspfParameters.LINK_LOCAL_OPAQUE_LSA ||
389 + lsaHeader.lsType() == OspfParameters.AREA_LOCAL_OPAQUE_LSA ||
390 + lsaHeader.lsType() == OspfParameters.AS_OPAQUE_LSA) {
391 + OpaqueLsaHeader header = (OpaqueLsaHeader) lsaHeader;
392 + bodyLst.addAll(Bytes.asList(header.getOpaqueLsaHeaderAsByteArray()));
393 + } else {
394 + bodyLst.addAll(Bytes.asList(lsaHeader.getLsaHeaderAsByteArray()));
395 + }
396 + }
397 + } catch (Exception e) {
398 + log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage());
399 + return Bytes.toArray(bodyLst);
400 + }
401 +
402 + return Bytes.toArray(bodyLst);
403 + }
404 +
405 + @Override
406 + public String toString() {
407 + return MoreObjects.toStringHelper(getClass())
408 + .omitNullValues()
409 + .add("imtu", imtu)
410 + .add("options", options)
411 + .add("ims", ims)
412 + .add("isMaster", isMaster)
413 + .add("isInitialize", isInitialize)
414 + .add("isMore", isMore)
415 + .add("sequenceNo", sequenceNo)
416 + .add("isOpaqueCapable", isOpaqueCapable)
417 + .add("lsaHeaderList", lsaHeaderList)
418 + .toString();
419 + }
420 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onlab.packet.Ip4Address;
22 +import org.onosproject.ospf.exceptions.OspfErrorType;
23 +import org.onosproject.ospf.exceptions.OspfParseException;
24 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
25 +import org.onosproject.ospf.protocol.util.OspfPacketType;
26 +import org.onosproject.ospf.protocol.util.OspfUtil;
27 +import org.slf4j.Logger;
28 +import org.slf4j.LoggerFactory;
29 +
30 +import java.util.ArrayList;
31 +import java.util.List;
32 +
33 +/**
34 + * Defines an OSPF Hello Message, and the fields and methods to access it.
35 + * Hello packets are OSPF packet type 1. These packets are sent
36 + * periodically on all interfaces in order to establish and
37 + * maintain neighbor relationships.
38 + */
39 +public class HelloPacket extends OspfPacketHeader {
40 +
41 + /*
42 + 0 1 2 3
43 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
44 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 + | Version # | 1 | Packet length |
46 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 + | Router ID |
48 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 + | Area ID |
50 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 + | Checksum | AuType |
52 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 + | Authentication |
54 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 + | Authentication |
56 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 + | Network Mask |
58 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 + | HelloInterval | Options | Rtr Pri |
60 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 + | RouterDeadInterval |
62 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 + | Designated Router |
64 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 + | Backup Designated Router |
66 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 + | Neighbor |
68 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 + | ... |
70 +
71 + Hello Message Format
72 + REFERENCE : RFC 2328
73 + */
74 +
75 + private static final Logger log = LoggerFactory.getLogger(HelloPacket.class);
76 + private Ip4Address networkMask;
77 + private int options;
78 + private int helloInterval;
79 + private int routerPriority;
80 + private int routerDeadInterval;
81 + private Ip4Address bdr;
82 + private Ip4Address dr;
83 + private List<Ip4Address> neighborAddress = new ArrayList<>();
84 +
85 + /**
86 + * Creates an instance of Hello packet.
87 + */
88 + public HelloPacket() {
89 + }
90 +
91 + /**
92 + * Creates an instance of Hello packet.
93 + *
94 + * @param ospfHeader OSPF header instance.
95 + */
96 + public HelloPacket(OspfPacketHeader ospfHeader) {
97 + populateHeader(ospfHeader);
98 + }
99 +
100 + /**
101 + * Gets network mask.
102 + *
103 + * @return network mask
104 + */
105 + public Ip4Address networkMask() {
106 + return networkMask;
107 + }
108 +
109 + /**
110 + * Sets network mask.
111 + *
112 + * @param networkMask network mask
113 + */
114 + public void setNetworkMask(Ip4Address networkMask) {
115 + this.networkMask = networkMask;
116 + }
117 +
118 + /**
119 + * Gets BDRs IP address.
120 + *
121 + * @return BDRs IP address
122 + */
123 + public Ip4Address bdr() {
124 + return bdr;
125 + }
126 +
127 + /**
128 + * Sets BDR IP address.
129 + *
130 + * @param bdr BDR IP address
131 + */
132 + public void setBdr(Ip4Address bdr) {
133 + this.bdr = bdr;
134 + }
135 +
136 + /**
137 + * Gets DRs IP address.
138 + *
139 + * @return DRs IP address
140 + */
141 + public Ip4Address dr() {
142 + return dr;
143 + }
144 +
145 + /**
146 + * Sets DRs IP address.
147 + *
148 + * @param dr DRs IP address
149 + */
150 + public void setDr(Ip4Address dr) {
151 + this.dr = dr;
152 + }
153 +
154 + /**
155 + * Adds neighbor to map.
156 + *
157 + * @param neighborID neighbors id
158 + */
159 + public void addNeighbor(Ip4Address neighborID) {
160 + if (!neighborAddress.contains(neighborID)) {
161 + neighborAddress.add(neighborID);
162 + }
163 + }
164 +
165 + /**
166 + * Checks neighbor is in map or not.
167 + *
168 + * @param neighborID neighbors id
169 + * @return true if neighbor exist else false
170 + */
171 + public boolean containsNeighbour(Ip4Address neighborID) {
172 + return (neighborAddress.contains(neighborID)) ? true : false;
173 + }
174 +
175 + /**
176 + * Gets options value.
177 + *
178 + * @return options value
179 + */
180 + public int options() {
181 + return options;
182 + }
183 +
184 + /**
185 + * Sets options value.
186 + *
187 + * @param options options value
188 + */
189 + public void setOptions(int options) {
190 + this.options = options;
191 + }
192 +
193 + /**
194 + * Gets router priority.
195 + *
196 + * @return routerPriority
197 + */
198 + public int routerPriority() {
199 + return routerPriority;
200 + }
201 +
202 + /**
203 + * Sets router priority.
204 + *
205 + * @param routerPriority router priority
206 + */
207 + public void setRouterPriority(int routerPriority) {
208 + this.routerPriority = routerPriority;
209 + }
210 +
211 + /**
212 + * Gets hello interval.
213 + *
214 + * @return hello Interval
215 + */
216 + public int helloInterval() {
217 + return helloInterval;
218 + }
219 +
220 + /**
221 + * Sets hello Interval.
222 + *
223 + * @param helloInterval hello Interval
224 + */
225 + public void setHelloInterval(int helloInterval) {
226 + this.helloInterval = helloInterval;
227 + }
228 +
229 + /**
230 + * Gets router dead interval.
231 + *
232 + * @return router dead interval
233 + */
234 + public int routerDeadInterval() {
235 + return routerDeadInterval;
236 + }
237 +
238 + /**
239 + * Sets router dead interval.
240 + *
241 + * @param routerDeadInterval router dead interval
242 + */
243 + public void setRouterDeadInterval(int routerDeadInterval) {
244 + this.routerDeadInterval = routerDeadInterval;
245 + }
246 +
247 + @Override
248 + public OspfPacketType ospfMessageType() {
249 + return OspfPacketType.HELLO;
250 + }
251 +
252 + @Override
253 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
254 +
255 + try {
256 + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
257 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
258 + this.setNetworkMask(Ip4Address.valueOf(tempByteArray));
259 + this.setHelloInterval(channelBuffer.readShort());
260 + this.setOptions(channelBuffer.readByte());
261 + this.setRouterPriority(channelBuffer.readByte() & 0xff);
262 + this.setRouterDeadInterval(channelBuffer.readInt());
263 + tempByteArray = new byte[OspfUtil.FOUR_BYTES];
264 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
265 + this.setDr(Ip4Address.valueOf(tempByteArray));
266 + tempByteArray = new byte[OspfUtil.FOUR_BYTES];
267 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
268 + this.setBdr(Ip4Address.valueOf(tempByteArray));
269 +
270 + while (channelBuffer.readableBytes() > 0) {
271 + tempByteArray = new byte[OspfUtil.FOUR_BYTES];
272 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
273 + this.addNeighbor(Ip4Address.valueOf(tempByteArray));
274 + }
275 +
276 + } catch (Exception e) {
277 + log.debug("Error::HelloPacket:: {}", e.getMessage());
278 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
279 + }
280 + }
281 +
282 + @Override
283 + public byte[] asBytes() {
284 +
285 + byte[] helloMessage = null;
286 + byte[] helloHeader = getHelloHeaderAsByteArray();
287 + byte[] helloBody = getHelloBodyAsByteArray();
288 + helloMessage = Bytes.concat(helloHeader, helloBody);
289 +
290 + log.debug("HelloPacket::asBytes::Hello asBytes:: {}", helloMessage);
291 +
292 + return helloMessage;
293 + }
294 +
295 + /**
296 + * Gets hello header as byte array.
297 + *
298 + * @return hello header
299 + */
300 + public byte[] getHelloHeaderAsByteArray() {
301 + List<Byte> headerLst = new ArrayList<>();
302 + try {
303 + headerLst.add((byte) this.ospfVersion());
304 + headerLst.add((byte) this.ospfType());
305 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
306 + headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
307 + headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
308 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
309 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
310 + //Authentication is 0 always. Total 8 bytes consist of zero
311 + byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
312 + headerLst.addAll(Bytes.asList(auth));
313 + } catch (Exception e) {
314 + log.debug("Error::getHelloHeaderAsByteArray {}", e.getMessage());
315 + return Bytes.toArray(headerLst);
316 + }
317 +
318 + return Bytes.toArray(headerLst);
319 + }
320 +
321 + /**
322 + * Gets hello body as byte array.
323 + *
324 + * @return hello body as byte array
325 + */
326 + public byte[] getHelloBodyAsByteArray() {
327 + List<Byte> bodyLst = new ArrayList<>();
328 +
329 + try {
330 + bodyLst.addAll(Bytes.asList(this.networkMask().toOctets()));
331 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.helloInterval())));
332 + bodyLst.add((byte) this.options());
333 + bodyLst.add((byte) this.routerPriority());
334 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.routerDeadInterval())));
335 + bodyLst.addAll(Bytes.asList(this.dr().toOctets()));
336 + bodyLst.addAll(Bytes.asList(this.bdr().toOctets()));
337 + for (Ip4Address neighbour : neighborAddress) {
338 + bodyLst.addAll(Bytes.asList(neighbour.toOctets()));
339 + }
340 +
341 + } catch (Exception e) {
342 + log.debug("Error::getHelloBodyAsByteArray {}", e.getMessage());
343 + return Bytes.toArray(bodyLst);
344 + }
345 +
346 + return Bytes.toArray(bodyLst);
347 + }
348 +
349 + @Override
350 + public String toString() {
351 + return MoreObjects.toStringHelper(getClass())
352 + .omitNullValues()
353 + .add("networkMask", networkMask)
354 + .add("options", options)
355 + .add("helloInterval", helloInterval)
356 + .add("routerPriority", routerPriority)
357 + .add("routerDeadInterval", routerDeadInterval)
358 + .add("bdr", bdr)
359 + .add("dr", dr)
360 + .toString();
361 + }
362 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.ospf.exceptions.OspfErrorType;
22 +import org.onosproject.ospf.exceptions.OspfParseException;
23 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
24 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
25 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
26 +import org.onosproject.ospf.protocol.util.OspfPacketType;
27 +import org.onosproject.ospf.protocol.util.OspfParameters;
28 +import org.onosproject.ospf.protocol.util.OspfUtil;
29 +import org.slf4j.Logger;
30 +import org.slf4j.LoggerFactory;
31 +
32 +import java.util.ArrayList;
33 +import java.util.List;
34 +
35 +/**
36 + * Representation of an OSPF Link State Acknowledgment Message.
37 + * Link State Acknowledgment Packets are OSPF packet type 5.
38 + * To make the flooding of LSAs reliable, flooded LSAs are explicitly
39 + * acknowledged. This acknowledgment is accomplished through the
40 + * sending and receiving of Link State Acknowledgment packets.
41 + * Multiple LSAs can be acknowledged in a single Link State Acknowledgment packet.
42 + */
43 +public class LsAcknowledge extends OspfPacketHeader {
44 + /*
45 + 0 1 2 3
46 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
47 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 + | Version # | 5 | Packet length |
49 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 + | Router ID |
51 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 + | Area ID |
53 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 + | Checksum | AuType |
55 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 + | Authentication |
57 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 + | Authentication |
59 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 + | |
61 + +- -+
62 + | |
63 + +- An LSA Header -+
64 + | |
65 + +- -+
66 + | |
67 + +- -+
68 + | |
69 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 + | ... |
71 + */
72 + private static final Logger log = LoggerFactory.getLogger(LsAcknowledge.class);
73 + private List<LsaHeader> linkStateHeaders = new ArrayList<>();
74 +
75 + /**
76 + * Creates an instance of Link State Acknowledgment instance.
77 + */
78 + public LsAcknowledge() {
79 + }
80 +
81 + /**
82 + * Creates an instance of Link State Acknowledgment instance.
83 + *
84 + * @param ospfHeader OSPF header instance.
85 + */
86 + public LsAcknowledge(OspfPacketHeader ospfHeader) {
87 + populateHeader(ospfHeader);
88 + }
89 +
90 + /**
91 + * Gets ls headers.
92 + *
93 + * @return ls headers
94 + */
95 + public List<LsaHeader> getLinkStateHeaders() {
96 + return linkStateHeaders;
97 + }
98 +
99 + /**
100 + * Adds link state header to list.
101 + *
102 + * @param lsaHeader LSA header
103 + */
104 + public void addLinkStateHeader(LsaHeader lsaHeader) {
105 + if (!linkStateHeaders.contains(lsaHeader)) {
106 + linkStateHeaders.add(lsaHeader);
107 + }
108 + }
109 +
110 + @Override
111 + public OspfPacketType ospfMessageType() {
112 + return OspfPacketType.LSAACK;
113 + }
114 +
115 + @Override
116 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
117 + try {
118 + //add all the LSA Headers - one header is of 20 bytes
119 + while (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) {
120 + LsaHeader header = OspfUtil.readLsaHeader(channelBuffer);
121 + //add the LSAHeader to acknowledge
122 + addLinkStateHeader(header);
123 + }
124 +
125 + } catch (Exception e) {
126 + log.debug("Error::LsAckPacket:: {}", e.getMessage());
127 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
128 + }
129 + }
130 +
131 + @Override
132 + public byte[] asBytes() {
133 + byte[] lsAckMessage = null;
134 +
135 + byte[] lsAckHeader = getLsAckAsByteArray();
136 + byte[] lsAckBody = getLsAckBodyAsByteArray();
137 + lsAckMessage = Bytes.concat(lsAckHeader, lsAckBody);
138 +
139 + return lsAckMessage;
140 + }
141 +
142 + /**
143 + * Gets LSAcknowledge as byte array.
144 + *
145 + * @return byte array
146 + */
147 + public byte[] getLsAckAsByteArray() {
148 + List<Byte> headerLst = new ArrayList<>();
149 + try {
150 + headerLst.add((byte) this.ospfVersion());
151 + headerLst.add((byte) this.ospfType());
152 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
153 + headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
154 + headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
155 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
156 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
157 + //Authentication is 0 always. Total 8 bytes consist of zero
158 + byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
159 + headerLst.addAll(Bytes.asList(auth));
160 + } catch (Exception e) {
161 + log.debug("Error::LsAckPacket:: {}", e.getMessage());
162 + return Bytes.toArray(headerLst);
163 + }
164 +
165 + return Bytes.toArray(headerLst);
166 + }
167 +
168 + /**
169 + * Gets LsAck body as byte array.
170 + *
171 + * @return byte array
172 + */
173 + public byte[] getLsAckBodyAsByteArray() {
174 + List<Byte> bodyLst = new ArrayList<>();
175 +
176 + try {
177 + for (LsaHeader lsaHeader : linkStateHeaders) {
178 + if (lsaHeader.lsType() == OspfParameters.LINK_LOCAL_OPAQUE_LSA ||
179 + lsaHeader.lsType() == OspfParameters.AREA_LOCAL_OPAQUE_LSA ||
180 + lsaHeader.lsType() == OspfParameters.AS_OPAQUE_LSA) {
181 + OpaqueLsaHeader header = (OpaqueLsaHeader) lsaHeader;
182 + bodyLst.addAll(Bytes.asList(header.getOpaqueLsaHeaderAsByteArray()));
183 + } else {
184 + bodyLst.addAll(Bytes.asList(lsaHeader.getLsaHeaderAsByteArray()));
185 + }
186 + }
187 + } catch (Exception e) {
188 + log.debug("Error::getLsAckBodyAsByteArray {}", e.getMessage());
189 + return Bytes.toArray(bodyLst);
190 + }
191 +
192 + return Bytes.toArray(bodyLst);
193 + }
194 +
195 + @Override
196 + public String toString() {
197 + return MoreObjects.toStringHelper(getClass())
198 + .omitNullValues()
199 + .add("linkStateHeaders", linkStateHeaders)
200 + .toString();
201 + }
202 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.util;
17 +
18 +/**
19 + * Representation of different OSPF packet types.
20 + */
21 +public enum OspfPacketType {
22 +
23 + HELLO(1),
24 + DD(2),
25 + LSREQUEST(3),
26 + LSUPDATE(4),
27 + LSAACK(5);
28 +
29 + private int value;
30 +
31 + /**
32 + * Creates instance of OSPF packet types.
33 + *
34 + * @param value
35 + */
36 + OspfPacketType(int value) {
37 + this.value = value;
38 + }
39 +
40 + /**
41 + * Gets the value.
42 + *
43 + * @return value
44 + */
45 + public int value() {
46 + return value;
47 + }
48 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2014 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 + */
16 +
17 +/**
18 + * Implementation of the OSPF Packet.
19 + */
20 +package org.onosproject.ospf.protocol.ospfpacket.types;
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.subtype;
17 +
18 +import org.junit.After;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.CoreMatchers.is;
23 +import static org.hamcrest.CoreMatchers.notNullValue;
24 +import static org.hamcrest.MatcherAssert.assertThat;
25 +
26 +/**
27 + * Unit test class for LsRequestPacket.
28 + */
29 +public class LsRequestPacketTest {
30 +
31 + private LsRequestPacket lsrPacket;
32 + private int result;
33 +
34 + @Before
35 + public void setUp() throws Exception {
36 + lsrPacket = new LsRequestPacket();
37 + }
38 +
39 + @After
40 + public void tearDown() throws Exception {
41 + lsrPacket = null;
42 + }
43 +
44 + /**
45 + * Tests lsType() getter method.
46 + */
47 + @Test
48 + public void testGetLsType() throws Exception {
49 + lsrPacket.setLsType(1);
50 + assertThat(lsrPacket.lsType(), is(1));
51 + }
52 +
53 + /**
54 + * Tests lsType() setter method.
55 + */
56 + @Test
57 + public void testSetLsType() throws Exception {
58 + lsrPacket.setLsType(1);
59 + assertThat(lsrPacket.lsType(), is(1));
60 + }
61 +
62 + /**
63 + * Tests linkStateId() getter method.
64 + */
65 + @Test
66 + public void testGetLinkStateId() throws Exception {
67 + lsrPacket.setLinkStateId("1.1.1.1");
68 + assertThat(lsrPacket.linkStateId(), is("1.1.1.1"));
69 + }
70 +
71 + /**
72 + * Tests linkStateId() setter method.
73 + */
74 + @Test
75 + public void testSetLinkStateId() throws Exception {
76 + lsrPacket.setLinkStateId("1.1.1.1");
77 + assertThat(lsrPacket.linkStateId(), is("1.1.1.1"));
78 + }
79 +
80 + /**
81 + * Tests ownRouterId() getter method.
82 + */
83 + @Test
84 + public void testGetOwnRouterId() throws Exception {
85 + lsrPacket.setOwnRouterId("1.1.1.1");
86 + assertThat(lsrPacket.ownRouterId(), is("1.1.1.1"));
87 + }
88 +
89 + /**
90 + * Tests ownRouterId() setter method.
91 + */
92 + @Test
93 + public void testSetOwnRouterId() throws Exception {
94 + lsrPacket.setOwnRouterId("1.1.1.1");
95 + assertThat(lsrPacket.ownRouterId(), is("1.1.1.1"));
96 + }
97 +
98 + /**
99 + * Tests to string method.
100 + */
101 + @Test
102 + public void testToString() throws Exception {
103 + assertThat(lsrPacket.toString(), is(notNullValue()));
104 + }
105 +
106 + /**
107 + * Tests equals() method.
108 + */
109 + @Test
110 + public void testEquals() throws Exception {
111 + assertThat(lsrPacket.equals(new LsRequestPacket()), is(false));
112 + }
113 +
114 + /**
115 + * Tests hashCode() method.
116 + */
117 + @Test
118 + public void testHashCode() throws Exception {
119 + result = lsrPacket.hashCode();
120 + assertThat(result, is(notNullValue()));
121 + }
122 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Assert;
22 +import org.junit.Before;
23 +import org.junit.Test;
24 +import org.onlab.packet.Ip4Address;
25 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
27 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
28 +import org.onosproject.ospf.protocol.util.OspfPacketType;
29 +
30 +import java.util.List;
31 +import java.util.Vector;
32 +
33 +import static org.hamcrest.CoreMatchers.is;
34 +import static org.hamcrest.CoreMatchers.notNullValue;
35 +import static org.hamcrest.MatcherAssert.assertThat;
36 +
37 +/**
38 + * Unit test class for OspfRouterId.
39 + */
40 +public class DdPacketTest {
41 +
42 + private byte[] packet;
43 + private byte[] result2;
44 + private DdPacket ddPacket;
45 + private Vector<LsaHeader> lsaHeaderList = new Vector<LsaHeader>();
46 + private int result;
47 + private long result1;
48 + private OpaqueLsaHeader opqueHeader;
49 + private OpaqueLsaHeader opqueHeader1;
50 + private List<LsaHeader> header;
51 + private OspfPacketHeader ospfPacketHeader;
52 + private ChannelBuffer channelBuffer;
53 + private LsaHeader lsaHeader;
54 + private long result3;
55 + private OspfPacketType ospfPacketType;
56 +
57 + @Before
58 + public void setUp() throws Exception {
59 + ddPacket = new DdPacket();
60 + ddPacket.setAuthType(1);
61 + ddPacket.setOspftype(2);
62 + ddPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
63 + ddPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
64 + ddPacket.setChecksum(201);
65 + ddPacket.setAuthentication(2);
66 + ddPacket.setOspfPacLength(48);
67 + ddPacket.setOspfVer(2);
68 + }
69 +
70 + @After
71 + public void tearDown() throws Exception {
72 + ddPacket = null;
73 + lsaHeaderList.clear();
74 + opqueHeader = null;
75 + opqueHeader1 = null;
76 + header = null;
77 + ospfPacketHeader = null;
78 + channelBuffer = null;
79 + lsaHeader = null;
80 + ospfPacketType = null;
81 + }
82 +
83 + /**
84 + * Tests isOpaqueCapable() getter method.
85 + */
86 + @Test
87 + public void testIsOpaqueCapable() throws Exception {
88 + ddPacket.setIsOpaqueCapable(true);
89 + assertThat(ddPacket.isOpaqueCapable(), is(true));
90 + }
91 +
92 + /**
93 + * Tests isOpaqueCapable() setter method.
94 + */
95 + @Test
96 + public void testSetIsOpaqueCapable() throws Exception {
97 + ddPacket.setIsOpaqueCapable(true);
98 + assertThat(ddPacket.isOpaqueCapable(), is(true));
99 + }
100 +
101 + /**
102 + * Tests ims() getter method.
103 + */
104 + @Test
105 + public void testGetIms() throws Exception {
106 + ddPacket.setIms(1);
107 + result = ddPacket.ims();
108 + assertThat(result, is(notNullValue()));
109 + assertThat(result, is(1));
110 + }
111 +
112 + /**
113 + * Tests ims() setter method.
114 + */
115 + @Test
116 + public void testSetIms() throws Exception {
117 + ddPacket.setIms(1);
118 + result = ddPacket.ims();
119 + assertThat(result, is(notNullValue()));
120 + assertThat(result, is(1));
121 + }
122 +
123 + /**
124 + * Tests isMaster() getter method.
125 + */
126 + @Test
127 + public void testGetIsMaster() throws Exception {
128 + ddPacket.setIsMaster(2);
129 + result = ddPacket.isMaster();
130 + assertThat(result, is(notNullValue()));
131 + assertThat(result, is(2));
132 + }
133 +
134 + /**
135 + * Tests isMaster() setter method.
136 + */
137 + @Test
138 + public void testSetIsMaster() throws Exception {
139 + ddPacket.setIsMaster(2);
140 + result = ddPacket.isMaster();
141 + assertThat(result, is(notNullValue()));
142 + assertThat(result, is(2));
143 + }
144 +
145 + /**
146 + * Tests isInitialize() getter method.
147 + */
148 + @Test
149 + public void testGetIsInitialize() throws Exception {
150 + ddPacket.setIsInitialize(3);
151 + result = ddPacket.isInitialize();
152 + assertThat(result, is(notNullValue()));
153 + assertThat(result, is(3));
154 + }
155 +
156 + /**
157 + * Tests isInitialize() setter method.
158 + */
159 + @Test
160 + public void testSetIsInitialize() throws Exception {
161 + ddPacket.setIsInitialize(3);
162 + int result = ddPacket.isInitialize();
163 + assertThat(result, is(notNullValue()));
164 + assertThat(result, is(3));
165 + }
166 +
167 + /**
168 + * Tests isMore() getter method.
169 + */
170 + @Test
171 + public void testGetIsMore() throws Exception {
172 + ddPacket.setIsMore(4);
173 + result = ddPacket.isMore();
174 + assertThat(result, is(notNullValue()));
175 + assertThat(result, is(4));
176 + }
177 +
178 + /**
179 + * Tests isMore() setter method.
180 + */
181 + @Test
182 + public void testSetIsMore() throws Exception {
183 + ddPacket.setIsMore(4);
184 + int result = ddPacket.isMore();
185 + assertThat(result, is(notNullValue()));
186 + assertThat(result, is(4));
187 + }
188 +
189 + /**
190 + * Tests imtu() getter method.
191 + */
192 + @Test
193 + public void testGetImtu() throws Exception {
194 + ddPacket.setImtu(5);
195 + result = ddPacket.imtu();
196 + assertThat(result, is(notNullValue()));
197 + assertThat(result, is(5));
198 + }
199 +
200 + /**
201 + * Tests imtu() setter method.
202 + */
203 + @Test
204 + public void testSetImtu() throws Exception {
205 + ddPacket.setImtu(5);
206 + result = ddPacket.imtu();
207 + assertThat(result, is(notNullValue()));
208 + assertThat(result, is(5));
209 + }
210 +
211 + /**
212 + * Tests options() getter method.
213 + */
214 + @Test
215 + public void testGetOptions() throws Exception {
216 + ddPacket.setOptions(2);
217 + result = ddPacket.options();
218 + assertThat(result, is(notNullValue()));
219 + assertThat(result, is(2));
220 + }
221 +
222 + /**
223 + * Tests options() setter method.
224 + */
225 + @Test
226 + public void testSetOptions() throws Exception {
227 + ddPacket.setOptions(2);
228 + result = ddPacket.options();
229 + Assert.assertNotNull(result);
230 + Assert.assertEquals(2, result);
231 + }
232 +
233 + /**
234 + * Tests sequenceNo() getter method.
235 + */
236 + @Test
237 + public void testGetSequenceno() throws Exception {
238 + ddPacket.setSequenceNo(2020);
239 + result1 = ddPacket.sequenceNo();
240 + assertThat(result1, is(notNullValue()));
241 + assertThat(result1, is(2020L));
242 + }
243 +
244 + /**
245 + * Tests sequenceNo() setter method.
246 + */
247 + @Test
248 + public void testSetSequenceno() throws Exception {
249 + ddPacket.setSequenceNo(2020);
250 + result3 = ddPacket.sequenceNo();
251 + assertThat(result3, is(notNullValue()));
252 + assertThat(result3, is(2020L));
253 + }
254 +
255 + /**
256 + * Tests getLsaHeaderList() getter method.
257 + */
258 + @Test
259 + public void testGetLsaHeaderList() throws Exception {
260 + ddPacket.addLsaHeader(createLsaHeader());
261 + opqueHeader = new OpaqueLsaHeader();
262 + opqueHeader.setLsType(9);
263 + opqueHeader.setLsPacketLen(48);
264 + opqueHeader.setLsCheckSum(10);
265 + opqueHeader.setAge(4);
266 + opqueHeader.setOpaqueId(9);
267 + opqueHeader.setOpaqueType(9);
268 + opqueHeader.setLsSequenceNo(250);
269 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
270 + opqueHeader.setOptions(2);
271 + ddPacket.setIsOpaqueCapable(true);
272 + ddPacket.addLsaHeader(opqueHeader);
273 + opqueHeader1 = new OpaqueLsaHeader();
274 + opqueHeader1.setLsType(10);
275 + opqueHeader1.setLsPacketLen(48);
276 + opqueHeader1.setLsCheckSum(10);
277 + opqueHeader1.setAge(4);
278 + opqueHeader1.setOpaqueId(9);
279 + opqueHeader1.setOpaqueType(9);
280 + opqueHeader1.setLsSequenceNo(250);
281 + opqueHeader1.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
282 + opqueHeader1.setOptions(66);
283 + ddPacket.addLsaHeader(opqueHeader1);
284 + header = ddPacket.getLsaHeaderList();
285 + assertThat(header, is(notNullValue()));
286 + }
287 +
288 + /**
289 + * Tests getLsaHeaderList() setter method.
290 + */
291 + @Test
292 + public void testSetLsaHeaderList() throws Exception {
293 + ddPacket.addLsaHeader(createLsaHeader());
294 + opqueHeader = new OpaqueLsaHeader();
295 + opqueHeader.setLsType(9);
296 + opqueHeader.setLsPacketLen(48);
297 + opqueHeader.setLsCheckSum(10);
298 + opqueHeader.setAge(4);
299 + opqueHeader.setOpaqueId(9);
300 + opqueHeader.setOpaqueType(9);
301 + opqueHeader.setLsSequenceNo(250);
302 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
303 + opqueHeader.setOptions(66);
304 + ddPacket.addLsaHeader(opqueHeader);
305 + opqueHeader1 = new OpaqueLsaHeader();
306 + opqueHeader1.setLsType(10);
307 + opqueHeader1.setLsPacketLen(48);
308 + opqueHeader1.setLsCheckSum(10);
309 + opqueHeader1.setAge(4);
310 + opqueHeader1.setOpaqueId(9);
311 + opqueHeader1.setOpaqueType(9);
312 + opqueHeader1.setLsSequenceNo(250);
313 + opqueHeader1.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
314 + opqueHeader1.setOptions(2);
315 + ddPacket.addLsaHeader(opqueHeader1);
316 + header = ddPacket.getLsaHeaderList();
317 + assertThat(header.contains(createLsaHeader()), is(true));
318 + }
319 +
320 + /**
321 + * Tests addLsaHeader() method.
322 + */
323 + @Test
324 + public void testAddLsaHeader() throws Exception {
325 + ddPacket.addLsaHeader(createLsaHeader());
326 + assertThat(ddPacket, is(notNullValue()));
327 + }
328 +
329 + /**
330 + * Tests ospfMessageType() getter method.
331 + */
332 + @Test
333 + public void testGetOspfMessageType() throws Exception {
334 + ospfPacketType = ddPacket.ospfMessageType();
335 + assertThat(ospfPacketType, is(notNullValue()));
336 + assertThat(ospfPacketType, is(OspfPacketType.DD));
337 + }
338 +
339 + /**
340 + * Tests readFrom() method.
341 + */
342 + @Test
343 + public void testReadFrom() throws Exception {
344 + ospfPacketHeader = new OspfPacketHeader();
345 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
346 + ospfPacketHeader.setAuthentication(0);
347 + ospfPacketHeader.setAuthType(0);
348 + ospfPacketHeader.setChecksum(12345);
349 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
350 + ospfPacketHeader.setOspfPacLength(56);
351 + ospfPacketHeader.setOspftype(2);
352 + ospfPacketHeader.setOspfVer(2);
353 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
354 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
355 + ddPacket.setIsOpaqueCapable(true);
356 + ddPacket.setOptions(66);
357 + ddPacket = new DdPacket(ospfPacketHeader);
358 + packet = createByteForDdPacket();
359 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
360 + ddPacket.readFrom(channelBuffer);
361 + assertThat(ddPacket, is(notNullValue()));
362 + assertThat(ddPacket.ospfMessageType(), is(OspfPacketType.DD));
363 + }
364 +
365 + /**
366 + * Tests asBytes() method.
367 + */
368 + @Test
369 + public void testAsBytes() throws Exception {
370 + result2 = ddPacket.asBytes();
371 + assertThat(result2, is(notNullValue()));
372 + }
373 +
374 + /**
375 + * Tests getDdHeaderAsByteArray() method.
376 + */
377 + @Test
378 + public void testGetDdHeaderAsByteArray() throws Exception {
379 + opqueHeader = new OpaqueLsaHeader();
380 + opqueHeader.setLsType(9);
381 + opqueHeader.setLsPacketLen(48);
382 + opqueHeader.setLsCheckSum(10);
383 + opqueHeader.setAge(4);
384 + opqueHeader.setOpaqueId(9);
385 + opqueHeader.setOpaqueType(9);
386 + opqueHeader.setLsSequenceNo(250);
387 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
388 + opqueHeader.setOptions(66);
389 + ddPacket.addLsaHeader(opqueHeader);
390 + opqueHeader1 = new OpaqueLsaHeader();
391 + opqueHeader1.setLsType(10);
392 + opqueHeader1.setLsPacketLen(48);
393 + opqueHeader1.setLsCheckSum(10);
394 + opqueHeader1.setAge(4);
395 + opqueHeader1.setOpaqueId(9);
396 + opqueHeader1.setOpaqueType(9);
397 + opqueHeader1.setLsSequenceNo(250);
398 + opqueHeader1.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
399 + opqueHeader1.setOptions(2);
400 + ddPacket.addLsaHeader(opqueHeader1);
401 + result2 = ddPacket.getDdHeaderAsByteArray();
402 + assertThat(result2, is(notNullValue()));
403 + }
404 +
405 + /**
406 + * Tests getDdBodyAsByteArray() method.
407 + */
408 + @Test
409 + public void testGetDdBodyAsByteArray() throws Exception {
410 + lsaHeader = createLsaHeader();
411 + ddPacket.addLsaHeader(lsaHeader);
412 + result2 = ddPacket.getDdBodyAsByteArray();
413 + assertThat(result2, is(notNullValue()));
414 + }
415 +
416 + /**
417 + * Tests to string method.
418 + */
419 + @Test
420 + public void testToString() throws Exception {
421 + assertThat(ddPacket.toString(), is(notNullValue()));
422 + }
423 +
424 + /**
425 + * Utility method used by junit methods.
426 + */
427 + private LsaHeader createLsaHeader() {
428 + lsaHeader = new LsaHeader();
429 + lsaHeader.setAge(10);
430 + lsaHeader.setLinkStateId("10.226.165.164");
431 + lsaHeader.setLsCheckSum(222);
432 + lsaHeader.setLsPacketLen(48);
433 + lsaHeader.setLsSequenceNo(2020);
434 + lsaHeader.setLsType(2);
435 + lsaHeader.setOptions(2);
436 + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165"));
437 + return lsaHeader;
438 + }
439 +
440 + /**
441 + * Utility method used by junit methods.
442 + */
443 + private byte[] createByteForDdPacket() {
444 + byte[] ddPacket = {5, -36, 66, 1, 65, 119, -87, 126, 0, 23, 2, 1, 10, 10,
445 + 10, 10, 10, 10, 10, 10, -128, 0, 0, 6, -69, 26, 0, 36};
446 +
447 + return ddPacket;
448 + }
449 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
25 +import org.onosproject.ospf.protocol.util.OspfPacketType;
26 +
27 +import java.util.Vector;
28 +
29 +import static org.hamcrest.CoreMatchers.is;
30 +import static org.hamcrest.CoreMatchers.notNullValue;
31 +import static org.junit.Assert.assertThat;
32 +
33 +/**
34 + * Unit test class for HelloPacket.
35 + */
36 +public class HelloPacketTest {
37 +
38 + private boolean result1;
39 + private OspfPacketType ospfPacketType;
40 + private OspfPacketHeader ospfPacketHeader;
41 + private HelloPacket helloPacket;
42 + private Vector<String> neighborAddress = new Vector();
43 + private Ip4Address result;
44 + private int result2;
45 + private byte[] packet;
46 + private ChannelBuffer channelBuffer;
47 + private byte[] result3;
48 +
49 + @Before
50 + public void setUp() throws Exception {
51 + helloPacket = new HelloPacket();
52 + helloPacket.setAuthType(1);
53 + helloPacket.setOspftype(2);
54 + helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
55 + helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
56 + helloPacket.setChecksum(201);
57 + helloPacket.setAuthentication(2);
58 + helloPacket.setOspfPacLength(48);
59 + helloPacket.setOspfVer(2);
60 +
61 + }
62 +
63 + @After
64 + public void tearDown() throws Exception {
65 + helloPacket = null;
66 + result = null;
67 + ospfPacketType = null;
68 + ospfPacketHeader = null;
69 + packet = null;
70 + channelBuffer = null;
71 + result3 = null;
72 + }
73 +
74 + /**
75 + * Tests networkMask() getter method.
76 + */
77 + @Test
78 + public void testGetNetworkMask() throws Exception {
79 + helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
80 + result = helloPacket.networkMask();
81 + assertThat(result, is(notNullValue()));
82 + assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
83 + }
84 +
85 + /**
86 + * Tests networkMask() setter method.
87 + */
88 + @Test
89 + public void testSetNetworkMask() throws Exception {
90 + helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
91 + result = helloPacket.networkMask();
92 + assertThat(result, is(notNullValue()));
93 + assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
94 + }
95 +
96 + /**
97 + * Tests bdr() setter method.
98 + */
99 + @Test
100 + public void testSetBdr() throws Exception {
101 + helloPacket.setBdr(Ip4Address.valueOf("10.226.165.166"));
102 + result = helloPacket.bdr();
103 + assertThat(result, is(notNullValue()));
104 + assertThat(result, is(Ip4Address.valueOf("10.226.165.166")));
105 + }
106 +
107 + /**
108 + * Tests dr() getter method.
109 + */
110 + @Test
111 + public void testGetDr() throws Exception {
112 + helloPacket.setDr(Ip4Address.valueOf("10.226.165.167"));
113 + result = helloPacket.dr();
114 + assertThat(result, is(notNullValue()));
115 + assertThat(result, is(Ip4Address.valueOf("10.226.165.167")));
116 + }
117 +
118 + /**
119 + * Tests dr() setter method.
120 + */
121 + @Test
122 + public void testSetDr() throws Exception {
123 + helloPacket.setDr(Ip4Address.valueOf("10.226.165.167"));
124 + result = helloPacket.dr();
125 + assertThat(result, is(notNullValue()));
126 + assertThat(result, is(Ip4Address.valueOf("10.226.165.167")));
127 + }
128 +
129 + /**
130 + * Tests addNeighbor() method.
131 + */
132 + @Test
133 + public void testAddNeighbor() throws Exception {
134 + helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.170"));
135 + result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.170"));
136 + assertThat(result1, is(true));
137 + }
138 +
139 + /**
140 + * Tests containsNeighbour() method.
141 + */
142 + @Test
143 + public void testContainsNeighbour() throws Exception {
144 + helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.200"));
145 + result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.200"));
146 + assertThat(result1, is(true));
147 + }
148 +
149 +
150 + /**
151 + * Tests options() getter method.
152 + */
153 + @Test
154 + public void testGetOptions() throws Exception {
155 + helloPacket.setOptions(10);
156 + result2 = helloPacket.options();
157 + assertThat(result2, is(notNullValue()));
158 + assertThat(result2, is(10));
159 + }
160 +
161 + /**
162 + * Tests options() setter method.
163 + */
164 + @Test
165 + public void testSetOptions() throws Exception {
166 + helloPacket.setOptions(11);
167 + result2 = helloPacket.options();
168 + assertThat(result2, is(notNullValue()));
169 + assertThat(result2, is(11));
170 + }
171 +
172 + /**
173 + * Tests routerPriority() getter method.
174 + */
175 + @Test
176 + public void testGetRouterPriority() throws Exception {
177 + helloPacket.setRouterPriority(1);
178 + result2 = helloPacket.routerPriority();
179 + assertThat(result2, is(notNullValue()));
180 + assertThat(result2, is(1));
181 + }
182 +
183 + /**
184 + * Tests routerPriority() setter method.
185 + */
186 + @Test
187 + public void testSetRouterPriority() throws Exception {
188 + helloPacket.setRouterPriority(2);
189 + result2 = helloPacket.routerPriority();
190 + assertThat(result2, is(notNullValue()));
191 + assertThat(result2, is(2));
192 + }
193 +
194 + /**
195 + * Tests helloInterval() getter method.
196 + */
197 + @Test
198 + public void testGetHelloInterval() throws Exception {
199 + helloPacket.setHelloInterval(10);
200 + result2 = helloPacket.helloInterval();
201 + assertThat(result2, is(notNullValue()));
202 + assertThat(result2, is(10));
203 + }
204 +
205 + /**
206 + * Tests helloInterval() setter method.
207 + */
208 + @Test
209 + public void testSetHelloInterval() throws Exception {
210 + helloPacket.setHelloInterval(10);
211 + result2 = helloPacket.helloInterval();
212 + assertThat(result2, is(notNullValue()));
213 + assertThat(result2, is(10));
214 + }
215 +
216 + /**
217 + * Tests routerDeadInterval() getter method.
218 + */
219 + @Test
220 + public void testGetRouterDeadInterval() throws Exception {
221 + helloPacket.setRouterDeadInterval(50);
222 + result2 = helloPacket.routerDeadInterval();
223 + assertThat(result2, is(notNullValue()));
224 + assertThat(result2, is(50));
225 + }
226 +
227 + /**
228 + * Tests routerDeadInterval() setter method.
229 + */
230 + @Test
231 + public void testSetRouterDeadInterval() throws Exception {
232 + helloPacket.setRouterDeadInterval(50);
233 + result2 = helloPacket.routerDeadInterval();
234 + assertThat(result2, is(notNullValue()));
235 + assertThat(result2, is(50));
236 + }
237 +
238 + /**
239 + * Tests ospfMessageType() getter method.
240 + */
241 + @Test
242 + public void testGetOspfMessageType() throws Exception {
243 + ospfPacketType = helloPacket.ospfMessageType();
244 + assertThat(ospfPacketType, is(notNullValue()));
245 + assertThat(ospfPacketType, is(OspfPacketType.HELLO));
246 + }
247 +
248 + /**
249 + * Tests readFrom() method.
250 + */
251 + @Test
252 + public void testReadFrom() throws Exception {
253 + ospfPacketHeader = new OspfPacketHeader();
254 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
255 + ospfPacketHeader.setAuthentication(0);
256 + ospfPacketHeader.setAuthType(0);
257 + ospfPacketHeader.setChecksum(12345);
258 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
259 + ospfPacketHeader.setOspfPacLength(56);
260 + ospfPacketHeader.setOspftype(1);
261 + ospfPacketHeader.setOspfVer(2);
262 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
263 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
264 + packet = createByteForHelloPacket();
265 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
266 + helloPacket.readFrom(channelBuffer);
267 + assertThat(helloPacket, is(notNullValue()));
268 + assertThat(helloPacket.ospfMessageType(), is(OspfPacketType.HELLO));
269 + }
270 +
271 + /**
272 + * Tests asBytes() method.
273 + */
274 + @Test
275 + public void testAsBytes() throws Exception {
276 + result3 = helloPacket.asBytes();
277 + assertThat(result3, is(notNullValue()));
278 + }
279 +
280 + /**
281 + * Tests getHelloHeaderAsByteArray() method.
282 + */
283 + @Test
284 + public void testGetHelloHeaderAsByteArray() throws Exception {
285 + result3 = helloPacket.getHelloHeaderAsByteArray();
286 + assertThat(result3, is(notNullValue()));
287 + }
288 +
289 + /**
290 + * Tests getHelloBodyAsByteArray() method.
291 + */
292 + @Test
293 + public void testGetHelloBodyAsByteArray() throws Exception {
294 + neighborAddress.add("10.226.165.100");
295 + result3 = helloPacket.getHelloBodyAsByteArray();
296 + assertThat(result3, is(notNullValue()));
297 + }
298 +
299 + /**
300 + * Tests getHelloBodyAsByteArray() method.
301 + */
302 + @Test
303 + public void testReadHelloBody() throws Exception {
304 + helloPacket.getHelloBodyAsByteArray();
305 + assertThat(helloPacket, is(notNullValue()));
306 + }
307 +
308 + /**
309 + * Tests to string method.
310 + */
311 + @Test
312 + public void testToString() throws Exception {
313 + assertThat(helloPacket.toString(), is(notNullValue()));
314 + }
315 +
316 + /**
317 + * Utility method used by junit methods.
318 + */
319 + private byte[] createByteForHelloPacket() {
320 + byte[] helloPacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59, 0, 0, 0, 0,
321 + 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
322 +
323 + return helloPacket;
324 + }
325 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import org.hamcrest.MatcherAssert;
19 +import org.jboss.netty.buffer.ChannelBuffer;
20 +import org.jboss.netty.buffer.ChannelBuffers;
21 +import org.junit.After;
22 +import org.junit.Before;
23 +import org.junit.Test;
24 +import org.onlab.packet.Ip4Address;
25 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
27 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
28 +import org.onosproject.ospf.protocol.util.OspfPacketType;
29 +
30 +import java.util.List;
31 +
32 +import static org.hamcrest.CoreMatchers.is;
33 +import static org.hamcrest.CoreMatchers.notNullValue;
34 +import static org.junit.Assert.assertThat;
35 +
36 +/**
37 + * Unit test class for LsAck.
38 + */
39 +public class LsAcknowledgeTest {
40 +
41 + private LsaHeader lsaHeader;
42 + private LsAcknowledge lsAck;
43 + private OspfPacketType ospfPacketType;
44 + private OspfPacketHeader ospfPacketHeader;
45 + private byte[] result;
46 + private ChannelBuffer channelBuffer;
47 + private OpaqueLsaHeader opaqueLsaHeader;
48 +
49 + @Before
50 + public void setUp() throws Exception {
51 + lsaHeader = new LsaHeader();
52 + lsAck = new LsAcknowledge();
53 + lsAck.setAuthType(1);
54 + lsAck.setOspftype(5);
55 + lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
56 + lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
57 + lsAck.setChecksum(201);
58 + lsAck.setAuthentication(2);
59 + lsAck.setOspfPacLength(48);
60 + lsAck.setOspfVer(2);
61 + }
62 +
63 + @After
64 + public void tearDown() throws Exception {
65 + lsaHeader = null;
66 + lsAck = null;
67 + ospfPacketType = null;
68 + ospfPacketHeader = null;
69 + result = null;
70 + channelBuffer = null;
71 + opaqueLsaHeader = null;
72 + }
73 +
74 + /**
75 + * Tests getLinkStateHeaders() getter method.
76 + */
77 + @Test
78 + public void testGetLinkStateHeaders() throws Exception {
79 + lsaHeader = createLsaHeader();
80 + lsAck.addLinkStateHeader(lsaHeader);
81 + lsAck.addLinkStateHeader(lsaHeader);
82 + List headers = lsAck.getLinkStateHeaders();
83 + assertThat(headers.size(), is(1));
84 +
85 + }
86 +
87 + /**
88 + * Tests addLinkStateHeader() method.
89 + */
90 + @Test
91 + public void testAddLinkStateHeader() throws Exception {
92 + lsaHeader = createLsaHeader();
93 + lsAck.addLinkStateHeader(lsaHeader);
94 + lsAck.addLinkStateHeader(lsaHeader);
95 + assertThat(lsAck, is(notNullValue()));
96 + }
97 +
98 +
99 + /**
100 + * Tests ospfMessageType() getter method.
101 + */
102 + @Test
103 + public void testGetOSPFMessageType() throws Exception {
104 + ospfPacketType = lsAck.ospfMessageType();
105 + assertThat(ospfPacketType, is(notNullValue()));
106 + assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
107 + }
108 +
109 + /**
110 + * Tests ospfMessageType() getter method.
111 + */
112 + @Test
113 + public void testGetOspfMessageType() throws Exception {
114 + ospfPacketType = lsAck.ospfMessageType();
115 + assertThat(ospfPacketType, is(notNullValue()));
116 + assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
117 + }
118 +
119 + /**
120 + * Tests readFrom() method.
121 + */
122 + @Test
123 + public void testReadFrom() throws Exception {
124 + ospfPacketHeader = new OspfPacketHeader();
125 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
126 + ospfPacketHeader.setAuthentication(0);
127 + ospfPacketHeader.setAuthType(0);
128 + ospfPacketHeader.setChecksum(12345);
129 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
130 + ospfPacketHeader.setOspfPacLength(56);
131 + ospfPacketHeader.setOspftype(5);
132 + ospfPacketHeader.setOspfVer(2);
133 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
134 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
135 + result = createByteForLSAck();
136 + lsAck = new LsAcknowledge(ospfPacketHeader);
137 + channelBuffer = ChannelBuffers.copiedBuffer(result);
138 + lsAck.readFrom(channelBuffer);
139 + assertThat(lsAck, is(notNullValue()));
140 + assertThat(lsAck.ospfMessageType(), is(OspfPacketType.LSAACK));
141 + }
142 +
143 + /**
144 + * Tests asBytes() method.
145 + */
146 + @Test
147 + public void testAsBytes() throws Exception {
148 + result = lsAck.asBytes();
149 + assertThat(result, is(notNullValue()));
150 + }
151 +
152 + /**
153 + * Tests getLsAckAsByteArray() method.
154 + */
155 + @Test
156 + public void testGetLsAckAsByteArray() throws Exception {
157 + result = lsAck.getLsAckAsByteArray();
158 + assertThat(result, is(notNullValue()));
159 + }
160 +
161 + /**
162 + * Tests getLsAckBodyAsByteArray() method.
163 + */
164 + @Test
165 + public void testGetLsAckBodyAsByteArray() throws Exception {
166 + lsaHeader = createLsaHeader();
167 + opaqueLsaHeader = new OpaqueLsaHeader();
168 + lsAck.addLinkStateHeader(lsaHeader);
169 + lsAck.addLinkStateHeader(opaqueLsaHeader);
170 + result = lsAck.getLsAckBodyAsByteArray();
171 + assertThat(result, is(notNullValue()));
172 + }
173 +
174 + /**
175 + * Tests to string method.
176 + */
177 + @Test
178 + public void testToString() throws Exception {
179 + MatcherAssert.assertThat(lsAck.toString(), is(notNullValue()));
180 + }
181 +
182 + /**
183 + * Utility method used by junit methods.
184 + */
185 + private LsaHeader createLsaHeader() {
186 + lsaHeader = new LsaHeader();
187 + lsaHeader.setAge(10);
188 + lsaHeader.setLinkStateId("10.226.165.164");
189 + lsaHeader.setLsCheckSum(222);
190 + lsaHeader.setLsPacketLen(48);
191 + lsaHeader.setLsSequenceNo(2020);
192 + lsaHeader.setLsType(5);
193 + lsaHeader.setOptions(2);
194 + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165"));
195 + return lsaHeader;
196 + }
197 +
198 + /**
199 + * Utility method used by junit methods.
200 + */
201 + private byte[] createByteForLSAck() {
202 + byte[] lsAckPacket = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, -30,
203 + -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
204 + 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
205 +
206 + return lsAckPacket;
207 + }
208 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 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 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
25 +import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket;
26 +import org.onosproject.ospf.protocol.util.OspfPacketType;
27 +
28 +import java.net.UnknownHostException;
29 +import java.util.List;
30 +
31 +import static org.hamcrest.CoreMatchers.is;
32 +import static org.hamcrest.CoreMatchers.notNullValue;
33 +import static org.junit.Assert.assertThat;
34 +
35 +/**
36 + * Unit test class for LsRequest.
37 + */
38 +public class LsRequestTest {
39 +
40 + private LsRequest lsRequest;
41 + private List<LsRequestPacket> result;
42 + private OspfPacketType ospfMessageType;
43 + private OspfPacketHeader ospfPacketHeader;
44 + private byte[] result1;
45 + private String result2;
46 + private ChannelBuffer channelBuffer;
47 + private LsRequestPacket lsRequestPacket;
48 +
49 + @Before
50 + public void setUp() throws Exception {
51 + lsRequest = new LsRequest();
52 + lsRequest.setAuthType(1);
53 + lsRequest.setOspftype(3);
54 + lsRequest.setRouterId(Ip4Address.valueOf("10.226.165.164"));
55 + lsRequest.setAreaId(Ip4Address.valueOf("10.226.165.163"));
56 + lsRequest.setChecksum(201);
57 + lsRequest.setAuthentication(2);
58 + lsRequest.setOspfPacLength(48);
59 + lsRequest.setOspfVer(2);
60 + }
61 +
62 + @After
63 + public void tearDown() throws Exception {
64 + lsRequest = null;
65 + result = null;
66 + ospfMessageType = null;
67 + ospfPacketHeader = null;
68 + result1 = null;
69 + channelBuffer = null;
70 + lsRequestPacket = null;
71 + }
72 +
73 + /**
74 + * Tests addLinkStateRequests() method.
75 + */
76 + @Test
77 + public void testAddLinkStateRequests() throws Exception {
78 + lsRequest.addLinkStateRequests(createLsRequestPacket());
79 + result = lsRequest.getLinkStateRequests();
80 + assertThat(result, is(notNullValue()));
81 + assertThat(result.size(), is(1));
82 + }
83 +
84 + /**
85 + * Tests getLinkStateRequests() method.
86 + */
87 + @Test
88 + public void testGetLinkStateRequests() throws Exception {
89 + lsRequest.addLinkStateRequests(createLsRequestPacket());
90 + lsRequest.addLinkStateRequests(new LsRequestPacket());
91 + result = lsRequest.getLinkStateRequests();
92 + assertThat(result, is(notNullValue()));
93 + assertThat(result.size(), is(2));
94 + }
95 +
96 + /**
97 + * Tests ospfMessageType()getter method.
98 + */
99 + @Test
100 + public void testGetOspfMessageType() throws Exception {
101 + ospfMessageType = lsRequest.ospfMessageType();
102 + assertThat(ospfMessageType, is(notNullValue()));
103 + assertThat(ospfMessageType, is(OspfPacketType.LSREQUEST));
104 + }
105 +
106 + /**
107 + * Tests readFrom() method.
108 + */
109 + @Test
110 + public void testReadFrom() throws Exception {
111 + ospfPacketHeader = new OspfPacketHeader();
112 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
113 + ospfPacketHeader.setAuthentication(0);
114 + ospfPacketHeader.setAuthType(0);
115 + ospfPacketHeader.setChecksum(12345);
116 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
117 + ospfPacketHeader.setOspfPacLength(56);
118 + ospfPacketHeader.setOspftype(3);
119 + ospfPacketHeader.setOspfVer(2);
120 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
121 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
122 + lsRequest = new LsRequest(ospfPacketHeader);
123 + result1 = createByteLsReqestPacket();
124 + channelBuffer = ChannelBuffers.copiedBuffer(result1);
125 + lsRequest.readFrom(channelBuffer);
126 + assertThat(lsRequest, is(notNullValue()));
127 + assertThat(lsRequest.ospfMessageType(), is(OspfPacketType.LSREQUEST));
128 + }
129 +
130 + /**
131 + * Tests asBytes() method.
132 + */
133 + @Test
134 + public void testAsBytes() throws Exception {
135 + result1 = lsRequest.asBytes();
136 + assertThat(result1, is(notNullValue()));
137 + }
138 +
139 + /**
140 + * Tests getLsrHeaderAsByteArray() method.
141 + */
142 + @Test
143 + public void testGetLsrHeaderAsByteArray() throws Exception {
144 + result1 = lsRequest.getLsrHeaderAsByteArray();
145 + assertThat(result1, is(notNullValue()));
146 + }
147 +
148 + /**
149 + * Tests getLsrBodyAsByteArray() method.
150 + */
151 + @Test
152 + public void testGetLsrBodyAsByteArray() throws Exception {
153 + lsRequest.addLinkStateRequests(createLsRequestPacket());
154 + lsRequest.addLinkStateRequests(new LsRequestPacket());
155 + result1 = lsRequest.getLsrBodyAsByteArray();
156 + assertThat(result1, is(notNullValue()));
157 + }
158 +
159 + /**
160 + * Tests to string method.
161 + */
162 + @Test
163 + public void testToString() throws Exception {
164 + result2 = lsRequest.toString();
165 + assertThat(result2, is(notNullValue()));
166 + }
167 +
168 + private LsRequestPacket createLsRequestPacket() throws UnknownHostException {
169 + lsRequestPacket = new LsRequestPacket();
170 + lsRequestPacket.setOwnRouterId("165");
171 + lsRequestPacket.setLinkStateId("10.226.165.164");
172 + lsRequestPacket.setLsType(2);
173 + return lsRequestPacket;
174 + }
175 +
176 + private byte[] createByteLsReqestPacket() {
177 + byte[] lsRequestPacket = {2, 3, 0, 36, -64, -88, -86, 3, 0, 0, 0, 1, -67,
178 + -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88, -86, 8,
179 + -64, -88, -86, 8};
180 + return lsRequestPacket;
181 + }
182 +}
...\ No newline at end of file ...\ No newline at end of file