Dhruv Dhody
Committed by Gerrit Code Review

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

Change-Id: Ifaaa4d3640c6a4fec4931db54a1f03100cc9d9e6
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 Sub Types.
19 + */
20 +package org.onosproject.ospf.protocol.lsa.subtypes;
...\ 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.lsa.types;
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.jboss.netty.buffer.ChannelBuffer;
22 +import org.onosproject.ospf.controller.OspfLsaType;
23 +import org.onosproject.ospf.exceptions.OspfErrorType;
24 +import org.onosproject.ospf.exceptions.OspfParseException;
25 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.TlvHeader;
27 +import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
28 +import org.onosproject.ospf.protocol.lsa.tlvtypes.OpaqueTopLevelTlvTypes;
29 +import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
30 +import org.onosproject.ospf.protocol.util.OspfParameters;
31 +
32 +import java.util.ArrayList;
33 +import java.util.List;
34 +
35 +/**
36 + * Representation of an Opaque LSA of type area local (10).
37 + */
38 +public class OpaqueLsa10 extends OpaqueLsaHeader {
39 + /*
40 + 0 1 2 3
41 + 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
42 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 + | LS age | Options | 9, 10 or 11 |
44 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 + | Opaque Type | Opaque ID |
46 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 + | Advertising Router |
48 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 + | LS Sequence Number |
50 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 + | LS checksum | Length |
52 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 + | |
54 + + +
55 + | Opaque Information |
56 + + +
57 + | ... |
58 +
59 + Opaque LSA format
60 + REFERENCE : RFC 5250
61 + */
62 + private List<TopLevelTlv> topLevelValues = new ArrayList<>();
63 + private byte[] opaqueInfo = null;
64 +
65 + /**
66 + * Creates an instance of Opaque type 10 LSA.
67 + *
68 + * @param lsaHeader LSA header instance
69 + */
70 + public OpaqueLsa10(OpaqueLsaHeader lsaHeader) {
71 + populateHeader(lsaHeader);
72 + }
73 +
74 + /**
75 + * Returns the list of top level TLVs.
76 + *
77 + * @return list of top level TLVs
78 + */
79 + public List<TopLevelTlv> topLevelValues() {
80 + return topLevelValues;
81 + }
82 +
83 + /**
84 + * Adds TLV value.
85 + *
86 + * @param value TLV value
87 + */
88 + public void addValue(TopLevelTlv value) {
89 + topLevelValues.add(value);
90 + }
91 +
92 + /**
93 + * Reads from channel buffer and populate instance.
94 + *
95 + * @param channelBuffer channelBuffer instance
96 + * @throws OspfParseException might throws exception while parsing buffer
97 + */
98 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
99 +
100 + try {
101 + if (this.opaqueId() == OspfParameters.TRAFFIC_ENGINEERING) {
102 + while (channelBuffer.readableBytes() > 0) {
103 + TlvHeader tlvHeader = new TlvHeader();
104 + tlvHeader.setTlvType(channelBuffer.readUnsignedShort());
105 + tlvHeader.setTlvLength(channelBuffer.readUnsignedShort());
106 + if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.ROUTER.value()) {
107 + RouterTlv routerTlv = new RouterTlv(tlvHeader);
108 + routerTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
109 + this.addValue(routerTlv);
110 + } else if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.LINK.value()) {
111 + LinkTlv linkTlv = new LinkTlv(tlvHeader);
112 + linkTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
113 + this.addValue(linkTlv);
114 + }
115 + }
116 + } else {
117 + int length = channelBuffer.readableBytes();
118 + opaqueInfo = new byte[length];
119 + channelBuffer.readBytes(opaqueInfo, 0, length);
120 + }
121 +
122 + } catch (Exception e) {
123 + log.debug("Error::OpaqueLsa10:: {}", e.getMessage());
124 + throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
125 + OspfErrorType.BAD_MESSAGE);
126 + }
127 + }
128 +
129 + /**
130 + * Returns instance as bytes.
131 + *
132 + * @return instance as bytes
133 + * @throws Exception might throws exception while parsing packet
134 + */
135 + public byte[] asBytes() throws Exception {
136 +
137 + byte[] lsaMessage = null;
138 + byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray();
139 + byte[] lsaBody = getLsaBodyAsByteArray();
140 + lsaMessage = Bytes.concat(lsaHeader, lsaBody);
141 + return lsaMessage;
142 +
143 + }
144 +
145 + /**
146 + * Gets the LSA body as byte array.
147 + *
148 + * @return the lsa body as byte array
149 + * @throws Exception might throws exception while parsing packet
150 + */
151 + public byte[] getLsaBodyAsByteArray() throws Exception {
152 + List<Byte> bodyLst = new ArrayList<>();
153 + if (this.opaqueId() == 1) {
154 + for (TopLevelTlv tlv : this.topLevelValues) {
155 + //Check the sub type of lsa and build bytes accordingly
156 + if (tlv instanceof RouterTlv) {
157 + RouterTlv routerTlv = (RouterTlv) tlv;
158 + bodyLst.addAll(Bytes.asList(routerTlv.asBytes()));
159 + } else if (tlv instanceof LinkTlv) {
160 + LinkTlv linkTlv = (LinkTlv) tlv;
161 + bodyLst.addAll(Bytes.asList(linkTlv.asBytes()));
162 + }
163 + }
164 + } else {
165 + return opaqueInfo;
166 + }
167 +
168 + return Bytes.toArray(bodyLst);
169 + }
170 +
171 + @Override
172 + public OspfLsaType getOspfLsaType() {
173 + return OspfLsaType.AREA_LOCAL_OPAQUE_LSA;
174 + }
175 +
176 + @Override
177 + public String toString() {
178 + return MoreObjects.toStringHelper(getClass())
179 + .omitNullValues()
180 + .add("topLevelValues", topLevelValues)
181 + .add("opaqueInfo", opaqueInfo)
182 + .toString();
183 + }
184 +
185 + @Override
186 + public boolean equals(Object o) {
187 + if (this == o) {
188 + return true;
189 + }
190 + if (o == null || getClass() != o.getClass()) {
191 + return false;
192 + }
193 + OpaqueLsa10 that = (OpaqueLsa10) o;
194 + return Objects.equal(topLevelValues, that.topLevelValues) &&
195 + Objects.equal(opaqueInfo, that.opaqueInfo);
196 + }
197 +
198 + @Override
199 + public int hashCode() {
200 + return Objects.hashCode(opaqueInfo, topLevelValues);
201 + }
202 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ospf.protocol.lsa.types;
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.jboss.netty.buffer.ChannelBuffer;
22 +import org.onosproject.ospf.exceptions.OspfErrorType;
23 +import org.onosproject.ospf.exceptions.OspfParseException;
24 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
25 +import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
26 +import org.onosproject.ospf.protocol.util.OspfUtil;
27 +import org.slf4j.Logger;
28 +import org.slf4j.LoggerFactory;
29 +
30 +import java.net.InetAddress;
31 +import java.util.ArrayList;
32 +import java.util.List;
33 +
34 +/**
35 + * Representation of a Router LSA, and the fields and methods to access them.
36 + */
37 +public class RouterLsa extends LsaHeader {
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 | 1 |
43 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 + | Link State ID |
45 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 + | Advertising Router |
47 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 + | LS sequence number |
49 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 + | LS checksum | length |
51 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 + | 0 |V|E|B| 0 | # links |
53 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 + | Link ID |
55 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 + | Link Data |
57 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 + | Type | # TOS | metric |
59 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 + | ... |
61 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 + | TOS | 0 | TOS metric |
63 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 + | Link ID |
65 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 + | Link Data |
67 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 + | ... |
69 + */
70 + private static final Logger log =
71 + LoggerFactory.getLogger(RouterLsa.class);
72 + private boolean isVirtualEndPoint;
73 + private boolean isAsBoundaryRouter;
74 + private boolean isAreaBorderRouter;
75 + private int noLink;
76 + private List<OspfLsaLink> routerLinks = new ArrayList<>();
77 +
78 + /**
79 + * Creates an instance of Router LSA.
80 + */
81 + public RouterLsa() {
82 + }
83 +
84 + /**
85 + * Creates an instance of Router LSA.
86 + *
87 + * @param lsaHeader lsa header instance
88 + */
89 + public RouterLsa(LsaHeader lsaHeader) {
90 + populateHeader(lsaHeader);
91 + }
92 +
93 + /**
94 + * Sets virtual endpoint or not.
95 + *
96 + * @param isVirtualEndPoint true or false
97 + */
98 + public void setVirtualEndPoint(boolean isVirtualEndPoint) {
99 + this.isVirtualEndPoint = isVirtualEndPoint;
100 + }
101 +
102 + /**
103 + * Sets if it is an AS boundary router or not.
104 + *
105 + * @param isAsBoundaryRouter true if AS boundary router else false
106 + */
107 + public void setAsBoundaryRouter(boolean isAsBoundaryRouter) {
108 + this.isAsBoundaryRouter = isAsBoundaryRouter;
109 + }
110 +
111 + /**
112 + * Sets whether it is an ABR or not.
113 + *
114 + * @param isAreaBorderRouter true if ABR else false
115 + */
116 + public void setAreaBorderRouter(boolean isAreaBorderRouter) {
117 + this.isAreaBorderRouter = isAreaBorderRouter;
118 + }
119 +
120 + /**
121 + * Gets number of links.
122 + *
123 + * @return number of links
124 + */
125 + public int noLink() {
126 + return noLink;
127 + }
128 +
129 + /**
130 + * Sets number of links.
131 + *
132 + * @param noLink number of links
133 + */
134 + public void setNoLink(int noLink) {
135 + this.noLink = noLink;
136 + }
137 +
138 +
139 + /**
140 + * Adds router link.
141 + *
142 + * @param lsaLink LSA link
143 + */
144 + public void addRouterLink(OspfLsaLink lsaLink) {
145 + if (!this.routerLinks.contains(lsaLink)) {
146 + this.routerLinks.add(lsaLink);
147 + }
148 + }
149 +
150 + /**
151 + * Gets router link.
152 + *
153 + * @return routerLinks LSA link list
154 + */
155 + public List<OspfLsaLink> routerLink() {
156 + return this.routerLinks;
157 + }
158 +
159 + /**
160 + * Reads from channel buffer and populate this.
161 + *
162 + * @param channelBuffer channelBuffer instance.
163 + * @throws OspfParseException might throws exception while parsing buffer
164 + */
165 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
166 +
167 + try {
168 + int veb = channelBuffer.readByte();
169 + int unUsed = channelBuffer.readByte();
170 + //Convert the byte to veb bits
171 + String strVeb = Integer.toBinaryString(veb);
172 + if (strVeb.length() == 3) {
173 + this.setVirtualEndPoint((Integer.parseInt(Character.toString(strVeb.charAt(0))) == 1) ? true : false);
174 + this.setAsBoundaryRouter((Integer.parseInt(Character.toString(strVeb.charAt(1))) == 1) ? true : false);
175 + this.setAreaBorderRouter((Integer.parseInt(Character.toString(strVeb.charAt(2))) == 1) ? true : false);
176 + } else if (strVeb.length() == 2) {
177 + this.setVirtualEndPoint(false);
178 + this.setAsBoundaryRouter((Integer.parseInt(Character.toString(strVeb.charAt(0))) == 1) ? true : false);
179 + this.setAreaBorderRouter((Integer.parseInt(Character.toString(strVeb.charAt(1))) == 1) ? true : false);
180 + } else if (strVeb.length() == 1) {
181 + this.setVirtualEndPoint(false);
182 + this.setAsBoundaryRouter(false);
183 + this.setAreaBorderRouter((Integer.parseInt(Character.toString(strVeb.charAt(0))) == 1) ? true : false);
184 + }
185 + this.setNoLink(channelBuffer.readUnsignedShort());
186 + while (channelBuffer.readableBytes() >= OspfUtil.TWELVE_BYTES) {
187 + OspfLsaLink ospfLsaLink = new OspfLsaLink();
188 +
189 + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
190 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
191 + ospfLsaLink.setLinkId(InetAddress.getByAddress(tempByteArray).getHostName());
192 + tempByteArray = new byte[OspfUtil.FOUR_BYTES];
193 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
194 + ospfLsaLink.setLinkData(InetAddress.getByAddress(tempByteArray).getHostName());
195 + ospfLsaLink.setLinkType(channelBuffer.readByte());
196 + ospfLsaLink.setTos(channelBuffer.readByte());
197 + ospfLsaLink.setMetric(channelBuffer.readUnsignedShort());
198 + //add the link
199 + this.addRouterLink(ospfLsaLink);
200 + }
201 + } catch (Exception e) {
202 + log.debug("Error::RouterLsa:: {}", e.getMessage());
203 + throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
204 + }
205 + }
206 +
207 + /**
208 + * Returns instance as bytes.
209 + *
210 + * @return instance as bytes
211 + * @throws OspfParseException might throws exception while parsing packet
212 + */
213 + public byte[] asBytes() throws OspfParseException {
214 + byte[] lsaMessage = null;
215 +
216 + byte[] lsaHeader = getLsaHeaderAsByteArray();
217 + byte[] lsaBody = getLsaBodyAsByteArray();
218 + lsaMessage = Bytes.concat(lsaHeader, lsaBody);
219 +
220 + return lsaMessage;
221 + }
222 +
223 + /**
224 + * Gets the LSA body as bytes.
225 + *
226 + * @return LSA body as bytes
227 + */
228 + public byte[] getLsaBodyAsByteArray() {
229 + List<Byte> bodyLst = new ArrayList<>();
230 +
231 + try {
232 + int isVirtualEndPointVal = this.isVirtualEndPoint ? 1 : 0;
233 + int isASBoundaryRouterVal = this.isAsBoundaryRouter ? 1 : 0;
234 + int isAreaBorderRouterVal = this.isAreaBorderRouter ? 1 : 0;
235 +
236 + StringBuilder sb = new StringBuilder();
237 + sb.append(Integer.toBinaryString(isVirtualEndPointVal));
238 + sb.append(Integer.toBinaryString(isASBoundaryRouterVal));
239 + sb.append(Integer.toBinaryString(isAreaBorderRouterVal));
240 +
241 + //added VEB
242 + bodyLst.add((byte) Integer.parseInt(sb.toString(), 2));
243 + //second byte is 0.
244 + bodyLst.add((byte) 0);
245 + //Number of links
246 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.noLink())));
247 +
248 + //add each link details
249 + for (OspfLsaLink lsaLink : routerLinks) {
250 + bodyLst.addAll(Bytes.asList(InetAddress.getByName(lsaLink.linkId()).getAddress()));
251 + bodyLst.addAll(Bytes.asList(InetAddress.getByName(lsaLink.linkData()).getAddress()));
252 + bodyLst.add((byte) lsaLink.linkType());
253 + bodyLst.add((byte) lsaLink.tos());
254 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(lsaLink.metric())));
255 + }
256 + } catch (Exception e) {
257 + log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage());
258 + return Bytes.toArray(bodyLst);
259 + }
260 +
261 + return Bytes.toArray(bodyLst);
262 + }
263 +
264 + /**
265 + * Increment the link by 1.
266 + */
267 + public void incrementLinkNo() {
268 + this.noLink++;
269 + }
270 +
271 + @Override
272 + public String toString() {
273 + return MoreObjects.toStringHelper(getClass())
274 + .omitNullValues()
275 + .add("isVirtualEndPoint", isVirtualEndPoint)
276 + .add("isAsBoundaryRouter", isAsBoundaryRouter)
277 + .add("isAreaBorderRouter", isAreaBorderRouter)
278 + .add("noLink", noLink)
279 + .add("routerLinks", routerLinks)
280 + .toString();
281 + }
282 +
283 + @Override
284 + public boolean equals(Object o) {
285 + if (this == o) {
286 + return true;
287 + }
288 + if (o == null || getClass() != o.getClass()) {
289 + return false;
290 + }
291 + RouterLsa that = (RouterLsa) o;
292 + return Objects.equal(isVirtualEndPoint, that.isVirtualEndPoint) &&
293 + Objects.equal(isAsBoundaryRouter, that.isAsBoundaryRouter) &&
294 + Objects.equal(isAreaBorderRouter, that.isAreaBorderRouter) &&
295 + Objects.equal(noLink, that.noLink) &&
296 + Objects.equal(routerLinks, that.routerLinks);
297 + }
298 +
299 + @Override
300 + public int hashCode() {
301 + return Objects.hashCode(isVirtualEndPoint, isAsBoundaryRouter, isAreaBorderRouter,
302 + noLink, routerLinks);
303 + }
304 +}
...\ 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 LSA types.
19 + */
20 +package org.onosproject.ospf.protocol.lsa.types;
...\ 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.ospfpacket;
18 +
19 +import org.jboss.netty.buffer.ChannelBuffer;
20 +import org.onlab.packet.Ip4Address;
21 +import org.onosproject.ospf.exceptions.OspfErrorType;
22 +import org.onosproject.ospf.exceptions.OspfParseException;
23 +import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
24 +import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
25 +import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
26 +import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
27 +import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
28 +import org.onosproject.ospf.protocol.util.OspfParameters;
29 +import org.onosproject.ospf.protocol.util.OspfUtil;
30 +import org.slf4j.Logger;
31 +import org.slf4j.LoggerFactory;
32 +
33 +/**
34 + * A message reader which reads OSPF messages from ChannelBuffer and converts to OspfMessage instances.
35 + */
36 +public class OspfMessageReader {
37 + private static final Logger log = LoggerFactory.getLogger(OspfMessageReader.class);
38 +
39 + /**
40 + * Reads and Converts the channel buffer to OspfMessage instance.
41 + *
42 + * @param channelBuffer channel buffer instance.
43 + * @return OSPF message instance.
44 + * @throws Exception might throws exception while parsing buffer
45 + */
46 + public OspfMessage readFromBuffer(ChannelBuffer channelBuffer)
47 + throws Exception {
48 +
49 + if (channelBuffer.readableBytes() < OspfUtil.PACKET_MINIMUM_LENGTH) {
50 + log.error("Packet should have minimum length...");
51 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
52 + }
53 +
54 + try {
55 + OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer);
56 + int len = ospfHeader.ospfPacLength() - OspfUtil.OSPF_HEADER_LENGTH;
57 +
58 + OspfMessage ospfMessage = null;
59 + switch (ospfHeader.ospfType()) {
60 + case OspfParameters.HELLO:
61 + ospfMessage = new HelloPacket(ospfHeader);
62 + break;
63 + case OspfParameters.DD:
64 + ospfMessage = new DdPacket(ospfHeader);
65 + break;
66 + case OspfParameters.LSREQUEST:
67 + ospfMessage = new LsRequest(ospfHeader);
68 + break;
69 + case OspfParameters.LSUPDATE:
70 + ospfMessage = new LsUpdate(ospfHeader);
71 + break;
72 + case OspfParameters.LSACK:
73 + ospfMessage = new LsAcknowledge(ospfHeader);
74 + break;
75 + default:
76 + log.debug("Message Reader[Decoder] - Unknown LSA type..!!!");
77 + break;
78 + }
79 +
80 + if (ospfMessage != null) {
81 + try {
82 + log.debug("{} Received::Message Length :: {} ", ospfMessage.ospfMessageType(),
83 + ospfHeader.ospfPacLength());
84 + ospfMessage.readFrom(channelBuffer.readBytes(len));
85 + } catch (Exception e) {
86 + throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
87 + OspfErrorType.BAD_MESSAGE);
88 + }
89 +
90 + }
91 +
92 + return ospfMessage;
93 + } catch (Exception e) {
94 + throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
95 + OspfErrorType.BAD_MESSAGE);
96 + }
97 + }
98 +
99 + /**
100 + * Gets the OSPF packet Header.
101 + *
102 + * @param channelBuffer channel buffer instance.
103 + * @return Ospf Header instance.
104 + */
105 + private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception {
106 + OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();
107 +
108 + byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
109 + channelBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
110 + Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
111 +
112 + // Determine ospf version & Packet Type
113 + int version = channelBuffer.readByte(); //byte 1 is ospf version
114 + int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type
115 +
116 + // byte 3 & 4 combine is packet length.
117 + int packetLength = channelBuffer.readShort();
118 +
119 + if (packetLength > channelBuffer.readableBytes() + OspfUtil.FOUR_BYTES) {
120 + log.error("Packet should have minimum length...");
121 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
122 + }
123 +
124 + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
125 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
126 + Ip4Address routerId = Ip4Address.valueOf(tempByteArray);
127 +
128 + tempByteArray = new byte[OspfUtil.FOUR_BYTES];
129 + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
130 + Ip4Address areaId = Ip4Address.valueOf(tempByteArray);
131 +
132 + int checkSum = channelBuffer.readUnsignedShort();
133 + int auType = channelBuffer.readUnsignedShort();
134 + int authentication = (int) channelBuffer.readLong();
135 +
136 + ospfPacketHeader.setSourceIp(sourceIP);
137 + ospfPacketHeader.setOspfVer(version);
138 + ospfPacketHeader.setOspftype(packetType);
139 + ospfPacketHeader.setOspfPacLength(packetLength);
140 + ospfPacketHeader.setRouterId(routerId);
141 + ospfPacketHeader.setAreaId(areaId);
142 + ospfPacketHeader.setChecksum(checkSum);
143 + ospfPacketHeader.setAuthType(auType);
144 + ospfPacketHeader.setAuthentication(authentication);
145 +
146 + return ospfPacketHeader;
147 + }
148 +}
...\ 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.onosproject.ospf.protocol.util.OspfParameters;
21 +import org.onosproject.ospf.protocol.util.OspfUtil;
22 +import org.slf4j.Logger;
23 +import org.slf4j.LoggerFactory;
24 +
25 +/**
26 + * A message writer which writes an OspfMessage to ChannelBuffer.
27 + */
28 +public class OspfMessageWriter {
29 + private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class);
30 +
31 + /**
32 + * Writes OSPF message to ChannelBuffer.
33 + *
34 + * @param ospfMessage OSPF message
35 + * @param interfaceState interface state
36 + * @param interfaceType interface type
37 + * @return channelBuffer channel buffer instance
38 + * @throws Exception might throws exception while parsing message
39 + */
40 + public ChannelBuffer writeToBuffer(OspfMessage ospfMessage, int interfaceState,
41 + int interfaceType) throws Exception {
42 +
43 + ChannelBuffer buf = null;
44 + switch (ospfMessage.ospfMessageType().value()) {
45 + case OspfParameters.HELLO:
46 + case OspfParameters.LSACK:
47 + buf = writeMessageToBuffer(ospfMessage, interfaceState);
48 + break;
49 + case OspfParameters.DD:
50 + case OspfParameters.LSREQUEST:
51 + case OspfParameters.LSUPDATE:
52 + buf = writeMessageToBuffer(ospfMessage, interfaceState);
53 + break;
54 + default:
55 + log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!");
56 + break;
57 + }
58 +
59 + return buf;
60 + }
61 +
62 + /**
63 + * Writes an OSPF Message to channel buffer.
64 + *
65 + * @param ospfMessage OSPF Message instance
66 + * @param interfaceState interface state
67 + * @return channelBuffer instance
68 + */
69 + private ChannelBuffer writeMessageToBuffer(OspfMessage ospfMessage, int interfaceState) throws Exception {
70 + ChannelBuffer channelBuffer = null;
71 + byte[] ospfMessageAsByte = ospfMessage.asBytes();
72 + //Add the length and checksum in byte array at length position 2 & 3 and Checksum position
73 + ospfMessageAsByte = OspfUtil.addLengthAndCheckSum(ospfMessageAsByte, OspfUtil.OSPFPACKET_LENGTH_POS1,
74 + OspfUtil.OSPFPACKET_LENGTH_POS2,
75 + OspfUtil.OSPFPACKET_CHECKSUM_POS1,
76 + OspfUtil.OSPFPACKET_CHECKSUM_POS2);
77 + //Add Interface State Info and destination IP as metadata
78 + if (interfaceState == OspfParameters.DR || interfaceState == OspfParameters.BDR) {
79 + ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
80 + ospfMessage.destinationIp());
81 + } else {
82 + ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
83 + ospfMessage.destinationIp());
84 + }
85 +
86 + channelBuffer = ChannelBuffers.buffer(ospfMessageAsByte.length);
87 + channelBuffer.writeBytes(ospfMessageAsByte);
88 +
89 + return channelBuffer;
90 + }
91 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.ospf.controller.OspfLsa;
22 +import org.onosproject.ospf.exceptions.OspfErrorType;
23 +import org.onosproject.ospf.exceptions.OspfParseException;
24 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
25 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
27 +import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
28 +import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
29 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
30 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
31 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
32 +import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
33 +import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
34 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
35 +import org.onosproject.ospf.protocol.util.OspfPacketType;
36 +import org.onosproject.ospf.protocol.util.OspfParameters;
37 +import org.onosproject.ospf.protocol.util.OspfUtil;
38 +import org.slf4j.Logger;
39 +import org.slf4j.LoggerFactory;
40 +
41 +import java.util.ArrayList;
42 +import java.util.LinkedList;
43 +import java.util.List;
44 +
45 +/**
46 + * Representation of an OSPF Link State Update packet.
47 + * Link State Update packets are OSPF packet type 4. These packets
48 + * implement the flooding of LSAs. Each Link State Update packet
49 + * carries a collection of LSAs one hop further from their origin.
50 + * Several LSAs may be included in a single packet.
51 + */
52 +public class LsUpdate extends OspfPacketHeader {
53 + /*
54 + 0 1 2 3
55 + 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
56 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 + | Version # | 4 | Packet length |
58 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 + | Router ID |
60 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 + | Area ID |
62 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 + | Checksum | AuType |
64 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 + | Authentication |
66 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 + | Authentication |
68 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 + | # LSAs |
70 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 + | |
72 + +- +-+
73 + | LSAs |
74 + +- +-+
75 + | ... |
76 + */
77 + private static final Logger log = LoggerFactory.getLogger(LsUpdate.class);
78 + private int numberOfLsa;
79 + private List<OspfLsa> lsaList = new LinkedList<>();
80 +
81 + /**
82 + * Creates an instance of Link State Update packet.
83 + */
84 + public LsUpdate() {
85 + }
86 +
87 + /**
88 + * Creates an instance of Link State Update packet.
89 + *
90 + * @param ospfHeader ospf header instance.
91 + */
92 + public LsUpdate(OspfPacketHeader ospfHeader) {
93 + populateHeader(ospfHeader);
94 + }
95 +
96 + /**
97 + * Gets the LSA list.
98 + *
99 + * @return list of LSA
100 + */
101 + public List getLsaList() {
102 + return lsaList;
103 + }
104 +
105 + /**
106 + * Adds the LSA to list.
107 + *
108 + * @param lsa LSA
109 + */
110 + public void addLsa(OspfLsa lsa) {
111 + if (!lsaList.contains(lsa)) {
112 + lsaList.add(lsa);
113 + }
114 + }
115 +
116 + /**
117 + * Gets the number of LSA.
118 + *
119 + * @return number of LSA
120 + */
121 + public int noLsa() {
122 + return numberOfLsa;
123 + }
124 +
125 + /**
126 + * Sets number of LSA.
127 + *
128 + * @param numberOfLsa number of LSA
129 + */
130 + public void setNumberOfLsa(int numberOfLsa) {
131 + this.numberOfLsa = numberOfLsa;
132 + }
133 +
134 +
135 + @Override
136 + public OspfPacketType ospfMessageType() {
137 + return OspfPacketType.LSUPDATE;
138 + }
139 +
140 + @Override
141 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
142 + try {
143 + //From header 4 bytes is number of lsa's
144 + this.setNumberOfLsa(channelBuffer.readInt());
145 + //get the remaining bytes represents Number of LSA's present. Add all the LSA's
146 + while (channelBuffer.readableBytes() > OspfUtil.LSA_HEADER_LENGTH) {
147 +
148 + LsaHeader header = OspfUtil.readLsaHeader(channelBuffer.readBytes(OspfUtil.LSA_HEADER_LENGTH));
149 + int lsaLength = header.lsPacketLen();
150 + int lsType = header.lsType();
151 +
152 + switch (lsType) {
153 + case OspfParameters.LINK_LOCAL_OPAQUE_LSA:
154 + OpaqueLsa9 opaqueLsa9 = new OpaqueLsa9((OpaqueLsaHeader) header);
155 + opaqueLsa9.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
156 + addLsa(opaqueLsa9);
157 + break;
158 + case OspfParameters.AREA_LOCAL_OPAQUE_LSA:
159 + OpaqueLsa10 opaqueLsa10 = new OpaqueLsa10((OpaqueLsaHeader) header);
160 + opaqueLsa10.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
161 + addLsa(opaqueLsa10);
162 + break;
163 + case OspfParameters.AS_OPAQUE_LSA:
164 + OpaqueLsa11 opaqueLsa11 = new OpaqueLsa11((OpaqueLsaHeader) header);
165 + opaqueLsa11.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
166 + addLsa(opaqueLsa11);
167 + break;
168 + case OspfParameters.ROUTER:
169 + RouterLsa routerLsa = new RouterLsa(header);
170 + routerLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
171 + addLsa(routerLsa);
172 + break;
173 + case OspfParameters.NETWORK:
174 + NetworkLsa networkLsa = new NetworkLsa(header);
175 + networkLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
176 + addLsa(networkLsa);
177 + break;
178 + case OspfParameters.ASBR_SUMMARY:
179 + AsbrSummaryLsa asbrSummaryLsa = new AsbrSummaryLsa(header);
180 + asbrSummaryLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
181 + addLsa(asbrSummaryLsa);
182 + break;
183 + case OspfParameters.SUMMARY:
184 + SummaryLsa summaryLSA = new SummaryLsa(header);
185 + summaryLSA.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
186 + addLsa(summaryLSA);
187 + break;
188 + case OspfParameters.EXTERNAL_LSA:
189 + ExternalLsa externalLsa = new ExternalLsa(header);
190 + externalLsa.readFrom(channelBuffer.readBytes(lsaLength - OspfUtil.LSA_HEADER_LENGTH));
191 + addLsa(externalLsa);
192 + break;
193 + default:
194 + log.debug("LSUpdate::readLsUpdateBody::UnKnown LS Type: {}", lsType);
195 + break;
196 + }
197 + }
198 + } catch (Exception e) {
199 + log.debug("Error::LsUpdate:: {}", e.getMessage());
200 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
201 + }
202 + }
203 +
204 + @Override
205 + public byte[] asBytes() {
206 + byte[] lsuMessage = null;
207 +
208 + byte[] ospfHeader = getLsuHeaderAsByteArray();
209 + byte[] lsuBody = getLsuBodyAsByteArray();
210 + lsuMessage = Bytes.concat(ospfHeader, lsuBody);
211 +
212 + return lsuMessage;
213 + }
214 +
215 + /**
216 + * Gets lsu header.
217 + *
218 + * @return lsu header as byte array
219 + */
220 + public byte[] getLsuHeaderAsByteArray() {
221 + List<Byte> headerLst = new ArrayList<>();
222 + try {
223 + headerLst.add((byte) this.ospfVersion());
224 + headerLst.add((byte) this.ospfType());
225 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
226 + headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
227 + headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
228 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
229 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
230 + //Authentication is 0 always. Total 8 bytes consist of zero
231 + byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
232 + headerLst.addAll(Bytes.asList(auth));
233 + } catch (Exception e) {
234 + log.debug("Error::LSUpdate::getLsuHeaderAsByteArray:: {}", e.getMessage());
235 + return Bytes.toArray(headerLst);
236 + }
237 +
238 + return Bytes.toArray(headerLst);
239 + }
240 +
241 + /**
242 + * Get lsu body as byte array.
243 + *
244 + * @return lsu body as byte array
245 + */
246 + public byte[] getLsuBodyAsByteArray() {
247 + List<Byte> bodyLst = new ArrayList<>();
248 +
249 + try {
250 + //add number of LSA's
251 + bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.noLsa())));
252 + //for each type of LSA's from the list get lsa bytes
253 + for (OspfLsa ospfLsa : lsaList) {
254 + //Check the type of lsa and build bytes accordingly
255 + switch (ospfLsa.getOspfLsaType().value()) {
256 + case OspfParameters.LINK_LOCAL_OPAQUE_LSA:
257 + OpaqueLsa9 opaqueLsa9 = (OpaqueLsa9) ospfLsa;
258 + bodyLst.addAll(Bytes.asList(opaqueLsa9.asBytes()));
259 + break;
260 + case OspfParameters.AREA_LOCAL_OPAQUE_LSA:
261 + OpaqueLsa10 opaqueLsa10 = (OpaqueLsa10) ospfLsa;
262 + bodyLst.addAll(Bytes.asList(opaqueLsa10.asBytes()));
263 + break;
264 + case OspfParameters.AS_OPAQUE_LSA:
265 + OpaqueLsa11 opaqueLsa11 = (OpaqueLsa11) ospfLsa;
266 + bodyLst.addAll(Bytes.asList(opaqueLsa11.asBytes()));
267 + break;
268 + case OspfParameters.ROUTER:
269 + RouterLsa routerLsa = (RouterLsa) ospfLsa;
270 + bodyLst.addAll(Bytes.asList(routerLsa.asBytes()));
271 + break;
272 + case OspfParameters.NETWORK:
273 + NetworkLsa networkLsa = (NetworkLsa) ospfLsa;
274 + bodyLst.addAll(Bytes.asList(networkLsa.asBytes()));
275 + break;
276 + case OspfParameters.ASBR_SUMMARY:
277 + AsbrSummaryLsa asbrSummaryLsa = (AsbrSummaryLsa) ospfLsa;
278 + bodyLst.addAll(Bytes.asList(asbrSummaryLsa.asBytes()));
279 + break;
280 + case OspfParameters.SUMMARY:
281 + SummaryLsa summaryLsa = (SummaryLsa) ospfLsa;
282 + bodyLst.addAll(Bytes.asList(summaryLsa.asBytes()));
283 + break;
284 + case OspfParameters.EXTERNAL_LSA:
285 + ExternalLsa externalLsa = (ExternalLsa) ospfLsa;
286 + bodyLst.addAll(Bytes.asList(externalLsa.asBytes()));
287 + break;
288 + default:
289 + log.debug("LSUpdate::getLsuBodyAsByteArray::UnKnown ospfLsa: {}", ospfLsa);
290 + break;
291 + }
292 + }
293 +
294 + } catch (Exception e) {
295 + log.debug("Error::getLsuBodyAsByteArray {}", e.getMessage());
296 + return Bytes.toArray(bodyLst);
297 + }
298 +
299 + return Bytes.toArray(bodyLst);
300 + }
301 +
302 + @Override
303 + public String toString() {
304 + return MoreObjects.toStringHelper(getClass())
305 + .omitNullValues()
306 + .add("noLsa", numberOfLsa)
307 + .add("lsaList", lsaList)
308 + .toString();
309 + }
310 +}
...\ 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 org.onosproject.ospf.controller.OspfLsa;
19 +import org.onosproject.ospf.controller.OspfLsaType;
20 +import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
21 +import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
22 +import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
23 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
24 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
25 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
26 +import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
27 +import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
28 +import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
29 +import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
30 +import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
31 +import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
32 +import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
33 +import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
34 +
35 +import java.util.Arrays;
36 +
37 +/**
38 + * Calculates checksum for different types of OSPF packets.
39 + */
40 +public class ChecksumCalculator {
41 +
42 + /**
43 + * Converts given string to sixteen bits integer.
44 + * If hexasum is more than 16 bit value, needs to be reduced to 16 bit value.
45 + *
46 + * @param strToConvert hexasum value to convert
47 + * @return 16 bit integer value
48 + */
49 + public static int convertToSixteenBits(String strToConvert) {
50 + StringBuilder sb = new StringBuilder(strToConvert);
51 + sb = sb.reverse();
52 + StringBuilder s1 = new StringBuilder(sb.substring(0, 4));
53 + s1 = s1.reverse();
54 + StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length()));
55 + s2 = s2.reverse();
56 + int num = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16);
57 + return num;
58 + }
59 +
60 + /**
61 + * Checks whether checksum is valid or not in the given OSPF message.
62 + *
63 + * @param ospfMessage ospf message instance
64 + * @param checksumPos1 position of checksum bit in packet
65 + * @param checksumPos2 position of checksum bit in packet
66 + * @return true if valid else false
67 + */
68 + public boolean isValidOspfCheckSum(OspfMessage ospfMessage, int checksumPos1, int checksumPos2) {
69 +
70 + switch (ospfMessage.ospfMessageType().value()) {
71 + case OspfParameters.HELLO:
72 + ospfMessage = (HelloPacket) ospfMessage;
73 + break;
74 + case OspfParameters.DD:
75 + ospfMessage = (DdPacket) ospfMessage;
76 + break;
77 + case OspfParameters.LSREQUEST:
78 + ospfMessage = (LsRequest) ospfMessage;
79 + break;
80 + case OspfParameters.LSUPDATE:
81 + ospfMessage = (LsUpdate) ospfMessage;
82 + break;
83 + case OspfParameters.LSACK:
84 + ospfMessage = (LsAcknowledge) ospfMessage;
85 + break;
86 + default:
87 + break;
88 + }
89 +
90 + byte[] messageAsBytes = ospfMessage.asBytes();
91 + return validateOspfCheckSum(messageAsBytes, checksumPos1, checksumPos2);
92 + }
93 +
94 + /**
95 + * Checks whether checksum is valid or not in the given OSPF LSA.
96 + *
97 + * @param ospfLsa lsa instance
98 + * @param lsType lsa type
99 + * @param lsaChecksumPos1 lsa checksum position in packet
100 + * @param lsaChecksumPos2 lsa checksum position in packet
101 + * @return true if valid else false
102 + * @throws Exception might throw exception while processing
103 + */
104 + public boolean isValidLsaCheckSum(OspfLsa ospfLsa, int lsType, int lsaChecksumPos1,
105 + int lsaChecksumPos2) throws Exception {
106 + if (lsType == OspfLsaType.ROUTER.value()) {
107 + RouterLsa lsa = (RouterLsa) ospfLsa;
108 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
109 + } else if (lsType == OspfLsaType.NETWORK.value()) {
110 + NetworkLsa lsa = (NetworkLsa) ospfLsa;
111 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
112 + } else if (lsType == OspfLsaType.SUMMARY.value()) {
113 + SummaryLsa lsa = (SummaryLsa) ospfLsa;
114 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
115 + } else if (lsType == OspfLsaType.ASBR_SUMMARY.value()) {
116 + AsbrSummaryLsa lsa = (AsbrSummaryLsa) ospfLsa;
117 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
118 + } else if (lsType == OspfLsaType.EXTERNAL_LSA.value()) {
119 + ExternalLsa lsa = (ExternalLsa) ospfLsa;
120 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
121 + } else if (lsType == OspfLsaType.LINK_LOCAL_OPAQUE_LSA.value()) {
122 + OpaqueLsa9 lsa = (OpaqueLsa9) ospfLsa;
123 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
124 + } else if (lsType == OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()) {
125 + OpaqueLsa10 lsa = (OpaqueLsa10) ospfLsa;
126 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
127 + } else if (lsType == OspfLsaType.AS_OPAQUE_LSA.value()) {
128 + OpaqueLsa11 lsa = (OpaqueLsa11) ospfLsa;
129 + return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
130 + }
131 +
132 + return false;
133 + }
134 +
135 + /**
136 + * Verifies the checksum is valid in given LSA packet bytes.
137 + *
138 + * @param lsaPacket lsa as byte array
139 + * @param lsaChecksumPos1 position of checksum bit in packet
140 + * @param lsaChecksumPos2 position of checksum bit in packet
141 + * @return true if valid else false
142 + */
143 + public boolean validateLsaCheckSum(byte[] lsaPacket, int lsaChecksumPos1, int lsaChecksumPos2) {
144 +
145 + byte[] checksum = calculateLsaChecksum(lsaPacket, lsaChecksumPos1, lsaChecksumPos2);
146 +
147 + if (lsaPacket[lsaChecksumPos1] == checksum[0] && lsaPacket[lsaChecksumPos2] == checksum[1]) {
148 + return true;
149 + }
150 +
151 + return false;
152 + }
153 +
154 + /**
155 + * Verifies the checksum is valid in given OSPF packet bytes.
156 + *
157 + * @param ospfPacket as byte array
158 + * @param checksumPos1 position of checksum bit in packet
159 + * @param checksumPos2 position of checksum bit in packet
160 + * @return true if valid else false
161 + */
162 + public boolean validateOspfCheckSum(byte[] ospfPacket, int checksumPos1, int checksumPos2) {
163 +
164 + byte[] checkSum = calculateOspfCheckSum(ospfPacket, checksumPos1, checksumPos2);
165 +
166 + if (ospfPacket[checksumPos1] == checkSum[0] && ospfPacket[checksumPos2] == checkSum[1]) {
167 + return true;
168 + }
169 +
170 + return false;
171 + }
172 +
173 + /**
174 + * Calculates the LSA checksum.
175 + *
176 + * @param lsaBytes as byte array
177 + * @param lsaChecksumPos1 position of checksum bit in packet
178 + * @param lsaChecksumPos2 position of checksum bit in packet
179 + * @return checksum bytes
180 + */
181 + public byte[] calculateLsaChecksum(byte[] lsaBytes, int lsaChecksumPos1, int lsaChecksumPos2) {
182 +
183 + byte[] tempLsaByte = Arrays.copyOf(lsaBytes, lsaBytes.length);
184 +
185 + int[] checksumOut = {0, 0};
186 + tempLsaByte[lsaChecksumPos1] = 0;
187 + tempLsaByte[lsaChecksumPos2] = 0;
188 + byte[] byteCheckSum = {0, 0};
189 + if (lsaBytes != null) {
190 + for (int i = 2; i < tempLsaByte.length; i++) {
191 + checksumOut[0] = checksumOut[0] + ((int) tempLsaByte[i] & 0xFF);
192 + checksumOut[1] = checksumOut[1] + checksumOut[0];
193 + }
194 + checksumOut[0] = checksumOut[0] % 255;
195 + checksumOut[1] = checksumOut[1] % 255;
196 + }
197 + int byte1 = (int) ((tempLsaByte.length - lsaChecksumPos1 - 1) * checksumOut[0] - checksumOut[1]) % 255;
198 + if (byte1 <= 0) {
199 + byte1 += 255;
200 + }
201 + int byte2 = 510 - checksumOut[0] - byte1;
202 + if (byte2 > 255) {
203 + byte2 -= 255;
204 + }
205 +
206 + byteCheckSum[0] = (byte) byte1;
207 + byteCheckSum[1] = (byte) byte2;
208 +
209 + return byteCheckSum;
210 + }
211 +
212 + /**
213 + * Calculate checksum from hexasum.
214 + *
215 + * @param hexasum total of 16 bits hexadecimal values
216 + * @return checksum value
217 + */
218 + private int calculateChecksum(int hexasum) {
219 +
220 + char[] tempZeros = {'0', '0', '0', '0'};
221 + StringBuffer hexaAsBinaryStr = new StringBuffer(Integer.toBinaryString(hexasum));
222 + int length = hexaAsBinaryStr.length();
223 + while (length > 16) {
224 + if (hexaAsBinaryStr.length() % 4 != 0) {
225 + int offset = hexaAsBinaryStr.length() % 4;
226 + hexaAsBinaryStr.insert(0, tempZeros, 0, 4 - offset);
227 + }
228 + StringBuffer hexaStr1 = new StringBuffer(hexaAsBinaryStr.reverse().substring(0, 16));
229 + String revHexaStr1 = hexaStr1.reverse().toString();
230 + StringBuffer hexaStr2 = new StringBuffer(hexaAsBinaryStr.reverse());
231 + StringBuffer hexaStr3 = new StringBuffer(hexaStr2.reverse().substring(16, hexaStr2.length()));
232 + String revHexaStr3 = hexaStr3.reverse().toString();
233 + int lastSixteenHexaBits = Integer.parseInt(revHexaStr1, 2);
234 + int remainingHexaBits = Integer.parseInt(revHexaStr3, 2);
235 + int totalCheckSum = lastSixteenHexaBits + remainingHexaBits;
236 + hexaAsBinaryStr = new StringBuffer(Integer.toBinaryString(totalCheckSum));
237 + length = hexaAsBinaryStr.length();
238 + }
239 + if (hexaAsBinaryStr.length() < 16) {
240 + int count = 16 - hexaAsBinaryStr.length();
241 + String s = hexaAsBinaryStr.toString();
242 + for (int i = 0; i < count; i++) {
243 + s = "0" + s;
244 + }
245 +
246 + hexaAsBinaryStr = new StringBuffer(s);
247 +
248 + }
249 + StringBuffer checksum = negate(hexaAsBinaryStr);
250 + return Integer.parseInt(checksum.toString(), 2);
251 + }
252 +
253 + /**
254 + * Negates given hexasum.
255 + *
256 + * @param binaryString binary form of hexasum
257 + * @return binary from of calculateChecksum
258 + */
259 + private StringBuffer negate(StringBuffer binaryString) {
260 + for (int i = 0; i < binaryString.length(); i++) {
261 + if (binaryString.charAt(i) == '1') {
262 + binaryString.replace(i, i + 1, "0");
263 + } else {
264 + binaryString.replace(i, i + 1, "1");
265 + }
266 + }
267 +
268 + return binaryString;
269 + }
270 +
271 + /**
272 + * Calculates the OSPF checksum for the given packet.
273 + *
274 + * @param packet as byte array
275 + * @param checksumPos1 position of checksum bit in packet
276 + * @param checksumPos2 position of checksum bit in packet
277 + * @return checksum bytes
278 + */
279 + public byte[] calculateOspfCheckSum(byte[] packet, int checksumPos1, int checksumPos2) {
280 +
281 + int hexasum = 0;
282 + for (int i = 0; i < packet.length; i = i + 2) {
283 + if (i != 12) {
284 + byte b1 = packet[i];
285 + String s1 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
286 + b1 = packet[i + 1];
287 + String s2 = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
288 + String hexa = s1 + s2;
289 + int num1 = Integer.parseInt(hexa, 2);
290 + hexasum = hexasum + num1;
291 + String convertTo16 = Integer.toHexString(hexasum);
292 + if (convertTo16.length() > 4) {
293 + hexasum = convertToSixteenBits(convertTo16);
294 + }
295 + }
296 + }
297 + StringBuilder sb = new StringBuilder(Integer.toHexString(hexasum));
298 + if (sb.length() > 4) {
299 + sb = sb.reverse();
300 + StringBuilder s1 = new StringBuilder(sb.substring(0, 4));
301 + s1 = s1.reverse();
302 + StringBuilder s2 = new StringBuilder(sb.substring(4, sb.length()));
303 + s2 = s2.reverse();
304 + hexasum = Integer.parseInt(s1.toString(), 16) + Integer.parseInt(s2.toString(), 16);
305 + }
306 + int finalChecksum = calculateChecksum(hexasum);
307 + return OspfUtil.convertToTwoBytes(finalChecksum);
308 + }
309 +}
...\ 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.lsa.types;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onosproject.ospf.controller.OspfLsaType;
25 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.TlvHeader;
27 +import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
28 +import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
29 +
30 +
31 +import java.util.List;
32 +
33 +import static org.hamcrest.MatcherAssert.assertThat;
34 +import static org.hamcrest.Matchers.is;
35 +import static org.hamcrest.Matchers.notNullValue;
36 +
37 +/**
38 + * Unit test class for OpaqueLsa10.
39 + */
40 +public class OpaqueLsa10Test {
41 +
42 + private final byte[] packet = {0, 1, 0, 4, 1, 1, 1, 1, 0, 2, 0, 84, 0, 0, 0,
43 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
45 + private OpaqueLsa10 opaqueLsa10;
46 + private TopLevelTlv tlv;
47 + private OpaqueLsaHeader opqueHeader;
48 + private ChannelBuffer channelBuffer;
49 + private byte[] result;
50 + private RouterTlv routerTlv;
51 + private RouterTlv routerTlv1;
52 + private LinkTlv linkTlv;
53 + private LinkTlv linkTlv1;
54 + private OspfLsaType ospflsaType;
55 + private int result1;
56 + private List result2;
57 +
58 + @Before
59 + public void setUp() throws Exception {
60 + opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
61 + }
62 +
63 + @After
64 + public void tearDown() throws Exception {
65 + opaqueLsa10 = null;
66 + tlv = null;
67 + opqueHeader = null;
68 + channelBuffer = null;
69 + result = null;
70 + routerTlv = null;
71 + routerTlv1 = null;
72 + linkTlv1 = null;
73 + ospflsaType = null;
74 + }
75 +
76 + /**
77 + * Tests to string method.
78 + */
79 + @Test
80 + public void testToString() throws Exception {
81 + assertThat(opaqueLsa10.toString(), is(notNullValue()));
82 + }
83 +
84 + /**
85 + * Tests addValue() method.
86 + */
87 + @Test
88 + public void testAddValue() throws Exception {
89 + tlv = new RouterTlv(new TlvHeader());
90 + opaqueLsa10.addValue(tlv);
91 + assertThat(opaqueLsa10, is(notNullValue()));
92 + }
93 +
94 + /**
95 + * Tests readFrom() method.
96 + */
97 + @Test(expected = Exception.class)
98 + public void testReadFrom() throws Exception {
99 + opqueHeader = new OpaqueLsaHeader();
100 + opqueHeader.setLsType(10);
101 + opqueHeader.setLsPacketLen(48);
102 + opqueHeader.setLsCheckSum(10);
103 + opqueHeader.setAge(4);
104 + opqueHeader.setOpaqueId(1);
105 + opqueHeader.setOpaqueType(10);
106 + opqueHeader.setLsSequenceNo(250);
107 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
108 + opqueHeader.setOptions(66);
109 + opaqueLsa10 = new OpaqueLsa10(opqueHeader);
110 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
111 + opaqueLsa10.readFrom(channelBuffer);
112 + result = opaqueLsa10.asBytes();
113 + assertThat(result, is(notNullValue()));
114 + }
115 +
116 + /**
117 + * Tests asBytes() method.
118 + */
119 + @Test(expected = Exception.class)
120 + public void testAsBytes() throws Exception {
121 + opqueHeader = new OpaqueLsaHeader();
122 + opqueHeader.setLsType(10);
123 + opqueHeader.setLsPacketLen(48);
124 + opqueHeader.setLsCheckSum(10);
125 + opqueHeader.setAge(4);
126 + opqueHeader.setOpaqueId(1);
127 + opqueHeader.setOpaqueType(10);
128 + opqueHeader.setLsSequenceNo(250);
129 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
130 + opqueHeader.setOptions(66);
131 + opaqueLsa10 = new OpaqueLsa10(opqueHeader);
132 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
133 + opaqueLsa10.readFrom(channelBuffer);
134 + result = opaqueLsa10.getLsaBodyAsByteArray();
135 + result = opaqueLsa10.asBytes();
136 + assertThat(result, is(notNullValue()));
137 + }
138 +
139 + /**
140 + * Tests getLsaBodyAsByteArray() method.
141 + */
142 + @Test(expected = Exception.class)
143 + public void testGetLsaBodyAsByteArray() throws Exception {
144 + opqueHeader = new OpaqueLsaHeader();
145 + opqueHeader.setLsType(10);
146 + opqueHeader.setLsPacketLen(48);
147 + opqueHeader.setLsCheckSum(10);
148 + opqueHeader.setAge(4);
149 + opqueHeader.setOpaqueId(1);
150 + opqueHeader.setOpaqueType(10);
151 + opqueHeader.setLsSequenceNo(250);
152 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
153 + opqueHeader.setOptions(2);
154 + opaqueLsa10 = new OpaqueLsa10(opqueHeader);
155 + routerTlv = new RouterTlv(new TlvHeader());
156 + linkTlv = new LinkTlv(new TlvHeader());
157 + opaqueLsa10.addValue(routerTlv);
158 + opaqueLsa10.addValue(linkTlv);
159 + routerTlv1 = new RouterTlv(new TlvHeader());
160 + linkTlv1 = new LinkTlv(new TlvHeader());
161 + opaqueLsa10.addValue(routerTlv1);
162 + opaqueLsa10.addValue(linkTlv1);
163 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
164 + opaqueLsa10.readFrom(channelBuffer);
165 + result = opaqueLsa10.getLsaBodyAsByteArray();
166 + assertThat(result, is(notNullValue()));
167 + }
168 +
169 + /**
170 + * Tests getLsaBodyAsByteArray() method.
171 + */
172 + @Test(expected = Exception.class)
173 + public void testGetLsaBodyAsByteArray1() throws Exception {
174 + opqueHeader = new OpaqueLsaHeader();
175 + opqueHeader.setLsType(10);
176 + opqueHeader.setLsPacketLen(48);
177 + opqueHeader.setLsCheckSum(10);
178 + opqueHeader.setAge(4);
179 + opqueHeader.setOpaqueId(1);
180 + opqueHeader.setOpaqueType(10);
181 + opqueHeader.setLsSequenceNo(250);
182 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
183 + opqueHeader.setOptions(2);
184 + opaqueLsa10 = new OpaqueLsa10(opqueHeader);
185 + routerTlv = new RouterTlv(new TlvHeader());
186 + opaqueLsa10.addValue(routerTlv);
187 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
188 + result = opaqueLsa10.getLsaBodyAsByteArray();
189 + assertThat(result, is(notNullValue()));
190 + }
191 +
192 + /**
193 + * Tests getLsaBodyAsByteArray() method.
194 + */
195 + @Test(expected = Exception.class)
196 + public void testGetLsaBodyAsByteArray2() throws Exception {
197 + opqueHeader = new OpaqueLsaHeader();
198 + opqueHeader.setLsType(10);
199 + opqueHeader.setLsPacketLen(48);
200 + opqueHeader.setLsCheckSum(10);
201 + opqueHeader.setAge(4);
202 + opqueHeader.setOpaqueId(1);
203 + opqueHeader.setOpaqueType(10);
204 + opqueHeader.setLsSequenceNo(250);
205 + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
206 + opqueHeader.setOptions(2);
207 + opaqueLsa10 = new OpaqueLsa10(opqueHeader);
208 + linkTlv = new LinkTlv(new TlvHeader());
209 + opaqueLsa10.addValue(linkTlv);
210 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
211 + opaqueLsa10.readFrom(channelBuffer);
212 + result = opaqueLsa10.getLsaBodyAsByteArray();
213 + assertThat(result, is(notNullValue()));
214 + }
215 +
216 + /**
217 + * Tests getOspfLsaType() getter method.
218 + */
219 + @Test
220 + public void testGetOspfLsaType() throws Exception {
221 + opaqueLsa10.setLsType(10);
222 + ospflsaType = opaqueLsa10.getOspfLsaType();
223 + assertThat(ospflsaType, is(notNullValue()));
224 + assertThat(ospflsaType, is(OspfLsaType.AREA_LOCAL_OPAQUE_LSA));
225 + }
226 +
227 + /**
228 + * Tests hashCode() method.
229 + */
230 + @Test
231 + public void testHashcode() throws Exception {
232 +
233 + result1 = opaqueLsa10.hashCode();
234 + assertThat(result1, is(notNullValue()));
235 +
236 + }
237 +
238 + /**
239 + * Tests topLevelValues() method.
240 + */
241 + @Test
242 + public void testTopLevelValues() throws Exception {
243 +
244 + result2 = opaqueLsa10.topLevelValues();
245 + assertThat(result2, is(notNullValue()));
246 +
247 + }
248 +
249 +}
...\ 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.lsa.types;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onosproject.ospf.controller.OspfLsaType;
25 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
27 +
28 +import static org.hamcrest.MatcherAssert.assertThat;
29 +import static org.hamcrest.Matchers.*;
30 +
31 +/**
32 + * Unit test class for RouterLsa.
33 + */
34 +public class RouterLsaTest {
35 +
36 + private RouterLsa routerLsa;
37 + private int result1;
38 + private OspfLsaLink ospflsaLink;
39 + private byte[] inputArray;
40 + private LsaHeader lsaHeader;
41 + private ChannelBuffer channelBuffer;
42 + private byte[] result2;
43 + private OspfLsaType result3;
44 +
45 + @Before
46 + public void setUp() throws Exception {
47 + routerLsa = new RouterLsa();
48 + }
49 +
50 + @After
51 + public void tearDown() throws Exception {
52 + routerLsa = null;
53 + ospflsaLink = null;
54 + inputArray = null;
55 + lsaHeader = null;
56 + channelBuffer = null;
57 + result2 = null;
58 + result3 = null;
59 + }
60 +
61 +
62 + /**
63 + * Tests virtualEndPoint() setter method.
64 + */
65 + @Test
66 + public void testSetVirtualEndPoint() throws Exception {
67 + routerLsa.setVirtualEndPoint(true);
68 + assertThat(routerLsa, is(notNullValue()));
69 + }
70 +
71 +
72 + /**
73 + * Tests isAsBoundaryRouter() setter method.
74 + */
75 + @Test
76 + public void testSetAsBoundaryRouter() throws Exception {
77 + routerLsa.setAsBoundaryRouter(true);
78 + assertThat(routerLsa, is(notNullValue()));
79 + }
80 +
81 + /**
82 + * Tests areaBorderRouter() setter method.
83 + */
84 + @Test
85 + public void testSetAreaBorderRouter() throws Exception {
86 + routerLsa.setAreaBorderRouter(true);
87 + assertThat(routerLsa, is(notNullValue()));
88 + }
89 +
90 + /**
91 + * Tests noLink() getter method.
92 + */
93 + @Test
94 + public void testGetNoLink() throws Exception {
95 + routerLsa.setNoLink(10);
96 + result1 = routerLsa.noLink();
97 + assertThat(result1, is(10));
98 + }
99 +
100 + /**
101 + * Tests noLink() setter method.
102 + */
103 + @Test
104 + public void testSetNoLink() throws Exception {
105 + routerLsa.setNoLink(10);
106 + result1 = routerLsa.noLink();
107 + assertThat(result1, is(10));
108 + }
109 +
110 + /**
111 + * Tests addRouterLink() method.
112 + */
113 + @Test
114 + public void testAddRouterLink() throws Exception {
115 + routerLsa.setNoLink(0);
116 + ospflsaLink = createOspfLsaLink();
117 + routerLsa.addRouterLink(ospflsaLink);
118 + routerLsa.incrementLinkNo();
119 + result1 = routerLsa.noLink();
120 + assertThat(result1, is(1));
121 +
122 + }
123 +
124 +
125 + /**
126 + * Tests readFrom() method.
127 + */
128 + @Test
129 + public void testReadFrom() throws Exception {
130 + ospflsaLink = createOspfLsaLink();
131 + routerLsa.addRouterLink(ospflsaLink);
132 + inputArray = createByteForRouterLsa();
133 + lsaHeader = createLsaHeader();
134 + routerLsa = new RouterLsa(lsaHeader);
135 + channelBuffer = ChannelBuffers.copiedBuffer(inputArray);
136 + routerLsa.readFrom(channelBuffer);
137 + assertThat(routerLsa, is(notNullValue()));
138 + }
139 +
140 + /**
141 + * Tests readFrom() method.
142 + */
143 + @Test(expected = Exception.class)
144 + public void testReadFrom1() throws Exception {
145 + byte[] temp = {0, 0, 0};
146 + ospflsaLink = createOspfLsaLink();
147 + routerLsa.addRouterLink(ospflsaLink);
148 + inputArray = temp;
149 + lsaHeader = createLsaHeader();
150 + routerLsa = new RouterLsa(lsaHeader);
151 + channelBuffer = ChannelBuffers.copiedBuffer(inputArray);
152 + routerLsa.readFrom(channelBuffer);
153 + assertThat(routerLsa, is(notNullValue()));
154 + }
155 +
156 + /**
157 + * Tests asBytes() method.
158 + */
159 + @Test
160 + public void testAsBytes() throws Exception {
161 + result2 = routerLsa.asBytes();
162 + assertThat(result2, is(notNullValue()));
163 + }
164 +
165 + /**
166 + * Tests getLsaBodyAsByteArray() method.
167 + */
168 + @Test
169 + public void testGetLsaBodyAsByteArray() throws Exception {
170 + routerLsa.setAreaBorderRouter(true);
171 + routerLsa.setVirtualEndPoint(true);
172 + routerLsa.setAreaBorderRouter(true);
173 + ospflsaLink = createOspfLsaLink();
174 + routerLsa.addRouterLink(ospflsaLink);
175 + result2 = routerLsa.getLsaBodyAsByteArray();
176 + assertThat(result2, is(notNullValue()));
177 + }
178 +
179 + /**
180 + * Tests getOspfLsaType() getter method.
181 + */
182 + @Test
183 + public void testGetOspfLsaType() throws Exception {
184 + routerLsa.setLsType(1);
185 + result3 = routerLsa.getOspfLsaType();
186 + assertThat(result3, is(OspfLsaType.ROUTER));
187 + }
188 +
189 + /**
190 + * Tests incrementLinkNo() method.
191 + */
192 + @Test
193 + public void testIncrementLinkNo() throws Exception {
194 + routerLsa.setNoLink(1);
195 + routerLsa.incrementLinkNo();
196 + assertThat(routerLsa.noLink(), is(2));
197 + }
198 +
199 + /**
200 + * Tests lsaHeader() method.
201 + */
202 + @Test
203 + public void testGetLsaHeader() throws Exception {
204 + lsaHeader = (LsaHeader) routerLsa.lsaHeader();
205 + assertThat(lsaHeader, instanceOf(RouterLsa.class));
206 + }
207 +
208 + /**
209 + * Tests to string method.
210 + */
211 + @Test
212 + public void testToString() throws Exception {
213 + assertThat(routerLsa.toString(), is(notNullValue()));
214 +
215 + }
216 +
217 + /**
218 + * Utility method used by junit methods.
219 + */
220 + private OspfLsaLink createOspfLsaLink() {
221 + ospflsaLink = new OspfLsaLink();
222 + ospflsaLink.setLinkId("10.226.165.164");
223 + ospflsaLink.setMetric(10);
224 + ospflsaLink.setTos(50);
225 + ospflsaLink.setLinkType(2);
226 + ospflsaLink.setLinkData("10.226.165.170");
227 + return ospflsaLink;
228 + }
229 +
230 + /**
231 + * Utility method used by junit methods.
232 + */
233 + private byte[] createByteForRouterLsa() {
234 + byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64,
235 + -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56,
236 + 1};
237 + return packet;
238 + }
239 +
240 + /**
241 + * Utility method used by junit methods.
242 + */
243 + private LsaHeader createLsaHeader() {
244 + lsaHeader = new LsaHeader();
245 + lsaHeader.setLsType(1);
246 + lsaHeader.setLsPacketLen(48);
247 + lsaHeader.setLsCheckSum(10);
248 + lsaHeader.setAge(4);
249 + lsaHeader.setLinkStateId("10.226.165.164");
250 + lsaHeader.setLsSequenceNo(250);
251 + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
252 + lsaHeader.setOptions(2);
253 + return lsaHeader;
254 + }
255 +
256 + /**
257 + * Tests hashcode() method.
258 + */
259 + @Test
260 + public void testHashcode() throws Exception {
261 +
262 + result1 = routerLsa.hashCode();
263 + assertThat(result1, is(notNullValue()));
264 +
265 + }
266 +}
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.Before;
22 +import org.junit.Test;
23 +
24 +import static org.hamcrest.CoreMatchers.is;
25 +import static org.hamcrest.CoreMatchers.notNullValue;
26 +import static org.hamcrest.MatcherAssert.assertThat;
27 +
28 +/**
29 + * Unit test class for OspfMessageReader.
30 + */
31 +
32 +public class OspfMessageReaderTest {
33 +
34 + private final byte[] packet1 = {1, 1, 1, 1, 2, 1, 0, 44, -64, -88, -86, 8,
35 + 0, 0, 0, 1, 39, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0,
36 + 10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
37 + private final byte[] packet2 = {1, 1, 1, 1, 2, 2, 0, 52, -64, -88, -86, 8, 0,
38 + 0, 0, 1, -96, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119,
39 + -87, 126, 0, 23, 2, 1, 10, 10, 10, 10, 10, 10, 10, 10, -128, 0, 0, 6,
40 + -69, 26, 0, 36};
41 + private final byte[] packet3 = {1, 1, 1, 1, 2, 3, 0, 36, -64, -88, -86, 3, 0,
42 + 0, 0, 1, -67, -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88,
43 + -86, 8, -64, -88, -86, 8};
44 + private final byte[] packet4 = {1, 1, 1, 1, 2, 4, 1, 36, -64, -88, -86, 3, 0,
45 + 0, 0, 1, 54, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0,
46 + 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0
47 + , 0, 1, 58, -100, 0, 48, 2, 0, 0, 2, -64, -88, -86
48 + , 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0,
49 + -1, -1, -1, 0, 3, 0, 0, 10, 0, 3, 2, 5, 80, -44,
50 + 16, 0, -64, -88, -86, 2, -128, 0, 0, 1, 42, 73, 0,
51 + 36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0, 0, 0, 0,
52 + 0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88,
53 + -86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1,
54 + 0, -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0,
55 + 3, 2, 5, -64, -126, 120, 0, -64, -88, -86, 2, -128, 0,
56 + 0, 1, -45, 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20,
57 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0,
58 + -64, -88, -86, 2, -128, 0, 0, 1, 55, 8, 0, 36, -1, -1
59 + , -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 + 3, 2, 5, -64, -88, 1, 0, -64, -88, -86, 2, -128, 0, 0,
61 + 1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0,
62 + 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64,
63 + -88, -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0,
64 + -128, 0, 0, 20, -64, -88, -86, 10, 0, 0, 0, 0};
65 + private final byte[] packet5 = {1, 1, 1, 1, 2, 5, 0, 44, -64, -88, -86, 8, 0, 0,
66 + 0, 1, -30, -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
67 + 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
68 + private OspfMessageReader ospfMessageReader;
69 + private ChannelBuffer channelBuffer;
70 +
71 + @Before
72 + public void setUp() throws Exception {
73 + ospfMessageReader = new OspfMessageReader();
74 + }
75 +
76 + @After
77 + public void tearDown() throws Exception {
78 + ospfMessageReader = null;
79 + channelBuffer = null;
80 + }
81 +
82 + /**
83 + * Tests readFromBuffer() method.
84 + */
85 + @Test
86 + public void testReadFromBuffer() throws Exception {
87 + channelBuffer = ChannelBuffers.copiedBuffer(packet1);
88 + ospfMessageReader.readFromBuffer(channelBuffer);
89 +
90 + channelBuffer = ChannelBuffers.copiedBuffer(packet2);
91 + ospfMessageReader.readFromBuffer(channelBuffer);
92 +
93 + channelBuffer = ChannelBuffers.copiedBuffer(packet3);
94 + ospfMessageReader.readFromBuffer(channelBuffer);
95 +
96 + channelBuffer = ChannelBuffers.copiedBuffer(packet4);
97 + ospfMessageReader.readFromBuffer(channelBuffer);
98 +
99 + channelBuffer = ChannelBuffers.copiedBuffer(packet5);
100 + ospfMessageReader.readFromBuffer(channelBuffer);
101 + assertThat(ospfMessageReader, is(notNullValue()));
102 +
103 + }
104 +}
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.junit.After;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +import org.onlab.packet.Ip4Address;
22 +import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
23 +import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
24 +import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
25 +import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
26 +import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
27 +
28 +import static org.hamcrest.CoreMatchers.is;
29 +import static org.hamcrest.CoreMatchers.notNullValue;
30 +import static org.junit.Assert.assertThat;
31 +
32 +/**
33 + * Unit test class for OspfMessageWriter.
34 + */
35 +public class OspfMessageWriterTest {
36 +
37 + private OspfMessageWriter ospfMessageWriter;
38 + private HelloPacket helloPacket;
39 + private DdPacket ddPacket;
40 + private LsAcknowledge lsAck;
41 + private LsRequest lsReq;
42 + private LsUpdate lsUpdate;
43 +
44 + @Before
45 + public void setUp() throws Exception {
46 + ospfMessageWriter = new OspfMessageWriter();
47 + }
48 +
49 + @After
50 + public void tearDown() throws Exception {
51 + ospfMessageWriter = null;
52 + helloPacket = null;
53 + ddPacket = null;
54 + lsAck = null;
55 + lsReq = null;
56 + lsUpdate = null;
57 + }
58 +
59 + /**
60 + * Tests writeToBuffer() method.
61 + */
62 + @Test
63 + public void testWriteToBuffer() throws Exception {
64 + helloPacket = new HelloPacket();
65 + helloPacket.setAuthType(1);
66 + helloPacket.setOspftype(1);
67 + helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
68 + helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
69 + helloPacket.setChecksum(201);
70 + helloPacket.setAuthentication(2);
71 + helloPacket.setOspfPacLength(48);
72 + helloPacket.setOspfVer(2);
73 + helloPacket.setNetworkMask(Ip4Address.valueOf("255.255.255.255"));
74 + helloPacket.setOptions(2); //not setting now
75 + helloPacket.setHelloInterval(10);
76 + helloPacket.setRouterPriority(1);
77 + helloPacket.setRouterDeadInterval(40);
78 + helloPacket.setDr(Ip4Address.valueOf("1.1.1.1"));
79 + helloPacket.setBdr(Ip4Address.valueOf("2.2.2.2"));
80 + helloPacket.addNeighbor(Ip4Address.valueOf("8.8.8.8"));
81 + helloPacket.setDestinationIp(Ip4Address.valueOf("5.5.5.5"));
82 + ospfMessageWriter.writeToBuffer(helloPacket, 7, 1);
83 + assertThat(ospfMessageWriter, is(notNullValue()));
84 + }
85 +
86 + @Test(expected = Exception.class)
87 + public void testWriteToBuffer1() throws Exception {
88 +
89 + ddPacket = new DdPacket();
90 + ddPacket.setAuthType(1);
91 + ddPacket.setOspftype(2);
92 + ddPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
93 + ddPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
94 + ddPacket.setChecksum(201);
95 + ddPacket.setAuthentication(2);
96 + ddPacket.setOspfPacLength(48);
97 + ddPacket.setOspfVer(2);
98 + ospfMessageWriter.writeToBuffer(ddPacket, 1, 1);
99 + assertThat(ospfMessageWriter, is(notNullValue()));
100 + }
101 +
102 + @Test(expected = Exception.class)
103 + public void testWriteToBuffer2() throws Exception {
104 +
105 + lsAck = new LsAcknowledge();
106 + lsAck.setAuthType(1);
107 + lsAck.setOspftype(5);
108 + lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
109 + lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
110 + lsAck.setChecksum(201);
111 + lsAck.setAuthentication(2);
112 + lsAck.setOspfPacLength(48);
113 + lsAck.setOspfVer(2);
114 + ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
115 + assertThat(ospfMessageWriter, is(notNullValue()));
116 + }
117 +
118 + @Test(expected = Exception.class)
119 + public void testWriteToBuffer3() throws Exception {
120 + lsReq = new LsRequest();
121 + lsReq.setAuthType(1);
122 + lsReq.setOspftype(3);
123 + lsReq.setRouterId(Ip4Address.valueOf("10.226.165.164"));
124 + lsReq.setAreaId(Ip4Address.valueOf("10.226.165.100"));
125 + lsReq.setChecksum(201);
126 + lsReq.setAuthentication(2);
127 + lsReq.setOspfPacLength(48);
128 + lsReq.setOspfVer(2);
129 + ospfMessageWriter.writeToBuffer(lsReq, 1, 1);
130 + assertThat(ospfMessageWriter, is(notNullValue()));
131 + }
132 +
133 + @Test(expected = Exception.class)
134 + public void testWriteToBuffer4() throws Exception {
135 + lsUpdate = new LsUpdate();
136 + lsUpdate.setAuthType(1);
137 + lsUpdate.setOspftype(3);
138 + lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164"));
139 + lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100"));
140 + lsUpdate.setChecksum(201);
141 + lsUpdate.setAuthentication(2);
142 + lsUpdate.setOspfPacLength(48);
143 + lsUpdate.setOspfVer(2);
144 + ospfMessageWriter.writeToBuffer(lsUpdate, 1, 1);
145 + assertThat(ospfMessageWriter, is(notNullValue()));
146 + }
147 +
148 + /**
149 + * Tests writeToBuffer() method.
150 + */
151 + @Test(expected = Exception.class)
152 + public void testWriteToBuffer5() throws Exception {
153 + lsAck = new LsAcknowledge();
154 + lsAck.setAuthType(1);
155 + lsAck.setOspftype(5);
156 + lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
157 + lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
158 + lsAck.setChecksum(201);
159 + lsAck.setAuthentication(2);
160 + lsAck.setOspfPacLength(48);
161 + lsAck.setOspfVer(2);
162 + ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
163 + }
164 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ospf.protocol.ospfpacket.types;
17 +
18 +import org.jboss.netty.buffer.ChannelBuffer;
19 +import org.jboss.netty.buffer.ChannelBuffers;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onosproject.ospf.controller.OspfLsa;
25 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
27 +import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
28 +import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
29 +import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
30 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
31 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
32 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
33 +import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
34 +import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
35 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
36 +import org.onosproject.ospf.protocol.util.OspfPacketType;
37 +
38 +import java.util.List;
39 +import java.util.Vector;
40 +
41 +import static org.hamcrest.CoreMatchers.*;
42 +import static org.hamcrest.MatcherAssert.assertThat;
43 +
44 +/**
45 + * Unit test class for LsUpdate.
46 + */
47 +public class LsUpdateTest {
48 +
49 + private final byte[] packet1 = {0, 0, 0, 2, 0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7,
50 + -128, 0, 0, 2, 46, -126, 0, 48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1,
51 + 0, 0, 10, 10, 10, 10, 0, -1, -1, -1, 0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0,
52 + 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0, 116, 0, 1, 0, 4, 0, 0, 0, 0,
53 + 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10, 10, 10, 0, 0, 5, 0,
54 + 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73, -104, -106,
55 + -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106,
56 + -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73,
57 + -104, -106, -128, 73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0};
58 + private final byte[] packet3 = {0, 0, 0, 1, 0, 100, 2, 10, 1, 0, 0, 1, 9, 9, 9, 9,
59 + -128, 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2,
60 + 0, 4, -64, -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0,
61 + 0, 0, 5, 0, 4, 0, 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0,
62 + 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0,
64 + 0, 0, 1};
65 + private final byte[] packet2 = {0, 0,
66 + 0, 1, 0, 1, 2, 2, -64, -88,
67 + -86, 8, -64, -88, -86, 8, -128, 0, 0, 1, 55, -73, 0, 32, -1, -1, -1, 0, -64,
68 + -88, -86, 3, -64, -88, -86, 8};
69 + private final byte[] packet4 = {0, 0, 0, 1, 0, 100, 2, 9, 1, 0, 0, 1, 9, 9, 9, 9, -128,
70 + 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
71 + -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0,
72 + 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0,
73 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 + 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
75 +
76 + private final byte[] packet5 = {0, 0, 0, 1, 0, 100, 2, 11, 1, 0, 0, 1, 9, 9, 9, 9, -128, 0,
77 + 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64, -88,
78 + 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0,
79 + 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0,
80 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 + 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
82 + private final byte[] packet6 = {0, 0, 0, 1, 0, 100, 2, 3, 1, 0, 0, 1, 9, 9, 9, 9, -128,
83 + 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
84 + -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4,
85 + 0, 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0,
86 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 + 0, 0, 0, 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
88 + private final byte[] packet7 = {0, 0, 0, 1, 0, 100, 2, 4, 1, 0, 0, 1, 9, 9, 9, 9, -128,
89 + 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
90 + -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0,
91 + 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0,
92 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 + 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
94 +
95 + private final byte[] packet8 = {0, 0, 0, 2, 1, 4, 2, 1, 5, 5, 5, 5, 5, 5, 5, 5, -128, 0, 0,
96 + 4, -39, -84, 0, 36, 1, 0, 0, 1, -64, -88, 7, 90, -64, -88, 7, 92, 2, 0, 0, 10, 1, 4,
97 + 2, 4, -34, -34, -34, -34, 5, 5, 5, 5, -128, 0, 0, 1, 31, -93, 0, 28, 0, 0, 0, 0, 0,
98 + 0, 0, 10};
99 + private LsUpdate lsUpdate;
100 + private RouterLsa ospflsa;
101 + private NetworkLsa ospflsa1;
102 + private SummaryLsa ospflsa2;
103 + private AsbrSummaryLsa ospflsa3;
104 + private ExternalLsa ospflsa4;
105 + private Vector<OspfLsa> listLSA = new Vector();
106 + private List lsa;
107 + private int result;
108 + private OspfPacketType ospfMessageType;
109 + private OspfPacketHeader ospfPacketHeader;
110 + private byte[] result1;
111 + private ChannelBuffer channelBuffer;
112 + private OpaqueLsa10 opaqueLsa10;
113 + private OpaqueLsa9 opaqueLsa9;
114 + private OpaqueLsa11 opaqueLsa11;
115 +
116 + @Before
117 + public void setUp() throws Exception {
118 + lsUpdate = new LsUpdate();
119 + ospflsa = new RouterLsa();
120 + lsUpdate.setAuthType(1);
121 + lsUpdate.setOspftype(2);
122 + lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164"));
123 + lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100"));
124 + lsUpdate.setChecksum(201);
125 + lsUpdate.setAuthentication(2);
126 + lsUpdate.setOspfPacLength(48);
127 + lsUpdate.setOspfVer(2);
128 + ospflsa.setLsType(1);
129 + lsUpdate.addLsa(ospflsa);
130 + ospflsa1 = new NetworkLsa();
131 + ospflsa1.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
132 + ospflsa1.setLsType(2);
133 + lsUpdate.addLsa(ospflsa1);
134 + ospflsa2 = new SummaryLsa(new LsaHeader());
135 + ospflsa2.setLsType(3);
136 + lsUpdate.addLsa(ospflsa2);
137 + ospflsa3 = new AsbrSummaryLsa(new LsaHeader());
138 + ospflsa3.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
139 + ospflsa3.setLsType(4);
140 + lsUpdate.addLsa(ospflsa3);
141 + ospflsa4 = new ExternalLsa(new LsaHeader());
142 + ospflsa4.setLsType(5);
143 + lsUpdate.addLsa(ospflsa4);
144 + }
145 +
146 + @After
147 + public void tearDown() throws Exception {
148 + lsUpdate = null;
149 + ospflsa = null;
150 + ospflsa1 = null;
151 + ospflsa2 = null;
152 + ospflsa3 = null;
153 + ospflsa4 = null;
154 + listLSA.clear();
155 + lsa = null;
156 + ospfMessageType = null;
157 + ospfPacketHeader = null;
158 + result1 = null;
159 + channelBuffer = null;
160 + opaqueLsa9 = null;
161 + opaqueLsa10 = null;
162 + opaqueLsa11 = null;
163 + }
164 +
165 + /**
166 + * Tests getLsaList() getter method.
167 + */
168 + @Test
169 + public void testGetListLsa() throws Exception {
170 + lsUpdate.addLsa(ospflsa);
171 + lsUpdate.addLsa(ospflsa);
172 + lsa = lsUpdate.getLsaList();
173 + assertThat(lsa, is(notNullValue()));
174 + assertThat(lsa.size(), is(5));
175 + }
176 +
177 + /**
178 + * Tests addLsa() method.
179 + */
180 + @Test
181 + public void testAddLsa() throws Exception {
182 + lsUpdate.addLsa(ospflsa);
183 + assertThat(lsUpdate, is(notNullValue()));
184 + }
185 +
186 + /**
187 + * Tests noLsa() getter method.
188 + */
189 + @Test
190 + public void testGetNoLsa() throws Exception {
191 + lsUpdate.setNumberOfLsa(5);
192 + result = lsUpdate.noLsa();
193 + assertThat(result, is(notNullValue()));
194 + assertThat(result, is(5));
195 + }
196 +
197 + /**
198 + * Tests noLsa() setter method.
199 + */
200 + @Test
201 + public void testSetNoLsa() throws Exception {
202 + lsUpdate.setNumberOfLsa(5);
203 + result = lsUpdate.noLsa();
204 + assertThat(result, is(notNullValue()));
205 + assertThat(result, is(5));
206 + }
207 +
208 + /**
209 + * Tests ospfMessageType() getter method.
210 + */
211 + @Test
212 + public void testGetOspfMessageType() throws Exception {
213 + ospfMessageType = lsUpdate.ospfMessageType();
214 + assertThat(ospfMessageType, is(OspfPacketType.LSUPDATE));
215 + }
216 +
217 + /**
218 + * Tests readFrom() method.
219 + */
220 + @Test
221 + public void testReadFrom() throws Exception {
222 + ospfPacketHeader = new OspfPacketHeader();
223 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
224 + ospfPacketHeader.setAuthentication(0);
225 + ospfPacketHeader.setAuthType(0);
226 + ospfPacketHeader.setChecksum(12345);
227 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
228 + ospfPacketHeader.setOspfPacLength(56);
229 + ospfPacketHeader.setOspftype(4);
230 + ospfPacketHeader.setOspfVer(2);
231 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
232 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
233 + lsUpdate = new LsUpdate(ospfPacketHeader);
234 + result1 = createLsUpdatePacket();
235 + channelBuffer = ChannelBuffers.copiedBuffer(result1);
236 + lsUpdate.readFrom(channelBuffer);
237 + channelBuffer = ChannelBuffers.copiedBuffer(packet1);
238 + lsUpdate.readFrom(channelBuffer);
239 + channelBuffer = ChannelBuffers.copiedBuffer(packet2);
240 + lsUpdate.readFrom(channelBuffer);
241 + channelBuffer = ChannelBuffers.copiedBuffer(packet3);
242 + lsUpdate.readFrom(channelBuffer);
243 + channelBuffer = ChannelBuffers.copiedBuffer(packet4);
244 + lsUpdate.readFrom(channelBuffer);
245 + channelBuffer = ChannelBuffers.copiedBuffer(packet5);
246 + lsUpdate.readFrom(channelBuffer);
247 + channelBuffer = ChannelBuffers.copiedBuffer(packet6);
248 + lsUpdate.readFrom(channelBuffer);
249 + channelBuffer = ChannelBuffers.copiedBuffer(packet7);
250 + lsUpdate.readFrom(channelBuffer);
251 + channelBuffer = ChannelBuffers.copiedBuffer(packet8);
252 + lsUpdate.readFrom(channelBuffer);
253 + assertThat(lsUpdate, is(notNullValue()));
254 + assertThat(lsUpdate.ospfMessageType(), is(OspfPacketType.LSUPDATE));
255 + }
256 +
257 + /**
258 + * Tests asBytes() method.
259 + */
260 + @Test
261 + public void testAsBytes() throws Exception {
262 + result1 = lsUpdate.asBytes();
263 + assertThat(result1, is(notNullValue()));
264 + }
265 +
266 + /**
267 + * Tests getLsuHeaderAsByteArray() method.
268 + */
269 + @Test
270 + public void testGetLsuHeaderAsByteArray() throws Exception {
271 + result1 = lsUpdate.getLsuHeaderAsByteArray();
272 + assertThat(result1, is(notNullValue()));
273 + }
274 +
275 + /**
276 + * Tests getLsuBodyAsByteArray() method.
277 + */
278 + @Test
279 + public void testGetLsuBodyAsByteArray() throws Exception {
280 + lsUpdate.setNumberOfLsa(8);
281 + lsUpdate.addLsa(ospflsa3);
282 + opaqueLsa9 = new OpaqueLsa9(new OpaqueLsaHeader());
283 + opaqueLsa9.setLsType(9);
284 + lsUpdate.addLsa(opaqueLsa9);
285 + opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
286 + opaqueLsa10.setLsType(10);
287 + lsUpdate.addLsa(opaqueLsa10);
288 + opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader());
289 + opaqueLsa10.setLsType(11);
290 + lsUpdate.addLsa(opaqueLsa11);
291 + result1 = lsUpdate.getLsuBodyAsByteArray();
292 + assertThat(result1, is(notNullValue()));
293 + }
294 +
295 + @Test
296 + public void testGetLsuBodyAsByteArray1() throws Exception {
297 + lsUpdate.setNumberOfLsa(8);
298 + opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
299 + opaqueLsa10.setLsType(10);
300 + lsUpdate.addLsa(opaqueLsa10);
301 + assertThat(result1, is(nullValue()));
302 + }
303 +
304 + @Test
305 + public void testGetLsuBodyAsByteArray2() throws Exception {
306 + opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader());
307 + opaqueLsa11.setLsType(11);
308 + lsUpdate.addLsa(opaqueLsa11);
309 + result1 = lsUpdate.getLsuBodyAsByteArray();
310 + assertThat(result1, is(notNullValue()));
311 + }
312 +
313 + /**
314 + * Tests to string method.
315 + */
316 + @Test
317 + public void testToString() throws Exception {
318 + assertThat(lsUpdate.toString(), is(notNullValue()));
319 + }
320 +
321 + /**
322 + * Utility method used by junit methods.
323 + */
324 + private byte[] createLsUpdatePacket() {
325 + byte[] lsUpdatePacket = {0, 0, 0, 7, 0, 2, 2,
326 + 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58,
327 + -100, 0, 48, 2, 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0,
328 + 3, 0, 0, 10, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10
329 + , 0, 3, 2, 5, 80, -44, 16, 0, -64, -88, -86, 2, -128, 0, 0
330 + , 1, 42, 73, 0, 36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0,
331 + 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88
332 + , -86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1, 0,
333 + -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0, 3, 2, 5
334 + , -64, -126, 120, 0, -64, -88, -86, 2, -128, 0, 0, 1, -45,
335 + 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0,
336 + 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0, -64, -88, -86, 2,
337 + -128, 0, 0, 1, 55, 8, 0, 36, -1, -1, -1, 0, -128, 0, 0,
338 + 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 1, 0,
339 + -64, -88, -86, 2, -128, 0, 0, 1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0,
340 + 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64, -88,
341 + -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, -64,
342 + -88, -86, 10, 0, 0, 0, 0};
343 + return lsUpdatePacket;
344 + }
345 +}
...\ 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 org.junit.After;
19 +import org.junit.Before;
20 +import org.junit.Test;
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.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
25 +import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
26 +import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
27 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
28 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
29 +import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
30 +import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
31 +import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
32 +import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
33 +import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
34 +import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
35 +import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
36 +import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
37 +
38 +import static org.hamcrest.CoreMatchers.is;
39 +import static org.hamcrest.CoreMatchers.notNullValue;
40 +import static org.hamcrest.MatcherAssert.assertThat;
41 +
42 +/**
43 + * Unit test class for ChecksumCalculator.
44 + */
45 +public class ChecksumCalculatorTest {
46 +
47 + private final int ospfChecksumPos1 = 12;
48 + private final int ospfChecksumPos2 = 13;
49 + private final int lsaChecksumPos1 = 16;
50 + private final int lsaChecksumPos2 = 17;
51 + private final byte[] updatePacket = {1, 1, 1, 1, 2, 4, 0, -96, 9, 9, 9, 9, 5, 5, 5, 5, 62, 125,
52 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 100, 2, 10, 1, 0, 0, 1, 9, 9, 9, 9, -128,
53 + 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64, -88,
54 + 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 1,
55 + 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0,
56 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
57 + 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
58 +
59 + private final byte[] helloPacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59,
60 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, 40, -64, -88,
61 + -86, 8, 0, 0, 0, 0};
62 + private final byte[] rlsa = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0,
63 + 0, 1, 74, -114, 0, 48, 2, 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10,
64 + -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10};
65 + private ChecksumCalculator checksumCalculator;
66 + private boolean validate;
67 + private HelloPacket hello;
68 + private LsUpdate message;
69 + private DdPacket message1;
70 + private LsRequest message2;
71 + private RouterLsa router;
72 + private LsAcknowledge lsack;
73 + private ExternalLsa external;
74 + private NetworkLsa external1;
75 + private SummaryLsa external2;
76 + private AsbrSummaryLsa external3;
77 + private OpaqueLsa9 external4;
78 + private OpaqueLsa10 external5;
79 + private OpaqueLsa11 external6;
80 + private byte[] result;
81 +
82 + @Before
83 + public void setUp() throws Exception {
84 + checksumCalculator = new ChecksumCalculator();
85 + }
86 +
87 + @After
88 + public void tearDown() throws Exception {
89 + checksumCalculator = null;
90 + hello = null;
91 + message = null;
92 + message1 = null;
93 + message2 = null;
94 + router = null;
95 + lsack = null;
96 + external = null;
97 + external1 = null;
98 + external2 = null;
99 + external3 = null;
100 + result = null;
101 + }
102 +
103 + /**
104 + * Tests convertToSixteenBits() method.
105 + */
106 + @Test
107 + public void testConvertToSixteenBits() throws Exception {
108 + int num = checksumCalculator.convertToSixteenBits("16cdd");
109 + assertThat(num, is(27870));
110 + }
111 +
112 + /**
113 + * Tests isValidOspfCheckSum() method.
114 + */
115 + @Test
116 + public void testIsValidOspfCheckSum() throws Exception {
117 + hello = new HelloPacket();
118 + hello.setOspfVer(2);
119 + hello.setOspftype(1);
120 + hello.setOspfPacLength(172);
121 + hello.setRouterId(Ip4Address.valueOf("192.168.170.3"));
122 + hello.setAreaId(Ip4Address.valueOf("0.0.0.1"));
123 + hello.setChecksum(5537);
124 + hello.setAuthType(0);
125 + hello.setAuthentication(0);
126 + validate = checksumCalculator.isValidOspfCheckSum(hello, ospfChecksumPos1, ospfChecksumPos2);
127 + assertThat(validate, is(false));
128 + lsack = new LsAcknowledge();
129 + lsack.setOspfVer(2);
130 + lsack.setOspftype(5);
131 + lsack.setOspfPacLength(172);
132 + lsack.setRouterId(Ip4Address.valueOf("192.168.170.3"));
133 + lsack.setAreaId(Ip4Address.valueOf("0.0.0.1"));
134 + lsack.setChecksum(37537);
135 + lsack.setAuthType(0);
136 + lsack.setAuthentication(0);
137 + validate = checksumCalculator.isValidOspfCheckSum(lsack, ospfChecksumPos1, ospfChecksumPos2);
138 + assertThat(validate, is(true));
139 + message = new LsUpdate();
140 + message.setOspfVer(2);
141 + message.setOspftype(5);
142 + message.setOspfPacLength(172);
143 + message.setRouterId(Ip4Address.valueOf("192.168.170.3"));
144 + message.setAreaId(Ip4Address.valueOf("0.0.0.1"));
145 + message.setChecksum(37537);
146 + message.setAuthType(0);
147 + message.setAuthentication(0);
148 + validate = checksumCalculator.isValidOspfCheckSum(message, ospfChecksumPos1, ospfChecksumPos2);
149 + assertThat(validate, is(true));
150 + message1 = new DdPacket();
151 + message1.setOspfVer(2);
152 + message1.setOspftype(5);
153 + message1.setOspfPacLength(172);
154 + message1.setRouterId(Ip4Address.valueOf("192.168.170.3"));
155 + message1.setAreaId(Ip4Address.valueOf("0.0.0.1"));
156 + message1.setChecksum(37537);
157 + message1.setAuthType(0);
158 + message1.setAuthentication(0);
159 + validate = checksumCalculator.isValidOspfCheckSum(message1, ospfChecksumPos1, ospfChecksumPos2);
160 + assertThat(validate, is(true));
161 + message2 = new LsRequest();
162 + message2.setOspfVer(2);
163 + message2.setOspftype(5);
164 + message2.setOspfPacLength(172);
165 + message2.setRouterId(Ip4Address.valueOf("192.168.170.3"));
166 + message2.setAreaId(Ip4Address.valueOf("0.0.0.1"));
167 + message2.setChecksum(37537);
168 + message2.setAuthType(0);
169 + message2.setAuthentication(0);
170 + validate = checksumCalculator.isValidOspfCheckSum(message2, ospfChecksumPos1, ospfChecksumPos2);
171 + assertThat(validate, is(true));
172 + }
173 +
174 + /**
175 + * Tests isValidLsaCheckSum() method.
176 + */
177 + @Test
178 + public void testIsValidLsaCheckSum() throws Exception {
179 + router = new RouterLsa();
180 + router.setAge(1);
181 + router.setOptions(2);
182 + router.setLsType(1);
183 + router.setLinkStateId("192.168.170.3");
184 + router.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.3"));
185 + router.setLsSequenceNo(2147483649L);
186 + router.setLsCheckSum(49499);
187 + router.setLsPacketLen(48);
188 + validate = checksumCalculator.isValidLsaCheckSum(router, router.lsType(), lsaChecksumPos1, lsaChecksumPos2);
189 + assertThat(validate, is(true));
190 +
191 + }
192 +
193 + /**
194 + * Tests isValidLsaCheckSum() method.
195 + */
196 + @Test
197 + public void testIsValidLsaCheckSum4() throws Exception {
198 + external = new ExternalLsa(new LsaHeader());
199 + external.setAge(2);
200 + external.setOptions(2);
201 + external.setLsType(5);
202 + external.setLinkStateId("80.212.16.0");
203 + external.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
204 + external.setLsSequenceNo(2147483649L);
205 + external.setLsCheckSum(25125);
206 + external.setLsPacketLen(36);
207 + validate = checksumCalculator.isValidLsaCheckSum(external, external.lsType(), lsaChecksumPos1, lsaChecksumPos2);
208 + assertThat(validate, is(false));
209 + }
210 +
211 + /**
212 + * Tests isValidLsaCheckSum() method.
213 + */
214 + @Test(expected = Exception.class)
215 + public void testIsValidLsaCheckSum5() throws Exception {
216 + external1 = new NetworkLsa();
217 + external1.setAge(2);
218 + external1.setOptions(2);
219 + external1.setLsType(2);
220 + external1.setLinkStateId("80.212.16.0");
221 + external1.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
222 + external1.setLsSequenceNo(2147483649L);
223 + external1.setLsCheckSum(25125);
224 + external1.setLsPacketLen(36);
225 + validate = checksumCalculator.isValidLsaCheckSum(external1, external1.lsType(),
226 + lsaChecksumPos1, lsaChecksumPos2);
227 + assertThat(validate, is(false));
228 + }
229 +
230 + /**
231 + * Tests isValidLsaCheckSum() method.
232 + */
233 + @Test
234 + public void testIsValidLsaCheckSum6() throws Exception {
235 +
236 + external2 = new SummaryLsa(new LsaHeader());
237 + external2.setAge(2);
238 + external2.setOptions(2);
239 + external2.setLsType(3);
240 + external2.setLinkStateId("80.212.16.0");
241 + external2.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
242 + external2.setLsSequenceNo(2147483649L);
243 + external2.setLsCheckSum(25125);
244 + external2.setLsPacketLen(36);
245 + validate = checksumCalculator.isValidLsaCheckSum(external2, external2.lsType(),
246 + lsaChecksumPos1, lsaChecksumPos2);
247 + assertThat(validate, is(false));
248 + }
249 +
250 + /**
251 + * Tests isValidLsaCheckSum() method.
252 + */
253 + @Test(expected = Exception.class)
254 + public void testIsValidLsaCheckSum7() throws Exception {
255 + external3 = new AsbrSummaryLsa(new LsaHeader());
256 + external3.setAge(2);
257 + external3.setOptions(2);
258 + external3.setLsType(4);
259 + external3.setLinkStateId("80.212.16.0");
260 + external3.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
261 + external3.setLsSequenceNo(2147483649L);
262 + external3.setLsCheckSum(25125);
263 + external3.setLsPacketLen(36);
264 + validate = checksumCalculator.isValidLsaCheckSum(external3, external3.lsType(),
265 + lsaChecksumPos1, lsaChecksumPos2);
266 + assertThat(validate, is(false));
267 + }
268 +
269 + /**
270 + * Tests isValidLsaCheckSum() method.
271 + */
272 + @Test(expected = Exception.class)
273 + public void testIsValidLsaCheckSum1() throws Exception {
274 + external4 = new OpaqueLsa9(new OpaqueLsaHeader());
275 + external4.setAge(2);
276 + external4.setOptions(2);
277 + external4.setLsType(9);
278 + external4.setLinkStateId("80.212.16.0");
279 + external4.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
280 + external4.setLsSequenceNo(2147483649L);
281 + external4.setLsCheckSum(25125);
282 + external4.setLsPacketLen(36);
283 + validate = checksumCalculator.isValidLsaCheckSum(external4, external4.lsType(),
284 + lsaChecksumPos1, lsaChecksumPos2);
285 + assertThat(validate, is(false));
286 + }
287 +
288 + /**
289 + * Tests isValidLsaCheckSum() method.
290 + */
291 + @Test(expected = Exception.class)
292 + public void testIsValidLsaCheckSum2() throws Exception {
293 + external5 = new OpaqueLsa10(new OpaqueLsaHeader());
294 + external5.setAge(2);
295 + external5.setOptions(2);
296 + external5.setLsType(10);
297 + external5.setLinkStateId("80.212.16.0");
298 + external5.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
299 + external5.setLsSequenceNo(2147483649L);
300 + external5.setLsCheckSum(25125);
301 + external5.setLsPacketLen(36);
302 + validate = checksumCalculator.isValidLsaCheckSum(external5, external5.lsType(),
303 + lsaChecksumPos1, lsaChecksumPos2);
304 +
305 + assertThat(validate, is(false));
306 + }
307 +
308 + /**
309 + * Tests isValidLsaCheckSum() method.
310 + */
311 + @Test(expected = Exception.class)
312 + public void testIsValidLsaCheckSum3() throws Exception {
313 + external6 = new OpaqueLsa11(new OpaqueLsaHeader());
314 + external6.setAge(2);
315 + external6.setOptions(2);
316 + external6.setLsType(10);
317 + external6.setLinkStateId("80.212.16.0");
318 + external6.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2"));
319 + external6.setLsSequenceNo(2147483649L);
320 + external6.setLsCheckSum(25125);
321 + external6.setLsPacketLen(36);
322 + validate = checksumCalculator.isValidLsaCheckSum(external6, external6.lsType(),
323 + lsaChecksumPos1, lsaChecksumPos2);
324 + assertThat(validate, is(false));
325 + }
326 +
327 + /**
328 + * Tests validateLsaCheckSum() method.
329 + */
330 + @Test
331 + public void testValidateLsaCheckSum() throws Exception {
332 + assertThat(checksumCalculator.validateLsaCheckSum(rlsa, lsaChecksumPos1,
333 + lsaChecksumPos2), is(true));
334 +
335 + }
336 +
337 + /**
338 + * Tests validateOspfCheckSum() method.
339 + */
340 + @Test
341 + public void testValidateOspfCheckSum() throws Exception {
342 + assertThat(checksumCalculator.validateOspfCheckSum(helloPacket, ospfChecksumPos1,
343 + ospfChecksumPos2), is(true));
344 + }
345 +
346 + /**
347 + * Tests calculateLsaChecksum() method.
348 + */
349 + @Test
350 + public void testCalculateLsaChecksum() throws Exception {
351 + result = checksumCalculator.calculateLsaChecksum(rlsa, lsaChecksumPos1, lsaChecksumPos2);
352 + assertThat(result, is(notNullValue()));
353 + }
354 +
355 + /**
356 + * Tests calculateOspfCheckSum() method.
357 + */
358 + @Test
359 + public void testCalculateOspfCheckSum() throws Exception {
360 + result = checksumCalculator.calculateOspfCheckSum(helloPacket, ospfChecksumPos1,
361 + ospfChecksumPos2);
362 + assertThat(result, is(notNullValue()));
363 + result = checksumCalculator.calculateOspfCheckSum(updatePacket, ospfChecksumPos1,
364 + ospfChecksumPos2);
365 + assertThat(result, is(notNullValue()));
366 + }
367 +}
...\ No newline at end of file ...\ No newline at end of file