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.netconf.sal.restconf.impl.PATCHStatusContext;
28 import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusEntity;
29 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
30 import org.opendaylight.restconf.Rfc8040;
31 import org.opendaylight.restconf.utils.RestconfConstants;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
36 @Produces({Draft02.MediaTypes.PATCH_STATUS + RestconfService.JSON,
37 Rfc8040.MediaTypes.PATCH_STATUS + RestconfConstants.JSON})
38 public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext> {
41 public boolean isWriteable(final Class<?> type, final Type genericType,
42 final Annotation[] annotations, final MediaType mediaType) {
43 return type.equals(PATCHStatusContext.class);
47 public long getSize(final PATCHStatusContext patchStatusContext, final Class<?> type, final Type genericType,
48 final Annotation[] annotations, final MediaType mediaType) {
53 public void writeTo(final PATCHStatusContext patchStatusContext, final Class<?> type, final Type genericType,
54 final Annotation[] annotations, final MediaType mediaType,
55 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
56 throws IOException, WebApplicationException {
58 final JsonWriter jsonWriter = createJsonWriter(entityStream);
59 jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
60 jsonWriter.beginObject();
61 jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
62 if (patchStatusContext.isOk()) {
63 reportSuccess(jsonWriter);
65 if (patchStatusContext.getGlobalErrors() != null) {
66 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
69 jsonWriter.name("edit-status");
70 jsonWriter.beginObject();
71 jsonWriter.name("edit");
72 jsonWriter.beginArray();
73 for (final PATCHStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
74 jsonWriter.beginObject();
75 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
76 if (patchStatusEntity.getEditErrors() != null) {
77 reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
79 if (patchStatusEntity.isOk()) {
80 reportSuccess(jsonWriter);
83 jsonWriter.endObject();
85 jsonWriter.endArray();
86 jsonWriter.endObject();
88 jsonWriter.endObject();
89 jsonWriter.endObject();
93 private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
94 jsonWriter.name("ok").beginArray().nullValue().endArray();
97 private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
98 jsonWriter.name("errors");
99 jsonWriter.beginObject();
100 jsonWriter.name("error");
101 jsonWriter.beginArray();
103 for (final RestconfError restconfError : errors) {
104 jsonWriter.beginObject();
105 jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
106 jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
109 if (restconfError.getErrorPath() != null) {
110 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
114 if (restconfError.getErrorMessage() != null) {
115 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
118 jsonWriter.endObject();
121 jsonWriter.endArray();
122 jsonWriter.endObject();
125 private static JsonWriter createJsonWriter(final OutputStream entityStream) {
126 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));