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 +
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.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