1283d362cd82ef5fd4bd69ec75b7b7f7c1fb1077
[netconf.git] / restconf / sal-rest-connector / 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.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.Draft17;
31 import org.opendaylight.restconf.utils.RestconfConstants;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
33
34
35 @Provider
36 @Produces({Draft02.MediaTypes.PATCH_STATUS + RestconfService.JSON,
37         Draft17.MediaTypes.PATCH_STATUS + RestconfConstants.JSON})
38 public class PATCHJsonBodyWriter implements MessageBodyWriter<PATCHStatusContext> {
39
40     @Override
41     public boolean isWriteable(final Class<?> type, final Type genericType,
42                                final Annotation[] annotations, final MediaType mediaType) {
43         return type.equals(PATCHStatusContext.class);
44     }
45
46     @Override
47     public long getSize(final PATCHStatusContext patchStatusContext, final Class<?> type, final Type genericType,
48                         final Annotation[] annotations, final MediaType mediaType) {
49         return -1;
50     }
51
52     @Override
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 {
57
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);
64         } else {
65             if (patchStatusContext.getGlobalErrors() != null) {
66                 reportErrors(patchStatusContext.getGlobalErrors(), jsonWriter);
67             }
68
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);
78                 } else {
79                     if (patchStatusEntity.isOk()) {
80                         reportSuccess(jsonWriter);
81                     }
82                 }
83                 jsonWriter.endObject();
84             }
85             jsonWriter.endArray();
86             jsonWriter.endObject();
87         }
88         jsonWriter.endObject();
89         jsonWriter.endObject();
90         jsonWriter.flush();
91     }
92
93     private void reportSuccess(final JsonWriter jsonWriter) throws IOException {
94         jsonWriter.name("ok").beginArray().nullValue().endArray();
95     }
96
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();
102
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());
107
108             // optional node
109             if (restconfError.getErrorPath() != null) {
110                 jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
111             }
112
113             // optional node
114             if (restconfError.getErrorMessage() != null) {
115                 jsonWriter.name("error-message").value(restconfError.getErrorMessage());
116             }
117
118             jsonWriter.endObject();
119         }
120
121         jsonWriter.endArray();
122         jsonWriter.endObject();
123     }
124
125     private static JsonWriter createJsonWriter(final OutputStream entityStream) {
126         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
127     }
128 }