diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/subtype/LsRequestPacket.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/subtype/LsRequestPacket.java new file mode 100644 index 0000000000..8d254de3ee --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/subtype/LsRequestPacket.java @@ -0,0 +1,92 @@ +/* + * 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 diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/subtype/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/subtype/package-info.java new file mode 100644 index 0000000000..5fa10f119a --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/subtype/package-info.java @@ -0,0 +1,20 @@ +/* + * 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 diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/DdPacket.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/DdPacket.java new file mode 100644 index 0000000000..82fe90900f --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/DdPacket.java @@ -0,0 +1,420 @@ +/* + * 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 Database Description packet. + * Database Description packets are OSPF packet type 2. + * These packets are exchanged when an adjacency is being initialized. + * They describe the contents of the link-state database. + */ +public class DdPacket 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 # | 2 | Packet length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Router ID | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Area ID | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Checksum | AuType | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Authentication | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Authentication | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Interface MTU | Options |0|0|0|0|0|I|M|MS + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | DD sequence number | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + +- -+ + | | + +- An LSA Header -+ + | | + +- -+ + | | + +- -+ + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | ... | + */ + private static final Logger log = LoggerFactory.getLogger(DdPacket.class); + private int imtu; + private int options; + private int ims; // initialize , more but / master slave bit + private int isMaster; + private int isInitialize; + private int isMore; + private long sequenceNo; + private boolean isOpaqueCapable; + private List lsaHeaderList = new ArrayList<>(); + + /** + * Creates an instance of DD packet. + */ + public DdPacket() { + } + + /** + * Creates an instance of DD packet. + * + * @param ospfHeader OSPF header instance + */ + public DdPacket(OspfPacketHeader ospfHeader) { + populateHeader(ospfHeader); + } + + /** + * Gets is opaque capable or not. + * + * @return true if opaque capable else false + */ + public boolean isOpaqueCapable() { + return isOpaqueCapable; + } + + /** + * Sets is opaque capable or not. + * + * @param isOpaqueCapable true or false + */ + public void setIsOpaqueCapable(boolean isOpaqueCapable) { + this.isOpaqueCapable = isOpaqueCapable; + } + + /** + * Gets IMS value. + * + * @return IMS bits as an int value + */ + public int ims() { + return ims; + } + + /** + * Sets IMS value. + * + * @param ims IMS value + */ + public void setIms(int ims) { + this.ims = ims; + } + + /** + * Gets master bit value. + * + * @return 1 if master else 0 + */ + public int isMaster() { + return isMaster; + } + + /** + * Sets master value. + * + * @param isMaster 1 represents master + */ + public void setIsMaster(int isMaster) { + this.isMaster = isMaster; + } + + /** + * Gets Initialize bit value. + * + * @return 1 if initialize else 0 + */ + public int isInitialize() { + return isInitialize; + } + + /** + * Sets initialize value. + * + * @param isInitialize 1 is initialize else 0 + */ + public void setIsInitialize(int isInitialize) { + this.isInitialize = isInitialize; + } + + /** + * Gets is more bit set or not. + * + * @return 1 if more set else 0 + */ + public int isMore() { + return isMore; + } + + /** + * Sets more bit value to 0 or 1. + * + * @param isMore 1 if more set else 0 + */ + public void setIsMore(int isMore) { + this.isMore = isMore; + } + + + /** + * Gets IMTU value. + * + * @return IMTU value + */ + public int imtu() { + return imtu; + } + + /** + * Sets IMTU value. + * + * @param imtu value + */ + public void setImtu(int imtu) { + this.imtu = imtu; + } + + /** + * Gets options value. + * + * @return options + */ + public int options() { + return options; + } + + /** + * Sets options value. + * + * @param options options value + */ + public void setOptions(int options) { + this.options = options; + } + + /** + * Gets sequence number. + * + * @return sequenceNo + */ + public long sequenceNo() { + return sequenceNo; + } + + /** + * Sets Sequence number. + * + * @param sequenceNo sequence number + */ + public void setSequenceNo(long sequenceNo) { + this.sequenceNo = sequenceNo; + } + + /** + * Gets LSA header list. + * + * @return LSA header + */ + public List getLsaHeaderList() { + return lsaHeaderList; + } + + /** + * Adds LSA header to header list. + * + * @param lsaHeader lsa header instance + */ + public void addLsaHeader(LsaHeader lsaHeader) { + + if (!lsaHeaderList.contains(lsaHeader)) { + lsaHeaderList.add(lsaHeader); + } + } + + @Override + public OspfPacketType ospfMessageType() { + return OspfPacketType.DD; + } + + @Override + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { + + try { + this.setImtu(channelBuffer.readShort()); + + int options = channelBuffer.readByte(); + String obit = Integer.toHexString(options); + if (obit.length() == 1) { + obit = "0" + obit; + } + String toBinary = Integer.toBinaryString(Integer.parseInt(new Character(obit.charAt(0)).toString())); + if (toBinary.length() == 1) { + toBinary = "000" + toBinary; + } else if (toBinary.length() == 2) { + toBinary = "00" + toBinary; + } else if (toBinary.length() == 3) { + toBinary = "0" + toBinary; + } + if (Integer.parseInt(new Character(toBinary.charAt(1)).toString()) == 1) { + this.setIsOpaqueCapable(true); + } + this.setOptions(options); + this.setIms(channelBuffer.readByte()); + //Convert the byte to ims bits + String strIms = Integer.toBinaryString(this.ims()); + if (strIms.length() == 3) { + this.setIsInitialize(Integer.parseInt(Character.toString(strIms.charAt(0)))); + this.setIsMore(Integer.parseInt(Character.toString(strIms.charAt(1)))); + this.setIsMaster(Integer.parseInt(Character.toString(strIms.charAt(2)))); + } else if (strIms.length() == 2) { + this.setIsInitialize(0); + this.setIsMore(Integer.parseInt(Character.toString(strIms.charAt(0)))); + this.setIsMaster(Integer.parseInt(Character.toString(strIms.charAt(1)))); + } else if (strIms.length() == 1) { + this.setIsInitialize(0); + this.setIsMore(0); + this.setIsMaster(Integer.parseInt(Character.toString(strIms.charAt(0)))); + } + this.setSequenceNo(channelBuffer.readInt()); + + //add all the LSA Headers - header is of 20 bytes + while (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { + LsaHeader header = OspfUtil.readLsaHeader(channelBuffer.readBytes(OspfUtil.LSA_HEADER_LENGTH)); + //add the LSAHeader to DDPacket + addLsaHeader(header); + } + + } catch (Exception e) { + log.debug("Error::DdPacket:: {}", e.getMessage()); + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH); + } + } + + @Override + public byte[] asBytes() { + + byte[] ddMessage = null; + + byte[] ddHeader = getDdHeaderAsByteArray(); + byte[] ddBody = getDdBodyAsByteArray(); + ddMessage = Bytes.concat(ddHeader, ddBody); + + return ddMessage; + } + + /** + * Gets DD Header as byte array. + * + * @return dd header as byte array. + */ + public byte[] getDdHeaderAsByteArray() { + List 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"); + + } + + return Bytes.toArray(headerLst); + } + + + /** + * Gets DD body as byte array. + * + * @return DD body + */ + public byte[] getDdBodyAsByteArray() { + List bodyLst = new ArrayList<>(); + + try { + bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.imtu()))); + bodyLst.add((byte) this.options()); + + StringBuilder sb = new StringBuilder(); + sb.append(this.isInitialize()); + sb.append(this.isMore()); + sb.append(this.isMaster()); + + bodyLst.add((byte) Integer.parseInt(sb.toString(), 2)); + bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.sequenceNo()))); // passing long value + + for (LsaHeader lsaHeader : lsaHeaderList) { + 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::getLsrBodyAsByteArray {}", e.getMessage()); + return Bytes.toArray(bodyLst); + } + + return Bytes.toArray(bodyLst); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("imtu", imtu) + .add("options", options) + .add("ims", ims) + .add("isMaster", isMaster) + .add("isInitialize", isInitialize) + .add("isMore", isMore) + .add("sequenceNo", sequenceNo) + .add("isOpaqueCapable", isOpaqueCapable) + .add("lsaHeaderList", lsaHeaderList) + .toString(); + } +} \ No newline at end of file diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/HelloPacket.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/HelloPacket.java new file mode 100644 index 0000000000..cf0d6aaa0c --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/HelloPacket.java @@ -0,0 +1,362 @@ +/* + * 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.onlab.packet.Ip4Address; +import org.onosproject.ospf.exceptions.OspfErrorType; +import org.onosproject.ospf.exceptions.OspfParseException; +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader; +import org.onosproject.ospf.protocol.util.OspfPacketType; +import org.onosproject.ospf.protocol.util.OspfUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +/** + * Defines an OSPF Hello Message, and the fields and methods to access it. + * Hello packets are OSPF packet type 1. These packets are sent + * periodically on all interfaces in order to establish and + * maintain neighbor relationships. + */ +public class HelloPacket 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 # | 1 | Packet length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Router ID | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Area ID | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Checksum | AuType | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Authentication | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Authentication | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Network Mask | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | HelloInterval | Options | Rtr Pri | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | RouterDeadInterval | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Designated Router | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Backup Designated Router | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Neighbor | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | ... | + + Hello Message Format + REFERENCE : RFC 2328 + */ + + private static final Logger log = LoggerFactory.getLogger(HelloPacket.class); + private Ip4Address networkMask; + private int options; + private int helloInterval; + private int routerPriority; + private int routerDeadInterval; + private Ip4Address bdr; + private Ip4Address dr; + private List neighborAddress = new ArrayList<>(); + + /** + * Creates an instance of Hello packet. + */ + public HelloPacket() { + } + + /** + * Creates an instance of Hello packet. + * + * @param ospfHeader OSPF header instance. + */ + public HelloPacket(OspfPacketHeader ospfHeader) { + populateHeader(ospfHeader); + } + + /** + * Gets network mask. + * + * @return network mask + */ + public Ip4Address networkMask() { + return networkMask; + } + + /** + * Sets network mask. + * + * @param networkMask network mask + */ + public void setNetworkMask(Ip4Address networkMask) { + this.networkMask = networkMask; + } + + /** + * Gets BDRs IP address. + * + * @return BDRs IP address + */ + public Ip4Address bdr() { + return bdr; + } + + /** + * Sets BDR IP address. + * + * @param bdr BDR IP address + */ + public void setBdr(Ip4Address bdr) { + this.bdr = bdr; + } + + /** + * Gets DRs IP address. + * + * @return DRs IP address + */ + public Ip4Address dr() { + return dr; + } + + /** + * Sets DRs IP address. + * + * @param dr DRs IP address + */ + public void setDr(Ip4Address dr) { + this.dr = dr; + } + + /** + * Adds neighbor to map. + * + * @param neighborID neighbors id + */ + public void addNeighbor(Ip4Address neighborID) { + if (!neighborAddress.contains(neighborID)) { + neighborAddress.add(neighborID); + } + } + + /** + * Checks neighbor is in map or not. + * + * @param neighborID neighbors id + * @return true if neighbor exist else false + */ + public boolean containsNeighbour(Ip4Address neighborID) { + return (neighborAddress.contains(neighborID)) ? true : false; + } + + /** + * Gets options value. + * + * @return options value + */ + public int options() { + return options; + } + + /** + * Sets options value. + * + * @param options options value + */ + public void setOptions(int options) { + this.options = options; + } + + /** + * Gets router priority. + * + * @return routerPriority + */ + public int routerPriority() { + return routerPriority; + } + + /** + * Sets router priority. + * + * @param routerPriority router priority + */ + public void setRouterPriority(int routerPriority) { + this.routerPriority = routerPriority; + } + + /** + * Gets hello interval. + * + * @return hello Interval + */ + public int helloInterval() { + return helloInterval; + } + + /** + * Sets hello Interval. + * + * @param helloInterval hello Interval + */ + public void setHelloInterval(int helloInterval) { + this.helloInterval = helloInterval; + } + + /** + * Gets router dead interval. + * + * @return router dead interval + */ + public int routerDeadInterval() { + return routerDeadInterval; + } + + /** + * Sets router dead interval. + * + * @param routerDeadInterval router dead interval + */ + public void setRouterDeadInterval(int routerDeadInterval) { + this.routerDeadInterval = routerDeadInterval; + } + + @Override + public OspfPacketType ospfMessageType() { + return OspfPacketType.HELLO; + } + + @Override + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { + + try { + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); + this.setNetworkMask(Ip4Address.valueOf(tempByteArray)); + this.setHelloInterval(channelBuffer.readShort()); + this.setOptions(channelBuffer.readByte()); + this.setRouterPriority(channelBuffer.readByte() & 0xff); + this.setRouterDeadInterval(channelBuffer.readInt()); + tempByteArray = new byte[OspfUtil.FOUR_BYTES]; + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); + this.setDr(Ip4Address.valueOf(tempByteArray)); + tempByteArray = new byte[OspfUtil.FOUR_BYTES]; + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); + this.setBdr(Ip4Address.valueOf(tempByteArray)); + + while (channelBuffer.readableBytes() > 0) { + tempByteArray = new byte[OspfUtil.FOUR_BYTES]; + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); + this.addNeighbor(Ip4Address.valueOf(tempByteArray)); + } + + } catch (Exception e) { + log.debug("Error::HelloPacket:: {}", e.getMessage()); + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH); + } + } + + @Override + public byte[] asBytes() { + + byte[] helloMessage = null; + byte[] helloHeader = getHelloHeaderAsByteArray(); + byte[] helloBody = getHelloBodyAsByteArray(); + helloMessage = Bytes.concat(helloHeader, helloBody); + + log.debug("HelloPacket::asBytes::Hello asBytes:: {}", helloMessage); + + return helloMessage; + } + + /** + * Gets hello header as byte array. + * + * @return hello header + */ + public byte[] getHelloHeaderAsByteArray() { + List 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::getHelloHeaderAsByteArray {}", e.getMessage()); + return Bytes.toArray(headerLst); + } + + return Bytes.toArray(headerLst); + } + + /** + * Gets hello body as byte array. + * + * @return hello body as byte array + */ + public byte[] getHelloBodyAsByteArray() { + List bodyLst = new ArrayList<>(); + + try { + bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); + bodyLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.helloInterval()))); + bodyLst.add((byte) this.options()); + bodyLst.add((byte) this.routerPriority()); + bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.routerDeadInterval()))); + bodyLst.addAll(Bytes.asList(this.dr().toOctets())); + bodyLst.addAll(Bytes.asList(this.bdr().toOctets())); + for (Ip4Address neighbour : neighborAddress) { + bodyLst.addAll(Bytes.asList(neighbour.toOctets())); + } + + } catch (Exception e) { + log.debug("Error::getHelloBodyAsByteArray {}", e.getMessage()); + return Bytes.toArray(bodyLst); + } + + return Bytes.toArray(bodyLst); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .omitNullValues() + .add("networkMask", networkMask) + .add("options", options) + .add("helloInterval", helloInterval) + .add("routerPriority", routerPriority) + .add("routerDeadInterval", routerDeadInterval) + .add("bdr", bdr) + .add("dr", dr) + .toString(); + } +} \ No newline at end of file diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsAcknowledge.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsAcknowledge.java new file mode 100644 index 0000000000..3de74e4411 --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsAcknowledge.java @@ -0,0 +1,202 @@ +/* + * 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 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 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 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 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 diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsRequest.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsRequest.java new file mode 100644 index 0000000000..636d1b68db --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/LsRequest.java @@ -0,0 +1,48 @@ +/* + * 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 diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/package-info.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/package-info.java new file mode 100644 index 0000000000..80108bd81c --- /dev/null +++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/types/package-info.java @@ -0,0 +1,20 @@ +/* + * 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; diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/subtype/LsRequestPacketTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/subtype/LsRequestPacketTest.java new file mode 100644 index 0000000000..91974dad76 --- /dev/null +++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/subtype/LsRequestPacketTest.java @@ -0,0 +1,122 @@ +/* + * 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 diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/DdPacketTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/DdPacketTest.java new file mode 100644 index 0000000000..6b7ead0dba --- /dev/null +++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/DdPacketTest.java @@ -0,0 +1,449 @@ +/* + * 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.Assert; +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 java.util.Vector; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Unit test class for OspfRouterId. + */ +public class DdPacketTest { + + private byte[] packet; + private byte[] result2; + private DdPacket ddPacket; + private Vector lsaHeaderList = new Vector(); + private int result; + private long result1; + private OpaqueLsaHeader opqueHeader; + private OpaqueLsaHeader opqueHeader1; + private List header; + private OspfPacketHeader ospfPacketHeader; + private ChannelBuffer channelBuffer; + private LsaHeader lsaHeader; + private long result3; + private OspfPacketType ospfPacketType; + + @Before + public void setUp() 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); + } + + @After + public void tearDown() throws Exception { + ddPacket = null; + lsaHeaderList.clear(); + opqueHeader = null; + opqueHeader1 = null; + header = null; + ospfPacketHeader = null; + channelBuffer = null; + lsaHeader = null; + ospfPacketType = null; + } + + /** + * Tests isOpaqueCapable() getter method. + */ + @Test + public void testIsOpaqueCapable() throws Exception { + ddPacket.setIsOpaqueCapable(true); + assertThat(ddPacket.isOpaqueCapable(), is(true)); + } + + /** + * Tests isOpaqueCapable() setter method. + */ + @Test + public void testSetIsOpaqueCapable() throws Exception { + ddPacket.setIsOpaqueCapable(true); + assertThat(ddPacket.isOpaqueCapable(), is(true)); + } + + /** + * Tests ims() getter method. + */ + @Test + public void testGetIms() throws Exception { + ddPacket.setIms(1); + result = ddPacket.ims(); + assertThat(result, is(notNullValue())); + assertThat(result, is(1)); + } + + /** + * Tests ims() setter method. + */ + @Test + public void testSetIms() throws Exception { + ddPacket.setIms(1); + result = ddPacket.ims(); + assertThat(result, is(notNullValue())); + assertThat(result, is(1)); + } + + /** + * Tests isMaster() getter method. + */ + @Test + public void testGetIsMaster() throws Exception { + ddPacket.setIsMaster(2); + result = ddPacket.isMaster(); + assertThat(result, is(notNullValue())); + assertThat(result, is(2)); + } + + /** + * Tests isMaster() setter method. + */ + @Test + public void testSetIsMaster() throws Exception { + ddPacket.setIsMaster(2); + result = ddPacket.isMaster(); + assertThat(result, is(notNullValue())); + assertThat(result, is(2)); + } + + /** + * Tests isInitialize() getter method. + */ + @Test + public void testGetIsInitialize() throws Exception { + ddPacket.setIsInitialize(3); + result = ddPacket.isInitialize(); + assertThat(result, is(notNullValue())); + assertThat(result, is(3)); + } + + /** + * Tests isInitialize() setter method. + */ + @Test + public void testSetIsInitialize() throws Exception { + ddPacket.setIsInitialize(3); + int result = ddPacket.isInitialize(); + assertThat(result, is(notNullValue())); + assertThat(result, is(3)); + } + + /** + * Tests isMore() getter method. + */ + @Test + public void testGetIsMore() throws Exception { + ddPacket.setIsMore(4); + result = ddPacket.isMore(); + assertThat(result, is(notNullValue())); + assertThat(result, is(4)); + } + + /** + * Tests isMore() setter method. + */ + @Test + public void testSetIsMore() throws Exception { + ddPacket.setIsMore(4); + int result = ddPacket.isMore(); + assertThat(result, is(notNullValue())); + assertThat(result, is(4)); + } + + /** + * Tests imtu() getter method. + */ + @Test + public void testGetImtu() throws Exception { + ddPacket.setImtu(5); + result = ddPacket.imtu(); + assertThat(result, is(notNullValue())); + assertThat(result, is(5)); + } + + /** + * Tests imtu() setter method. + */ + @Test + public void testSetImtu() throws Exception { + ddPacket.setImtu(5); + result = ddPacket.imtu(); + assertThat(result, is(notNullValue())); + assertThat(result, is(5)); + } + + /** + * Tests options() getter method. + */ + @Test + public void testGetOptions() throws Exception { + ddPacket.setOptions(2); + result = ddPacket.options(); + assertThat(result, is(notNullValue())); + assertThat(result, is(2)); + } + + /** + * Tests options() setter method. + */ + @Test + public void testSetOptions() throws Exception { + ddPacket.setOptions(2); + result = ddPacket.options(); + Assert.assertNotNull(result); + Assert.assertEquals(2, result); + } + + /** + * Tests sequenceNo() getter method. + */ + @Test + public void testGetSequenceno() throws Exception { + ddPacket.setSequenceNo(2020); + result1 = ddPacket.sequenceNo(); + assertThat(result1, is(notNullValue())); + assertThat(result1, is(2020L)); + } + + /** + * Tests sequenceNo() setter method. + */ + @Test + public void testSetSequenceno() throws Exception { + ddPacket.setSequenceNo(2020); + result3 = ddPacket.sequenceNo(); + assertThat(result3, is(notNullValue())); + assertThat(result3, is(2020L)); + } + + /** + * Tests getLsaHeaderList() getter method. + */ + @Test + public void testGetLsaHeaderList() throws Exception { + ddPacket.addLsaHeader(createLsaHeader()); + opqueHeader = new OpaqueLsaHeader(); + opqueHeader.setLsType(9); + opqueHeader.setLsPacketLen(48); + opqueHeader.setLsCheckSum(10); + opqueHeader.setAge(4); + opqueHeader.setOpaqueId(9); + opqueHeader.setOpaqueType(9); + opqueHeader.setLsSequenceNo(250); + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); + opqueHeader.setOptions(2); + ddPacket.setIsOpaqueCapable(true); + ddPacket.addLsaHeader(opqueHeader); + opqueHeader1 = new OpaqueLsaHeader(); + opqueHeader1.setLsType(10); + opqueHeader1.setLsPacketLen(48); + opqueHeader1.setLsCheckSum(10); + opqueHeader1.setAge(4); + opqueHeader1.setOpaqueId(9); + opqueHeader1.setOpaqueType(9); + opqueHeader1.setLsSequenceNo(250); + opqueHeader1.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); + opqueHeader1.setOptions(66); + ddPacket.addLsaHeader(opqueHeader1); + header = ddPacket.getLsaHeaderList(); + assertThat(header, is(notNullValue())); + } + + /** + * Tests getLsaHeaderList() setter method. + */ + @Test + public void testSetLsaHeaderList() throws Exception { + ddPacket.addLsaHeader(createLsaHeader()); + opqueHeader = new OpaqueLsaHeader(); + opqueHeader.setLsType(9); + opqueHeader.setLsPacketLen(48); + opqueHeader.setLsCheckSum(10); + opqueHeader.setAge(4); + opqueHeader.setOpaqueId(9); + opqueHeader.setOpaqueType(9); + opqueHeader.setLsSequenceNo(250); + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); + opqueHeader.setOptions(66); + ddPacket.addLsaHeader(opqueHeader); + opqueHeader1 = new OpaqueLsaHeader(); + opqueHeader1.setLsType(10); + opqueHeader1.setLsPacketLen(48); + opqueHeader1.setLsCheckSum(10); + opqueHeader1.setAge(4); + opqueHeader1.setOpaqueId(9); + opqueHeader1.setOpaqueType(9); + opqueHeader1.setLsSequenceNo(250); + opqueHeader1.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); + opqueHeader1.setOptions(2); + ddPacket.addLsaHeader(opqueHeader1); + header = ddPacket.getLsaHeaderList(); + assertThat(header.contains(createLsaHeader()), is(true)); + } + + /** + * Tests addLsaHeader() method. + */ + @Test + public void testAddLsaHeader() throws Exception { + ddPacket.addLsaHeader(createLsaHeader()); + assertThat(ddPacket, is(notNullValue())); + } + + /** + * Tests ospfMessageType() getter method. + */ + @Test + public void testGetOspfMessageType() throws Exception { + ospfPacketType = ddPacket.ospfMessageType(); + assertThat(ospfPacketType, is(notNullValue())); + assertThat(ospfPacketType, is(OspfPacketType.DD)); + } + + /** + * 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(2); + ospfPacketHeader.setOspfVer(2); + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2")); + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3")); + ddPacket.setIsOpaqueCapable(true); + ddPacket.setOptions(66); + ddPacket = new DdPacket(ospfPacketHeader); + packet = createByteForDdPacket(); + channelBuffer = ChannelBuffers.copiedBuffer(packet); + ddPacket.readFrom(channelBuffer); + assertThat(ddPacket, is(notNullValue())); + assertThat(ddPacket.ospfMessageType(), is(OspfPacketType.DD)); + } + + /** + * Tests asBytes() method. + */ + @Test + public void testAsBytes() throws Exception { + result2 = ddPacket.asBytes(); + assertThat(result2, is(notNullValue())); + } + + /** + * Tests getDdHeaderAsByteArray() method. + */ + @Test + public void testGetDdHeaderAsByteArray() throws Exception { + opqueHeader = new OpaqueLsaHeader(); + opqueHeader.setLsType(9); + opqueHeader.setLsPacketLen(48); + opqueHeader.setLsCheckSum(10); + opqueHeader.setAge(4); + opqueHeader.setOpaqueId(9); + opqueHeader.setOpaqueType(9); + opqueHeader.setLsSequenceNo(250); + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); + opqueHeader.setOptions(66); + ddPacket.addLsaHeader(opqueHeader); + opqueHeader1 = new OpaqueLsaHeader(); + opqueHeader1.setLsType(10); + opqueHeader1.setLsPacketLen(48); + opqueHeader1.setLsCheckSum(10); + opqueHeader1.setAge(4); + opqueHeader1.setOpaqueId(9); + opqueHeader1.setOpaqueType(9); + opqueHeader1.setLsSequenceNo(250); + opqueHeader1.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); + opqueHeader1.setOptions(2); + ddPacket.addLsaHeader(opqueHeader1); + result2 = ddPacket.getDdHeaderAsByteArray(); + assertThat(result2, is(notNullValue())); + } + + /** + * Tests getDdBodyAsByteArray() method. + */ + @Test + public void testGetDdBodyAsByteArray() throws Exception { + lsaHeader = createLsaHeader(); + ddPacket.addLsaHeader(lsaHeader); + result2 = ddPacket.getDdBodyAsByteArray(); + assertThat(result2, is(notNullValue())); + } + + /** + * Tests to string method. + */ + @Test + public void testToString() throws Exception { + assertThat(ddPacket.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(2); + lsaHeader.setOptions(2); + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165")); + return lsaHeader; + } + + /** + * Utility method used by junit methods. + */ + private byte[] createByteForDdPacket() { + byte[] ddPacket = {5, -36, 66, 1, 65, 119, -87, 126, 0, 23, 2, 1, 10, 10, + 10, 10, 10, 10, 10, 10, -128, 0, 0, 6, -69, 26, 0, 36}; + + return ddPacket; + } +} \ No newline at end of file diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/HelloPacketTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/HelloPacketTest.java new file mode 100644 index 0000000000..8d8b0f6da2 --- /dev/null +++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/HelloPacketTest.java @@ -0,0 +1,325 @@ +/* + * 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 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 diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsAcknowledgeTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsAcknowledgeTest.java new file mode 100644 index 0000000000..dd5af91a4c --- /dev/null +++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsAcknowledgeTest.java @@ -0,0 +1,208 @@ +/* + * 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 diff --git a/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsRequestTest.java b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsRequestTest.java new file mode 100644 index 0000000000..41c9c4fa6a --- /dev/null +++ b/protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/ospfpacket/types/LsRequestTest.java @@ -0,0 +1,182 @@ +/* + * 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 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