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
9 package org.opendaylight.netconf.sal.rest.impl;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import java.nio.charset.StandardCharsets;
18 import java.util.List;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyWriter;
24 import javax.ws.rs.ext.Provider;
25 import org.opendaylight.netconf.sal.rest.api.Draft02;
26 import org.opendaylight.netconf.sal.rest.api.RestconfService;
27 import org.opendaylight.restconf.common.errors.RestconfError;
28 import org.opendaylight.restconf.common.patch.PatchStatusContext;
29 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
34 @Produces({ Draft02.MediaTypes.PATCH_STATUS + RestconfService.JSON })
35 public class PatchJsonBodyWriter implements MessageBodyWriter<PatchStatusContext> {
38 public boolean isWriteable(final Class<?> type, final Type genericType,
39 final Annotation[] annotations, final MediaType mediaType) {
40 return type.equals(PatchStatusContext.class);
44 public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
45 final Annotation[] annotations, final MediaType mediaType) {
50 public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
51 final Annotation[] annotations, final MediaType mediaType,
52 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
53 throws IOException, WebApplicationException {
55 final JsonWriter jsonWriter = createJsonWriter(entityStream);
56 jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
57 jsonWriter.beginObject();
58 jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
59 if (patchStatusContext.isOk()) {
60 reportSuccess(jsonWriter);
62 if (patchStatusContext.getGlobalErrors() != null) {
63 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
66 jsonWriter.name("edit-status");
67 jsonWriter.beginObject();
68 jsonWriter.name("edit");
69 jsonWriter.beginArray();
70 for (final PatchStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
71 jsonWriter.beginObject();
72 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
73 if (patchStatusEntity.getEditErrors() != null) {
74 reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
76 if (patchStatusEntity.isOk()) {
77 reportSuccess(jsonWriter);
80 jsonWriter.endObject();
82 jsonWriter.endArray();
83 jsonWriter.endObject();
85 jsonWriter.endObject();
86 jsonWriter.endObject();
90 private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
91 jsonWriter.name("ok").beginArray().nullValue().endArray();
94 private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
95 jsonWriter.name("errors");
96 jsonWriter.beginObject();
97 jsonWriter.name("error");
98 jsonWriter.beginArray();
100 for (final RestconfError restconfError : errors) {
101 jsonWriter.beginObject();
102 jsonWriter.name("error-type").value(restconfError.getErrorType().elementBody());
103 jsonWriter.name("error-tag").value(restconfError.getErrorTag().elementBody());
106 if (restconfError.getErrorPath() != null) {
107 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
111 if (restconfError.getErrorMessage() != null) {
112 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
116 if (restconfError.getErrorInfo() != null) {
117 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
120 jsonWriter.endObject();
123 jsonWriter.endArray();
124 jsonWriter.endObject();
127 private static JsonWriter createJsonWriter(final OutputStream entityStream) {
128 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));