Remove javax.annotation nullness annotations
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / JSONRestconfServiceRfc8040Impl.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.lang.annotation.Annotation;
18 import java.nio.charset.StandardCharsets;
19 import java.util.List;
20 import java.util.Optional;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.Response;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError;
30 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
31 import org.opendaylight.restconf.common.patch.PatchContext;
32 import org.opendaylight.restconf.common.patch.PatchStatusContext;
33 import org.opendaylight.restconf.common.util.MultivaluedHashMap;
34 import org.opendaylight.restconf.common.util.SimpleUriInfo;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
36 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
37 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
38 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeJsonBodyWriter;
39 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonToPatchBodyReader;
40 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchJsonBodyWriter;
41 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.JSONRestconfService;
42 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.TransactionServicesWrapper;
43 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
44 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
45 import org.opendaylight.yangtools.yang.common.OperationFailedException;
46 import org.opendaylight.yangtools.yang.common.RpcError;
47 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Implementation of the JSONRestconfService interface using the restconf Draft18 implementation.
54  *
55  * @author Thomas Pantelis
56  */
57 public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, AutoCloseable {
58     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceRfc8040Impl.class);
59
60     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
61
62     private final TransactionServicesWrapper services;
63     private final DOMMountPointServiceHandler mountPointServiceHandler;
64     private final SchemaContextHandler schemaContextHandler;
65
66     public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
67             final DOMMountPointServiceHandler mountPointServiceHandler,
68             final SchemaContextHandler schemaContextHandler) {
69         this.services = services;
70         this.mountPointServiceHandler = mountPointServiceHandler;
71         this.schemaContextHandler = schemaContextHandler;
72     }
73
74     @SuppressWarnings("checkstyle:IllegalCatch")
75     @Override
76     public void put(final String uriPath, final String payload) throws OperationFailedException {
77         requireNonNull(payload, "payload can't be null");
78
79         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
80
81         final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
82
83         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
84         LOG.debug("Parsed NormalizedNode: {}", context.getData());
85
86         try {
87             services.putData(uriPath, context, new SimpleUriInfo(uriPath));
88         } catch (final Exception e) {
89             propagateExceptionAs(uriPath, e, "PUT");
90         }
91     }
92
93     @SuppressWarnings("checkstyle:IllegalCatch")
94     @Override
95     public void post(final String uriPath, final String payload) throws OperationFailedException {
96         requireNonNull(payload, "payload can't be null");
97
98         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
99
100         final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
101
102         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
103         LOG.debug("Parsed NormalizedNode: {}", context.getData());
104
105         try {
106             services.postData(uriPath, context, new SimpleUriInfo(uriPath));
107         } catch (final Exception e) {
108             propagateExceptionAs(uriPath, e, "POST");
109         }
110     }
111
112     @SuppressWarnings("checkstyle:IllegalCatch")
113     @Override
114     public void delete(final String uriPath) throws OperationFailedException {
115         LOG.debug("delete: uriPath: {}", uriPath);
116
117         try {
118             services.deleteData(uriPath);
119         } catch (final Exception e) {
120             propagateExceptionAs(uriPath, e, "DELETE");
121         }
122     }
123
124     @SuppressWarnings("checkstyle:IllegalCatch")
125     @Override
126     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
127             throws OperationFailedException {
128         LOG.debug("get: uriPath: {}", uriPath);
129
130         try {
131             final MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
132             queryParams.putSingle(RestconfDataServiceConstant.ReadData.CONTENT,
133                     datastoreType == LogicalDatastoreType.CONFIGURATION ? RestconfDataServiceConstant.ReadData.CONFIG :
134                         RestconfDataServiceConstant.ReadData.NONCONFIG);
135
136             final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
137             final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
138
139             final Optional<String> result = Optional.of(toJson(readData));
140
141             LOG.debug("get returning: {}", result.get());
142
143             return result;
144         } catch (final Exception e) {
145             if (!isDataMissing(e)) {
146                 propagateExceptionAs(uriPath, e, "GET");
147             }
148
149             LOG.debug("Data missing - returning absent");
150             return Optional.empty();
151         }
152     }
153
154     @SuppressWarnings("checkstyle:IllegalCatch")
155     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
156     @Override
157     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
158             throws OperationFailedException {
159         requireNonNull(uriPath, "uriPath can't be null");
160
161         final String actualInput = input.isPresent() ? input.get() : null;
162
163         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
164
165         String output = null;
166         try {
167             final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
168
169             LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
170                     .getInstanceIdentifier());
171             LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
172
173             final NormalizedNodeContext outputContext =
174                     services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
175
176             if (outputContext.getData() != null) {
177                 output = toJson(outputContext);
178             }
179         } catch (RuntimeException | IOException e) {
180             propagateExceptionAs(uriPath, e, "RPC");
181         }
182
183         return Optional.ofNullable(output);
184     }
185
186     @SuppressWarnings("checkstyle:IllegalCatch")
187     @Override
188     public Optional<String> patch(final String uriPath, final String payload)
189             throws OperationFailedException {
190
191         String output = null;
192         requireNonNull(payload, "payload can't be null");
193
194         LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
195
196         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
197
198         JsonToPatchBodyReader jsonToPatchBodyReader =
199                 new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
200         final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
201
202         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
203         LOG.debug("Parsed NormalizedNode: {}", context.getData());
204
205         try {
206             PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
207             output = toJson(patchStatusContext);
208         } catch (final Exception e) {
209             propagateExceptionAs(uriPath, e, "PATCH");
210         }
211         return Optional.ofNullable(output);
212     }
213
214     @Override
215     public void close() {
216     }
217
218     private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, final @Nullable String payload,
219             final boolean isPost) throws OperationFailedException {
220         final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
221                 uriPath, schemaContextHandler.get(), Optional.of(mountPointServiceHandler.get()));
222
223         if (payload == null) {
224             return new NormalizedNodeContext(instanceIdentifierContext, null);
225         }
226
227         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
228         return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
229     }
230
231     private static String toJson(final PatchStatusContext patchStatusContext) throws IOException {
232         final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
233         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
234         writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
235                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
236         return outputStream.toString(StandardCharsets.UTF_8.name());
237     }
238
239     private static String toJson(final NormalizedNodeContext readData) throws IOException {
240         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
241         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
242         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
243                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
244         return outputStream.toString(StandardCharsets.UTF_8.name());
245     }
246
247     private static boolean isDataMissing(final Exception exception) {
248         if (exception instanceof RestconfDocumentedException) {
249             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
250             return !rde.getErrors().isEmpty() && rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING;
251         }
252
253         return false;
254     }
255
256     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
257             throws OperationFailedException {
258         LOG.debug("Error for uriPath: {}", uriPath, exception);
259
260         if (exception instanceof RestconfDocumentedException) {
261             throw new OperationFailedException(String.format(
262                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
263                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
264         }
265
266         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
267     }
268
269     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
270         final RpcError[] to = new RpcError[from.size()];
271         int index = 0;
272         for (final RestconfError e: from) {
273             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
274                     e.getErrorMessage());
275         }
276
277         return to;
278     }
279
280     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
281         switch (errorType) {
282             case TRANSPORT:
283                 return ErrorType.TRANSPORT;
284             case RPC:
285                 return ErrorType.RPC;
286             case PROTOCOL:
287                 return ErrorType.PROTOCOL;
288             default:
289                 return ErrorType.APPLICATION;
290         }
291     }
292 }