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.common.base.Charsets;
12 import com.google.gson.stream.JsonWriter;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.io.OutputStreamWriter;
16 import java.lang.annotation.Annotation;
17 import java.lang.reflect.Type;
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.MediaTypes;
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.yangtools.yang.data.codec.gson.JsonWriterFactory;
33 @Produces({MediaTypes.PATCH_STATUS + RestconfService.JSON})
34 public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext> {
37 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
38 return type.equals(PATCHStatusContext.class);
42 public long getSize(PATCHStatusContext patchStatusContext, Class<?> type, Type genericType, Annotation[]
43 annotations, MediaType mediaType) {
48 public void writeTo(PATCHStatusContext patchStatusContext, Class<?> type, Type genericType, Annotation[]
49 annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
50 throws IOException, WebApplicationException {
52 final JsonWriter jsonWriter = createJsonWriter(entityStream);
53 jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
54 jsonWriter.beginObject();
55 jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
56 if (patchStatusContext.isOk()) {
57 jsonWriter.name("ok").nullValue();
59 if (patchStatusContext.getGlobalErrors() != null) {
60 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
63 jsonWriter.name("edit-status");
64 jsonWriter.beginObject();
65 jsonWriter.name("edit");
66 jsonWriter.beginArray();
67 for (PATCHStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
68 jsonWriter.beginObject();
69 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
70 if (patchStatusEntity.getEditErrors() != null) {
71 reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
73 if (patchStatusEntity.isOk()) {
74 jsonWriter.name("ok").nullValue();
77 jsonWriter.endObject();
79 jsonWriter.endArray();
80 jsonWriter.endObject();
82 jsonWriter.endObject();
83 jsonWriter.endObject();
88 private static void reportErrors(List<RestconfError> errors, JsonWriter jsonWriter) throws IOException {
89 jsonWriter.name("errors");
90 jsonWriter.beginObject();
91 jsonWriter.name("error");
92 jsonWriter.beginArray();
94 for (RestconfError restconfError : errors) {
95 jsonWriter.beginObject();
96 jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
97 jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
98 //TODO: fix error-path reporting (separate error-path from error-message)
99 //jsonWriter.name("error-path").value(restconfError.getErrorPath());
100 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
101 jsonWriter.endObject();
104 jsonWriter.endArray();
105 jsonWriter.endObject();
108 private static JsonWriter createJsonWriter(final OutputStream entityStream) {
109 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));