Dhruv Dhody
Committed by Gerrit Code Review

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

Change-Id: Ifaaa4d3640c6a4fec4931db54a1f03100cc9d9e6
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implementation of the OSPF LSA Sub Types.
*/
package org.onosproject.ospf.protocol.lsa.subtypes;
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.lsa.types;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.ospf.controller.OspfLsaType;
import org.onosproject.ospf.exceptions.OspfErrorType;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.lsa.TlvHeader;
import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
import org.onosproject.ospf.protocol.lsa.tlvtypes.OpaqueTopLevelTlvTypes;
import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
import org.onosproject.ospf.protocol.util.OspfParameters;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of an Opaque LSA of type area local (10).
*/
public class OpaqueLsa10 extends OpaqueLsaHeader {
/*
0 1 2 3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| LS age | Options | 9, 10 or 11 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Opaque Type | Opaque ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Advertising Router |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| LS Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| LS checksum | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| Opaque Information |
+ +
| ... |
Opaque LSA format
REFERENCE : RFC 5250
*/
private List<TopLevelTlv> topLevelValues = new ArrayList<>();
private byte[] opaqueInfo = null;
/**
* Creates an instance of Opaque type 10 LSA.
*
* @param lsaHeader LSA header instance
*/
public OpaqueLsa10(OpaqueLsaHeader lsaHeader) {
populateHeader(lsaHeader);
}
/**
* Returns the list of top level TLVs.
*
* @return list of top level TLVs
*/
public List<TopLevelTlv> topLevelValues() {
return topLevelValues;
}
/**
* Adds TLV value.
*
* @param value TLV value
*/
public void addValue(TopLevelTlv value) {
topLevelValues.add(value);
}
/**
* Reads from channel buffer and populate instance.
*
* @param channelBuffer channelBuffer instance
* @throws OspfParseException might throws exception while parsing buffer
*/
public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
try {
if (this.opaqueId() == OspfParameters.TRAFFIC_ENGINEERING) {
while (channelBuffer.readableBytes() > 0) {
TlvHeader tlvHeader = new TlvHeader();
tlvHeader.setTlvType(channelBuffer.readUnsignedShort());
tlvHeader.setTlvLength(channelBuffer.readUnsignedShort());
if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.ROUTER.value()) {
RouterTlv routerTlv = new RouterTlv(tlvHeader);
routerTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
this.addValue(routerTlv);
} else if (tlvHeader.tlvType() == OpaqueTopLevelTlvTypes.LINK.value()) {
LinkTlv linkTlv = new LinkTlv(tlvHeader);
linkTlv.readFrom(channelBuffer.readBytes(tlvHeader.tlvLength()));
this.addValue(linkTlv);
}
}
} else {
int length = channelBuffer.readableBytes();
opaqueInfo = new byte[length];
channelBuffer.readBytes(opaqueInfo, 0, length);
}
} catch (Exception e) {
log.debug("Error::OpaqueLsa10:: {}", e.getMessage());
throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
OspfErrorType.BAD_MESSAGE);
}
}
/**
* Returns instance as bytes.
*
* @return instance as bytes
* @throws Exception might throws exception while parsing packet
*/
public byte[] asBytes() throws Exception {
byte[] lsaMessage = null;
byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray();
byte[] lsaBody = getLsaBodyAsByteArray();
lsaMessage = Bytes.concat(lsaHeader, lsaBody);
return lsaMessage;
}
/**
* Gets the LSA body as byte array.
*
* @return the lsa body as byte array
* @throws Exception might throws exception while parsing packet
*/
public byte[] getLsaBodyAsByteArray() throws Exception {
List<Byte> bodyLst = new ArrayList<>();
if (this.opaqueId() == 1) {
for (TopLevelTlv tlv : this.topLevelValues) {
//Check the sub type of lsa and build bytes accordingly
if (tlv instanceof RouterTlv) {
RouterTlv routerTlv = (RouterTlv) tlv;
bodyLst.addAll(Bytes.asList(routerTlv.asBytes()));
} else if (tlv instanceof LinkTlv) {
LinkTlv linkTlv = (LinkTlv) tlv;
bodyLst.addAll(Bytes.asList(linkTlv.asBytes()));
}
}
} else {
return opaqueInfo;
}
return Bytes.toArray(bodyLst);
}
@Override
public OspfLsaType getOspfLsaType() {
return OspfLsaType.AREA_LOCAL_OPAQUE_LSA;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("topLevelValues", topLevelValues)
.add("opaqueInfo", opaqueInfo)
.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OpaqueLsa10 that = (OpaqueLsa10) o;
return Objects.equal(topLevelValues, that.topLevelValues) &&
Objects.equal(opaqueInfo, that.opaqueInfo);
}
@Override
public int hashCode() {
return Objects.hashCode(opaqueInfo, topLevelValues);
}
}
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implementation of the OSPF LSA types.
*/
package org.onosproject.ospf.protocol.lsa.types;
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.ospfpacket;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.exceptions.OspfErrorType;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A message reader which reads OSPF messages from ChannelBuffer and converts to OspfMessage instances.
*/
public class OspfMessageReader {
private static final Logger log = LoggerFactory.getLogger(OspfMessageReader.class);
/**
* Reads and Converts the channel buffer to OspfMessage instance.
*
* @param channelBuffer channel buffer instance.
* @return OSPF message instance.
* @throws Exception might throws exception while parsing buffer
*/
public OspfMessage readFromBuffer(ChannelBuffer channelBuffer)
throws Exception {
if (channelBuffer.readableBytes() < OspfUtil.PACKET_MINIMUM_LENGTH) {
log.error("Packet should have minimum length...");
throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
}
try {
OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer);
int len = ospfHeader.ospfPacLength() - OspfUtil.OSPF_HEADER_LENGTH;
OspfMessage ospfMessage = null;
switch (ospfHeader.ospfType()) {
case OspfParameters.HELLO:
ospfMessage = new HelloPacket(ospfHeader);
break;
case OspfParameters.DD:
ospfMessage = new DdPacket(ospfHeader);
break;
case OspfParameters.LSREQUEST:
ospfMessage = new LsRequest(ospfHeader);
break;
case OspfParameters.LSUPDATE:
ospfMessage = new LsUpdate(ospfHeader);
break;
case OspfParameters.LSACK:
ospfMessage = new LsAcknowledge(ospfHeader);
break;
default:
log.debug("Message Reader[Decoder] - Unknown LSA type..!!!");
break;
}
if (ospfMessage != null) {
try {
log.debug("{} Received::Message Length :: {} ", ospfMessage.ospfMessageType(),
ospfHeader.ospfPacLength());
ospfMessage.readFrom(channelBuffer.readBytes(len));
} catch (Exception e) {
throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
OspfErrorType.BAD_MESSAGE);
}
}
return ospfMessage;
} catch (Exception e) {
throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
OspfErrorType.BAD_MESSAGE);
}
}
/**
* Gets the OSPF packet Header.
*
* @param channelBuffer channel buffer instance.
* @return Ospf Header instance.
*/
private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception {
OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();
byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
// Determine ospf version & Packet Type
int version = channelBuffer.readByte(); //byte 1 is ospf version
int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type
// byte 3 & 4 combine is packet length.
int packetLength = channelBuffer.readShort();
if (packetLength > channelBuffer.readableBytes() + OspfUtil.FOUR_BYTES) {
log.error("Packet should have minimum length...");
throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
}
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
Ip4Address routerId = Ip4Address.valueOf(tempByteArray);
tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
Ip4Address areaId = Ip4Address.valueOf(tempByteArray);
int checkSum = channelBuffer.readUnsignedShort();
int auType = channelBuffer.readUnsignedShort();
int authentication = (int) channelBuffer.readLong();
ospfPacketHeader.setSourceIp(sourceIP);
ospfPacketHeader.setOspfVer(version);
ospfPacketHeader.setOspftype(packetType);
ospfPacketHeader.setOspfPacLength(packetLength);
ospfPacketHeader.setRouterId(routerId);
ospfPacketHeader.setAreaId(areaId);
ospfPacketHeader.setChecksum(checkSum);
ospfPacketHeader.setAuthType(auType);
ospfPacketHeader.setAuthentication(authentication);
return ospfPacketHeader;
}
}
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.ospfpacket;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A message writer which writes an OspfMessage to ChannelBuffer.
*/
public class OspfMessageWriter {
private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class);
/**
* Writes OSPF message to ChannelBuffer.
*
* @param ospfMessage OSPF message
* @param interfaceState interface state
* @param interfaceType interface type
* @return channelBuffer channel buffer instance
* @throws Exception might throws exception while parsing message
*/
public ChannelBuffer writeToBuffer(OspfMessage ospfMessage, int interfaceState,
int interfaceType) throws Exception {
ChannelBuffer buf = null;
switch (ospfMessage.ospfMessageType().value()) {
case OspfParameters.HELLO:
case OspfParameters.LSACK:
buf = writeMessageToBuffer(ospfMessage, interfaceState);
break;
case OspfParameters.DD:
case OspfParameters.LSREQUEST:
case OspfParameters.LSUPDATE:
buf = writeMessageToBuffer(ospfMessage, interfaceState);
break;
default:
log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!");
break;
}
return buf;
}
/**
* Writes an OSPF Message to channel buffer.
*
* @param ospfMessage OSPF Message instance
* @param interfaceState interface state
* @return channelBuffer instance
*/
private ChannelBuffer writeMessageToBuffer(OspfMessage ospfMessage, int interfaceState) throws Exception {
ChannelBuffer channelBuffer = null;
byte[] ospfMessageAsByte = ospfMessage.asBytes();
//Add the length and checksum in byte array at length position 2 & 3 and Checksum position
ospfMessageAsByte = OspfUtil.addLengthAndCheckSum(ospfMessageAsByte, OspfUtil.OSPFPACKET_LENGTH_POS1,
OspfUtil.OSPFPACKET_LENGTH_POS2,
OspfUtil.OSPFPACKET_CHECKSUM_POS1,
OspfUtil.OSPFPACKET_CHECKSUM_POS2);
//Add Interface State Info and destination IP as metadata
if (interfaceState == OspfParameters.DR || interfaceState == OspfParameters.BDR) {
ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
ospfMessage.destinationIp());
} else {
ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
ospfMessage.destinationIp());
}
channelBuffer = ChannelBuffers.buffer(ospfMessageAsByte.length);
channelBuffer.writeBytes(ospfMessageAsByte);
return channelBuffer;
}
}
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.lsa.types;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfLsaType;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.lsa.TlvHeader;
import org.onosproject.ospf.protocol.lsa.tlvtypes.LinkTlv;
import org.onosproject.ospf.protocol.lsa.tlvtypes.RouterTlv;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* Unit test class for OpaqueLsa10.
*/
public class OpaqueLsa10Test {
private final byte[] packet = {0, 1, 0, 4, 1, 1, 1, 1, 0, 2, 0, 84, 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, 0, 0, 0, 0, 0, 0, 0, 0};
private OpaqueLsa10 opaqueLsa10;
private TopLevelTlv tlv;
private OpaqueLsaHeader opqueHeader;
private ChannelBuffer channelBuffer;
private byte[] result;
private RouterTlv routerTlv;
private RouterTlv routerTlv1;
private LinkTlv linkTlv;
private LinkTlv linkTlv1;
private OspfLsaType ospflsaType;
private int result1;
private List result2;
@Before
public void setUp() throws Exception {
opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
}
@After
public void tearDown() throws Exception {
opaqueLsa10 = null;
tlv = null;
opqueHeader = null;
channelBuffer = null;
result = null;
routerTlv = null;
routerTlv1 = null;
linkTlv1 = null;
ospflsaType = null;
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(opaqueLsa10.toString(), is(notNullValue()));
}
/**
* Tests addValue() method.
*/
@Test
public void testAddValue() throws Exception {
tlv = new RouterTlv(new TlvHeader());
opaqueLsa10.addValue(tlv);
assertThat(opaqueLsa10, is(notNullValue()));
}
/**
* Tests readFrom() method.
*/
@Test(expected = Exception.class)
public void testReadFrom() throws Exception {
opqueHeader = new OpaqueLsaHeader();
opqueHeader.setLsType(10);
opqueHeader.setLsPacketLen(48);
opqueHeader.setLsCheckSum(10);
opqueHeader.setAge(4);
opqueHeader.setOpaqueId(1);
opqueHeader.setOpaqueType(10);
opqueHeader.setLsSequenceNo(250);
opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
opqueHeader.setOptions(66);
opaqueLsa10 = new OpaqueLsa10(opqueHeader);
channelBuffer = ChannelBuffers.copiedBuffer(packet);
opaqueLsa10.readFrom(channelBuffer);
result = opaqueLsa10.asBytes();
assertThat(result, is(notNullValue()));
}
/**
* Tests asBytes() method.
*/
@Test(expected = Exception.class)
public void testAsBytes() throws Exception {
opqueHeader = new OpaqueLsaHeader();
opqueHeader.setLsType(10);
opqueHeader.setLsPacketLen(48);
opqueHeader.setLsCheckSum(10);
opqueHeader.setAge(4);
opqueHeader.setOpaqueId(1);
opqueHeader.setOpaqueType(10);
opqueHeader.setLsSequenceNo(250);
opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
opqueHeader.setOptions(66);
opaqueLsa10 = new OpaqueLsa10(opqueHeader);
channelBuffer = ChannelBuffers.copiedBuffer(packet);
opaqueLsa10.readFrom(channelBuffer);
result = opaqueLsa10.getLsaBodyAsByteArray();
result = opaqueLsa10.asBytes();
assertThat(result, is(notNullValue()));
}
/**
* Tests getLsaBodyAsByteArray() method.
*/
@Test(expected = Exception.class)
public void testGetLsaBodyAsByteArray() throws Exception {
opqueHeader = new OpaqueLsaHeader();
opqueHeader.setLsType(10);
opqueHeader.setLsPacketLen(48);
opqueHeader.setLsCheckSum(10);
opqueHeader.setAge(4);
opqueHeader.setOpaqueId(1);
opqueHeader.setOpaqueType(10);
opqueHeader.setLsSequenceNo(250);
opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
opqueHeader.setOptions(2);
opaqueLsa10 = new OpaqueLsa10(opqueHeader);
routerTlv = new RouterTlv(new TlvHeader());
linkTlv = new LinkTlv(new TlvHeader());
opaqueLsa10.addValue(routerTlv);
opaqueLsa10.addValue(linkTlv);
routerTlv1 = new RouterTlv(new TlvHeader());
linkTlv1 = new LinkTlv(new TlvHeader());
opaqueLsa10.addValue(routerTlv1);
opaqueLsa10.addValue(linkTlv1);
channelBuffer = ChannelBuffers.copiedBuffer(packet);
opaqueLsa10.readFrom(channelBuffer);
result = opaqueLsa10.getLsaBodyAsByteArray();
assertThat(result, is(notNullValue()));
}
/**
* Tests getLsaBodyAsByteArray() method.
*/
@Test(expected = Exception.class)
public void testGetLsaBodyAsByteArray1() throws Exception {
opqueHeader = new OpaqueLsaHeader();
opqueHeader.setLsType(10);
opqueHeader.setLsPacketLen(48);
opqueHeader.setLsCheckSum(10);
opqueHeader.setAge(4);
opqueHeader.setOpaqueId(1);
opqueHeader.setOpaqueType(10);
opqueHeader.setLsSequenceNo(250);
opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
opqueHeader.setOptions(2);
opaqueLsa10 = new OpaqueLsa10(opqueHeader);
routerTlv = new RouterTlv(new TlvHeader());
opaqueLsa10.addValue(routerTlv);
channelBuffer = ChannelBuffers.copiedBuffer(packet);
result = opaqueLsa10.getLsaBodyAsByteArray();
assertThat(result, is(notNullValue()));
}
/**
* Tests getLsaBodyAsByteArray() method.
*/
@Test(expected = Exception.class)
public void testGetLsaBodyAsByteArray2() throws Exception {
opqueHeader = new OpaqueLsaHeader();
opqueHeader.setLsType(10);
opqueHeader.setLsPacketLen(48);
opqueHeader.setLsCheckSum(10);
opqueHeader.setAge(4);
opqueHeader.setOpaqueId(1);
opqueHeader.setOpaqueType(10);
opqueHeader.setLsSequenceNo(250);
opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
opqueHeader.setOptions(2);
opaqueLsa10 = new OpaqueLsa10(opqueHeader);
linkTlv = new LinkTlv(new TlvHeader());
opaqueLsa10.addValue(linkTlv);
channelBuffer = ChannelBuffers.copiedBuffer(packet);
opaqueLsa10.readFrom(channelBuffer);
result = opaqueLsa10.getLsaBodyAsByteArray();
assertThat(result, is(notNullValue()));
}
/**
* Tests getOspfLsaType() getter method.
*/
@Test
public void testGetOspfLsaType() throws Exception {
opaqueLsa10.setLsType(10);
ospflsaType = opaqueLsa10.getOspfLsaType();
assertThat(ospflsaType, is(notNullValue()));
assertThat(ospflsaType, is(OspfLsaType.AREA_LOCAL_OPAQUE_LSA));
}
/**
* Tests hashCode() method.
*/
@Test
public void testHashcode() throws Exception {
result1 = opaqueLsa10.hashCode();
assertThat(result1, is(notNullValue()));
}
/**
* Tests topLevelValues() method.
*/
@Test
public void testTopLevelValues() throws Exception {
result2 = opaqueLsa10.topLevelValues();
assertThat(result2, is(notNullValue()));
}
}
\ No newline at end of file
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.lsa.types;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.controller.OspfLsaType;
import org.onosproject.ospf.protocol.lsa.LsaHeader;
import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
/**
* Unit test class for RouterLsa.
*/
public class RouterLsaTest {
private RouterLsa routerLsa;
private int result1;
private OspfLsaLink ospflsaLink;
private byte[] inputArray;
private LsaHeader lsaHeader;
private ChannelBuffer channelBuffer;
private byte[] result2;
private OspfLsaType result3;
@Before
public void setUp() throws Exception {
routerLsa = new RouterLsa();
}
@After
public void tearDown() throws Exception {
routerLsa = null;
ospflsaLink = null;
inputArray = null;
lsaHeader = null;
channelBuffer = null;
result2 = null;
result3 = null;
}
/**
* Tests virtualEndPoint() setter method.
*/
@Test
public void testSetVirtualEndPoint() throws Exception {
routerLsa.setVirtualEndPoint(true);
assertThat(routerLsa, is(notNullValue()));
}
/**
* Tests isAsBoundaryRouter() setter method.
*/
@Test
public void testSetAsBoundaryRouter() throws Exception {
routerLsa.setAsBoundaryRouter(true);
assertThat(routerLsa, is(notNullValue()));
}
/**
* Tests areaBorderRouter() setter method.
*/
@Test
public void testSetAreaBorderRouter() throws Exception {
routerLsa.setAreaBorderRouter(true);
assertThat(routerLsa, is(notNullValue()));
}
/**
* Tests noLink() getter method.
*/
@Test
public void testGetNoLink() throws Exception {
routerLsa.setNoLink(10);
result1 = routerLsa.noLink();
assertThat(result1, is(10));
}
/**
* Tests noLink() setter method.
*/
@Test
public void testSetNoLink() throws Exception {
routerLsa.setNoLink(10);
result1 = routerLsa.noLink();
assertThat(result1, is(10));
}
/**
* Tests addRouterLink() method.
*/
@Test
public void testAddRouterLink() throws Exception {
routerLsa.setNoLink(0);
ospflsaLink = createOspfLsaLink();
routerLsa.addRouterLink(ospflsaLink);
routerLsa.incrementLinkNo();
result1 = routerLsa.noLink();
assertThat(result1, is(1));
}
/**
* Tests readFrom() method.
*/
@Test
public void testReadFrom() throws Exception {
ospflsaLink = createOspfLsaLink();
routerLsa.addRouterLink(ospflsaLink);
inputArray = createByteForRouterLsa();
lsaHeader = createLsaHeader();
routerLsa = new RouterLsa(lsaHeader);
channelBuffer = ChannelBuffers.copiedBuffer(inputArray);
routerLsa.readFrom(channelBuffer);
assertThat(routerLsa, is(notNullValue()));
}
/**
* Tests readFrom() method.
*/
@Test(expected = Exception.class)
public void testReadFrom1() throws Exception {
byte[] temp = {0, 0, 0};
ospflsaLink = createOspfLsaLink();
routerLsa.addRouterLink(ospflsaLink);
inputArray = temp;
lsaHeader = createLsaHeader();
routerLsa = new RouterLsa(lsaHeader);
channelBuffer = ChannelBuffers.copiedBuffer(inputArray);
routerLsa.readFrom(channelBuffer);
assertThat(routerLsa, is(notNullValue()));
}
/**
* Tests asBytes() method.
*/
@Test
public void testAsBytes() throws Exception {
result2 = routerLsa.asBytes();
assertThat(result2, is(notNullValue()));
}
/**
* Tests getLsaBodyAsByteArray() method.
*/
@Test
public void testGetLsaBodyAsByteArray() throws Exception {
routerLsa.setAreaBorderRouter(true);
routerLsa.setVirtualEndPoint(true);
routerLsa.setAreaBorderRouter(true);
ospflsaLink = createOspfLsaLink();
routerLsa.addRouterLink(ospflsaLink);
result2 = routerLsa.getLsaBodyAsByteArray();
assertThat(result2, is(notNullValue()));
}
/**
* Tests getOspfLsaType() getter method.
*/
@Test
public void testGetOspfLsaType() throws Exception {
routerLsa.setLsType(1);
result3 = routerLsa.getOspfLsaType();
assertThat(result3, is(OspfLsaType.ROUTER));
}
/**
* Tests incrementLinkNo() method.
*/
@Test
public void testIncrementLinkNo() throws Exception {
routerLsa.setNoLink(1);
routerLsa.incrementLinkNo();
assertThat(routerLsa.noLink(), is(2));
}
/**
* Tests lsaHeader() method.
*/
@Test
public void testGetLsaHeader() throws Exception {
lsaHeader = (LsaHeader) routerLsa.lsaHeader();
assertThat(lsaHeader, instanceOf(RouterLsa.class));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(routerLsa.toString(), is(notNullValue()));
}
/**
* Utility method used by junit methods.
*/
private OspfLsaLink createOspfLsaLink() {
ospflsaLink = new OspfLsaLink();
ospflsaLink.setLinkId("10.226.165.164");
ospflsaLink.setMetric(10);
ospflsaLink.setTos(50);
ospflsaLink.setLinkType(2);
ospflsaLink.setLinkData("10.226.165.170");
return ospflsaLink;
}
/**
* Utility method used by junit methods.
*/
private byte[] createByteForRouterLsa() {
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,
-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,
1};
return packet;
}
/**
* Utility method used by junit methods.
*/
private LsaHeader createLsaHeader() {
lsaHeader = new LsaHeader();
lsaHeader.setLsType(1);
lsaHeader.setLsPacketLen(48);
lsaHeader.setLsCheckSum(10);
lsaHeader.setAge(4);
lsaHeader.setLinkStateId("10.226.165.164");
lsaHeader.setLsSequenceNo(250);
lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
lsaHeader.setOptions(2);
return lsaHeader;
}
/**
* Tests hashcode() method.
*/
@Test
public void testHashcode() throws Exception {
result1 = routerLsa.hashCode();
assertThat(result1, is(notNullValue()));
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.ospfpacket;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* Unit test class for OspfMessageReader.
*/
public class OspfMessageReaderTest {
private final byte[] packet1 = {1, 1, 1, 1, 2, 1, 0, 44, -64, -88, -86, 8,
0, 0, 0, 1, 39, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0,
10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
private final byte[] packet2 = {1, 1, 1, 1, 2, 2, 0, 52, -64, -88, -86, 8, 0,
0, 0, 1, -96, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119,
-87, 126, 0, 23, 2, 1, 10, 10, 10, 10, 10, 10, 10, 10, -128, 0, 0, 6,
-69, 26, 0, 36};
private final byte[] packet3 = {1, 1, 1, 1, 2, 3, 0, 36, -64, -88, -86, 3, 0,
0, 0, 1, -67, -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88,
-86, 8, -64, -88, -86, 8};
private final byte[] packet4 = {1, 1, 1, 1, 2, 4, 1, 36, -64, -88, -86, 3, 0,
0, 0, 1, 54, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0,
2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0
, 0, 1, 58, -100, 0, 48, 2, 0, 0, 2, -64, -88, -86
, 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0,
-1, -1, -1, 0, 3, 0, 0, 10, 0, 3, 2, 5, 80, -44,
16, 0, -64, -88, -86, 2, -128, 0, 0, 1, 42, 73, 0,
36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88,
-86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1,
0, -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0,
3, 2, 5, -64, -126, 120, 0, -64, -88, -86, 2, -128, 0,
0, 1, -45, 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20,
0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0,
-64, -88, -86, 2, -128, 0, 0, 1, 55, 8, 0, 36, -1, -1
, -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3, 2, 5, -64, -88, 1, 0, -64, -88, -86, 2, -128, 0, 0,
1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0,
0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64,
-88, -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0,
-128, 0, 0, 20, -64, -88, -86, 10, 0, 0, 0, 0};
private final byte[] packet5 = {1, 1, 1, 1, 2, 5, 0, 44, -64, -88, -86, 8, 0, 0,
0, 1, -30, -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
private OspfMessageReader ospfMessageReader;
private ChannelBuffer channelBuffer;
@Before
public void setUp() throws Exception {
ospfMessageReader = new OspfMessageReader();
}
@After
public void tearDown() throws Exception {
ospfMessageReader = null;
channelBuffer = null;
}
/**
* Tests readFromBuffer() method.
*/
@Test
public void testReadFromBuffer() throws Exception {
channelBuffer = ChannelBuffers.copiedBuffer(packet1);
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet2);
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet3);
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet4);
ospfMessageReader.readFromBuffer(channelBuffer);
channelBuffer = ChannelBuffers.copiedBuffer(packet5);
ospfMessageReader.readFromBuffer(channelBuffer);
assertThat(ospfMessageReader, is(notNullValue()));
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ospf.protocol.ospfpacket;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket;
import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Unit test class for OspfMessageWriter.
*/
public class OspfMessageWriterTest {
private OspfMessageWriter ospfMessageWriter;
private HelloPacket helloPacket;
private DdPacket ddPacket;
private LsAcknowledge lsAck;
private LsRequest lsReq;
private LsUpdate lsUpdate;
@Before
public void setUp() throws Exception {
ospfMessageWriter = new OspfMessageWriter();
}
@After
public void tearDown() throws Exception {
ospfMessageWriter = null;
helloPacket = null;
ddPacket = null;
lsAck = null;
lsReq = null;
lsUpdate = null;
}
/**
* Tests writeToBuffer() method.
*/
@Test
public void testWriteToBuffer() throws Exception {
helloPacket = new HelloPacket();
helloPacket.setAuthType(1);
helloPacket.setOspftype(1);
helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
helloPacket.setChecksum(201);
helloPacket.setAuthentication(2);
helloPacket.setOspfPacLength(48);
helloPacket.setOspfVer(2);
helloPacket.setNetworkMask(Ip4Address.valueOf("255.255.255.255"));
helloPacket.setOptions(2); //not setting now
helloPacket.setHelloInterval(10);
helloPacket.setRouterPriority(1);
helloPacket.setRouterDeadInterval(40);
helloPacket.setDr(Ip4Address.valueOf("1.1.1.1"));
helloPacket.setBdr(Ip4Address.valueOf("2.2.2.2"));
helloPacket.addNeighbor(Ip4Address.valueOf("8.8.8.8"));
helloPacket.setDestinationIp(Ip4Address.valueOf("5.5.5.5"));
ospfMessageWriter.writeToBuffer(helloPacket, 7, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer1() throws Exception {
ddPacket = new DdPacket();
ddPacket.setAuthType(1);
ddPacket.setOspftype(2);
ddPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
ddPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
ddPacket.setChecksum(201);
ddPacket.setAuthentication(2);
ddPacket.setOspfPacLength(48);
ddPacket.setOspfVer(2);
ospfMessageWriter.writeToBuffer(ddPacket, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer2() throws Exception {
lsAck = new LsAcknowledge();
lsAck.setAuthType(1);
lsAck.setOspftype(5);
lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
lsAck.setChecksum(201);
lsAck.setAuthentication(2);
lsAck.setOspfPacLength(48);
lsAck.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer3() throws Exception {
lsReq = new LsRequest();
lsReq.setAuthType(1);
lsReq.setOspftype(3);
lsReq.setRouterId(Ip4Address.valueOf("10.226.165.164"));
lsReq.setAreaId(Ip4Address.valueOf("10.226.165.100"));
lsReq.setChecksum(201);
lsReq.setAuthentication(2);
lsReq.setOspfPacLength(48);
lsReq.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsReq, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
@Test(expected = Exception.class)
public void testWriteToBuffer4() throws Exception {
lsUpdate = new LsUpdate();
lsUpdate.setAuthType(1);
lsUpdate.setOspftype(3);
lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164"));
lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100"));
lsUpdate.setChecksum(201);
lsUpdate.setAuthentication(2);
lsUpdate.setOspfPacLength(48);
lsUpdate.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsUpdate, 1, 1);
assertThat(ospfMessageWriter, is(notNullValue()));
}
/**
* Tests writeToBuffer() method.
*/
@Test(expected = Exception.class)
public void testWriteToBuffer5() throws Exception {
lsAck = new LsAcknowledge();
lsAck.setAuthType(1);
lsAck.setOspftype(5);
lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
lsAck.setChecksum(201);
lsAck.setAuthentication(2);
lsAck.setOspfPacLength(48);
lsAck.setOspfVer(2);
ospfMessageWriter.writeToBuffer(lsAck, 1, 1);
}
}
\ No newline at end of file