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