Mani
Committed by Gerrit Code Review

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

Change-Id: I33b48593ec38d28bdff215ce3a608a6d085a9077
/*
* 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.subtype;
import com.google.common.base.MoreObjects;
/**
* Representation of an LS Request packet and fields and access methods to access it.
*/
public class LsRequestPacket {
private int lsType;
private String linkStateId;
private String ownRouterId;
/**
* Gets the LSA type.
*
* @return LSA type
*/
public int lsType() {
return lsType;
}
/**
* Sets the LSA type.
*
* @param lsType LSA type
*/
public void setLsType(int lsType) {
this.lsType = lsType;
}
/**
* Gets the link state id.
*
* @return link state id
*/
public String linkStateId() {
return linkStateId;
}
/**
* Sets link state id.
*
* @param linkStateId state id
*/
public void setLinkStateId(String linkStateId) {
this.linkStateId = linkStateId;
}
/**
* Gets the router id.
*
* @return router id
*/
public String ownRouterId() {
return ownRouterId;
}
/**
* Sets the router id.
*
* @param ownRouterId router id
*/
public void setOwnRouterId(String ownRouterId) {
this.ownRouterId = ownRouterId;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("lsType", lsType)
.add("linkStateId", linkStateId)
.add("ownRouterId", ownRouterId)
.toString();
}
}
\ 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 packet sub types which is used in OSPF packets..
*/
package org.onosproject.ospf.protocol.ospfpacket.subtype;
\ 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.types;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.ospf.exceptions.OspfErrorType;
import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.lsa.LsaHeader;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import org.onosproject.ospf.protocol.util.OspfParameters;
import org.onosproject.ospf.protocol.util.OspfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Representation of an OSPF Link State Acknowledgment Message.
* Link State Acknowledgment Packets are OSPF packet type 5.
* To make the flooding of LSAs reliable, flooded LSAs are explicitly
* acknowledged. This acknowledgment is accomplished through the
* sending and receiving of Link State Acknowledgment packets.
* Multiple LSAs can be acknowledged in a single Link State Acknowledgment packet.
*/
public class LsAcknowledge extends OspfPacketHeader {
/*
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version # | 5 | Packet length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Router ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Area ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | AuType |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Authentication |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Authentication |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+- -+
| |
+- An LSA Header -+
| |
+- -+
| |
+- -+
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ... |
*/
private static final Logger log = LoggerFactory.getLogger(LsAcknowledge.class);
private List<LsaHeader> linkStateHeaders = new ArrayList<>();
/**
* Creates an instance of Link State Acknowledgment instance.
*/
public LsAcknowledge() {
}
/**
* Creates an instance of Link State Acknowledgment instance.
*
* @param ospfHeader OSPF header instance.
*/
public LsAcknowledge(OspfPacketHeader ospfHeader) {
populateHeader(ospfHeader);
}
/**
* Gets ls headers.
*
* @return ls headers
*/
public List<LsaHeader> getLinkStateHeaders() {
return linkStateHeaders;
}
/**
* Adds link state header to list.
*
* @param lsaHeader LSA header
*/
public void addLinkStateHeader(LsaHeader lsaHeader) {
if (!linkStateHeaders.contains(lsaHeader)) {
linkStateHeaders.add(lsaHeader);
}
}
@Override
public OspfPacketType ospfMessageType() {
return OspfPacketType.LSAACK;
}
@Override
public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
try {
//add all the LSA Headers - one header is of 20 bytes
while (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) {
LsaHeader header = OspfUtil.readLsaHeader(channelBuffer);
//add the LSAHeader to acknowledge
addLinkStateHeader(header);
}
} catch (Exception e) {
log.debug("Error::LsAckPacket:: {}", e.getMessage());
throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
}
}
@Override
public byte[] asBytes() {
byte[] lsAckMessage = null;
byte[] lsAckHeader = getLsAckAsByteArray();
byte[] lsAckBody = getLsAckBodyAsByteArray();
lsAckMessage = Bytes.concat(lsAckHeader, lsAckBody);
return lsAckMessage;
}
/**
* Gets LSAcknowledge as byte array.
*
* @return byte array
*/
public byte[] getLsAckAsByteArray() {
List<Byte> headerLst = new ArrayList<>();
try {
headerLst.add((byte) this.ospfVersion());
headerLst.add((byte) this.ospfType());
headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
//Authentication is 0 always. Total 8 bytes consist of zero
byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
headerLst.addAll(Bytes.asList(auth));
} catch (Exception e) {
log.debug("Error::LsAckPacket:: {}", e.getMessage());
return Bytes.toArray(headerLst);
}
return Bytes.toArray(headerLst);
}
/**
* Gets LsAck body as byte array.
*
* @return byte array
*/
public byte[] getLsAckBodyAsByteArray() {
List<Byte> bodyLst = new ArrayList<>();
try {
for (LsaHeader lsaHeader : linkStateHeaders) {
if (lsaHeader.lsType() == OspfParameters.LINK_LOCAL_OPAQUE_LSA ||
lsaHeader.lsType() == OspfParameters.AREA_LOCAL_OPAQUE_LSA ||
lsaHeader.lsType() == OspfParameters.AS_OPAQUE_LSA) {
OpaqueLsaHeader header = (OpaqueLsaHeader) lsaHeader;
bodyLst.addAll(Bytes.asList(header.getOpaqueLsaHeaderAsByteArray()));
} else {
bodyLst.addAll(Bytes.asList(lsaHeader.getLsaHeaderAsByteArray()));
}
}
} catch (Exception e) {
log.debug("Error::getLsAckBodyAsByteArray {}", e.getMessage());
return Bytes.toArray(bodyLst);
}
return Bytes.toArray(bodyLst);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("linkStateHeaders", linkStateHeaders)
.toString();
}
}
\ 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.util;
/**
* Representation of different OSPF packet types.
*/
public enum OspfPacketType {
HELLO(1),
DD(2),
LSREQUEST(3),
LSUPDATE(4),
LSAACK(5);
private int value;
/**
* Creates instance of OSPF packet types.
*
* @param value
*/
OspfPacketType(int value) {
this.value = value;
}
/**
* Gets the value.
*
* @return value
*/
public int value() {
return value;
}
}
\ No newline at end of file
/*
* Copyright 2014 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 Packet.
*/
package org.onosproject.ospf.protocol.ospfpacket.types;
/*
* 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.subtype;
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 LsRequestPacket.
*/
public class LsRequestPacketTest {
private LsRequestPacket lsrPacket;
private int result;
@Before
public void setUp() throws Exception {
lsrPacket = new LsRequestPacket();
}
@After
public void tearDown() throws Exception {
lsrPacket = null;
}
/**
* Tests lsType() getter method.
*/
@Test
public void testGetLsType() throws Exception {
lsrPacket.setLsType(1);
assertThat(lsrPacket.lsType(), is(1));
}
/**
* Tests lsType() setter method.
*/
@Test
public void testSetLsType() throws Exception {
lsrPacket.setLsType(1);
assertThat(lsrPacket.lsType(), is(1));
}
/**
* Tests linkStateId() getter method.
*/
@Test
public void testGetLinkStateId() throws Exception {
lsrPacket.setLinkStateId("1.1.1.1");
assertThat(lsrPacket.linkStateId(), is("1.1.1.1"));
}
/**
* Tests linkStateId() setter method.
*/
@Test
public void testSetLinkStateId() throws Exception {
lsrPacket.setLinkStateId("1.1.1.1");
assertThat(lsrPacket.linkStateId(), is("1.1.1.1"));
}
/**
* Tests ownRouterId() getter method.
*/
@Test
public void testGetOwnRouterId() throws Exception {
lsrPacket.setOwnRouterId("1.1.1.1");
assertThat(lsrPacket.ownRouterId(), is("1.1.1.1"));
}
/**
* Tests ownRouterId() setter method.
*/
@Test
public void testSetOwnRouterId() throws Exception {
lsrPacket.setOwnRouterId("1.1.1.1");
assertThat(lsrPacket.ownRouterId(), is("1.1.1.1"));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(lsrPacket.toString(), is(notNullValue()));
}
/**
* Tests equals() method.
*/
@Test
public void testEquals() throws Exception {
assertThat(lsrPacket.equals(new LsRequestPacket()), is(false));
}
/**
* Tests hashCode() method.
*/
@Test
public void testHashCode() throws Exception {
result = lsrPacket.hashCode();
assertThat(result, 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.ospfpacket.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.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import java.util.Vector;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Unit test class for HelloPacket.
*/
public class HelloPacketTest {
private boolean result1;
private OspfPacketType ospfPacketType;
private OspfPacketHeader ospfPacketHeader;
private HelloPacket helloPacket;
private Vector<String> neighborAddress = new Vector();
private Ip4Address result;
private int result2;
private byte[] packet;
private ChannelBuffer channelBuffer;
private byte[] result3;
@Before
public void setUp() throws Exception {
helloPacket = new HelloPacket();
helloPacket.setAuthType(1);
helloPacket.setOspftype(2);
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);
}
@After
public void tearDown() throws Exception {
helloPacket = null;
result = null;
ospfPacketType = null;
ospfPacketHeader = null;
packet = null;
channelBuffer = null;
result3 = null;
}
/**
* Tests networkMask() getter method.
*/
@Test
public void testGetNetworkMask() throws Exception {
helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
result = helloPacket.networkMask();
assertThat(result, is(notNullValue()));
assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
}
/**
* Tests networkMask() setter method.
*/
@Test
public void testSetNetworkMask() throws Exception {
helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
result = helloPacket.networkMask();
assertThat(result, is(notNullValue()));
assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
}
/**
* Tests bdr() setter method.
*/
@Test
public void testSetBdr() throws Exception {
helloPacket.setBdr(Ip4Address.valueOf("10.226.165.166"));
result = helloPacket.bdr();
assertThat(result, is(notNullValue()));
assertThat(result, is(Ip4Address.valueOf("10.226.165.166")));
}
/**
* Tests dr() getter method.
*/
@Test
public void testGetDr() throws Exception {
helloPacket.setDr(Ip4Address.valueOf("10.226.165.167"));
result = helloPacket.dr();
assertThat(result, is(notNullValue()));
assertThat(result, is(Ip4Address.valueOf("10.226.165.167")));
}
/**
* Tests dr() setter method.
*/
@Test
public void testSetDr() throws Exception {
helloPacket.setDr(Ip4Address.valueOf("10.226.165.167"));
result = helloPacket.dr();
assertThat(result, is(notNullValue()));
assertThat(result, is(Ip4Address.valueOf("10.226.165.167")));
}
/**
* Tests addNeighbor() method.
*/
@Test
public void testAddNeighbor() throws Exception {
helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.170"));
result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.170"));
assertThat(result1, is(true));
}
/**
* Tests containsNeighbour() method.
*/
@Test
public void testContainsNeighbour() throws Exception {
helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.200"));
result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.200"));
assertThat(result1, is(true));
}
/**
* Tests options() getter method.
*/
@Test
public void testGetOptions() throws Exception {
helloPacket.setOptions(10);
result2 = helloPacket.options();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(10));
}
/**
* Tests options() setter method.
*/
@Test
public void testSetOptions() throws Exception {
helloPacket.setOptions(11);
result2 = helloPacket.options();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(11));
}
/**
* Tests routerPriority() getter method.
*/
@Test
public void testGetRouterPriority() throws Exception {
helloPacket.setRouterPriority(1);
result2 = helloPacket.routerPriority();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(1));
}
/**
* Tests routerPriority() setter method.
*/
@Test
public void testSetRouterPriority() throws Exception {
helloPacket.setRouterPriority(2);
result2 = helloPacket.routerPriority();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(2));
}
/**
* Tests helloInterval() getter method.
*/
@Test
public void testGetHelloInterval() throws Exception {
helloPacket.setHelloInterval(10);
result2 = helloPacket.helloInterval();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(10));
}
/**
* Tests helloInterval() setter method.
*/
@Test
public void testSetHelloInterval() throws Exception {
helloPacket.setHelloInterval(10);
result2 = helloPacket.helloInterval();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(10));
}
/**
* Tests routerDeadInterval() getter method.
*/
@Test
public void testGetRouterDeadInterval() throws Exception {
helloPacket.setRouterDeadInterval(50);
result2 = helloPacket.routerDeadInterval();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(50));
}
/**
* Tests routerDeadInterval() setter method.
*/
@Test
public void testSetRouterDeadInterval() throws Exception {
helloPacket.setRouterDeadInterval(50);
result2 = helloPacket.routerDeadInterval();
assertThat(result2, is(notNullValue()));
assertThat(result2, is(50));
}
/**
* Tests ospfMessageType() getter method.
*/
@Test
public void testGetOspfMessageType() throws Exception {
ospfPacketType = helloPacket.ospfMessageType();
assertThat(ospfPacketType, is(notNullValue()));
assertThat(ospfPacketType, is(OspfPacketType.HELLO));
}
/**
* Tests readFrom() method.
*/
@Test
public void testReadFrom() throws Exception {
ospfPacketHeader = new OspfPacketHeader();
ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
ospfPacketHeader.setAuthentication(0);
ospfPacketHeader.setAuthType(0);
ospfPacketHeader.setChecksum(12345);
ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
ospfPacketHeader.setOspfPacLength(56);
ospfPacketHeader.setOspftype(1);
ospfPacketHeader.setOspfVer(2);
ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
packet = createByteForHelloPacket();
channelBuffer = ChannelBuffers.copiedBuffer(packet);
helloPacket.readFrom(channelBuffer);
assertThat(helloPacket, is(notNullValue()));
assertThat(helloPacket.ospfMessageType(), is(OspfPacketType.HELLO));
}
/**
* Tests asBytes() method.
*/
@Test
public void testAsBytes() throws Exception {
result3 = helloPacket.asBytes();
assertThat(result3, is(notNullValue()));
}
/**
* Tests getHelloHeaderAsByteArray() method.
*/
@Test
public void testGetHelloHeaderAsByteArray() throws Exception {
result3 = helloPacket.getHelloHeaderAsByteArray();
assertThat(result3, is(notNullValue()));
}
/**
* Tests getHelloBodyAsByteArray() method.
*/
@Test
public void testGetHelloBodyAsByteArray() throws Exception {
neighborAddress.add("10.226.165.100");
result3 = helloPacket.getHelloBodyAsByteArray();
assertThat(result3, is(notNullValue()));
}
/**
* Tests getHelloBodyAsByteArray() method.
*/
@Test
public void testReadHelloBody() throws Exception {
helloPacket.getHelloBodyAsByteArray();
assertThat(helloPacket, is(notNullValue()));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
assertThat(helloPacket.toString(), is(notNullValue()));
}
/**
* Utility method used by junit methods.
*/
private byte[] createByteForHelloPacket() {
byte[] helloPacket = {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};
return helloPacket;
}
}
\ 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.types;
import org.hamcrest.MatcherAssert;
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.protocol.lsa.LsaHeader;
import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Unit test class for LsAck.
*/
public class LsAcknowledgeTest {
private LsaHeader lsaHeader;
private LsAcknowledge lsAck;
private OspfPacketType ospfPacketType;
private OspfPacketHeader ospfPacketHeader;
private byte[] result;
private ChannelBuffer channelBuffer;
private OpaqueLsaHeader opaqueLsaHeader;
@Before
public void setUp() throws Exception {
lsaHeader = new LsaHeader();
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);
}
@After
public void tearDown() throws Exception {
lsaHeader = null;
lsAck = null;
ospfPacketType = null;
ospfPacketHeader = null;
result = null;
channelBuffer = null;
opaqueLsaHeader = null;
}
/**
* Tests getLinkStateHeaders() getter method.
*/
@Test
public void testGetLinkStateHeaders() throws Exception {
lsaHeader = createLsaHeader();
lsAck.addLinkStateHeader(lsaHeader);
lsAck.addLinkStateHeader(lsaHeader);
List headers = lsAck.getLinkStateHeaders();
assertThat(headers.size(), is(1));
}
/**
* Tests addLinkStateHeader() method.
*/
@Test
public void testAddLinkStateHeader() throws Exception {
lsaHeader = createLsaHeader();
lsAck.addLinkStateHeader(lsaHeader);
lsAck.addLinkStateHeader(lsaHeader);
assertThat(lsAck, is(notNullValue()));
}
/**
* Tests ospfMessageType() getter method.
*/
@Test
public void testGetOSPFMessageType() throws Exception {
ospfPacketType = lsAck.ospfMessageType();
assertThat(ospfPacketType, is(notNullValue()));
assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
}
/**
* Tests ospfMessageType() getter method.
*/
@Test
public void testGetOspfMessageType() throws Exception {
ospfPacketType = lsAck.ospfMessageType();
assertThat(ospfPacketType, is(notNullValue()));
assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
}
/**
* Tests readFrom() method.
*/
@Test
public void testReadFrom() throws Exception {
ospfPacketHeader = new OspfPacketHeader();
ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
ospfPacketHeader.setAuthentication(0);
ospfPacketHeader.setAuthType(0);
ospfPacketHeader.setChecksum(12345);
ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
ospfPacketHeader.setOspfPacLength(56);
ospfPacketHeader.setOspftype(5);
ospfPacketHeader.setOspfVer(2);
ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
result = createByteForLSAck();
lsAck = new LsAcknowledge(ospfPacketHeader);
channelBuffer = ChannelBuffers.copiedBuffer(result);
lsAck.readFrom(channelBuffer);
assertThat(lsAck, is(notNullValue()));
assertThat(lsAck.ospfMessageType(), is(OspfPacketType.LSAACK));
}
/**
* Tests asBytes() method.
*/
@Test
public void testAsBytes() throws Exception {
result = lsAck.asBytes();
assertThat(result, is(notNullValue()));
}
/**
* Tests getLsAckAsByteArray() method.
*/
@Test
public void testGetLsAckAsByteArray() throws Exception {
result = lsAck.getLsAckAsByteArray();
assertThat(result, is(notNullValue()));
}
/**
* Tests getLsAckBodyAsByteArray() method.
*/
@Test
public void testGetLsAckBodyAsByteArray() throws Exception {
lsaHeader = createLsaHeader();
opaqueLsaHeader = new OpaqueLsaHeader();
lsAck.addLinkStateHeader(lsaHeader);
lsAck.addLinkStateHeader(opaqueLsaHeader);
result = lsAck.getLsAckBodyAsByteArray();
assertThat(result, is(notNullValue()));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
MatcherAssert.assertThat(lsAck.toString(), is(notNullValue()));
}
/**
* Utility method used by junit methods.
*/
private LsaHeader createLsaHeader() {
lsaHeader = new LsaHeader();
lsaHeader.setAge(10);
lsaHeader.setLinkStateId("10.226.165.164");
lsaHeader.setLsCheckSum(222);
lsaHeader.setLsPacketLen(48);
lsaHeader.setLsSequenceNo(2020);
lsaHeader.setLsType(5);
lsaHeader.setOptions(2);
lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165"));
return lsaHeader;
}
/**
* Utility method used by junit methods.
*/
private byte[] createByteForLSAck() {
byte[] lsAckPacket = {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};
return lsAckPacket;
}
}
\ 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.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.protocol.ospfpacket.OspfPacketHeader;
import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket;
import org.onosproject.ospf.protocol.util.OspfPacketType;
import java.net.UnknownHostException;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Unit test class for LsRequest.
*/
public class LsRequestTest {
private LsRequest lsRequest;
private List<LsRequestPacket> result;
private OspfPacketType ospfMessageType;
private OspfPacketHeader ospfPacketHeader;
private byte[] result1;
private String result2;
private ChannelBuffer channelBuffer;
private LsRequestPacket lsRequestPacket;
@Before
public void setUp() throws Exception {
lsRequest = new LsRequest();
lsRequest.setAuthType(1);
lsRequest.setOspftype(3);
lsRequest.setRouterId(Ip4Address.valueOf("10.226.165.164"));
lsRequest.setAreaId(Ip4Address.valueOf("10.226.165.163"));
lsRequest.setChecksum(201);
lsRequest.setAuthentication(2);
lsRequest.setOspfPacLength(48);
lsRequest.setOspfVer(2);
}
@After
public void tearDown() throws Exception {
lsRequest = null;
result = null;
ospfMessageType = null;
ospfPacketHeader = null;
result1 = null;
channelBuffer = null;
lsRequestPacket = null;
}
/**
* Tests addLinkStateRequests() method.
*/
@Test
public void testAddLinkStateRequests() throws Exception {
lsRequest.addLinkStateRequests(createLsRequestPacket());
result = lsRequest.getLinkStateRequests();
assertThat(result, is(notNullValue()));
assertThat(result.size(), is(1));
}
/**
* Tests getLinkStateRequests() method.
*/
@Test
public void testGetLinkStateRequests() throws Exception {
lsRequest.addLinkStateRequests(createLsRequestPacket());
lsRequest.addLinkStateRequests(new LsRequestPacket());
result = lsRequest.getLinkStateRequests();
assertThat(result, is(notNullValue()));
assertThat(result.size(), is(2));
}
/**
* Tests ospfMessageType()getter method.
*/
@Test
public void testGetOspfMessageType() throws Exception {
ospfMessageType = lsRequest.ospfMessageType();
assertThat(ospfMessageType, is(notNullValue()));
assertThat(ospfMessageType, is(OspfPacketType.LSREQUEST));
}
/**
* Tests readFrom() method.
*/
@Test
public void testReadFrom() throws Exception {
ospfPacketHeader = new OspfPacketHeader();
ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
ospfPacketHeader.setAuthentication(0);
ospfPacketHeader.setAuthType(0);
ospfPacketHeader.setChecksum(12345);
ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
ospfPacketHeader.setOspfPacLength(56);
ospfPacketHeader.setOspftype(3);
ospfPacketHeader.setOspfVer(2);
ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
lsRequest = new LsRequest(ospfPacketHeader);
result1 = createByteLsReqestPacket();
channelBuffer = ChannelBuffers.copiedBuffer(result1);
lsRequest.readFrom(channelBuffer);
assertThat(lsRequest, is(notNullValue()));
assertThat(lsRequest.ospfMessageType(), is(OspfPacketType.LSREQUEST));
}
/**
* Tests asBytes() method.
*/
@Test
public void testAsBytes() throws Exception {
result1 = lsRequest.asBytes();
assertThat(result1, is(notNullValue()));
}
/**
* Tests getLsrHeaderAsByteArray() method.
*/
@Test
public void testGetLsrHeaderAsByteArray() throws Exception {
result1 = lsRequest.getLsrHeaderAsByteArray();
assertThat(result1, is(notNullValue()));
}
/**
* Tests getLsrBodyAsByteArray() method.
*/
@Test
public void testGetLsrBodyAsByteArray() throws Exception {
lsRequest.addLinkStateRequests(createLsRequestPacket());
lsRequest.addLinkStateRequests(new LsRequestPacket());
result1 = lsRequest.getLsrBodyAsByteArray();
assertThat(result1, is(notNullValue()));
}
/**
* Tests to string method.
*/
@Test
public void testToString() throws Exception {
result2 = lsRequest.toString();
assertThat(result2, is(notNullValue()));
}
private LsRequestPacket createLsRequestPacket() throws UnknownHostException {
lsRequestPacket = new LsRequestPacket();
lsRequestPacket.setOwnRouterId("165");
lsRequestPacket.setLinkStateId("10.226.165.164");
lsRequestPacket.setLsType(2);
return lsRequestPacket;
}
private byte[] createByteLsReqestPacket() {
byte[] lsRequestPacket = {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};
return lsRequestPacket;
}
}
\ No newline at end of file