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