63586af75b220ae8210864cae52102df902d4c86
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / 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.netconf.sal.rest.impl;
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.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;
31
32
33 @Provider
34 @Produces({ Draft02.MediaTypes.PATCH_STATUS + RestconfService.JSON })
35 public class PatchJsonBodyWriter implements MessageBodyWriter<PatchStatusContext> {
36
37     @Override
38     public boolean isWriteable(final Class<?> type, final Type genericType,
39                                final Annotation[] annotations, final MediaType mediaType) {
40         return type.equals(PatchStatusContext.class);
41     }
42
43     @Override
44     public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
45                         final Annotation[] annotations, final MediaType mediaType) {
46         return -1;
47     }
48
49     @Override
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 {
54
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);
61         } else {
62             if (patchStatusContext.getGlobalErrors() != null) {
63                 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
64             }
65
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);
75                 } else {
76                     if (patchStatusEntity.isOk()) {
77                         reportSuccess(jsonWriter);
78                     }
79                 }
80                 jsonWriter.endObject();
81             }
82             jsonWriter.endArray();
83             jsonWriter.endObject();
84         }
85         jsonWriter.endObject();
86         jsonWriter.endObject();
87         jsonWriter.flush();
88     }
89
90     private static void reportSuccess(final JsonWriter jsonWriter) throws IOException {
91         jsonWriter.name("ok").beginArray().nullValue().endArray();
92     }
93
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();
99
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());
104
105             // optional node
106             if (restconfError.getErrorPath() != null) {
107                 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
108             }
109
110             // optional node
111             if (restconfError.getErrorMessage() != null) {
112                 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
113             }
114
115             // optional node
116             if (restconfError.getErrorInfo() != null) {
117                 jsonWriter.name("error-info").value(restconfError.getErrorInfo());
118             }
119
120             jsonWriter.endObject();
121         }
122
123         jsonWriter.endArray();
124         jsonWriter.endObject();
125     }
126
127     private static JsonWriter createJsonWriter(final OutputStream entityStream) {
128         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
129     }
130 }