Bug 6325 - upgrade draft11 to draft15 - renaming
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / rest / impl / PATCHJsonBodyWriter.java
index 9f9ab949339890eaeb4de347ab2214cea15fe97e..b5a87966e483e5afc942d9e2b3321bfbefd2f62f 100644 (file)
@@ -8,13 +8,13 @@
 
 package org.opendaylight.netconf.sal.rest.impl;
 
-import com.google.common.base.Charsets;
 import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
@@ -22,39 +22,45 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
-import org.opendaylight.netconf.sal.rest.api.Draft02.MediaTypes;
+import org.opendaylight.netconf.sal.rest.api.Draft02;
 import org.opendaylight.netconf.sal.rest.api.RestconfService;
 import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusContext;
 import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusEntity;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
+import org.opendaylight.restconf.Draft15;
+import org.opendaylight.restconf.utils.RestconfConstants;
 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
 
+
 @Provider
-@Produces({MediaTypes.PATCH_STATUS + RestconfService.JSON})
+@Produces({Draft02.MediaTypes.PATCH_STATUS + RestconfService.JSON,
+        Draft15.MediaTypes.PATCH_STATUS + RestconfConstants.JSON})
 public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext> {
 
     @Override
-    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+    public boolean isWriteable(final Class<?> type, final Type genericType,
+                               final Annotation[] annotations, final MediaType mediaType) {
         return type.equals(PATCHStatusContext.class);
     }
 
     @Override
-    public long getSize(PATCHStatusContext patchStatusContext, Class<?> type, Type genericType, Annotation[]
-            annotations, MediaType mediaType) {
+    public long getSize(final PATCHStatusContext patchStatusContext, final Class<?> type, final Type genericType,
+                        final Annotation[] annotations, final MediaType mediaType) {
         return -1;
     }
 
     @Override
-    public void writeTo(PATCHStatusContext patchStatusContext, Class<?> type, Type genericType, Annotation[]
-            annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
-                throws IOException, WebApplicationException {
+    public void writeTo(final PATCHStatusContext patchStatusContext, final Class<?> type, final Type genericType,
+                        final Annotation[] annotations, final MediaType mediaType,
+                        final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
+            throws IOException, WebApplicationException {
 
         final JsonWriter jsonWriter = createJsonWriter(entityStream);
         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
         jsonWriter.beginObject();
         jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
         if (patchStatusContext.isOk()) {
-            jsonWriter.name("ok").nullValue();
+            reportSuccess(jsonWriter);
         } else {
             if (patchStatusContext.getGlobalErrors() != null) {
                 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
@@ -64,14 +70,14 @@ public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext
             jsonWriter.beginObject();
             jsonWriter.name("edit");
             jsonWriter.beginArray();
-            for (PATCHStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
+            for (final PATCHStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
                 jsonWriter.beginObject();
                 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
                 if (patchStatusEntity.getEditErrors() != null) {
                     reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
                 } else {
                     if (patchStatusEntity.isOk()) {
-                        jsonWriter.name("ok").nullValue();
+                        reportSuccess(jsonWriter);
                     }
                 }
                 jsonWriter.endObject();
@@ -82,22 +88,33 @@ public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext
         jsonWriter.endObject();
         jsonWriter.endObject();
         jsonWriter.flush();
+    }
 
+    private void reportSuccess(final JsonWriter jsonWriter) throws IOException {
+        jsonWriter.name("ok").beginArray().nullValue().endArray();
     }
 
-    private static void reportErrors(List<RestconfError> errors, JsonWriter jsonWriter) throws IOException {
+    private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
         jsonWriter.name("errors");
         jsonWriter.beginObject();
         jsonWriter.name("error");
         jsonWriter.beginArray();
 
-        for (RestconfError restconfError : errors) {
+        for (final RestconfError restconfError : errors) {
             jsonWriter.beginObject();
             jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
             jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
-            //TODO: fix error-path reporting (separate error-path from error-message)
-            //jsonWriter.name("error-path").value(restconfError.getErrorPath());
-            jsonWriter.name("error-message").value(restconfError.getErrorMessage());
+
+            // optional node
+            if (restconfError.getErrorPath() != null) {
+                jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
+            }
+
+            // optional node
+            if (restconfError.getErrorMessage() != null) {
+                jsonWriter.name("error-message").value(restconfError.getErrorMessage());
+            }
+
             jsonWriter.endObject();
         }
 
@@ -106,6 +123,6 @@ public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext
     }
 
     private static JsonWriter createJsonWriter(final OutputStream entityStream) {
-        return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
+        return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
     }
 }