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/api/src/main/java/org/onosproject/ospf/controller/OspfArea.java b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfArea.java
index 64cf9a7..f4e8255 100644
--- a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfArea.java
+++ b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfArea.java
@@ -16,6 +16,7 @@
 package org.onosproject.ospf.controller;
 
 import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.exceptions.OspfParseException;
 
 import java.util.List;
 
@@ -145,9 +146,8 @@
      * with a new sequence number.
      *
      * @param ospfInterface interface instance
-     * @throws Exception might throw exception
      */
-    void refreshArea(OspfInterface ospfInterface) throws Exception;
+    void refreshArea(OspfInterface ospfInterface);
 
     /**
      * Verifies no neighbor is in exchange process.
@@ -191,10 +191,8 @@
      * @param ospfLsa          LSA instance
      * @param isSelfOriginated true if the LSA is self originated else false
      * @param ospfInterface    interface instance
-     * @throws Exception might throws exception
      */
-    void addLsa(OspfLsa ospfLsa, boolean isSelfOriginated, OspfInterface ospfInterface)
-            throws Exception;
+    void addLsa(OspfLsa ospfLsa, boolean isSelfOriginated, OspfInterface ospfInterface);
 
     /**
      * Adds the received LSA to LSDB,this method creates an LSA wrapper for the LSA.
@@ -202,9 +200,9 @@
      *
      * @param ospfLsa       LSA instance
      * @param ospfInterface interface instance
-     * @throws Exception might throws exception
+     * @throws OspfParseException might throws exception
      */
-    void addLsa(OspfLsa ospfLsa, OspfInterface ospfInterface) throws Exception;
+    void addLsa(OspfLsa ospfLsa, OspfInterface ospfInterface) throws OspfParseException;
 
     /**
      * Sets router sequence number for router LSA.
@@ -229,8 +227,7 @@
      * @param linkStateID       link state id to form the key
      * @param advertisingRouter advertising router to form the key
      * @return LSA wrapper instance which contains the LSA
-     * @throws Exception might throws exception
      */
-    LsaWrapper getLsa(int lsType, String linkStateID, String advertisingRouter) throws Exception;
+    LsaWrapper getLsa(int lsType, String linkStateID, String advertisingRouter);
 
 }
\ No newline at end of file
diff --git a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfInterface.java b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfInterface.java
index 0e9f4d0..1c05ad5 100644
--- a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfInterface.java
+++ b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfInterface.java
@@ -243,16 +243,13 @@
      *
      * @param ospfMessage received OSPF message
      * @param ctx         channel handler context instance.
-     * @throws Exception might throws exception
      */
-    void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception;
+    void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx);
 
     /**
      * Represents an interface is up and connected.
-     *
-     * @throws Exception might throws exception
      */
-    void interfaceUp() throws Exception;
+    void interfaceUp();
 
     /**
      * Starts the timer which waits for configured seconds and sends Delayed Ack Packet.
diff --git a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfMessage.java b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfMessage.java
index 9949146..460457f 100644
--- a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfMessage.java
+++ b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfMessage.java
@@ -17,6 +17,7 @@
 
 import org.jboss.netty.buffer.ChannelBuffer;
 import org.onlab.packet.Ip4Address;
+import org.onosproject.ospf.exceptions.OspfParseException;
 
 /**
  * Representation of an OSPF message.
@@ -48,9 +49,9 @@
      * Reads from ChannelBuffer and initializes the type of LSA.
      *
      * @param channelBuffer channel buffer instance
-     * @throws Exception might throws exception while parsing buffer
+     * @throws OspfParseException might throws exception while parsing buffer
      */
-    void readFrom(ChannelBuffer channelBuffer) throws Exception;
+    void readFrom(ChannelBuffer channelBuffer) throws OspfParseException;
 
     /**
      * Returns OSPFMessage as byte array.
diff --git a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfNbr.java b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfNbr.java
index 2d7121c..9a62fad 100644
--- a/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfNbr.java
+++ b/protocols/ospf/api/src/main/java/org/onosproject/ospf/controller/OspfNbr.java
@@ -158,9 +158,8 @@
      * retransmission timer.
      *
      * @param ch netty channel instance
-     * @throws Exception might throw exception
      */
-    void badLSReq(Channel ch) throws Exception;
+    void badLSReq(Channel ch);
 
     /**
      * Gets the LS request list.
diff --git a/protocols/ospf/api/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java b/protocols/ospf/api/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
new file mode 100644
index 0000000..c228381
--- /dev/null
+++ b/protocols/ospf/api/src/main/java/org/onosproject/ospf/exceptions/OspfParseException.java
@@ -0,0 +1,102 @@
+/*
+ * 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/api/src/main/java/org/onosproject/ospf/exceptions/package-info.java b/protocols/ospf/api/src/main/java/org/onosproject/ospf/exceptions/package-info.java
new file mode 100644
index 0000000..db2cc81
--- /dev/null
+++ b/protocols/ospf/api/src/main/java/org/onosproject/ospf/exceptions/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementation of the OSPF exception types.
+ */
+package org.onosproject.ospf.exceptions;
\ No newline at end of file