Committed by
Thomas Vachuska
ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding
Change-Id: I4bf4b7eb26a0e2b5006b41b24d67c7f21450b11b
Showing
21 changed files
with
2796 additions
and
0 deletions
protocols/ospf/protocol/pom.xml
0 → 100644
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 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
17 | + xmlns="http://maven.apache.org/POM/4.0.0" | ||
18 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
19 | + <modelVersion>4.0.0</modelVersion> | ||
20 | + | ||
21 | + <parent> | ||
22 | + <groupId>org.onosproject</groupId> | ||
23 | + <artifactId>onos-ospf</artifactId> | ||
24 | + <version>1.4.0-SNAPSHOT</version> | ||
25 | + <relativePath>../pom.xml</relativePath> | ||
26 | + </parent> | ||
27 | + | ||
28 | + <artifactId>onos-ospf-protocol</artifactId> | ||
29 | + <packaging>bundle</packaging> | ||
30 | + | ||
31 | + <description>ONOS Ospf controller protocol</description> | ||
32 | + <dependencies> | ||
33 | + <dependency> | ||
34 | + <groupId>org.onosproject</groupId> | ||
35 | + <artifactId>onos-ospf-api</artifactId> | ||
36 | + <version>${project.version}</version> | ||
37 | + </dependency> | ||
38 | + <dependency> | ||
39 | + <groupId>org.onosproject</groupId> | ||
40 | + <artifactId>onos-ospf-api</artifactId> | ||
41 | + <version>1.4.0-SNAPSHOT</version> | ||
42 | + </dependency> | ||
43 | + </dependencies> | ||
44 | + | ||
45 | +</project> |
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.exceptions; | ||
17 | + | ||
18 | +/** | ||
19 | + * Defines all error codes and error sub codes. | ||
20 | + */ | ||
21 | +public final class OspfErrorType { | ||
22 | + | ||
23 | + //Represents an invalid OSPF message header | ||
24 | + public static final byte MESSAGE_HEADER_ERROR = 1; | ||
25 | + //Represents an invalid OSPF message body | ||
26 | + public static final byte OSPF_MESSAGE_ERROR = 2; | ||
27 | + //Message Header error sub codes | ||
28 | + //Represents an invalid OSPF message length | ||
29 | + public static final byte BAD_MESSAGE_LENGTH = 2; | ||
30 | + //Represents an invalid OSPF message | ||
31 | + public static final byte BAD_MESSAGE = 4; | ||
32 | + | ||
33 | + /** | ||
34 | + * Creates an instance of OSPF error type. | ||
35 | + */ | ||
36 | + private OspfErrorType() { | ||
37 | + } | ||
38 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
0 → 100644
1 | +/* | ||
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 | + */ | ||
16 | +package org.onosproject.ospf.exceptions; | ||
17 | + | ||
18 | +import com.google.common.base.MoreObjects; | ||
19 | + | ||
20 | +/** | ||
21 | + * Representation of a custom exception for OSPF. | ||
22 | + */ | ||
23 | +public class OspfParseException extends Exception { | ||
24 | + | ||
25 | + private static final long serialVersionUID = 1L; | ||
26 | + private byte errorCode; | ||
27 | + private byte errorSubCode; | ||
28 | + | ||
29 | + /** | ||
30 | + * Creates a new OSPF exception. | ||
31 | + */ | ||
32 | + public OspfParseException() { | ||
33 | + super(); | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * Creates a new OSPF exception based on the given arguments. | ||
38 | + * | ||
39 | + * @param message the detail of exception in string | ||
40 | + * @param cause underlying cause of the error | ||
41 | + */ | ||
42 | + public OspfParseException(final String message, final Throwable cause) { | ||
43 | + super(message, cause); | ||
44 | + } | ||
45 | + | ||
46 | + /** | ||
47 | + * Creates a new OSPF exception for the given message. | ||
48 | + * | ||
49 | + * @param message the detail of exception in string | ||
50 | + */ | ||
51 | + public OspfParseException(final String message) { | ||
52 | + super(message); | ||
53 | + } | ||
54 | + | ||
55 | + /** | ||
56 | + * Creates a new OSPF exception from throwable instance. | ||
57 | + * | ||
58 | + * @param cause underlying cause of the error | ||
59 | + */ | ||
60 | + public OspfParseException(final Throwable cause) { | ||
61 | + super(cause); | ||
62 | + } | ||
63 | + | ||
64 | + /** | ||
65 | + * Creates a new OSPF exception from error code and error sub code. | ||
66 | + * | ||
67 | + * @param errorCode error code of OSPF message | ||
68 | + * @param errorSubCode error sub code of OSPF message | ||
69 | + */ | ||
70 | + public OspfParseException(final byte errorCode, final byte errorSubCode) { | ||
71 | + super(); | ||
72 | + this.errorCode = errorCode; | ||
73 | + this.errorSubCode = errorSubCode; | ||
74 | + } | ||
75 | + | ||
76 | + /** | ||
77 | + * Returns error code for this exception. | ||
78 | + * | ||
79 | + * @return error code for this exception | ||
80 | + */ | ||
81 | + public byte errorCode() { | ||
82 | + return this.errorCode; | ||
83 | + } | ||
84 | + | ||
85 | + /** | ||
86 | + * Returns error sub code for this exception. | ||
87 | + * | ||
88 | + * @return error sub code for this exception | ||
89 | + */ | ||
90 | + public byte errorSubCode() { | ||
91 | + return this.errorSubCode; | ||
92 | + } | ||
93 | + | ||
94 | + @Override | ||
95 | + public String toString() { | ||
96 | + return MoreObjects.toStringHelper(getClass()) | ||
97 | + .omitNullValues() | ||
98 | + .add("errorCode", errorCode) | ||
99 | + .add("errorSubCode", errorSubCode) | ||
100 | + .toString(); | ||
101 | + } | ||
102 | +} | ||
... | \ 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 exception types. | ||
19 | + */ | ||
20 | +package org.onosproject.ospf.exceptions; | ||
... | \ 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 protocol. | ||
19 | + */ | ||
20 | +package org.onosproject.ospf; | ||
... | \ 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 | +package org.onosproject.ospf.protocol.lsa; | ||
18 | + | ||
19 | +import com.google.common.base.MoreObjects; | ||
20 | +import com.google.common.base.Objects; | ||
21 | +import com.google.common.primitives.Bytes; | ||
22 | +import org.onlab.packet.Ip4Address; | ||
23 | +import org.onosproject.ospf.controller.OspfLsa; | ||
24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
25 | +import org.onosproject.ospf.protocol.util.OspfUtil; | ||
26 | +import org.slf4j.Logger; | ||
27 | +import org.slf4j.LoggerFactory; | ||
28 | + | ||
29 | +import java.net.InetAddress; | ||
30 | +import java.util.ArrayList; | ||
31 | +import java.util.List; | ||
32 | + | ||
33 | +/** | ||
34 | + * Defines the LSA header, fields and the methods to access them. | ||
35 | + */ | ||
36 | +public class LsaHeader implements OspfLsa { | ||
37 | + | ||
38 | + /* | ||
39 | + 0 1 2 3 | ||
40 | + 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 | ||
41 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
42 | + | LS age | Options | LS type | | ||
43 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
44 | + | Link State ID | | ||
45 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
46 | + | Advertising Router | | ||
47 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
48 | + | LS sequence number | | ||
49 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
50 | + | LS checksum | length | | ||
51 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
52 | + LSA header format | ||
53 | + REFERENCE : RFC 2328 | ||
54 | + */ | ||
55 | + protected static final Logger log = LoggerFactory.getLogger(LsaHeader.class); | ||
56 | + private int age; | ||
57 | + private int options; | ||
58 | + private int lsType; | ||
59 | + private long lsSequenceNo; | ||
60 | + private int lsCheckSum; | ||
61 | + private int lsPacketLen; | ||
62 | + private String linkStateId; | ||
63 | + private Ip4Address advertisingRouter; | ||
64 | + | ||
65 | + /** | ||
66 | + * Gets LSA age. | ||
67 | + * | ||
68 | + * @return LSA age | ||
69 | + */ | ||
70 | + public int age() { | ||
71 | + return age; | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Sets LSA age. | ||
76 | + * | ||
77 | + * @param age LSA age | ||
78 | + */ | ||
79 | + public void setAge(int age) { | ||
80 | + this.age = age; | ||
81 | + } | ||
82 | + | ||
83 | + /** | ||
84 | + * Gets options value. | ||
85 | + * | ||
86 | + * @return options header value | ||
87 | + */ | ||
88 | + public int options() { | ||
89 | + return options; | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * Sets options header value. | ||
94 | + * | ||
95 | + * @param options header value | ||
96 | + */ | ||
97 | + public void setOptions(int options) { | ||
98 | + this.options = options; | ||
99 | + } | ||
100 | + | ||
101 | + /** | ||
102 | + * Gets LSA type. | ||
103 | + * | ||
104 | + * @return LSA type | ||
105 | + */ | ||
106 | + public int lsType() { | ||
107 | + return lsType; | ||
108 | + } | ||
109 | + | ||
110 | + /** | ||
111 | + * Sets LSA type. | ||
112 | + * | ||
113 | + * @param lsType LSA type | ||
114 | + */ | ||
115 | + public void setLsType(int lsType) { | ||
116 | + this.lsType = lsType; | ||
117 | + } | ||
118 | + | ||
119 | + /** | ||
120 | + * Gets link state id. | ||
121 | + * | ||
122 | + * @return linkStateId link state id | ||
123 | + */ | ||
124 | + public String linkStateId() { | ||
125 | + return linkStateId; | ||
126 | + } | ||
127 | + | ||
128 | + /** | ||
129 | + * Sets link state id. | ||
130 | + * | ||
131 | + * @param linkStateId link state id | ||
132 | + */ | ||
133 | + public void setLinkStateId(String linkStateId) { | ||
134 | + this.linkStateId = linkStateId; | ||
135 | + } | ||
136 | + | ||
137 | + /** | ||
138 | + * Gets advertising router IP. | ||
139 | + * | ||
140 | + * @return advertising router | ||
141 | + */ | ||
142 | + public Ip4Address advertisingRouter() { | ||
143 | + return advertisingRouter; | ||
144 | + } | ||
145 | + | ||
146 | + /** | ||
147 | + * Sets advertising router. | ||
148 | + * | ||
149 | + * @param advertisingRouter advertising router | ||
150 | + */ | ||
151 | + public void setAdvertisingRouter(Ip4Address advertisingRouter) { | ||
152 | + this.advertisingRouter = advertisingRouter; | ||
153 | + } | ||
154 | + | ||
155 | + /** | ||
156 | + * Gets LSA sequence number. | ||
157 | + * | ||
158 | + * @return LSA sequence number | ||
159 | + */ | ||
160 | + public long lsSequenceNo() { | ||
161 | + return lsSequenceNo; | ||
162 | + } | ||
163 | + | ||
164 | + /** | ||
165 | + * Sets LSA sequence number. | ||
166 | + * | ||
167 | + * @param lsSequenceNo LSA sequence number | ||
168 | + */ | ||
169 | + public void setLsSequenceNo(long lsSequenceNo) { | ||
170 | + this.lsSequenceNo = lsSequenceNo; | ||
171 | + } | ||
172 | + | ||
173 | + /** | ||
174 | + * Gets LSA check sum. | ||
175 | + * | ||
176 | + * @return lsCheckSum LSA checksum | ||
177 | + */ | ||
178 | + public int lsCheckSum() { | ||
179 | + return lsCheckSum; | ||
180 | + } | ||
181 | + | ||
182 | + /** | ||
183 | + * Sets LSA checksum. | ||
184 | + * | ||
185 | + * @param lsCheckSum LSA checksum | ||
186 | + */ | ||
187 | + public void setLsCheckSum(int lsCheckSum) { | ||
188 | + this.lsCheckSum = lsCheckSum; | ||
189 | + } | ||
190 | + | ||
191 | + /** | ||
192 | + * Gets lsa packet length. | ||
193 | + * | ||
194 | + * @return lsPacketLen LSA packet length | ||
195 | + */ | ||
196 | + public int lsPacketLen() { | ||
197 | + return lsPacketLen; | ||
198 | + } | ||
199 | + | ||
200 | + /** | ||
201 | + * Sets LSA packet length. | ||
202 | + * | ||
203 | + * @param lsPacketLen LSA packet length | ||
204 | + */ | ||
205 | + public void setLsPacketLen(int lsPacketLen) { | ||
206 | + this.lsPacketLen = lsPacketLen; | ||
207 | + } | ||
208 | + | ||
209 | + @Override | ||
210 | + public OspfLsaType getOspfLsaType() { | ||
211 | + if (lsType == OspfLsaType.ROUTER.value()) { | ||
212 | + return OspfLsaType.ROUTER; | ||
213 | + } else if (lsType == OspfLsaType.NETWORK.value()) { | ||
214 | + return OspfLsaType.NETWORK; | ||
215 | + } else if (lsType == OspfLsaType.SUMMARY.value()) { | ||
216 | + return OspfLsaType.SUMMARY; | ||
217 | + } else if (lsType == OspfLsaType.ASBR_SUMMARY.value()) { | ||
218 | + return OspfLsaType.ASBR_SUMMARY; | ||
219 | + } else if (lsType == OspfLsaType.EXTERNAL_LSA.value()) { | ||
220 | + return OspfLsaType.EXTERNAL_LSA; | ||
221 | + } else if (lsType == OspfLsaType.LINK_LOCAL_OPAQUE_LSA.value()) { | ||
222 | + return OspfLsaType.LINK_LOCAL_OPAQUE_LSA; | ||
223 | + } else if (lsType == OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()) { | ||
224 | + return OspfLsaType.AREA_LOCAL_OPAQUE_LSA; | ||
225 | + } else if (lsType == OspfLsaType.AS_OPAQUE_LSA.value()) { | ||
226 | + return OspfLsaType.AS_OPAQUE_LSA; | ||
227 | + } | ||
228 | + | ||
229 | + return OspfLsaType.UNDEFINED; | ||
230 | + } | ||
231 | + | ||
232 | + @Override | ||
233 | + public OspfLsa lsaHeader() { | ||
234 | + return this; | ||
235 | + } | ||
236 | + | ||
237 | + /** | ||
238 | + * Gets the LSA header as bytes. | ||
239 | + * | ||
240 | + * @return LSA header as bytes | ||
241 | + */ | ||
242 | + public byte[] getLsaHeaderAsByteArray() { | ||
243 | + List<Byte> headerLst = new ArrayList<>(); | ||
244 | + try { | ||
245 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age()))); | ||
246 | + headerLst.add((byte) this.options()); | ||
247 | + headerLst.add((byte) this.lsType()); | ||
248 | + headerLst.addAll(Bytes.asList(InetAddress.getByName(this.linkStateId()).getAddress())); | ||
249 | + headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets())); | ||
250 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo()))); | ||
251 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum()))); | ||
252 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen()))); | ||
253 | + } catch (Exception e) { | ||
254 | + log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage()); | ||
255 | + return Bytes.toArray(headerLst); | ||
256 | + } | ||
257 | + return Bytes.toArray(headerLst); | ||
258 | + } | ||
259 | + | ||
260 | + /** | ||
261 | + * Populates the header from the LSA header instance. | ||
262 | + * | ||
263 | + * @param lsaHeader LSA header instance | ||
264 | + */ | ||
265 | + public void populateHeader(LsaHeader lsaHeader) { | ||
266 | + //assign all the header values | ||
267 | + this.setAge(lsaHeader.age()); | ||
268 | + this.setOptions(lsaHeader.options()); | ||
269 | + this.setLsType(lsaHeader.lsType()); | ||
270 | + this.setLinkStateId(lsaHeader.linkStateId()); | ||
271 | + this.setAdvertisingRouter(lsaHeader.advertisingRouter()); | ||
272 | + this.setLsSequenceNo(lsaHeader.lsSequenceNo()); | ||
273 | + this.setLsCheckSum(lsaHeader.lsCheckSum()); | ||
274 | + this.setLsPacketLen(lsaHeader.lsPacketLen()); | ||
275 | + } | ||
276 | + | ||
277 | + @Override | ||
278 | + public boolean equals(Object o) { | ||
279 | + if (this == o) { | ||
280 | + return true; | ||
281 | + } | ||
282 | + if (o == null || getClass() != o.getClass()) { | ||
283 | + return false; | ||
284 | + } | ||
285 | + LsaHeader that = (LsaHeader) o; | ||
286 | + return Objects.equal(age, that.age) && | ||
287 | + Objects.equal(options, that.options) && | ||
288 | + Objects.equal(lsType, that.lsType) && | ||
289 | + Objects.equal(lsSequenceNo, that.lsSequenceNo) && | ||
290 | + Objects.equal(lsCheckSum, that.lsCheckSum) && | ||
291 | + Objects.equal(lsPacketLen, that.lsPacketLen) && | ||
292 | + Objects.equal(linkStateId, that.linkStateId) && | ||
293 | + Objects.equal(advertisingRouter, that.advertisingRouter); | ||
294 | + } | ||
295 | + | ||
296 | + @Override | ||
297 | + public int hashCode() { | ||
298 | + return Objects.hashCode(age, options, lsType, lsSequenceNo, lsCheckSum, | ||
299 | + lsPacketLen, linkStateId, advertisingRouter); | ||
300 | + } | ||
301 | + | ||
302 | + @Override | ||
303 | + public String toString() { | ||
304 | + return MoreObjects.toStringHelper(getClass()) | ||
305 | + .omitNullValues() | ||
306 | + .add("age", age) | ||
307 | + .add("options", options) | ||
308 | + .add("lsType", lsType) | ||
309 | + .add("lsSequenceNo", lsSequenceNo) | ||
310 | + .add("lsCheckSum", lsCheckSum) | ||
311 | + .add("lsPacketLen", lsPacketLen) | ||
312 | + .add("linkStateId;", linkStateId) | ||
313 | + .add("advertisingRouter", advertisingRouter) | ||
314 | + .toString(); | ||
315 | + } | ||
316 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/OpaqueLsaHeader.java
0 → 100644
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.lsa; | ||
17 | + | ||
18 | +import com.google.common.base.MoreObjects; | ||
19 | +import com.google.common.base.Objects; | ||
20 | +import com.google.common.primitives.Bytes; | ||
21 | +import org.onosproject.ospf.protocol.util.OspfUtil; | ||
22 | + | ||
23 | +import java.util.ArrayList; | ||
24 | +import java.util.List; | ||
25 | + | ||
26 | +/** | ||
27 | + * Defines the Opaque LSA header, fields and the methods to access them. | ||
28 | + */ | ||
29 | +public class OpaqueLsaHeader extends LsaHeader { | ||
30 | + /* | ||
31 | + 0 1 2 3 | ||
32 | + 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 | ||
33 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
34 | + | LS age | Options | 9, 10, or 11 | | ||
35 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
36 | + | Opaque Type | Opaque ID | | ||
37 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
38 | + | Advertising Router | | ||
39 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
40 | + | LS Sequence Number | | ||
41 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
42 | + | LS checksum | Length | | ||
43 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
44 | + Opaque LSA header format | ||
45 | + REFERENCE : RFC 5250 | ||
46 | + */ | ||
47 | + private int opaqueId; | ||
48 | + private int opaqueType; | ||
49 | + | ||
50 | + /** | ||
51 | + * Populates the header from the lsaHeader instance. | ||
52 | + * | ||
53 | + * @param lsaHeader lsa header instance. | ||
54 | + */ | ||
55 | + public void populateHeader(OpaqueLsaHeader lsaHeader) { | ||
56 | + //assign all the header values | ||
57 | + this.setAge(lsaHeader.age()); | ||
58 | + this.setOptions(lsaHeader.options()); | ||
59 | + this.setLsType(lsaHeader.lsType()); | ||
60 | + this.setLinkStateId(lsaHeader.linkStateId()); | ||
61 | + this.setAdvertisingRouter(lsaHeader.advertisingRouter()); | ||
62 | + this.setLsSequenceNo(lsaHeader.lsSequenceNo()); | ||
63 | + this.setLsCheckSum(lsaHeader.lsCheckSum()); | ||
64 | + this.setLsPacketLen(lsaHeader.lsPacketLen()); | ||
65 | + this.setOpaqueId(lsaHeader.opaqueId()); | ||
66 | + this.setOpaqueType(lsaHeader.opaqueType()); | ||
67 | + } | ||
68 | + | ||
69 | + /** | ||
70 | + * Gets the opaque id. | ||
71 | + * | ||
72 | + * @return opaque id | ||
73 | + */ | ||
74 | + public int opaqueId() { | ||
75 | + return opaqueId; | ||
76 | + } | ||
77 | + | ||
78 | + /** | ||
79 | + * Sets the opaque id. | ||
80 | + * | ||
81 | + * @param opaqueId opaque id | ||
82 | + */ | ||
83 | + public void setOpaqueId(int opaqueId) { | ||
84 | + this.opaqueId = opaqueId; | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * Gets opaque type. | ||
89 | + * | ||
90 | + * @return opaque type | ||
91 | + */ | ||
92 | + public int opaqueType() { | ||
93 | + return opaqueType; | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * Sets opaque type. | ||
98 | + * | ||
99 | + * @param opaqueType opaque type | ||
100 | + */ | ||
101 | + public void setOpaqueType(int opaqueType) { | ||
102 | + this.opaqueType = opaqueType; | ||
103 | + } | ||
104 | + | ||
105 | + /** | ||
106 | + * Gets header as byte array. | ||
107 | + * | ||
108 | + * @return header as byte array | ||
109 | + */ | ||
110 | + public byte[] getOpaqueLsaHeaderAsByteArray() { | ||
111 | + List<Byte> headerLst = new ArrayList<>(); | ||
112 | + try { | ||
113 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age()))); | ||
114 | + headerLst.add((byte) this.options()); | ||
115 | + headerLst.add((byte) this.lsType()); | ||
116 | + headerLst.add((byte) this.opaqueType()); | ||
117 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(this.opaqueId()))); | ||
118 | + headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets())); | ||
119 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo()))); | ||
120 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum()))); | ||
121 | + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen()))); | ||
122 | + } catch (Exception e) { | ||
123 | + log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage()); | ||
124 | + return Bytes.toArray(headerLst); | ||
125 | + } | ||
126 | + return Bytes.toArray(headerLst); | ||
127 | + } | ||
128 | + | ||
129 | + @Override | ||
130 | + public boolean equals(Object o) { | ||
131 | + if (this == o) { | ||
132 | + return true; | ||
133 | + } | ||
134 | + if (o == null || getClass() != o.getClass()) { | ||
135 | + return false; | ||
136 | + } | ||
137 | + OpaqueLsaHeader that = (OpaqueLsaHeader) o; | ||
138 | + return Objects.equal(opaqueId, that.opaqueId) && | ||
139 | + Objects.equal(opaqueType, that.opaqueType) && | ||
140 | + Objects.equal(age(), that.age()) && | ||
141 | + Objects.equal(options(), that.options()) && | ||
142 | + Objects.equal(lsType(), that.lsType()) && | ||
143 | + Objects.equal(lsSequenceNo(), that.lsSequenceNo()) && | ||
144 | + Objects.equal(lsCheckSum(), that.lsCheckSum()) && | ||
145 | + Objects.equal(lsPacketLen(), that.lsPacketLen()) && | ||
146 | + Objects.equal(linkStateId(), that.linkStateId()) && | ||
147 | + Objects.equal(advertisingRouter(), that.advertisingRouter()); | ||
148 | + } | ||
149 | + | ||
150 | + @Override | ||
151 | + public int hashCode() { | ||
152 | + return Objects.hashCode(opaqueId, opaqueType); | ||
153 | + } | ||
154 | + | ||
155 | + @Override | ||
156 | + public String toString() { | ||
157 | + return MoreObjects.toStringHelper(getClass()) | ||
158 | + .omitNullValues() | ||
159 | + .add("opaqueId", this.opaqueId) | ||
160 | + .add("opaqueType", opaqueType) | ||
161 | + .toString(); | ||
162 | + } | ||
163 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/package-info.java
0 → 100644
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 LSA. | ||
19 | + */ | ||
20 | +package org.onosproject.ospf.protocol.lsa; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessage.java
0 → 100644
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; | ||
17 | + | ||
18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
19 | +import org.onlab.packet.Ip4Address; | ||
20 | +import org.onosproject.ospf.exceptions.OspfParseException; | ||
21 | +import org.onosproject.ospf.protocol.util.OspfPacketType; | ||
22 | + | ||
23 | +/** | ||
24 | + * Representation of an OSPF message. | ||
25 | + */ | ||
26 | +public interface OspfMessage { | ||
27 | + | ||
28 | + /** | ||
29 | + * Returns the type of OSPF message. | ||
30 | + * | ||
31 | + * @return OSPF message type | ||
32 | + */ | ||
33 | + public OspfPacketType ospfMessageType(); | ||
34 | + | ||
35 | + /** | ||
36 | + * Reads from ChannelBuffer and initializes the type of LSA. | ||
37 | + * | ||
38 | + * @param channelBuffer channel buffer instance | ||
39 | + * @throws OspfParseException might throws exception while parsing buffer | ||
40 | + */ | ||
41 | + void readFrom(ChannelBuffer channelBuffer) throws OspfParseException; | ||
42 | + | ||
43 | + /** | ||
44 | + * Returns OSPFMessage as byte array. | ||
45 | + * | ||
46 | + * @return OSPF message as bytes | ||
47 | + */ | ||
48 | + byte[] asBytes(); | ||
49 | + | ||
50 | + /** | ||
51 | + * Sets the source IP address. | ||
52 | + * | ||
53 | + * @param sourceIp IP address | ||
54 | + */ | ||
55 | + public void setSourceIp(Ip4Address sourceIp); | ||
56 | + | ||
57 | + /** | ||
58 | + * Gets the destination IP address. | ||
59 | + * | ||
60 | + * @return destination IP address | ||
61 | + */ | ||
62 | + public Ip4Address destinationIp(); | ||
63 | + | ||
64 | + /** | ||
65 | + * Sets destination IP. | ||
66 | + * | ||
67 | + * @param destinationIp destination IP address | ||
68 | + */ | ||
69 | + public void setDestinationIp(Ip4Address destinationIp); | ||
70 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfPacketHeader.java
0 → 100644
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; | ||
17 | + | ||
18 | +import com.google.common.base.MoreObjects; | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.onlab.packet.Ip4Address; | ||
21 | +import org.onosproject.ospf.exceptions.OspfParseException; | ||
22 | +import org.onosproject.ospf.protocol.util.OspfPacketType; | ||
23 | + | ||
24 | +/** | ||
25 | + * Defines the OSPF Packet Header, fields and access methods. | ||
26 | + * Every OSPF packet starts with a standard 24 byte header. | ||
27 | + * This header contains all the information necessary to determine whether | ||
28 | + * the packet should be accepted for further processing | ||
29 | + */ | ||
30 | +public class OspfPacketHeader implements OspfMessage { | ||
31 | + | ||
32 | + /* | ||
33 | + 0 1 2 3 | ||
34 | + 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 | ||
35 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
36 | + | Version # | Type | Packet length | | ||
37 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
38 | + | Router ID | | ||
39 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
40 | + | Area ID | | ||
41 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
42 | + | Checksum | AuType | | ||
43 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
44 | + | Authentication | | ||
45 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
46 | + | Authentication | | ||
47 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
48 | + */ | ||
49 | + | ||
50 | + private int ospfVer; | ||
51 | + private int ospfType; | ||
52 | + private int ospfPackLength; | ||
53 | + private Ip4Address routerId; | ||
54 | + private Ip4Address areaId; | ||
55 | + private int checkSum; | ||
56 | + private int auType; | ||
57 | + private int authentication; | ||
58 | + private Ip4Address destinationIp; | ||
59 | + private Ip4Address sourceIp; | ||
60 | + | ||
61 | + /** | ||
62 | + * Gets the source IP. | ||
63 | + * | ||
64 | + * @return source IP address | ||
65 | + */ | ||
66 | + public Ip4Address sourceIp() { | ||
67 | + return sourceIp; | ||
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * Sets the source IP address. | ||
72 | + * | ||
73 | + * @param sourceIp source IP address | ||
74 | + */ | ||
75 | + public void setSourceIp(Ip4Address sourceIp) { | ||
76 | + this.sourceIp = sourceIp; | ||
77 | + } | ||
78 | + | ||
79 | + @Override | ||
80 | + public OspfPacketType ospfMessageType() { | ||
81 | + //default impl | ||
82 | + return null; | ||
83 | + } | ||
84 | + | ||
85 | + @Override | ||
86 | + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { | ||
87 | + //default impl | ||
88 | + } | ||
89 | + | ||
90 | + @Override | ||
91 | + public byte[] asBytes() { | ||
92 | + //default impl | ||
93 | + return new byte[0]; | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * Gets OSPF version. | ||
98 | + * | ||
99 | + * @return OSPF version | ||
100 | + */ | ||
101 | + public int ospfVersion() { | ||
102 | + return ospfVer; | ||
103 | + } | ||
104 | + | ||
105 | + /** | ||
106 | + * Sets OSPF version. | ||
107 | + * | ||
108 | + * @param ospfVer OSPF version | ||
109 | + */ | ||
110 | + public void setOspfVer(int ospfVer) { | ||
111 | + this.ospfVer = ospfVer; | ||
112 | + } | ||
113 | + | ||
114 | + /** | ||
115 | + * Gets OSPF packet type. | ||
116 | + * | ||
117 | + * @return OSPF packet type | ||
118 | + */ | ||
119 | + public int ospfType() { | ||
120 | + return ospfType; | ||
121 | + } | ||
122 | + | ||
123 | + /** | ||
124 | + * Sets OSPF packet type. | ||
125 | + * | ||
126 | + * @param ospfType packet type | ||
127 | + */ | ||
128 | + public void setOspftype(int ospfType) { | ||
129 | + this.ospfType = ospfType; | ||
130 | + } | ||
131 | + | ||
132 | + /** | ||
133 | + * Gets ospf packet length. | ||
134 | + * | ||
135 | + * @return OSPF packet length | ||
136 | + */ | ||
137 | + public int ospfPacLength() { | ||
138 | + return ospfPackLength; | ||
139 | + } | ||
140 | + | ||
141 | + /** | ||
142 | + * Sets OSPF packet length. | ||
143 | + * | ||
144 | + * @param ospfPacLength packet length | ||
145 | + */ | ||
146 | + public void setOspfPacLength(int ospfPacLength) { | ||
147 | + this.ospfPackLength = ospfPacLength; | ||
148 | + } | ||
149 | + | ||
150 | + /** | ||
151 | + * Gets router id. | ||
152 | + * | ||
153 | + * @return routerId | ||
154 | + */ | ||
155 | + public Ip4Address routerId() { | ||
156 | + return routerId; | ||
157 | + } | ||
158 | + | ||
159 | + /** | ||
160 | + * Sets router id. | ||
161 | + * | ||
162 | + * @param routerId router id | ||
163 | + */ | ||
164 | + public void setRouterId(Ip4Address routerId) { | ||
165 | + this.routerId = routerId; | ||
166 | + } | ||
167 | + | ||
168 | + /** | ||
169 | + * Gets area id. | ||
170 | + * | ||
171 | + * @return areaId area id | ||
172 | + */ | ||
173 | + public Ip4Address areaId() { | ||
174 | + return areaId; | ||
175 | + } | ||
176 | + | ||
177 | + /** | ||
178 | + * Sets area id. | ||
179 | + * | ||
180 | + * @param areaId area id | ||
181 | + */ | ||
182 | + public void setAreaId(Ip4Address areaId) { | ||
183 | + this.areaId = areaId; | ||
184 | + } | ||
185 | + | ||
186 | + /** | ||
187 | + * Gets checksum value. | ||
188 | + * | ||
189 | + * @return checkSum check sum value | ||
190 | + */ | ||
191 | + public int checksum() { | ||
192 | + return checkSum; | ||
193 | + } | ||
194 | + | ||
195 | + /** | ||
196 | + * Sets checksum. | ||
197 | + * | ||
198 | + * @param checkSum check sum value | ||
199 | + */ | ||
200 | + public void setChecksum(int checkSum) { | ||
201 | + this.checkSum = checkSum; | ||
202 | + } | ||
203 | + | ||
204 | + /** | ||
205 | + * Gets auth type. | ||
206 | + * | ||
207 | + * @return authType authentication type | ||
208 | + */ | ||
209 | + public int authType() { | ||
210 | + return auType; | ||
211 | + } | ||
212 | + | ||
213 | + /** | ||
214 | + * Sets auth Type. | ||
215 | + * | ||
216 | + * @param auType authentication type | ||
217 | + */ | ||
218 | + public void setAuthType(int auType) { | ||
219 | + this.auType = auType; | ||
220 | + } | ||
221 | + | ||
222 | + /** | ||
223 | + * Gets authentication. | ||
224 | + * | ||
225 | + * @return authentication | ||
226 | + */ | ||
227 | + public int authentication() { | ||
228 | + return authentication; | ||
229 | + } | ||
230 | + | ||
231 | + /** | ||
232 | + * Sets authentication. | ||
233 | + * | ||
234 | + * @param authentication authentication | ||
235 | + */ | ||
236 | + public void setAuthentication(int authentication) { | ||
237 | + this.authentication = authentication; | ||
238 | + } | ||
239 | + | ||
240 | + /** | ||
241 | + * Gets destination IP. | ||
242 | + * | ||
243 | + * @return destination IP | ||
244 | + */ | ||
245 | + public Ip4Address destinationIp() { | ||
246 | + return destinationIp; | ||
247 | + } | ||
248 | + | ||
249 | + /** | ||
250 | + * Sets destination IP. | ||
251 | + * | ||
252 | + * @param destinationIp destination IP | ||
253 | + */ | ||
254 | + public void setDestinationIp(Ip4Address destinationIp) { | ||
255 | + this.destinationIp = destinationIp; | ||
256 | + } | ||
257 | + | ||
258 | + /** | ||
259 | + * Populates the header from the packetHeader instance. | ||
260 | + * | ||
261 | + * @param ospfPacketHeader packet header instance. | ||
262 | + */ | ||
263 | + public void populateHeader(OspfPacketHeader ospfPacketHeader) { | ||
264 | + this.setSourceIp(ospfPacketHeader.sourceIp()); | ||
265 | + this.setOspfVer(ospfPacketHeader.ospfVersion()); | ||
266 | + this.setOspftype(ospfPacketHeader.ospfType()); | ||
267 | + this.setOspfPacLength(ospfPacketHeader.ospfPacLength()); | ||
268 | + this.setRouterId(ospfPacketHeader.routerId()); | ||
269 | + this.setAreaId(ospfPacketHeader.areaId()); | ||
270 | + this.setChecksum(ospfPacketHeader.checksum()); | ||
271 | + this.setAuthType(ospfPacketHeader.authType()); | ||
272 | + this.setAuthentication(ospfPacketHeader.authentication()); | ||
273 | + } | ||
274 | + | ||
275 | + @Override | ||
276 | + public String toString() { | ||
277 | + return MoreObjects.toStringHelper(getClass()) | ||
278 | + .omitNullValues() | ||
279 | + .add("ospfVersion", ospfVer) | ||
280 | + .add("ospfType", ospfType) | ||
281 | + .add("ospfPackLength", ospfPackLength) | ||
282 | + .add("routerId", routerId) | ||
283 | + .add("areaId", areaId) | ||
284 | + .add("checkSum", checkSum) | ||
285 | + .add("auType", auType) | ||
286 | + .add("authentication", authentication) | ||
287 | + .add("destinationIP", destinationIp) | ||
288 | + .toString(); | ||
289 | + } | ||
290 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/package-info.java
0 → 100644
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 different types of OSPF Packets. | ||
19 | + */ | ||
20 | +package org.onosproject.ospf.protocol.ospfpacket; |
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 protocol.. | ||
19 | + */ | ||
20 | +package org.onosproject.ospf.protocol; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfInterfaceState.java
0 → 100644
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 an OSPF Interface states. | ||
20 | + */ | ||
21 | +public enum OspfInterfaceState { | ||
22 | + | ||
23 | + DOWN(1), | ||
24 | + LOOPBACK(2), | ||
25 | + WAITING(3), | ||
26 | + POINT2POINT(4), | ||
27 | + DROTHER(5), | ||
28 | + BDR(6), | ||
29 | + DR(7); | ||
30 | + | ||
31 | + private int value; | ||
32 | + | ||
33 | + /** | ||
34 | + * Creates an instance of Interface State. | ||
35 | + * | ||
36 | + * @param value Interface State value | ||
37 | + */ | ||
38 | + OspfInterfaceState(int value) { | ||
39 | + this.value = value; | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * Gets value for Interface State. | ||
44 | + * | ||
45 | + * @return value Interface State | ||
46 | + */ | ||
47 | + public int value() { | ||
48 | + return value; | ||
49 | + } | ||
50 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfPacketType.java
0 → 100644
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 |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfParameters.java
0 → 100644
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 an OSPF configuration parameters and constants. | ||
20 | + */ | ||
21 | +public final class OspfParameters { | ||
22 | + | ||
23 | + public static final int LSREFRESHTIME = 1800; //max time between updates; | ||
24 | + public static final int MINLSINTERVAL = 5; // set to 5 second | ||
25 | + public static final int MINLSARRIVAL = 1; // set to 1 second | ||
26 | + public static final int MAXAGE = 3600; // set to 1 hour in seconds | ||
27 | + public static final int CHECKAGE = 300; // set to 5 mins | ||
28 | + public static final int MAXAGEDIFF = 900; // set to 15 mins | ||
29 | + public static final long MAXSEQUENCENUMBER = 2147483647; | ||
30 | + public static final long STARTLSSEQUENCENUM = -2147483647; | ||
31 | + public static final int AGECOUNTER = 1; | ||
32 | + public static final String VERIFYCHECKSUM = "verifyChecksum"; | ||
33 | + public static final String REFRESHLSA = "refreshLsa"; | ||
34 | + public static final String MAXAGELSA = "maxAgeLsa"; | ||
35 | + public static final int START_NOW = 0; | ||
36 | + public static final int TRAFFIC_ENGINEERING = 1; | ||
37 | + public static final int INITIAL_BANDWIDTH = 12500000; | ||
38 | + public static final int ROUTER = 1; | ||
39 | + public static final int NETWORK = 2; | ||
40 | + public static final int SUMMARY = 3; | ||
41 | + public static final int ASBR_SUMMARY = 4; | ||
42 | + public static final int EXTERNAL_LSA = 5; | ||
43 | + public static final int LINK_LOCAL_OPAQUE_LSA = 9; | ||
44 | + public static final int AREA_LOCAL_OPAQUE_LSA = 10; | ||
45 | + public static final int AS_OPAQUE_LSA = 11; | ||
46 | + public static final int HELLO = 1; | ||
47 | + public static final int DD = 2; | ||
48 | + public static final int LSREQUEST = 3; | ||
49 | + public static final int LSUPDATE = 4; | ||
50 | + public static final int LSACK = 5; | ||
51 | + public static final int INFTRA_NS_DELAY = 1; | ||
52 | + public static final int BDR = 6; | ||
53 | + public static final int DR = 7; | ||
54 | + public static final String OPAQUE_ENABLED_OPTION_VALUE = "01000010"; | ||
55 | + | ||
56 | + private OspfParameters() { | ||
57 | + } | ||
58 | +} | ||
... | \ 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 | +import com.google.common.primitives.Bytes; | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
21 | +import org.onlab.packet.Ip4Address; | ||
22 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
23 | +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; | ||
24 | +import org.slf4j.Logger; | ||
25 | +import org.slf4j.LoggerFactory; | ||
26 | + | ||
27 | +import javax.xml.bind.DatatypeConverter; | ||
28 | +import java.net.InetAddress; | ||
29 | +import java.util.Arrays; | ||
30 | +import java.util.Collections; | ||
31 | +import java.util.List; | ||
32 | +import java.util.Random; | ||
33 | +import java.util.StringTokenizer; | ||
34 | + | ||
35 | +/** | ||
36 | + * Representation of an OSPF constants and utility methods. | ||
37 | + */ | ||
38 | +public final class OspfUtil { | ||
39 | + | ||
40 | + public static final int OSPF_VERSION_2 = 2; | ||
41 | + public static final int OSPF_VERSION = OSPF_VERSION_2; | ||
42 | + public static final int PACKET_MINIMUM_LENGTH = 24; | ||
43 | + public static final int OSPF_HEADER_LENGTH = 24; | ||
44 | + public static final int LSA_HEADER_LENGTH = 20; | ||
45 | + public static final int DD_HEADER_LENGTH = OSPF_HEADER_LENGTH + 8; | ||
46 | + public static final int LSREQUEST_LENGTH = 12; | ||
47 | + public static final int OSPFPACKET_LENGTH_POS1 = 2; | ||
48 | + public static final int OSPFPACKET_LENGTH_POS2 = 3; | ||
49 | + public static final int OSPFPACKET_CHECKSUM_POS1 = 12; | ||
50 | + public static final int OSPFPACKET_CHECKSUM_POS2 = 13; | ||
51 | + public static final int LSAPACKET_CHECKSUM_POS1 = 16; | ||
52 | + public static final int LSAPACKET_CHECKSUM_POS2 = 17; | ||
53 | + public static final Ip4Address ALL_SPF_ROUTERS = Ip4Address.valueOf("224.0.0.5"); | ||
54 | + public static final Ip4Address ALL_DROUTERS = Ip4Address.valueOf("224.0.0.6"); | ||
55 | + public static final int ONLY_ALL_SPF_ROUTERS = 1; | ||
56 | + public static final int JOIN_ALL_DROUTERS = 2; | ||
57 | + public static final int INITIALIZE_SET = 1; | ||
58 | + public static final int INITIALIZE_NOTSET = 0; | ||
59 | + public static final int MORE_SET = 1; | ||
60 | + public static final int MORE_NOTSET = 0; | ||
61 | + public static final int IS_MASTER = 1; | ||
62 | + public static final int NOT_MASTER = 0; | ||
63 | + public static final int NOT_ASSIGNED = 0; | ||
64 | + public static final int FOUR_BYTES = 4; | ||
65 | + public static final int EIGHT_BYTES = 8; | ||
66 | + public static final int TWELVE_BYTES = 12; | ||
67 | + public static final int EXTERNAL_DESTINATION_LENGTH = 12; | ||
68 | + private static final Logger log = | ||
69 | + LoggerFactory.getLogger(OspfUtil.class); | ||
70 | + | ||
71 | + /** | ||
72 | + * Creates an instance. | ||
73 | + */ | ||
74 | + private OspfUtil() { | ||
75 | + | ||
76 | + } | ||
77 | + | ||
78 | + /** | ||
79 | + * Checks given IPs are in same network or not. | ||
80 | + * | ||
81 | + * @param ip1 IP address | ||
82 | + * @param ip2 IP address | ||
83 | + * @param mask network mask | ||
84 | + * @return true if both are in same network else false | ||
85 | + * @throws Exception might throws exception while parsing ip address | ||
86 | + */ | ||
87 | + public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask) | ||
88 | + throws Exception { | ||
89 | + | ||
90 | + byte[] a1 = ip1.toOctets(); | ||
91 | + byte[] a2 = ip2.toOctets(); | ||
92 | + byte[] m = mask.toOctets(); | ||
93 | + | ||
94 | + for (int i = 0; i < a1.length; i++) { | ||
95 | + if ((a1[i] & m[i]) != (a2[i] & m[i])) { | ||
96 | + return false; | ||
97 | + } | ||
98 | + } | ||
99 | + | ||
100 | + return true; | ||
101 | + } | ||
102 | + | ||
103 | + /** | ||
104 | + * Converts IP address to long. | ||
105 | + * | ||
106 | + * @param ipAddress IP address | ||
107 | + * @return long value represents IP address | ||
108 | + */ | ||
109 | + public static long ipAddressToLong(String ipAddress) { | ||
110 | + StringTokenizer st = new StringTokenizer(ipAddress, "."); | ||
111 | + long ipAsNumber = Long.parseLong(st.nextToken()) * (long) Math.pow(256, 3); | ||
112 | + ipAsNumber += Long.parseLong(st.nextToken()) * (long) Math.pow(256, 2); | ||
113 | + ipAsNumber += Long.parseLong(st.nextToken()) * 256; | ||
114 | + ipAsNumber += +Long.parseLong(st.nextToken()); | ||
115 | + | ||
116 | + return ipAsNumber; | ||
117 | + } | ||
118 | + | ||
119 | + /** | ||
120 | + * Checks option field to see whether opaque enabled or not. | ||
121 | + * 2nd Bit in options field of DdPacket represents Opaque. | ||
122 | + * 7th bit is external capability. | ||
123 | + * This method checks Opaque bit is set in the options or not. | ||
124 | + * | ||
125 | + * @param options options value | ||
126 | + * @return true if opaque enabled else false. | ||
127 | + */ | ||
128 | + public static boolean isOpaqueEnabled(int options) { | ||
129 | + Boolean[] bits = new Boolean[8]; | ||
130 | + for (int i = 7; i >= 0; i--) { | ||
131 | + bits[i] = (options & (1 << i)) != 0; | ||
132 | + } | ||
133 | + | ||
134 | + List<Boolean> list = Arrays.asList(bits); | ||
135 | + Collections.reverse(list); | ||
136 | + | ||
137 | + //2nd bit is Opaque. | ||
138 | + return list.get(1); | ||
139 | + } | ||
140 | + | ||
141 | + /** | ||
142 | + * Converts a byte to integer variable. | ||
143 | + * | ||
144 | + * @param bytesToConvert bytes to convert | ||
145 | + * @return integer representation of bytes | ||
146 | + */ | ||
147 | + public static int byteToInteger(byte[] bytesToConvert) { | ||
148 | + final StringBuilder builder = new StringBuilder(); | ||
149 | + for (byte eachByte : bytesToConvert) { | ||
150 | + builder.append(String.format("%02x", eachByte)); | ||
151 | + } | ||
152 | + int num = Integer.parseInt(builder.toString(), 16); | ||
153 | + return num; | ||
154 | + } | ||
155 | + | ||
156 | + /** | ||
157 | + * Converts a byte to long variable. | ||
158 | + * | ||
159 | + * @param bytesToConvert bytes to convert | ||
160 | + * @return long representation of bytes | ||
161 | + */ | ||
162 | + public static long byteToLong(byte[] bytesToConvert) { | ||
163 | + final StringBuilder builder = new StringBuilder(); | ||
164 | + for (byte eachByte : bytesToConvert) { | ||
165 | + builder.append(String.format("%02x", eachByte)); | ||
166 | + } | ||
167 | + long num = Long.parseLong(builder.toString(), 16); | ||
168 | + return num; | ||
169 | + } | ||
170 | + | ||
171 | + /** | ||
172 | + * Creates a random number. | ||
173 | + * | ||
174 | + * @return random number | ||
175 | + */ | ||
176 | + public static int createRandomNumber() { | ||
177 | + Random rnd = new Random(); | ||
178 | + int randomNumber = 10000000 + rnd.nextInt(90000000); | ||
179 | + return randomNumber; | ||
180 | + } | ||
181 | + | ||
182 | + /** | ||
183 | + * Reads the LSA header from channel buffer. | ||
184 | + * | ||
185 | + * @param channelBuffer channel buffer instance | ||
186 | + * @return LSA header instance. | ||
187 | + * @throws Exception might throws exception while parsing buffer | ||
188 | + */ | ||
189 | + public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception { | ||
190 | + //add all the LSA Headers - one header is of 20 bytes | ||
191 | + LsaHeader lsaHeader = null; | ||
192 | + if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { | ||
193 | + byte[] byteArray = new byte[OspfUtil.FOUR_BYTES]; | ||
194 | + channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES); | ||
195 | + ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray); | ||
196 | + int lsType = byteArray[3]; | ||
197 | + if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA | ||
198 | + || lsType == OspfParameters.AS_OPAQUE_LSA) { | ||
199 | + OpaqueLsaHeader header = new OpaqueLsaHeader(); | ||
200 | + header.setAge(tempBuffer.readShort()); | ||
201 | + header.setOptions(tempBuffer.readByte()); | ||
202 | + header.setLsType(tempBuffer.readByte()); | ||
203 | + header.setOpaqueType(channelBuffer.readByte()); | ||
204 | + header.setOpaqueId(channelBuffer.readUnsignedMedium()); | ||
205 | + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; | ||
206 | + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); | ||
207 | + header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); | ||
208 | + header.setLsSequenceNo(channelBuffer.readInt()); | ||
209 | + header.setLsCheckSum(channelBuffer.readUnsignedShort()); | ||
210 | + header.setLsPacketLen(channelBuffer.readShort()); | ||
211 | + byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId()); | ||
212 | + header.setLinkStateId(header.opaqueType() + "." + "0" + "." + | ||
213 | + opaqueIdBytes[0] + "." + opaqueIdBytes[1]); | ||
214 | + lsaHeader = header; | ||
215 | + } else { | ||
216 | + LsaHeader header = new LsaHeader(); | ||
217 | + header.setAge(tempBuffer.readShort()); | ||
218 | + header.setOptions(tempBuffer.readByte()); | ||
219 | + header.setLsType(tempBuffer.readByte()); | ||
220 | + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; | ||
221 | + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); | ||
222 | + header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName()); | ||
223 | + tempByteArray = new byte[OspfUtil.FOUR_BYTES]; | ||
224 | + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); | ||
225 | + header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); | ||
226 | + header.setLsSequenceNo(channelBuffer.readInt()); | ||
227 | + header.setLsCheckSum(channelBuffer.readUnsignedShort()); | ||
228 | + header.setLsPacketLen(channelBuffer.readShort()); | ||
229 | + lsaHeader = header; | ||
230 | + } | ||
231 | + } | ||
232 | + return lsaHeader; | ||
233 | + } | ||
234 | + | ||
235 | + | ||
236 | + /** | ||
237 | + * Converts an integer to two bytes. | ||
238 | + * | ||
239 | + * @param numberToConvert number to convert | ||
240 | + * @return given number as bytes | ||
241 | + */ | ||
242 | + public static byte[] convertToTwoBytes(int numberToConvert) { | ||
243 | + | ||
244 | + byte[] numInBytes = new byte[2]; | ||
245 | + String s1 = Integer.toHexString(numberToConvert); | ||
246 | + if (s1.length() % 2 != 0) { | ||
247 | + s1 = "0" + s1; | ||
248 | + } | ||
249 | + byte[] hexas = DatatypeConverter.parseHexBinary(s1); | ||
250 | + if (hexas.length == 1) { | ||
251 | + numInBytes[0] = 0; | ||
252 | + numInBytes[1] = hexas[0]; | ||
253 | + } else { | ||
254 | + numInBytes[0] = hexas[0]; | ||
255 | + numInBytes[1] = hexas[1]; | ||
256 | + } | ||
257 | + return numInBytes; | ||
258 | + } | ||
259 | + | ||
260 | + /** | ||
261 | + * Converts a number to three bytes. | ||
262 | + * | ||
263 | + * @param numberToConvert number to convert | ||
264 | + * @return given number as bytes | ||
265 | + */ | ||
266 | + public static byte[] convertToThreeBytes(int numberToConvert) { | ||
267 | + byte[] numInBytes = new byte[3]; | ||
268 | + String s1 = Integer.toHexString(numberToConvert); | ||
269 | + if (s1.length() % 2 != 0) { | ||
270 | + s1 = "0" + s1; | ||
271 | + } | ||
272 | + byte[] hexas = DatatypeConverter.parseHexBinary(s1); | ||
273 | + if (hexas.length == 1) { | ||
274 | + numInBytes[0] = 0; | ||
275 | + numInBytes[1] = 0; | ||
276 | + numInBytes[2] = hexas[0]; | ||
277 | + } else if (hexas.length == 2) { | ||
278 | + numInBytes[0] = 0; | ||
279 | + numInBytes[1] = hexas[0]; | ||
280 | + numInBytes[2] = hexas[1]; | ||
281 | + } else { | ||
282 | + numInBytes[0] = hexas[0]; | ||
283 | + numInBytes[1] = hexas[1]; | ||
284 | + numInBytes[2] = hexas[2]; | ||
285 | + } | ||
286 | + return numInBytes; | ||
287 | + } | ||
288 | + | ||
289 | + /** | ||
290 | + * Converts a number to four bytes. | ||
291 | + * | ||
292 | + * @param numberToConvert number to convert | ||
293 | + * @return given number as bytes | ||
294 | + */ | ||
295 | + public static byte[] convertToFourBytes(int numberToConvert) { | ||
296 | + | ||
297 | + byte[] numInBytes = new byte[4]; | ||
298 | + String s1 = Integer.toHexString(numberToConvert); | ||
299 | + if (s1.length() % 2 != 0) { | ||
300 | + s1 = "0" + s1; | ||
301 | + } | ||
302 | + byte[] hexas = DatatypeConverter.parseHexBinary(s1); | ||
303 | + if (hexas.length == 1) { | ||
304 | + numInBytes[0] = 0; | ||
305 | + numInBytes[1] = 0; | ||
306 | + numInBytes[2] = 0; | ||
307 | + numInBytes[3] = hexas[0]; | ||
308 | + } else if (hexas.length == 2) { | ||
309 | + numInBytes[0] = 0; | ||
310 | + numInBytes[1] = 0; | ||
311 | + numInBytes[2] = hexas[0]; | ||
312 | + numInBytes[3] = hexas[1]; | ||
313 | + } else if (hexas.length == 3) { | ||
314 | + numInBytes[0] = 0; | ||
315 | + numInBytes[1] = hexas[0]; | ||
316 | + numInBytes[2] = hexas[1]; | ||
317 | + numInBytes[3] = hexas[2]; | ||
318 | + } else { | ||
319 | + numInBytes[0] = hexas[0]; | ||
320 | + numInBytes[1] = hexas[1]; | ||
321 | + numInBytes[2] = hexas[2]; | ||
322 | + numInBytes[3] = hexas[3]; | ||
323 | + } | ||
324 | + return numInBytes; | ||
325 | + } | ||
326 | + | ||
327 | + /** | ||
328 | + * Converts a number to four bytes. | ||
329 | + * | ||
330 | + * @param numberToConvert number to convert | ||
331 | + * @return given number as bytes | ||
332 | + */ | ||
333 | + public static byte[] convertToFourBytes(long numberToConvert) { | ||
334 | + | ||
335 | + byte[] numInBytes = new byte[4]; | ||
336 | + String s1 = Long.toHexString(numberToConvert); | ||
337 | + if (s1.length() % 2 != 0) { | ||
338 | + s1 = "0" + s1; | ||
339 | + } | ||
340 | + if (s1.length() == 16) { | ||
341 | + s1 = s1.substring(8, s1.length()); | ||
342 | + } | ||
343 | + byte[] hexas = DatatypeConverter.parseHexBinary(s1); | ||
344 | + if (hexas.length == 1) { | ||
345 | + numInBytes[0] = 0; | ||
346 | + numInBytes[1] = 0; | ||
347 | + numInBytes[2] = 0; | ||
348 | + numInBytes[3] = hexas[0]; | ||
349 | + } else if (hexas.length == 2) { | ||
350 | + numInBytes[0] = 0; | ||
351 | + numInBytes[1] = 0; | ||
352 | + numInBytes[2] = hexas[0]; | ||
353 | + numInBytes[3] = hexas[1]; | ||
354 | + } else if (hexas.length == 3) { | ||
355 | + numInBytes[0] = 0; | ||
356 | + numInBytes[1] = hexas[0]; | ||
357 | + numInBytes[2] = hexas[1]; | ||
358 | + numInBytes[3] = hexas[2]; | ||
359 | + } else { | ||
360 | + numInBytes[0] = hexas[0]; | ||
361 | + numInBytes[1] = hexas[1]; | ||
362 | + numInBytes[2] = hexas[2]; | ||
363 | + numInBytes[3] = hexas[3]; | ||
364 | + } | ||
365 | + return numInBytes; | ||
366 | + } | ||
367 | + | ||
368 | + /** | ||
369 | + * Adds the checksum and length in packet. | ||
370 | + * | ||
371 | + * @param ospfPacket ospf packet | ||
372 | + * @param lengthBytePos1 length byte position | ||
373 | + * @param lengthBytePos2 length byte position | ||
374 | + * @param checksumBytePos1 checksum byte position | ||
375 | + * @param checksumBytePos2 checksum byte position | ||
376 | + * @return byte array with checksum and length | ||
377 | + */ | ||
378 | + public static byte[] addLengthAndCheckSum(byte[] ospfPacket, int lengthBytePos1, int lengthBytePos2, | ||
379 | + int checksumBytePos1, int checksumBytePos2) { | ||
380 | + //Set the length of the packet | ||
381 | + //Get the total length of the packet | ||
382 | + int length = ospfPacket.length; | ||
383 | + //Convert the lenth to two bytes as the length field is 2 bytes | ||
384 | + byte[] lenthInTwoBytes = OspfUtil.convertToTwoBytes(length); | ||
385 | + //ospf header 3rd and 4th position represents length | ||
386 | + ospfPacket[lengthBytePos1] = lenthInTwoBytes[0]; //assign 1st byte in lengthBytePos1 | ||
387 | + ospfPacket[lengthBytePos2] = lenthInTwoBytes[1]; //assign 2st byte in lengthBytePos2 | ||
388 | + | ||
389 | + //Get the checksum as two bytes. | ||
390 | + byte[] checkSumInTwoBytes = new ChecksumCalculator().calculateOspfCheckSum(ospfPacket, | ||
391 | + checksumBytePos1, checksumBytePos2); | ||
392 | + ospfPacket[checksumBytePos1] = checkSumInTwoBytes[0]; //assign 1st byte in checksumBytePos1 | ||
393 | + ospfPacket[checksumBytePos2] = checkSumInTwoBytes[1]; //assign 2st byte in checksumBytePos2 | ||
394 | + | ||
395 | + return ospfPacket; | ||
396 | + } | ||
397 | + | ||
398 | + /** | ||
399 | + * Adds metadata to ospf packet like whether to join multi cast group and destination IP. | ||
400 | + * | ||
401 | + * @param ospfPacket OSPF packet | ||
402 | + * @param allDroutersValue whether to join multi cast or not | ||
403 | + * @param destinationIp destination ip address | ||
404 | + * @return byte array | ||
405 | + */ | ||
406 | + public static byte[] addMetadata(byte[] ospfPacket, int allDroutersValue, Ip4Address destinationIp) { | ||
407 | + byte[] packet; | ||
408 | + byte[] allDroutersByteVal = {(byte) allDroutersValue}; | ||
409 | + byte[] destIpAsBytes = destinationIp.toOctets(); | ||
410 | + byte[] metadata = Bytes.concat(allDroutersByteVal, destIpAsBytes); | ||
411 | + | ||
412 | + packet = Bytes.concat(metadata, ospfPacket); | ||
413 | + | ||
414 | + return packet; | ||
415 | + } | ||
416 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/package-info.java
0 → 100644
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 protocol utilities. | ||
19 | + */ | ||
20 | +package org.onosproject.ospf.protocol.util; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/LsaHeaderTest.java
0 → 100644
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.lsa; | ||
17 | + | ||
18 | +import org.junit.After; | ||
19 | +import org.junit.Before; | ||
20 | +import org.junit.Test; | ||
21 | +import org.onlab.packet.Ip4Address; | ||
22 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
23 | + | ||
24 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
25 | +import static org.hamcrest.Matchers.*; | ||
26 | + | ||
27 | +/** | ||
28 | + * Unit test class for LsaHeader. | ||
29 | + */ | ||
30 | +public class LsaHeaderTest { | ||
31 | + | ||
32 | + private LsaHeader lsaHeader; | ||
33 | + private int result; | ||
34 | + private Ip4Address result1; | ||
35 | + private long result2; | ||
36 | + private OspfLsaType ospflsaType; | ||
37 | + private LsaHeader header; | ||
38 | + private byte[] result3; | ||
39 | + private LsaHeader lsaHeader1; | ||
40 | + private String result4; | ||
41 | + | ||
42 | + @Before | ||
43 | + public void setUp() throws Exception { | ||
44 | + lsaHeader = new LsaHeader(); | ||
45 | + } | ||
46 | + | ||
47 | + @After | ||
48 | + public void tearDown() throws Exception { | ||
49 | + lsaHeader = null; | ||
50 | + result1 = null; | ||
51 | + ospflsaType = null; | ||
52 | + header = null; | ||
53 | + result3 = null; | ||
54 | + lsaHeader1 = null; | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Tests equals() method. | ||
59 | + */ | ||
60 | + @Test | ||
61 | + public void testEquals() throws Exception { | ||
62 | + assertThat(lsaHeader.equals(new LsaHeader()), is(true)); | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * Tests hashCode() method. | ||
67 | + */ | ||
68 | + @Test | ||
69 | + public void testHashCode() throws Exception { | ||
70 | + result = lsaHeader.hashCode(); | ||
71 | + assertThat(result, is(notNullValue())); | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Tests age() getter method. | ||
76 | + */ | ||
77 | + @Test | ||
78 | + public void testGetAge() throws Exception { | ||
79 | + lsaHeader.setAge(10); | ||
80 | + result = lsaHeader.age(); | ||
81 | + assertThat(result, is(10)); | ||
82 | + } | ||
83 | + | ||
84 | + /** | ||
85 | + * Tests age() setter method. | ||
86 | + */ | ||
87 | + @Test | ||
88 | + public void testSetAge() throws Exception { | ||
89 | + lsaHeader.setAge(10); | ||
90 | + result = lsaHeader.age(); | ||
91 | + assertThat(result, is(10)); | ||
92 | + } | ||
93 | + | ||
94 | + /** | ||
95 | + * Tests options() getter method. | ||
96 | + */ | ||
97 | + @Test | ||
98 | + public void testGetOptions() throws Exception { | ||
99 | + lsaHeader.setOptions(2); | ||
100 | + result = lsaHeader.options(); | ||
101 | + assertThat(result, is(2)); | ||
102 | + } | ||
103 | + | ||
104 | + /** | ||
105 | + * Tests options() setter method. | ||
106 | + */ | ||
107 | + @Test | ||
108 | + public void testSetOptions() throws Exception { | ||
109 | + lsaHeader.setOptions(2); | ||
110 | + result = lsaHeader.options(); | ||
111 | + assertThat(result, is(2)); | ||
112 | + } | ||
113 | + | ||
114 | + /** | ||
115 | + * Tests lsType() getter method. | ||
116 | + */ | ||
117 | + @Test | ||
118 | + public void testGetLsType() throws Exception { | ||
119 | + lsaHeader.setLsType(1); | ||
120 | + result = lsaHeader.lsType(); | ||
121 | + assertThat(result, is(1)); | ||
122 | + } | ||
123 | + | ||
124 | + /** | ||
125 | + * Tests lsType() setter method. | ||
126 | + */ | ||
127 | + @Test | ||
128 | + public void testSetLsType() throws Exception { | ||
129 | + lsaHeader.setLsType(1); | ||
130 | + result = lsaHeader.lsType(); | ||
131 | + assertThat(result, is(1)); | ||
132 | + } | ||
133 | + | ||
134 | + /** | ||
135 | + * Tests linkStateId() getter method. | ||
136 | + */ | ||
137 | + @Test | ||
138 | + public void testGetLinkStateId() throws Exception { | ||
139 | + lsaHeader.setLinkStateId("10.226.165.164"); | ||
140 | + result4 = lsaHeader.linkStateId(); | ||
141 | + assertThat(result4, is("10.226.165.164")); | ||
142 | + } | ||
143 | + | ||
144 | + /** | ||
145 | + * Tests linkStateId() setter method. | ||
146 | + */ | ||
147 | + @Test | ||
148 | + public void testSetLinkStateId() throws Exception { | ||
149 | + lsaHeader.setLinkStateId("10.226.165.164"); | ||
150 | + result4 = lsaHeader.linkStateId(); | ||
151 | + assertThat(result4, is("10.226.165.164")); | ||
152 | + } | ||
153 | + | ||
154 | + /** | ||
155 | + * Tests advertisingRouter() setter method. | ||
156 | + */ | ||
157 | + @Test | ||
158 | + public void testGetAdvertisingRouter() throws Exception { | ||
159 | + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.164")); | ||
160 | + result1 = lsaHeader.advertisingRouter(); | ||
161 | + assertThat(result1, is(Ip4Address.valueOf("10.226.165.164"))); | ||
162 | + } | ||
163 | + | ||
164 | + /** | ||
165 | + * Tests advertisingRouter() setter method. | ||
166 | + */ | ||
167 | + @Test | ||
168 | + public void testSetAdvertisingRouter() throws Exception { | ||
169 | + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.164")); | ||
170 | + result1 = lsaHeader.advertisingRouter(); | ||
171 | + assertThat(result1, is(Ip4Address.valueOf("10.226.165.164"))); | ||
172 | + } | ||
173 | + | ||
174 | + /** | ||
175 | + * Tests lsSequenceNo() getter method. | ||
176 | + */ | ||
177 | + @Test | ||
178 | + public void testGetLsSequenceNo() throws Exception { | ||
179 | + lsaHeader.setLsSequenceNo(222); | ||
180 | + result2 = lsaHeader.lsSequenceNo(); | ||
181 | + assertThat(result2, is(222L)); | ||
182 | + } | ||
183 | + | ||
184 | + /** | ||
185 | + * Tests lsSequenceNo() setter method. | ||
186 | + */ | ||
187 | + @Test | ||
188 | + public void testSetLsSequenceNo() throws Exception { | ||
189 | + lsaHeader.setLsSequenceNo(222); | ||
190 | + result2 = lsaHeader.lsSequenceNo(); | ||
191 | + assertThat(result2, is(222L)); | ||
192 | + } | ||
193 | + | ||
194 | + /** | ||
195 | + * Tests lsCheckSum() getter method. | ||
196 | + */ | ||
197 | + @Test | ||
198 | + public void testGetLsChecksum() throws Exception { | ||
199 | + lsaHeader.setLsCheckSum(2); | ||
200 | + result = lsaHeader.lsCheckSum(); | ||
201 | + assertThat(result, is(2)); | ||
202 | + } | ||
203 | + | ||
204 | + /** | ||
205 | + * Tests lsCheckSum() setter method. | ||
206 | + */ | ||
207 | + @Test | ||
208 | + public void testSetLsChecksum() throws Exception { | ||
209 | + lsaHeader.setLsCheckSum(2); | ||
210 | + result = lsaHeader.lsCheckSum(); | ||
211 | + assertThat(result, is(2)); | ||
212 | + } | ||
213 | + | ||
214 | + /** | ||
215 | + * Tests lsPacketLen() getter method. | ||
216 | + */ | ||
217 | + @Test | ||
218 | + public void testGetLsPacketLen() throws Exception { | ||
219 | + lsaHeader.setLsPacketLen(48); | ||
220 | + result = lsaHeader.lsPacketLen(); | ||
221 | + assertThat(result, is(48)); | ||
222 | + } | ||
223 | + | ||
224 | + /** | ||
225 | + * Tests lsPacketLen() getter method. | ||
226 | + */ | ||
227 | + @Test | ||
228 | + public void testSetLsPacketLen() throws Exception { | ||
229 | + lsaHeader.setLsPacketLen(48); | ||
230 | + result = lsaHeader.lsPacketLen(); | ||
231 | + assertThat(result, is(48)); | ||
232 | + } | ||
233 | + | ||
234 | + /** | ||
235 | + * Tests getOspfLsaType() getter method. | ||
236 | + */ | ||
237 | + @Test | ||
238 | + public void testGetOspfLsaType() throws Exception { | ||
239 | + lsaHeader.setLsType(1); | ||
240 | + ospflsaType = lsaHeader.getOspfLsaType(); | ||
241 | + assertThat(ospflsaType, is(notNullValue())); | ||
242 | + assertThat(ospflsaType, is(OspfLsaType.ROUTER)); | ||
243 | + lsaHeader.setLsType(2); | ||
244 | + ospflsaType = lsaHeader.getOspfLsaType(); | ||
245 | + assertThat(ospflsaType, is(notNullValue())); | ||
246 | + assertThat(ospflsaType, is(OspfLsaType.NETWORK)); | ||
247 | + lsaHeader.setLsType(3); | ||
248 | + ospflsaType = lsaHeader.getOspfLsaType(); | ||
249 | + assertThat(ospflsaType, is(notNullValue())); | ||
250 | + assertThat(ospflsaType, is(OspfLsaType.SUMMARY)); | ||
251 | + lsaHeader.setLsType(4); | ||
252 | + ospflsaType = lsaHeader.getOspfLsaType(); | ||
253 | + assertThat(ospflsaType, is(notNullValue())); | ||
254 | + assertThat(ospflsaType, is(OspfLsaType.ASBR_SUMMARY)); | ||
255 | + lsaHeader.setLsType(5); | ||
256 | + ospflsaType = lsaHeader.getOspfLsaType(); | ||
257 | + assertThat(ospflsaType, is(notNullValue())); | ||
258 | + assertThat(ospflsaType, is(OspfLsaType.EXTERNAL_LSA)); | ||
259 | + lsaHeader.setLsType(6); | ||
260 | + ospflsaType = lsaHeader.getOspfLsaType(); | ||
261 | + assertThat(ospflsaType, is(notNullValue())); | ||
262 | + assertThat(ospflsaType, is(OspfLsaType.UNDEFINED)); | ||
263 | + } | ||
264 | + | ||
265 | + /** | ||
266 | + * Tests lsaHeader() getter method. | ||
267 | + */ | ||
268 | + @Test | ||
269 | + public void testGetLsaHeader() throws Exception { | ||
270 | + header = (LsaHeader) lsaHeader.lsaHeader(); | ||
271 | + assertThat(header, instanceOf(LsaHeader.class)); | ||
272 | + } | ||
273 | + | ||
274 | + /** | ||
275 | + * Tests getLsaHeaderAsByteArray() method. | ||
276 | + */ | ||
277 | + @Test | ||
278 | + public void testGetLsaHeaderAsByteArray() throws Exception { | ||
279 | + result3 = lsaHeader.getLsaHeaderAsByteArray(); | ||
280 | + assertThat(result3, is(notNullValue())); | ||
281 | + } | ||
282 | + | ||
283 | + /** | ||
284 | + * Tests to string method. | ||
285 | + */ | ||
286 | + @Test | ||
287 | + public void testToString() throws Exception { | ||
288 | + assertThat(lsaHeader.toString(), is(notNullValue())); | ||
289 | + } | ||
290 | + | ||
291 | + /** | ||
292 | + * Tests populateHeader() method. | ||
293 | + */ | ||
294 | + @Test | ||
295 | + public void testPopulateHeader() throws Exception { | ||
296 | + lsaHeader1 = new LsaHeader(); | ||
297 | + lsaHeader1.setLsPacketLen(10); | ||
298 | + lsaHeader1.setAdvertisingRouter(Ip4Address.valueOf("1.1.1.1")); | ||
299 | + lsaHeader1.setOptions(2); | ||
300 | + lsaHeader1.setAge(20); | ||
301 | + lsaHeader1.setLsType(3); | ||
302 | + lsaHeader1.setLinkStateId("2.2.2.2"); | ||
303 | + lsaHeader1.setLsCheckSum(1234); | ||
304 | + lsaHeader1.setLsSequenceNo(456789); | ||
305 | + lsaHeader.populateHeader(lsaHeader1); | ||
306 | + assertThat(lsaHeader1, is(notNullValue())); | ||
307 | + } | ||
308 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/OpaqueLsaHeaderTest.java
0 → 100644
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.lsa; | ||
17 | + | ||
18 | +import org.hamcrest.Matchers; | ||
19 | +import org.junit.After; | ||
20 | +import org.junit.Before; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onlab.packet.Ip4Address; | ||
23 | + | ||
24 | +import static org.hamcrest.CoreMatchers.notNullValue; | ||
25 | +import static org.hamcrest.Matchers.is; | ||
26 | +import static org.junit.Assert.assertThat; | ||
27 | + | ||
28 | +/** | ||
29 | + * Unit test class for OpaqueLsaHeader. | ||
30 | + */ | ||
31 | +public class OpaqueLsaHeaderTest { | ||
32 | + | ||
33 | + private OpaqueLsaHeader opaqueHeader; | ||
34 | + private OpaqueLsaHeader opaqueLsaHeader1; | ||
35 | + private int num; | ||
36 | + private byte[] result; | ||
37 | + private int result1; | ||
38 | + | ||
39 | + @Before | ||
40 | + public void setUp() throws Exception { | ||
41 | + opaqueHeader = new OpaqueLsaHeader(); | ||
42 | + } | ||
43 | + | ||
44 | + @After | ||
45 | + public void tearDown() throws Exception { | ||
46 | + opaqueHeader = null; | ||
47 | + opaqueLsaHeader1 = null; | ||
48 | + result = null; | ||
49 | + } | ||
50 | + | ||
51 | + /** | ||
52 | + * Tests populateHeader() method. | ||
53 | + */ | ||
54 | + @Test | ||
55 | + public void testPopulateHeader() throws Exception { | ||
56 | + opaqueLsaHeader1 = new OpaqueLsaHeader(); | ||
57 | + opaqueLsaHeader1.setLsPacketLen(10); | ||
58 | + opaqueLsaHeader1.setAdvertisingRouter(Ip4Address.valueOf("1.1.1.1")); | ||
59 | + opaqueLsaHeader1.setOptions(2); | ||
60 | + opaqueLsaHeader1.setAge(20); | ||
61 | + opaqueLsaHeader1.setLsType(3); | ||
62 | + opaqueLsaHeader1.setOpaqueId(1); | ||
63 | + opaqueLsaHeader1.setOpaqueType(3); | ||
64 | + opaqueLsaHeader1.setLsCheckSum(1234); | ||
65 | + opaqueLsaHeader1.setLsSequenceNo(456789); | ||
66 | + opaqueLsaHeader1.populateHeader(opaqueLsaHeader1); | ||
67 | + assertThat(opaqueLsaHeader1, is(notNullValue())); | ||
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * Tests opaqueId() getter method. | ||
72 | + */ | ||
73 | + @Test | ||
74 | + public void testGetOpaqueId() throws Exception { | ||
75 | + opaqueHeader.setOpaqueId(1); | ||
76 | + num = opaqueHeader.opaqueId(); | ||
77 | + assertThat(num, is(1)); | ||
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * Tests opaqueId() setter method. | ||
82 | + */ | ||
83 | + @Test | ||
84 | + public void testSetOpaqueId() throws Exception { | ||
85 | + opaqueHeader.setOpaqueId(1); | ||
86 | + num = opaqueHeader.opaqueId(); | ||
87 | + assertThat(num, is(1)); | ||
88 | + } | ||
89 | + | ||
90 | + /** | ||
91 | + * Tests opaqueType() getter method. | ||
92 | + */ | ||
93 | + @Test | ||
94 | + public void testGetOpaqueType() throws Exception { | ||
95 | + opaqueHeader.setOpaqueType(1); | ||
96 | + num = opaqueHeader.opaqueType(); | ||
97 | + assertThat(num, is(1)); | ||
98 | + } | ||
99 | + | ||
100 | + /** | ||
101 | + * Tests opaqueType() setter method. | ||
102 | + */ | ||
103 | + @Test | ||
104 | + public void testSetOpaqueType() throws Exception { | ||
105 | + opaqueHeader.setOpaqueType(1); | ||
106 | + num = opaqueHeader.opaqueType(); | ||
107 | + assertThat(num, is(1)); | ||
108 | + } | ||
109 | + | ||
110 | + /** | ||
111 | + * Tests getOpaqueLsaHeaderAsByteArray() method. | ||
112 | + */ | ||
113 | + @Test | ||
114 | + public void testGetOpaqueLsaHeaderAsByteArray() throws Exception { | ||
115 | + result = opaqueHeader.getOpaqueLsaHeaderAsByteArray(); | ||
116 | + assertThat(result, is(notNullValue())); | ||
117 | + } | ||
118 | + | ||
119 | + /** | ||
120 | + * Tests to string method. | ||
121 | + */ | ||
122 | + @Test | ||
123 | + public void testToString() throws Exception { | ||
124 | + assertThat(opaqueHeader.toString(), is(notNullValue())); | ||
125 | + } | ||
126 | + | ||
127 | + /** | ||
128 | + * Tests hashCode() method. | ||
129 | + */ | ||
130 | + @Test | ||
131 | + public void testHashcode() throws Exception { | ||
132 | + | ||
133 | + result1 = opaqueHeader.hashCode(); | ||
134 | + assertThat(result1, is(Matchers.notNullValue())); | ||
135 | + | ||
136 | + } | ||
137 | +} | ||
... | \ 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; | ||
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 | + | ||
26 | +import static org.hamcrest.CoreMatchers.*; | ||
27 | +import static org.junit.Assert.assertThat; | ||
28 | + | ||
29 | +/** | ||
30 | + * Unit test class for OspfPacketHeader. | ||
31 | + */ | ||
32 | +public class OspfPacketHeaderTest { | ||
33 | + | ||
34 | + private final byte[] packet = {0, 0, 0, 0}; | ||
35 | + private OspfPacketHeader ospfPacketHeader; | ||
36 | + private ChannelBuffer channelBuffer; | ||
37 | + private byte[] result2; | ||
38 | + private int result; | ||
39 | + private Ip4Address result1; | ||
40 | + | ||
41 | + @Before | ||
42 | + public void setUp() throws Exception { | ||
43 | + ospfPacketHeader = new OspfPacketHeader(); | ||
44 | + } | ||
45 | + | ||
46 | + @After | ||
47 | + public void tearDown() throws Exception { | ||
48 | + ospfPacketHeader = null; | ||
49 | + ospfPacketHeader = null; | ||
50 | + channelBuffer = null; | ||
51 | + result2 = null; | ||
52 | + result1 = null; | ||
53 | + } | ||
54 | + | ||
55 | + /** | ||
56 | + * Tests sourceIp() getter method. | ||
57 | + */ | ||
58 | + @Test | ||
59 | + public void testGetSourceIP() throws Exception { | ||
60 | + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("1.1.1.1")); | ||
61 | + assertThat(ospfPacketHeader.sourceIp(), is(Ip4Address.valueOf("1.1.1.1"))); | ||
62 | + } | ||
63 | + | ||
64 | + /** | ||
65 | + * Tests sourceIp() setter method. | ||
66 | + */ | ||
67 | + @Test | ||
68 | + public void testSetSourceIP() throws Exception { | ||
69 | + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("1.1.1.1")); | ||
70 | + assertThat(result, is(notNullValue())); | ||
71 | + assertThat(ospfPacketHeader.sourceIp(), is(Ip4Address.valueOf("1.1.1.1"))); | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Tests ospfMessageType() getter method. | ||
76 | + */ | ||
77 | + @Test | ||
78 | + public void testGetOspfMessageType() throws Exception { | ||
79 | + assertThat(ospfPacketHeader.ospfMessageType(), nullValue()); | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * Tests readFrom() method. | ||
84 | + */ | ||
85 | + @Test | ||
86 | + public void testReadFrom() throws Exception { | ||
87 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
88 | + ospfPacketHeader.readFrom(channelBuffer); | ||
89 | + assertThat(ospfPacketHeader, is(notNullValue())); | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * Tests asBytes() method. | ||
94 | + */ | ||
95 | + @Test | ||
96 | + public void testAsBytes() throws Exception { | ||
97 | + result2 = ospfPacketHeader.asBytes(); | ||
98 | + assertThat(result2, is(notNullValue())); | ||
99 | + } | ||
100 | + | ||
101 | + /** | ||
102 | + * Tests ospfVersion() getter method. | ||
103 | + */ | ||
104 | + @Test | ||
105 | + public void testGetOspfVer() throws Exception { | ||
106 | + ospfPacketHeader.setOspfVer(2); | ||
107 | + result = ospfPacketHeader.ospfVersion(); | ||
108 | + assertThat(result, is(notNullValue())); | ||
109 | + assertThat(result, is(2)); | ||
110 | + } | ||
111 | + | ||
112 | + /** | ||
113 | + * Tests ospfVersion() setter method. | ||
114 | + */ | ||
115 | + @Test | ||
116 | + public void testSetOspfVer() throws Exception { | ||
117 | + ospfPacketHeader.setOspfVer(2); | ||
118 | + result = ospfPacketHeader.ospfVersion(); | ||
119 | + assertThat(result, is(notNullValue())); | ||
120 | + assertThat(result, is(2)); | ||
121 | + } | ||
122 | + | ||
123 | + /** | ||
124 | + * Tests ospfType() getter method. | ||
125 | + */ | ||
126 | + @Test | ||
127 | + public void testGetOspfType() throws Exception { | ||
128 | + ospfPacketHeader.setOspftype(3); | ||
129 | + result = ospfPacketHeader.ospfType(); | ||
130 | + assertThat(result, is(notNullValue())); | ||
131 | + assertThat(result, is(3)); | ||
132 | + } | ||
133 | + | ||
134 | + /** | ||
135 | + * Tests ospfType() setter method. | ||
136 | + */ | ||
137 | + @Test | ||
138 | + public void testSetOspfType() throws Exception { | ||
139 | + ospfPacketHeader.setOspftype(3); | ||
140 | + result = ospfPacketHeader.ospfType(); | ||
141 | + assertThat(result, is(notNullValue())); | ||
142 | + assertThat(result, is(3)); | ||
143 | + } | ||
144 | + | ||
145 | + /** | ||
146 | + * Tests ospfPacLength() getter method. | ||
147 | + */ | ||
148 | + @Test | ||
149 | + public void testGetOspfPacLength() throws Exception { | ||
150 | + ospfPacketHeader.setOspfPacLength(3); | ||
151 | + result = ospfPacketHeader.ospfPacLength(); | ||
152 | + assertThat(result, is(notNullValue())); | ||
153 | + assertThat(result, is(3)); | ||
154 | + } | ||
155 | + | ||
156 | + /** | ||
157 | + * Tests ospfPacLength() setter method. | ||
158 | + */ | ||
159 | + @Test | ||
160 | + public void testSetOspfPacLength() throws Exception { | ||
161 | + ospfPacketHeader.setOspfPacLength(3); | ||
162 | + int result = ospfPacketHeader.ospfPacLength(); | ||
163 | + assertThat(result, is(notNullValue())); | ||
164 | + assertThat(result, is(3)); | ||
165 | + } | ||
166 | + | ||
167 | + /** | ||
168 | + * Tests routerId()getter method. | ||
169 | + */ | ||
170 | + @Test | ||
171 | + public void testGetRouterId() throws Exception { | ||
172 | + | ||
173 | + ospfPacketHeader.setRouterId(Ip4Address.valueOf("1.1.1.1")); | ||
174 | + result1 = ospfPacketHeader.routerId(); | ||
175 | + assertThat(result1, is(notNullValue())); | ||
176 | + assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); | ||
177 | + | ||
178 | + } | ||
179 | + | ||
180 | + /** | ||
181 | + * Tests routerId() setter method. | ||
182 | + */ | ||
183 | + @Test | ||
184 | + public void testSetRouterId() throws Exception { | ||
185 | + ospfPacketHeader.setRouterId(Ip4Address.valueOf("1.1.1.1")); | ||
186 | + result1 = ospfPacketHeader.routerId(); | ||
187 | + assertThat(result1, is(notNullValue())); | ||
188 | + assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); | ||
189 | + } | ||
190 | + | ||
191 | + /** | ||
192 | + * Tests areaId() getter method. | ||
193 | + */ | ||
194 | + @Test | ||
195 | + public void testGetAreaId() throws Exception { | ||
196 | + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); | ||
197 | + result1 = ospfPacketHeader.areaId(); | ||
198 | + assertThat(result1, is(notNullValue())); | ||
199 | + assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); | ||
200 | + } | ||
201 | + | ||
202 | + /** | ||
203 | + * Tests areaId() setter method. | ||
204 | + */ | ||
205 | + @Test | ||
206 | + public void testSetAreaId() throws Exception { | ||
207 | + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); | ||
208 | + result1 = ospfPacketHeader.areaId(); | ||
209 | + assertThat(result1, is(notNullValue())); | ||
210 | + assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); | ||
211 | + } | ||
212 | + | ||
213 | + /** | ||
214 | + * Tests checksum() getter method. | ||
215 | + */ | ||
216 | + @Test | ||
217 | + public void testGetChecksum() throws Exception { | ||
218 | + ospfPacketHeader.setChecksum(3); | ||
219 | + result = ospfPacketHeader.checksum(); | ||
220 | + assertThat(result, is(notNullValue())); | ||
221 | + assertThat(result, is(3)); | ||
222 | + } | ||
223 | + | ||
224 | + /** | ||
225 | + * Tests checksum() setter method. | ||
226 | + */ | ||
227 | + @Test | ||
228 | + public void testSetChecksum() throws Exception { | ||
229 | + ospfPacketHeader.setChecksum(3); | ||
230 | + result = ospfPacketHeader.checksum(); | ||
231 | + assertThat(result, is(notNullValue())); | ||
232 | + assertThat(result, is(3)); | ||
233 | + } | ||
234 | + | ||
235 | + /** | ||
236 | + * Tests authType() getter method. | ||
237 | + */ | ||
238 | + @Test | ||
239 | + public void testGetAutype() throws Exception { | ||
240 | + ospfPacketHeader.setAuthType(3); | ||
241 | + result = ospfPacketHeader.authType(); | ||
242 | + Assert.assertNotNull(result); | ||
243 | + Assert.assertEquals(3, result); | ||
244 | + } | ||
245 | + | ||
246 | + /** | ||
247 | + * Tests authType() setter method. | ||
248 | + */ | ||
249 | + @Test | ||
250 | + public void testSetAutype() throws Exception { | ||
251 | + ospfPacketHeader.setAuthType(3); | ||
252 | + result = ospfPacketHeader.authType(); | ||
253 | + assertThat(result, is(notNullValue())); | ||
254 | + assertThat(result, is(3)); | ||
255 | + } | ||
256 | + | ||
257 | + /** | ||
258 | + * Tests authentication() getter method. | ||
259 | + */ | ||
260 | + @Test | ||
261 | + public void testGetAuthentication() throws Exception { | ||
262 | + ospfPacketHeader.setAuthentication(3); | ||
263 | + result = ospfPacketHeader.authentication(); | ||
264 | + assertThat(result, is(notNullValue())); | ||
265 | + assertThat(result, is(3)); | ||
266 | + } | ||
267 | + | ||
268 | + /** | ||
269 | + * Tests authentication() setter method. | ||
270 | + */ | ||
271 | + @Test | ||
272 | + public void testSetAuthentication() throws Exception { | ||
273 | + ospfPacketHeader.setAuthentication(3); | ||
274 | + result = ospfPacketHeader.authentication(); | ||
275 | + assertThat(result, is(notNullValue())); | ||
276 | + assertThat(result, is(3)); | ||
277 | + } | ||
278 | + | ||
279 | + /** | ||
280 | + * Tests destinationIp() getter method. | ||
281 | + */ | ||
282 | + @Test | ||
283 | + public void testGetDestinationIP() throws Exception { | ||
284 | + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("1.1.1.1")); | ||
285 | + result1 = ospfPacketHeader.destinationIp(); | ||
286 | + assertThat(result1, is(notNullValue())); | ||
287 | + assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); | ||
288 | + } | ||
289 | + | ||
290 | + /** | ||
291 | + * Tests destinationIp() setter method. | ||
292 | + */ | ||
293 | + @Test | ||
294 | + public void testSetDestinationIP() throws Exception { | ||
295 | + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("1.1.1.1")); | ||
296 | + result1 = ospfPacketHeader.destinationIp(); | ||
297 | + assertThat(result1, is(notNullValue())); | ||
298 | + assertThat(result1, is(Ip4Address.valueOf("1.1.1.1"))); | ||
299 | + } | ||
300 | + | ||
301 | + /** | ||
302 | + * Tests to string method. | ||
303 | + */ | ||
304 | + @Test | ||
305 | + public void testToString() throws Exception { | ||
306 | + assertThat(ospfPacketHeader.toString(), is(notNullValue())); | ||
307 | + } | ||
308 | + | ||
309 | + /** | ||
310 | + * Tests populateHeader() method. | ||
311 | + */ | ||
312 | + @Test | ||
313 | + public void testPopulateHeader() throws Exception { | ||
314 | + ospfPacketHeader.populateHeader(new OspfPacketHeader()); | ||
315 | + assertThat(ospfPacketHeader, is(notNullValue())); | ||
316 | + } | ||
317 | + | ||
318 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/util/OspfUtilTest.java
0 → 100644
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 | +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.lsa.LsaHeader; | ||
25 | + | ||
26 | +import static org.hamcrest.CoreMatchers.is; | ||
27 | +import static org.hamcrest.CoreMatchers.notNullValue; | ||
28 | +import static org.junit.Assert.assertThat; | ||
29 | + | ||
30 | +/** | ||
31 | + * Unit test class for OspfUtil. | ||
32 | + */ | ||
33 | +public class OspfUtilTest { | ||
34 | + | ||
35 | + private final int ospfChecksumPos1 = 12; | ||
36 | + private final int ospfChecksumPos2 = 13; | ||
37 | + private final int lsaChecksumPos1 = 16; | ||
38 | + private final int lsaChecksumPos2 = 17; | ||
39 | + private final int ospfLengthPos1 = 2; | ||
40 | + private final int ospfLengthPos2 = 3; | ||
41 | + private final int lsaLengthPos1 = 18; | ||
42 | + private final int lsaLengthPos2 = 19; | ||
43 | + private final byte[] input = {0, 2}; | ||
44 | + private final byte[] packet = {2, 1, 0, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, | ||
45 | + 0, 0, -64, -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, | ||
46 | + -88, 56, 1}; | ||
47 | + private final byte[] rLsa = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48, 2, | ||
48 | + 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10}; | ||
49 | + private final byte[] opaqueheader = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, | ||
50 | + 0, 48}; | ||
51 | + private int num; | ||
52 | + private int result; | ||
53 | + private int input2; | ||
54 | + private long input4; | ||
55 | + private ChannelBuffer channelBuffer; | ||
56 | + private byte[] result1; | ||
57 | + private LsaHeader lsaHeader; | ||
58 | + private boolean result2; | ||
59 | + private long result3; | ||
60 | + | ||
61 | + | ||
62 | + @Before | ||
63 | + public void setUp() throws Exception { | ||
64 | + | ||
65 | + } | ||
66 | + | ||
67 | + @After | ||
68 | + public void tearDown() throws Exception { | ||
69 | + channelBuffer = null; | ||
70 | + result1 = null; | ||
71 | + lsaHeader = null; | ||
72 | + } | ||
73 | + | ||
74 | + | ||
75 | + /** | ||
76 | + * Tests byteToInteger() method. | ||
77 | + */ | ||
78 | + @Test | ||
79 | + public void testByteToInteger() throws Exception { | ||
80 | + result = OspfUtil.byteToInteger(input); | ||
81 | + assertThat(result, is(notNullValue())); | ||
82 | + assertThat(result, is(2)); | ||
83 | + | ||
84 | + } | ||
85 | + | ||
86 | + /** | ||
87 | + * Tests byteToLong() method. | ||
88 | + */ | ||
89 | + @Test | ||
90 | + public void testByteToLong() throws Exception { | ||
91 | + result3 = OspfUtil.byteToLong(input); | ||
92 | + assertThat(result3, is(notNullValue())); | ||
93 | + assertThat(result3, is(2L)); | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * Tests byteToInteger() method. | ||
98 | + */ | ||
99 | + @Test | ||
100 | + public void testByteToLong1() throws Exception { | ||
101 | + result3 = OspfUtil.byteToLong(input); | ||
102 | + assertThat(result3, is(notNullValue())); | ||
103 | + assertThat(result3, is(2L)); | ||
104 | + } | ||
105 | + | ||
106 | + /** | ||
107 | + * Tests byteToInteger() method. | ||
108 | + */ | ||
109 | + @Test | ||
110 | + public void testByteToInteger1() throws Exception { | ||
111 | + result = OspfUtil.byteToInteger(input); | ||
112 | + assertThat(result, is(notNullValue())); | ||
113 | + assertThat(result, is(2)); | ||
114 | + } | ||
115 | + | ||
116 | + /** | ||
117 | + * Tests to createRandomNumber() method. | ||
118 | + */ | ||
119 | + @Test | ||
120 | + public void testCreateRandomNumber() throws Exception { | ||
121 | + num = OspfUtil.createRandomNumber(); | ||
122 | + assertThat(num, is(notNullValue())); | ||
123 | + } | ||
124 | + | ||
125 | + /** | ||
126 | + * Tests readLsaHeader() method. | ||
127 | + */ | ||
128 | + @Test | ||
129 | + public void testReadLsaHeader2() throws Exception { | ||
130 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
131 | + lsaHeader = OspfUtil.readLsaHeader(channelBuffer); | ||
132 | + assertThat(lsaHeader, is(notNullValue())); | ||
133 | + } | ||
134 | + | ||
135 | + /** | ||
136 | + * Tests to readLsaHeader method. | ||
137 | + */ | ||
138 | + @Test | ||
139 | + public void testReadLsaHeader1() throws Exception { | ||
140 | + channelBuffer = ChannelBuffers.copiedBuffer(opaqueheader); | ||
141 | + lsaHeader = OspfUtil.readLsaHeader(channelBuffer); | ||
142 | + assertThat(lsaHeader, is(notNullValue())); | ||
143 | + } | ||
144 | + | ||
145 | + /** | ||
146 | + * Tests convertToTwoBytes() method. | ||
147 | + */ | ||
148 | + @Test | ||
149 | + public void testConvertToTwoBytes() throws Exception { | ||
150 | + input2 = 4; | ||
151 | + result1 = OspfUtil.convertToTwoBytes(input2); | ||
152 | + assertThat(result1.length, is(2)); | ||
153 | + input2 = 1000; | ||
154 | + result1 = OspfUtil.convertToTwoBytes(input2); | ||
155 | + assertThat(result1.length, is(2)); | ||
156 | + } | ||
157 | + | ||
158 | + /** | ||
159 | + * Tests convertToThreeBytes() method. | ||
160 | + */ | ||
161 | + @Test | ||
162 | + public void testConvertToThreeBytes() throws Exception { | ||
163 | + input2 = 1000000; | ||
164 | + result1 = OspfUtil.convertToThreeBytes(input2); | ||
165 | + assertThat(result1.length, is(3)); | ||
166 | + input2 = 1000; | ||
167 | + result1 = OspfUtil.convertToThreeBytes(input2); | ||
168 | + assertThat(result1.length, is(3)); | ||
169 | + input2 = 1; | ||
170 | + result1 = OspfUtil.convertToThreeBytes(input2); | ||
171 | + assertThat(result1.length, is(3)); | ||
172 | + } | ||
173 | + | ||
174 | + /** | ||
175 | + * Tests convertToFourBytes() method. | ||
176 | + */ | ||
177 | + @Test | ||
178 | + public void testConvertToFourBytes() throws Exception { | ||
179 | + input4 = 214748364110L; | ||
180 | + result1 = OspfUtil.convertToFourBytes(input4); | ||
181 | + assertThat(result1.length, is(4)); | ||
182 | + input4 = 1000000; | ||
183 | + result1 = OspfUtil.convertToFourBytes(input4); | ||
184 | + assertThat(result1.length, is(4)); | ||
185 | + input4 = 10000; | ||
186 | + result1 = OspfUtil.convertToFourBytes(input4); | ||
187 | + assertThat(result1.length, is(4)); | ||
188 | + input4 = 1; | ||
189 | + result1 = OspfUtil.convertToFourBytes(input4); | ||
190 | + assertThat(result1.length, is(4)); | ||
191 | + | ||
192 | + } | ||
193 | + | ||
194 | + /** | ||
195 | + * Tests convertToFourBytes() method. | ||
196 | + */ | ||
197 | + @Test | ||
198 | + public void testConvertToFourBytes1() throws Exception { | ||
199 | + input4 = 2147483635; | ||
200 | + result1 = OspfUtil.convertToFourBytes(this.input4); | ||
201 | + assertThat(result1.length, is(4)); | ||
202 | + this.input4 = 1000000; | ||
203 | + result1 = OspfUtil.convertToFourBytes(this.input4); | ||
204 | + assertThat(result1.length, is(4)); | ||
205 | + this.input4 = 10000; | ||
206 | + result1 = OspfUtil.convertToFourBytes(this.input4); | ||
207 | + assertThat(result1.length, is(4)); | ||
208 | + this.input4 = 1; | ||
209 | + result1 = OspfUtil.convertToFourBytes(this.input4); | ||
210 | + assertThat(result1.length, is(4)); | ||
211 | + | ||
212 | + } | ||
213 | + | ||
214 | + /** | ||
215 | + * Tests addLengthAndCheckSum() method. | ||
216 | + */ | ||
217 | + @Test | ||
218 | + public void testAddLengthAndCheckSum() throws Exception { | ||
219 | + result1 = OspfUtil.addLengthAndCheckSum(packet, ospfLengthPos1, ospfLengthPos2, | ||
220 | + ospfChecksumPos1, ospfChecksumPos2); | ||
221 | + assertThat(result1[ospfChecksumPos1], is(packet[ospfChecksumPos1])); | ||
222 | + assertThat(result1[ospfChecksumPos2], is(packet[ospfChecksumPos2])); | ||
223 | + assertThat(result1[ospfLengthPos1], is(packet[ospfLengthPos1])); | ||
224 | + assertThat(result1[ospfLengthPos2], is(packet[ospfLengthPos2])); | ||
225 | + } | ||
226 | + | ||
227 | + /** | ||
228 | + * Tests addMetadata() method. | ||
229 | + */ | ||
230 | + @Test | ||
231 | + public void testAddMetadata() throws Exception { | ||
232 | + result1 = OspfUtil.addMetadata(packet, 123, Ip4Address.valueOf("1.1.1.1")); | ||
233 | + assertThat(result1, is(notNullValue())); | ||
234 | + } | ||
235 | + | ||
236 | + /** | ||
237 | + * Tests addLengthAndCheckSum() method. | ||
238 | + */ | ||
239 | + @Test | ||
240 | + public void testAddLsaLengthAndCheckSum() throws Exception { | ||
241 | + result1 = OspfUtil.addLengthAndCheckSum(rLsa, lsaLengthPos1, lsaLengthPos2, | ||
242 | + lsaChecksumPos1, lsaChecksumPos2); | ||
243 | + assertThat(result1[lsaLengthPos1], is(rLsa[lsaLengthPos1])); | ||
244 | + assertThat(result1[lsaLengthPos2], is(rLsa[lsaLengthPos2])); | ||
245 | + assertThat(result1[lsaChecksumPos1], is(rLsa[lsaChecksumPos1])); | ||
246 | + assertThat(result1[lsaChecksumPos2], is(rLsa[lsaChecksumPos2])); | ||
247 | + } | ||
248 | + | ||
249 | + /** | ||
250 | + * Tests addMetadata() method. | ||
251 | + */ | ||
252 | + @Test | ||
253 | + public void testAddMetaData() throws Exception { | ||
254 | + result1 = OspfUtil.addMetadata(packet, 1, Ip4Address.valueOf("2.2.2.2")); | ||
255 | + assertThat(result1, is(notNullValue())); | ||
256 | + } | ||
257 | + | ||
258 | + /** | ||
259 | + * Tests sameNetwork() method. | ||
260 | + */ | ||
261 | + @Test | ||
262 | + public void testSameNetwork() throws Exception { | ||
263 | + result2 = OspfUtil.sameNetwork(Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.10.10.11"), | ||
264 | + Ip4Address.valueOf("255.255.255.255")); | ||
265 | + assertThat(result2, is(false)); | ||
266 | + result2 = OspfUtil.sameNetwork(Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.10.10.10"), | ||
267 | + Ip4Address.valueOf("255.255.255.255")); | ||
268 | + assertThat(result2, is(true)); | ||
269 | + } | ||
270 | + | ||
271 | + /** | ||
272 | + * Tests isOpaqueEnabled() method. | ||
273 | + */ | ||
274 | + @Test | ||
275 | + public void testIsOpaqueEnabled() throws Exception { | ||
276 | + result2 = OspfUtil.isOpaqueEnabled(2); | ||
277 | + assertThat(result2, is(false)); | ||
278 | + } | ||
279 | + | ||
280 | + /** | ||
281 | + * Tests sameNetwork() method. | ||
282 | + */ | ||
283 | + @Test | ||
284 | + public void testisIsOpaqueEnabled() throws Exception { | ||
285 | + result2 = OspfUtil.isOpaqueEnabled(2); | ||
286 | + assertThat(result2, is(false)); | ||
287 | + } | ||
288 | + | ||
289 | + /** | ||
290 | + * Tests readLsaHeader() method. | ||
291 | + */ | ||
292 | + @Test | ||
293 | + public void testReadLsaHeader() throws Exception { | ||
294 | + byte[] header = {0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7, -128, 0, 0, 2, 46, -126, 0, | ||
295 | + 48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1, 0, 0, 10, 10, 10, 10, 0, -1, -1, -1, | ||
296 | + 0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0, 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0, | ||
297 | + 116, 0, 1, 0, 4, 0, 0, 0, 0, 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10, | ||
298 | + 10, 10, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73 | ||
299 | + , -104, -106, -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, | ||
300 | + -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, | ||
301 | + 73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0}; | ||
302 | + channelBuffer = ChannelBuffers.copiedBuffer(header); | ||
303 | + lsaHeader = OspfUtil.readLsaHeader(channelBuffer); | ||
304 | + assertThat(lsaHeader, is(notNullValue())); | ||
305 | + } | ||
306 | + | ||
307 | + /** | ||
308 | + * Tests readLsaHeader() method. | ||
309 | + */ | ||
310 | + @Test | ||
311 | + public void testReadreadLsaHeader() throws Exception { | ||
312 | + byte[] header = {0, 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58, -100, 0, 48}; | ||
313 | + channelBuffer = ChannelBuffers.copiedBuffer(header); | ||
314 | + lsaHeader = OspfUtil.readLsaHeader(channelBuffer); | ||
315 | + assertThat(lsaHeader, is(notNullValue())); | ||
316 | + } | ||
317 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment