Add AbstractNormalizedNodeBodyWriter
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / PatchJsonBodyWriter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
9
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.io.OutputStreamWriter;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import java.nio.charset.StandardCharsets;
17 import java.util.List;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.ext.MessageBodyWriter;
23 import javax.ws.rs.ext.Provider;
24 import org.opendaylight.restconf.common.errors.RestconfError;
25 import org.opendaylight.restconf.common.patch.PatchStatusContext;
26 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
27 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
28 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
29
30 @Provider
31 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
32 public class PatchJsonBodyWriter implements MessageBodyWriter<PatchStatusContext> {
33
34     @Override
35     public boolean isWriteable(final Class<?> type, final Type genericType,
36                                final Annotation[] annotations, final MediaType mediaType) {
37         return type.equals(PatchStatusContext.class);
38     }
39
40     @Override
41     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
42                         final Annotation[] annotations, final MediaType mediaType,
43                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
44             throws IOException, WebApplicationException {
45
46         final JsonWriter jsonWriter = createJsonWriter(entityStream);
47         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
48         jsonWriter.beginObject();
49         jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
50         if (patchStatusContext.isOk()) {
51             reportSuccess(jsonWriter);
52         } else {
53             if (patchStatusContext.getGlobalErrors() != null) {
54                 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
55             }
56
57             jsonWriter.name("edit-status");
58             jsonWriter.beginObject();
59             jsonWriter.name("edit");
60             jsonWriter.beginArray();
61             for (final PatchStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
62                 jsonWriter.beginObject();
63                 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
64                 if (patchStatusEntity.getEditErrors() != null) {
65                     reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
66                 } else {
67                     if (patchStatusEntity.isOk()) {
68                         reportSuccess(jsonWriter);
69                     }
70                 }
71                 jsonWriter.endObject();
72             }
73             jsonWriter.endArray();
74             jsonWriter.endObject();
75         }
76         jsonWriter.endObject();
77         jsonWriter.endObject();
78         jsonWriter.flush();
79     }
80
81     private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
82         jsonWriter.name("ok").beginArray().nullValue().endArray();
83     }
84
85     private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
86         jsonWriter.name("errors");
87         jsonWriter.beginObject();
88         jsonWriter.name("error");
89         jsonWriter.beginArray();
90
91         for (final RestconfError restconfError : errors) {
92             jsonWriter.beginObject();
93             jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
94             jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
95
96             // optional node
97             if (restconfError.getErrorPath() != null) {
98                 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
99             }
100
101             // optional node
102             if (restconfError.getErrorMessage() != null) {
103                 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
104             }
105
106             // optional node
107             if (restconfError.getErrorInfo() != null) {
108                 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
109             }
110
111             jsonWriter.endObject();
112         }
113
114         jsonWriter.endArray();
115         jsonWriter.endObject();
116     }
117
118     private static JsonWriter createJsonWriter(final OutputStream entityStream) {
119         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
120     }
121 }