Refactor exception handling in the OSPF protocol
- removed unnecessary throws of Exception from methods
- converted throws of generic Exception to specific OspfParseException
Change-Id: I9d07167d92f221a234e9eba4c6082deb165afc0b
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
deleted file mode 100644
index c228381..0000000
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Copyright 2016-present Open Networking Foundation
- *
- * 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.exceptions;
-
-import com.google.common.base.MoreObjects;
-
-/**
- * Representation of a custom exception for OSPF.
- */
-public class OspfParseException extends Exception {
-
- private static final long serialVersionUID = 1L;
- private byte errorCode;
- private byte errorSubCode;
-
- /**
- * Creates a new OSPF exception.
- */
- public OspfParseException() {
- super();
- }
-
- /**
- * Creates a new OSPF exception based on the given arguments.
- *
- * @param message the detail of exception in string
- * @param cause underlying cause of the error
- */
- public OspfParseException(final String message, final Throwable cause) {
- super(message, cause);
- }
-
- /**
- * Creates a new OSPF exception for the given message.
- *
- * @param message the detail of exception in string
- */
- public OspfParseException(final String message) {
- super(message);
- }
-
- /**
- * Creates a new OSPF exception from throwable instance.
- *
- * @param cause underlying cause of the error
- */
- public OspfParseException(final Throwable cause) {
- super(cause);
- }
-
- /**
- * Creates a new OSPF exception from error code and error sub code.
- *
- * @param errorCode error code of OSPF message
- * @param errorSubCode error sub code of OSPF message
- */
- public OspfParseException(final byte errorCode, final byte errorSubCode) {
- super();
- this.errorCode = errorCode;
- this.errorSubCode = errorSubCode;
- }
-
- /**
- * Returns error code for this exception.
- *
- * @return error code for this exception
- */
- public byte errorCode() {
- return this.errorCode;
- }
-
- /**
- * Returns error sub code for this exception.
- *
- * @return error sub code for this exception
- */
- public byte errorSubCode() {
- return this.errorSubCode;
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(getClass())
- .omitNullValues()
- .add("errorCode", errorCode)
- .add("errorSubCode", errorSubCode)
- .toString();
- }
-}
\ No newline at end of file
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkId.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkId.java
index 015f2dc..e530097 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkId.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkId.java
@@ -26,6 +26,7 @@
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
+import java.net.UnknownHostException;
/**
* Representation of link id value of link tlv of Traffic Engineering.
@@ -58,14 +59,14 @@
* Reads bytes from channel buffer.
*
* @param channelBuffer channel buffer instance
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public void readFrom(ChannelBuffer channelBuffer) throws Exception {
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
try {
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
this.setLinkId(InetAddress.getByAddress(tempByteArray).getHostName());
- } catch (Exception e) {
+ } catch (UnknownHostException e) {
log.debug("Error::LinkId:: {}", e.getMessage());
throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
OspfErrorType.BAD_MESSAGE);
@@ -76,9 +77,9 @@
* Returns instance as byte array.
*
* @return instance as bytes
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] asBytes() throws Exception {
+ public byte[] asBytes() throws OspfParseException {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
@@ -92,9 +93,9 @@
* Gets byte array of link id sub tlv body.
*
* @return gets the body as byte array
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] getLinkSubTypeTlvBodyAsByteArray() throws Exception {
+ public byte[] getLinkSubTypeTlvBodyAsByteArray() throws OspfParseException {
byte[] linkSubTypeBody = null;
try {
linkSubTypeBody = InetAddress.getByName(this.linkId).getAddress();
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkType.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkType.java
index 4b5c6f4..a1a2ac9 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkType.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LinkType.java
@@ -63,9 +63,9 @@
* Reads from channel buffer.
*
* @param channelBuffer channel buffer instance
- * @throws Exception might throws exception while parsing buffer
+ * @throws OspfParseException might throws exception while parsing buffer
*/
- public void readFrom(ChannelBuffer channelBuffer) throws Exception {
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
try {
int len = channelBuffer.readableBytes();
byte[] tempByteArray = new byte[len];
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LocalInterfaceIpAddress.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LocalInterfaceIpAddress.java
index 92c3183..0214ccc 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LocalInterfaceIpAddress.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/LocalInterfaceIpAddress.java
@@ -26,6 +26,7 @@
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
@@ -69,15 +70,15 @@
* Reads bytes from channel buffer.
*
* @param channelBuffer channel buffer instance
- * @throws Exception might throws exception while parsing buffer
+ * @throws OspfParseException might throws exception while parsing buffer
*/
- public void readFrom(ChannelBuffer channelBuffer) throws Exception {
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
while (channelBuffer.readableBytes() >= OspfUtil.FOUR_BYTES) {
try {
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
this.addLocalInterfaceIPAddress(InetAddress.getByAddress(tempByteArray).getHostName());
- } catch (Exception e) {
+ } catch (UnknownHostException e) {
log.debug("Error::readFrom:: {}", e.getMessage());
throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR,
OspfErrorType.BAD_MESSAGE);
@@ -89,9 +90,9 @@
* Gets local interface ip address as byte array.
*
* @return local interface ip address as byte array
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] asBytes() throws Exception {
+ public byte[] asBytes() throws OspfParseException {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
@@ -105,9 +106,9 @@
* Gets byte array of local interface ip address.
*
* @return byte array of local interface ip address
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] getLinkSubTypeTlvBodyAsByteArray() throws Exception {
+ public byte[] getLinkSubTypeTlvBodyAsByteArray() throws OspfParseException {
List<Byte> linkSubTypeBody = new ArrayList<>();
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/RemoteInterfaceIpAddress.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/RemoteInterfaceIpAddress.java
index 07d1cdb..7aaf9d9 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/RemoteInterfaceIpAddress.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/linksubtype/RemoteInterfaceIpAddress.java
@@ -69,9 +69,9 @@
* Reads bytes from channel buffer .
*
* @param channelBuffer channel buffer instance
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public void readFrom(ChannelBuffer channelBuffer) throws Exception {
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
while (channelBuffer.readableBytes() >= OspfUtil.FOUR_BYTES) {
try {
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
@@ -89,9 +89,9 @@
* Gets byte array of remote interface ip address .
*
* @return byte array of remote interface ip address
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] asBytes() throws Exception {
+ public byte[] asBytes() throws OspfParseException {
byte[] linkSubType = null;
byte[] linkSubTlvHeader = getTlvHeaderAsByteArray();
@@ -105,9 +105,9 @@
* Gets byte array of remote interface ip address.
*
* @return byte array of remote interface ip address
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] getLinkSubTypeTlvBodyAsByteArray() throws Exception {
+ public byte[] getLinkSubTypeTlvBodyAsByteArray() throws OspfParseException {
List<Byte> linkSubTypeBody = new ArrayList<>();
for (String remoteAddress : this.remoteInterfaceAddress) {
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/LinkTlv.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/LinkTlv.java
index acc72e3..6521ab2 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/LinkTlv.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/LinkTlv.java
@@ -18,6 +18,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Bytes;
import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.lsa.TlvHeader;
import org.onosproject.ospf.protocol.lsa.linksubtype.AdministrativeGroup;
import org.onosproject.ospf.protocol.lsa.linksubtype.LinkId;
@@ -66,9 +67,9 @@
* Reads bytes from channel buffer .
*
* @param channelBuffer channel buffer instance
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public void readFrom(ChannelBuffer channelBuffer) throws Exception {
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
while (channelBuffer.readableBytes() > 0) {
TlvHeader tlvHeader = new TlvHeader();
tlvHeader.setTlvType(channelBuffer.readUnsignedShort());
@@ -126,9 +127,9 @@
* Gets link tlv as byte array.
*
* @return link tlv as byte array
- * @throws Exception might throws exception while parsing buffer
+ * @throws OspfParseException if the packet can't be parsed
*/
- public byte[] asBytes() throws Exception {
+ public byte[] asBytes() throws OspfParseException {
byte[] lsaMessage = null;
byte[] tlvHeader = getTlvHeaderAsByteArray();
@@ -142,9 +143,9 @@
* Gets tlv body as byte array.
*
* @return tlv body as byte array
- * @throws Exception might throws exception while parsing buffer
+ * @throws OspfParseException might throws exception while parsing buffer
*/
- public byte[] getTlvBodyAsByteArray() throws Exception {
+ public byte[] getTlvBodyAsByteArray() throws OspfParseException {
List<Byte> bodyLst = new ArrayList<>();
for (LinkSubType tlv : subTlv) {
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/RouterTlv.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/RouterTlv.java
index 83bf918..8ac443b 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/RouterTlv.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/tlvtypes/RouterTlv.java
@@ -71,9 +71,9 @@
* Reads bytes from channel buffer .
*
* @param channelBuffer channel buffer instance
- * @throws Exception might throws exception while parsing buffer
+ * @throws OspfParseException might throws exception while parsing buffer
*/
- public void readFrom(ChannelBuffer channelBuffer) throws Exception {
+ public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
try {
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java
index d87dfc5..b0fd4b6 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa10.java
@@ -131,9 +131,9 @@
* Returns instance as bytes.
*
* @return instance as bytes
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] asBytes() throws Exception {
+ public byte[] asBytes() throws OspfParseException {
byte[] lsaMessage = null;
byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray();
@@ -147,9 +147,9 @@
* Gets the LSA body as byte array.
*
* @return the lsa body as byte array
- * @throws Exception might throws exception while parsing packet
+ * @throws OspfParseException might throws exception while parsing packet
*/
- public byte[] getLsaBodyAsByteArray() throws Exception {
+ public byte[] getLsaBodyAsByteArray() throws OspfParseException {
List<Byte> bodyLst = new ArrayList<>();
if (this.opaqueId() == 1) {
for (TopLevelTlv tlv : this.topLevelValues) {
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java
index b7ac7a8..e51672f 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/ospfpacket/OspfMessageReader.java
@@ -42,10 +42,10 @@
*
* @param channelBuffer channel buffer instance.
* @return OSPF message instance.
- * @throws Exception might throws exception while parsing buffer
+ * @throws OspfParseException might throws exception while parsing buffer
*/
public OspfMessage readFromBuffer(ChannelBuffer channelBuffer)
- throws Exception {
+ throws OspfParseException {
try {
OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer);
@@ -97,7 +97,7 @@
* @param channelBuffer channel buffer instance.
* @return Ospf Header instance.
*/
- private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception {
+ private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) {
OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();
// Determine OSPF version & Packet Type
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java
index f346f45..bddb853 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/ChecksumCalculator.java
@@ -17,6 +17,7 @@
import org.onosproject.ospf.controller.OspfLsa;
import org.onosproject.ospf.controller.OspfLsaType;
+import org.onosproject.ospf.exceptions.OspfParseException;
import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
@@ -31,14 +32,19 @@
import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge;
import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest;
import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate;
+import org.slf4j.Logger;
import java.util.Arrays;
+import static org.slf4j.LoggerFactory.getLogger;
+
/**
* Calculates checksum for different types of OSPF packets.
*/
public class ChecksumCalculator {
+ private static final Logger log = getLogger(ChecksumCalculator.class);
+
/**
* Converts given string to sixteen bits integer.
* If hexasum is more than 16 bit value, needs to be reduced to 16 bit value.
@@ -98,11 +104,12 @@
* @param lsType lsa type
* @param lsaChecksumPos1 lsa checksum position in packet
* @param lsaChecksumPos2 lsa checksum position in packet
+ * @throws OspfParseException if packet can't be parsed
* @return true if valid else false
- * @throws Exception might throw exception while processing
*/
public boolean isValidLsaCheckSum(OspfLsa ospfLsa, int lsType, int lsaChecksumPos1,
- int lsaChecksumPos2) throws Exception {
+ int lsaChecksumPos2) throws OspfParseException {
+
if (lsType == OspfLsaType.ROUTER.value()) {
RouterLsa lsa = (RouterLsa) ospfLsa;
return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
@@ -129,6 +136,7 @@
return validateLsaCheckSum(lsa.asBytes(), lsaChecksumPos1, lsaChecksumPos2);
}
+
return false;
}
diff --git a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java
index fa15cdb..182b827 100644
--- a/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java
+++ b/protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/util/OspfUtil.java
@@ -26,6 +26,7 @@
import javax.xml.bind.DatatypeConverter;
import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
@@ -93,10 +94,8 @@
* @param ip2 IP address
* @param mask network mask
* @return true if both are in same network else false
- * @throws Exception might throws exception while parsing ip address
*/
- public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask)
- throws Exception {
+ public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask) {
byte[] a1 = ip1.toOctets();
byte[] a2 = ip2.toOctets();
@@ -195,9 +194,8 @@
*
* @param channelBuffer channel buffer instance
* @return LSA header instance.
- * @throws Exception might throws exception while parsing buffer
*/
- public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception {
+ public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) {
//add all the LSA Headers - one header is of 20 bytes
LsaHeader lsaHeader = null;
if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) {
@@ -230,7 +228,11 @@
header.setLsType(tempBuffer.readByte());
byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
- header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName());
+ try {
+ header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName());
+ } catch (UnknownHostException uhe) {
+ log.warn("Can't look up host", uhe);
+ }
tempByteArray = new byte[OspfUtil.FOUR_BYTES];
channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray));