Add support for RESTCONF standard errors

Change-Id: I74c0997bc8e06bc10c97cd610ff70c7a6aa68c8b
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java
index cd0b428..41fcf39 100644
--- a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java
+++ b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java
@@ -20,9 +20,9 @@
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.apache.commons.io.IOUtils;
 import org.onlab.osgi.DefaultServiceDirectory;
+import org.onosproject.restconf.api.RestconfError;
 import org.onosproject.restconf.api.RestconfException;
 import org.onosproject.restconf.api.RestconfRpcOutput;
-import org.onosproject.restconf.utils.exceptions.RestconfUtilsException;
 import org.onosproject.yang.model.DataNode;
 import org.onosproject.yang.model.DefaultResourceData;
 import org.onosproject.yang.model.ResourceData;
@@ -43,13 +43,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.util.Optional;
 
-import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
-import static javax.ws.rs.core.Response.Status.EXPECTATION_FAILED;
-import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
-import static javax.ws.rs.core.Response.Status.NO_CONTENT;
-import static javax.ws.rs.core.Response.Status.OK;
-import static javax.ws.rs.core.Response.Status.REQUEST_TIMEOUT;
+import static javax.ws.rs.core.Response.Status.*;
 
 /**
  * Utilities used by the RESTCONF app.
@@ -84,7 +80,9 @@
         try {
             rootNode = (ObjectNode) mapper.readTree(inputStream);
         } catch (IOException e) {
-            throw new RestconfUtilsException("ERROR: InputStream failed to parse");
+            throw new RestconfException("ERROR: InputStream failed to parse",
+                    e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR,
+                    Optional.empty());
         }
         return rootNode;
     }
@@ -101,7 +99,9 @@
         try {
             inputStream = IOUtils.toInputStream(json);
         } catch (Exception e) {
-            throw new RestconfUtilsException("ERROR: Json Node failed to parse");
+            throw new RestconfException("ERROR: Json Node failed to parse", e,
+                RestconfError.ErrorTag.MALFORMED_MESSAGE, BAD_REQUEST,
+                Optional.empty());
         }
         return inputStream;
     }
@@ -142,13 +142,19 @@
             // CompositeStream --- YangRuntimeService ---> CompositeData.
             CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context);
             resourceData = compositeData.resourceData();
+        } catch (RestconfException ex) {
+            throw ex;
         } catch (Exception ex) {
             log.error("convertJsonToDataNode failure: {}", ex.getMessage());
             log.debug("convertJsonToDataNode failure", ex);
+            throw new RestconfException("ERROR: JSON cannot be converted to DataNode",
+                    ex, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR,
+                    Optional.of(uri.getPath()));
         }
         if (resourceData == null) {
             throw new RestconfException("ERROR: JSON cannot be converted to DataNode",
-                                        INTERNAL_SERVER_ERROR);
+                RestconfError.ErrorTag.DATA_MISSING, CONFLICT,
+                Optional.of(uri.getPath()), Optional.empty());
         }
         return resourceData;
     }
@@ -193,7 +199,8 @@
         }
         if (rootNode == null) {
             throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode",
-                                        INTERNAL_SERVER_ERROR);
+                    null, RestconfError.ErrorTag.DATA_MISSING, CONFLICT,
+                    Optional.empty());
         }
         return rootNode;
     }
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/RestconfUtilsException.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/RestconfUtilsException.java
deleted file mode 100644
index b0db098..0000000
--- a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/RestconfUtilsException.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright 2017-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.restconf.utils.exceptions;
-
-/**
- * Represents class of errors related to RESTCONF utilities.
- */
-public class RestconfUtilsException extends RuntimeException {
-
-    /**
-     * Constructs an exception with the specified message.
-     *
-     * @param message the message describing the specific nature of the error
-     */
-    public RestconfUtilsException(String message) {
-        super(message);
-    }
-
-    /**
-     * Constructs an exception with the specified message and the underlying
-     * cause.
-     *
-     * @param message the message describing the specific nature of the error
-     * @param cause   the underlying cause of this error
-     */
-    public RestconfUtilsException(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/package-info.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/package-info.java
deleted file mode 100644
index a0fe88e..0000000
--- a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/package-info.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright 2017-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.
- */
-
-/**
- * Parse utils custom exceptions.
- */
-package org.onosproject.restconf.utils.exceptions;
\ No newline at end of file