f000f7c1338f9ac5507f778c8827d3a01820cb60
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / JsonPatchStatusBodyWriter.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.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.Provider;
22 import org.opendaylight.restconf.common.errors.RestconfError;
23 import org.opendaylight.restconf.common.patch.PatchStatusContext;
24 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
25 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
26 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
27
28 @Provider
29 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
30 public class JsonPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
31     @Override
32     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
33                         final Annotation[] annotations, final MediaType mediaType,
34                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
35             throws IOException {
36
37         final JsonWriter jsonWriter = createJsonWriter(entityStream);
38         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
39         jsonWriter.beginObject();
40         jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
41         if (patchStatusContext.isOk()) {
42             reportSuccess(jsonWriter);
43         } else {
44             if (patchStatusContext.getGlobalErrors() != null) {
45                 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
46             }
47
48             jsonWriter.name("edit-status");
49             jsonWriter.beginObject();
50             jsonWriter.name("edit");
51             jsonWriter.beginArray();
52             for (final PatchStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
53                 jsonWriter.beginObject();
54                 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
55                 if (patchStatusEntity.getEditErrors() != null) {
56                     reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
57                 } else {
58                     if (patchStatusEntity.isOk()) {
59                         reportSuccess(jsonWriter);
60                     }
61                 }
62                 jsonWriter.endObject();
63             }
64             jsonWriter.endArray();
65             jsonWriter.endObject();
66         }
67         jsonWriter.endObject();
68         jsonWriter.endObject();
69         jsonWriter.flush();
70     }
71
72     private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
73         jsonWriter.name("ok").beginArray().nullValue().endArray();
74     }
75
76     private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
77         jsonWriter.name("errors");
78         jsonWriter.beginObject();
79         jsonWriter.name("error");
80         jsonWriter.beginArray();
81
82         for (final RestconfError restconfError : errors) {
83             jsonWriter.beginObject();
84             jsonWriter.name("error-type").value(restconfError.getErrorType().elementBody());
85             jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
86
87             // optional node
88             if (restconfError.getErrorPath() != null) {
89                 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
90             }
91
92             // optional node
93             if (restconfError.getErrorMessage() != null) {
94                 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
95             }
96
97             // optional node
98             if (restconfError.getErrorInfo() != null) {
99                 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
100             }
101
102             jsonWriter.endObject();
103         }
104
105         jsonWriter.endArray();
106         jsonWriter.endObject();
107     }
108
109     private static JsonWriter createJsonWriter(final OutputStream entityStream) {
110         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
111     }
112 }