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.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;
56 * Implementation of the JSONRestconfService interface using the RFC8040 implementation.
58 * @author Thomas Pantelis
61 public final class JSONRestconfServiceRfc8040Impl implements JSONRestconfService {
62 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceRfc8040Impl.class);
64 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
66 private final TransactionServicesWrapper services;
67 private final DOMMountPointServiceHandler mountPointServiceHandler;
68 private final SchemaContextHandler schemaContextHandler;
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;
79 @SuppressWarnings("checkstyle:IllegalCatch")
81 public void put(final String uriPath, final String payload) throws OperationFailedException {
82 requireNonNull(payload, "payload can't be null");
84 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
86 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
88 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
89 LOG.debug("Parsed NormalizedNode: {}", context.getData());
92 services.putData(uriPath, context, new SimpleUriInfo(uriPath));
93 } catch (final Exception e) {
94 propagateExceptionAs(uriPath, e, "PUT");
98 @SuppressWarnings("checkstyle:IllegalCatch")
100 public void post(final String uriPath, final String payload) throws OperationFailedException {
101 requireNonNull(payload, "payload can't be null");
103 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
105 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
107 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
108 LOG.debug("Parsed NormalizedNode: {}", context.getData());
111 services.postData(uriPath, context, new SimpleUriInfo(uriPath));
112 } catch (final Exception e) {
113 propagateExceptionAs(uriPath, e, "POST");
117 @SuppressWarnings("checkstyle:IllegalCatch")
119 public void delete(final String uriPath) throws OperationFailedException {
120 LOG.debug("delete: uriPath: {}", uriPath);
123 services.deleteData(uriPath);
124 } catch (final Exception e) {
125 propagateExceptionAs(uriPath, e, "DELETE");
129 @SuppressWarnings("checkstyle:IllegalCatch")
131 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
132 throws OperationFailedException {
133 LOG.debug("get: uriPath: {}", uriPath);
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);
141 final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
142 final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
144 final Optional<String> result = Optional.of(toJson(readData));
146 LOG.debug("get returning: {}", result.get());
149 } catch (final Exception e) {
150 if (!isDataMissing(e)) {
151 propagateExceptionAs(uriPath, e, "GET");
154 LOG.debug("Data missing - returning absent");
155 return Optional.empty();
159 @SuppressWarnings("checkstyle:IllegalCatch")
160 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
162 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
163 throws OperationFailedException {
164 requireNonNull(uriPath, "uriPath can't be null");
166 final String actualInput = input.isPresent() ? input.get() : null;
168 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
170 String output = null;
172 final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
174 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
175 .getInstanceIdentifier());
176 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
178 final NormalizedNodeContext outputContext =
179 services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
181 if (outputContext.getData() != null) {
182 output = toJson(outputContext);
184 } catch (RuntimeException | IOException e) {
185 propagateExceptionAs(uriPath, e, "RPC");
188 return Optional.ofNullable(output);
191 @SuppressWarnings("checkstyle:IllegalCatch")
193 public Optional<String> patch(final String uriPath, final String payload)
194 throws OperationFailedException {
196 String output = null;
197 requireNonNull(payload, "payload can't be null");
199 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
201 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
203 JsonToPatchBodyReader jsonToPatchBodyReader =
204 new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
205 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
207 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
208 LOG.debug("Parsed NormalizedNode: {}", context.getData());
211 PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
212 output = toJson(patchStatusContext);
213 } catch (final Exception e) {
214 propagateExceptionAs(uriPath, e, "PATCH");
216 return Optional.ofNullable(output);
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()));
224 if (payload == null) {
225 return new NormalizedNodeContext(instanceIdentifierContext, null);
228 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
229 return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
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);
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);
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;
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);
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()));
267 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
270 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
271 final RpcError[] to = new RpcError[from.size()];
273 for (final RestconfError e: from) {
274 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
275 e.getErrorMessage());
281 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
284 return ErrorType.TRANSPORT;
286 return ErrorType.RPC;
288 return ErrorType.PROTOCOL;
290 return ErrorType.APPLICATION;