2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
10 import static java.util.Objects.requireNonNull;
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;
57 * Implementation of the JSONRestconfService interface using the restconf Draft18 implementation.
59 * @author Thomas Pantelis
62 public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, AutoCloseable {
63 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceRfc8040Impl.class);
65 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
67 private final TransactionServicesWrapper services;
68 private final DOMMountPointServiceHandler mountPointServiceHandler;
69 private final SchemaContextHandler schemaContextHandler;
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;
80 @SuppressWarnings("checkstyle:IllegalCatch")
82 public void put(final String uriPath, final String payload) throws OperationFailedException {
83 requireNonNull(payload, "payload can't be null");
85 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
87 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
89 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
90 LOG.debug("Parsed NormalizedNode: {}", context.getData());
93 services.putData(uriPath, context, new SimpleUriInfo(uriPath));
94 } catch (final Exception e) {
95 propagateExceptionAs(uriPath, e, "PUT");
99 @SuppressWarnings("checkstyle:IllegalCatch")
101 public void post(final String uriPath, final String payload) throws OperationFailedException {
102 requireNonNull(payload, "payload can't be null");
104 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
106 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
108 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
109 LOG.debug("Parsed NormalizedNode: {}", context.getData());
112 services.postData(uriPath, context, new SimpleUriInfo(uriPath));
113 } catch (final Exception e) {
114 propagateExceptionAs(uriPath, e, "POST");
118 @SuppressWarnings("checkstyle:IllegalCatch")
120 public void delete(final String uriPath) throws OperationFailedException {
121 LOG.debug("delete: uriPath: {}", uriPath);
124 services.deleteData(uriPath);
125 } catch (final Exception e) {
126 propagateExceptionAs(uriPath, e, "DELETE");
130 @SuppressWarnings("checkstyle:IllegalCatch")
132 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
133 throws OperationFailedException {
134 LOG.debug("get: uriPath: {}", uriPath);
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);
142 final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
143 final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
145 final Optional<String> result = Optional.of(toJson(readData));
147 LOG.debug("get returning: {}", result.get());
150 } catch (final Exception e) {
151 if (!isDataMissing(e)) {
152 propagateExceptionAs(uriPath, e, "GET");
155 LOG.debug("Data missing - returning absent");
156 return Optional.empty();
160 @SuppressWarnings("checkstyle:IllegalCatch")
161 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
163 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
164 throws OperationFailedException {
165 requireNonNull(uriPath, "uriPath can't be null");
167 final String actualInput = input.isPresent() ? input.get() : null;
169 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
171 String output = null;
173 final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
175 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
176 .getInstanceIdentifier());
177 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
179 final NormalizedNodeContext outputContext =
180 services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
182 if (outputContext.getData() != null) {
183 output = toJson(outputContext);
185 } catch (RuntimeException | IOException e) {
186 propagateExceptionAs(uriPath, e, "RPC");
189 return Optional.ofNullable(output);
192 @SuppressWarnings("checkstyle:IllegalCatch")
194 public Optional<String> patch(final String uriPath, final String payload)
195 throws OperationFailedException {
197 String output = null;
198 requireNonNull(payload, "payload can't be null");
200 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
202 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
204 JsonToPatchBodyReader jsonToPatchBodyReader =
205 new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
206 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
208 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
209 LOG.debug("Parsed NormalizedNode: {}", context.getData());
212 PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
213 output = toJson(patchStatusContext);
214 } catch (final Exception e) {
215 propagateExceptionAs(uriPath, e, "PATCH");
217 return Optional.ofNullable(output);
222 public void close() {
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()));
230 if (payload == null) {
231 return new NormalizedNodeContext(instanceIdentifierContext, null);
234 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
235 return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
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());
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());
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;
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);
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()));
273 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
276 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
277 final RpcError[] to = new RpcError[from.size()];
279 for (final RestconfError e: from) {
280 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
281 e.getErrorMessage());
287 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
290 return ErrorType.TRANSPORT;
292 return ErrorType.RPC;
294 return ErrorType.PROTOCOL;
296 return ErrorType.APPLICATION;