2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
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;
24 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
25 public class JsonPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
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());
33 reportSuccess(jsonWriter);
35 final var globalErrors = body.globalErrors();
36 if (globalErrors != null) {
37 reportErrors(body.databind(), globalErrors, jsonWriter);
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());
44 final var editErrors = editStatus.getEditErrors();
45 if (editErrors != null) {
46 reportErrors(body.databind(), editErrors, jsonWriter);
47 } else if (editStatus.isOk()) {
48 reportSuccess(jsonWriter);
50 jsonWriter.endObject();
52 jsonWriter.endArray().endObject();
55 jsonWriter.endObject().endObject().flush();
58 private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
59 jsonWriter.name("ok").beginArray().nullValue().endArray();
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();
66 for (var restconfError : errors) {
67 jsonWriter.beginObject()
68 .name("error-type").value(restconfError.getErrorType().elementBody())
69 .name("error-tag").value(restconfError.getErrorTag().elementBody());
71 final var errorPath = restconfError.getErrorPath();
72 if (errorPath != null) {
73 jsonWriter.name("error-path");
74 databind.jsonCodecs().instanceIdentifierCodec().writeValue(jsonWriter, errorPath);
76 final var errorMessage = restconfError.getErrorMessage();
77 if (errorMessage != null) {
78 jsonWriter.name("error-message").value(errorMessage);
80 final var errorInfo = restconfError.getErrorInfo();
81 if (errorInfo != null) {
82 jsonWriter.name("error-info").value(errorInfo);
85 jsonWriter.endObject();
88 jsonWriter.endArray().endObject();