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.patch;
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;
29 @Produces(MediaTypes.APPLICATION_YANG_DATA_JSON)
30 public class JsonPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
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)
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);
44 if (patchStatusContext.getGlobalErrors() != null) {
45 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
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);
58 if (patchStatusEntity.isOk()) {
59 reportSuccess(jsonWriter);
62 jsonWriter.endObject();
64 jsonWriter.endArray();
65 jsonWriter.endObject();
67 jsonWriter.endObject();
68 jsonWriter.endObject();
72 private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
73 jsonWriter.name("ok").beginArray().nullValue().endArray();
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();
82 for (final RestconfError restconfError : errors) {
83 jsonWriter.beginObject();
84 jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
85 jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
88 if (restconfError.getErrorPath() != null) {
89 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
93 if (restconfError.getErrorMessage() != null) {
94 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
98 if (restconfError.getErrorInfo() != null) {
99 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
102 jsonWriter.endObject();
105 jsonWriter.endArray();
106 jsonWriter.endObject();
109 private static JsonWriter createJsonWriter(final OutputStream entityStream) {
110 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));