Add support for RFC8072 media types
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / PatchJsonBodyWriter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10
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.restconf.common.errors.RestconfError;
26 import org.opendaylight.restconf.common.patch.PatchStatusContext;
27 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
28 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MediaTypes;
29 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
31
32 @Provider
33 @Produces({ MediaTypes.YANG_PATCH_STATUS + RestconfConstants.JSON, MediaTypes.DATA + RestconfConstants.JSON })
34 public class PatchJsonBodyWriter implements MessageBodyWriter<PatchStatusContext> {
35
36     @Override
37     public boolean isWriteable(final Class<?> type, final Type genericType,
38                                final Annotation[] annotations, final MediaType mediaType) {
39         return type.equals(PatchStatusContext.class);
40     }
41
42     @Override
43     public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
44                         final Annotation[] annotations, final MediaType mediaType) {
45         return -1;
46     }
47
48     @Override
49     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
50                         final Annotation[] annotations, final MediaType mediaType,
51                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
52             throws IOException, WebApplicationException {
53
54         final JsonWriter jsonWriter = createJsonWriter(entityStream);
55         jsonWriter.beginObject().name("ietf-yang-patch:yang-patch-status");
56         jsonWriter.beginObject();
57         jsonWriter.name("patch-id").value(patchStatusContext.getPatchId());
58         if (patchStatusContext.isOk()) {
59             reportSuccess(jsonWriter);
60         } else {
61             if (patchStatusContext.getGlobalErrors() != null) {
62                 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
63             }
64
65             jsonWriter.name("edit-status");
66             jsonWriter.beginObject();
67             jsonWriter.name("edit");
68             jsonWriter.beginArray();
69             for (final PatchStatusEntity patchStatusEntity : patchStatusContext.getEditCollection()) {
70                 jsonWriter.beginObject();
71                 jsonWriter.name("edit-id").value(patchStatusEntity.getEditId());
72                 if (patchStatusEntity.getEditErrors() != null) {
73                     reportErrors(patchStatusEntity.getEditErrors(), jsonWriter);
74                 } else {
75                     if (patchStatusEntity.isOk()) {
76                         reportSuccess(jsonWriter);
77                     }
78                 }
79                 jsonWriter.endObject();
80             }
81             jsonWriter.endArray();
82             jsonWriter.endObject();
83         }
84         jsonWriter.endObject();
85         jsonWriter.endObject();
86         jsonWriter.flush();
87     }
88
89     private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
90         jsonWriter.name("ok").beginArray().nullValue().endArray();
91     }
92
93     private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
94         jsonWriter.name("errors");
95         jsonWriter.beginObject();
96         jsonWriter.name("error");
97         jsonWriter.beginArray();
98
99         for (final RestconfError restconfError : errors) {
100             jsonWriter.beginObject();
101             jsonWriter.name("error-type").value(restconfError.getErrorType().getErrorTypeTag());
102             jsonWriter.name("error-tag").value(restconfError.getErrorTag().getTagValue());
103
104             // optional node
105             if (restconfError.getErrorPath() != null) {
106                 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
107             }
108
109             // optional node
110             if (restconfError.getErrorMessage() != null) {
111                 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
112             }
113
114             // optional node
115             if (restconfError.getErrorInfo() != null) {
116                 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
117             }
118
119             jsonWriter.endObject();
120         }
121
122         jsonWriter.endArray();
123         jsonWriter.endObject();
124     }
125
126     private static JsonWriter createJsonWriter(final OutputStream entityStream) {
127         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
128     }
129 }