Reduce use of Status
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / 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;
9
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.util.List;
14 import javax.ws.rs.Produces;
15 import javax.ws.rs.ext.Provider;
16 import org.opendaylight.restconf.api.MediaTypes;
17 import org.opendaylight.restconf.api.query.PrettyPrintParam;
18 import org.opendaylight.restconf.common.errors.RestconfError;
19 import org.opendaylight.restconf.server.api.DatabindContext;
20 import org.opendaylight.restconf.server.api.PatchStatusContext;
21 import org.opendaylight.restconf.server.spi.FormattableBodySupport;
22
23 @Provider
24 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
25 public class JsonPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
26     @Override
27     void writeTo(final PatchStatusContext body, final OutputStream out) throws IOException {
28         final var jsonWriter = FormattableBodySupport.createJsonWriter(out, () -> PrettyPrintParam.FALSE);
29         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status")
30             .beginObject().name("patch-id").value(body.patchId());
31
32         if (body.ok()) {
33             reportSuccess(jsonWriter);
34         } else {
35             final var globalErrors = body.globalErrors();
36             if (globalErrors != null) {
37                 reportErrors(body.databind(), globalErrors, jsonWriter);
38             } else {
39                 jsonWriter.name("edit-status").beginObject()
40                     .name("edit").beginArray();
41                 for (var editStatus : body.editCollection()) {
42                     jsonWriter.beginObject().name("edit-id").value(editStatus.getEditId());
43
44                     final var editErrors = editStatus.getEditErrors();
45                     if (editErrors != null) {
46                         reportErrors(body.databind(), editErrors, jsonWriter);
47                     } else if (editStatus.isOk()) {
48                         reportSuccess(jsonWriter);
49                     }
50                     jsonWriter.endObject();
51                 }
52                 jsonWriter.endArray().endObject();
53             }
54         }
55         jsonWriter.endObject().endObject().flush();
56     }
57
58     private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
59         jsonWriter.name("ok").beginArray().nullValue().endArray();
60     }
61
62     private static void reportErrors(final DatabindContext databind, final List<RestconfError> errors,
63             final JsonWriter jsonWriter) throws IOException {
64         jsonWriter.name("errors").beginObject().name("error").beginArray();
65
66         for (var restconfError : errors) {
67             jsonWriter.beginObject()
68                 .name("error-type").value(restconfError.getErrorType().elementBody())
69                 .name("error-tag").value(restconfError.getErrorTag().elementBody());
70
71             final var errorPath = restconfError.getErrorPath();
72             if (errorPath != null) {
73                 jsonWriter.name("error-path");
74                 databind.jsonCodecs().instanceIdentifierCodec().writeValue(jsonWriter, errorPath);
75             }
76             final var errorMessage = restconfError.getErrorMessage();
77             if (errorMessage != null) {
78                 jsonWriter.name("error-message").value(errorMessage);
79             }
80             final var errorInfo = restconfError.getErrorInfo();
81             if (errorInfo != null) {
82                 jsonWriter.name("error-info").value(errorInfo);
83             }
84
85             jsonWriter.endObject();
86         }
87
88         jsonWriter.endArray().endObject();
89     }
90 }