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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
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 javax.annotation.Nullable;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.Response;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
30 import org.opendaylight.restconf.common.patch.PatchContext;
31 import org.opendaylight.restconf.common.patch.PatchStatusContext;
32 import org.opendaylight.restconf.common.util.MultivaluedHashMap;
33 import org.opendaylight.restconf.common.util.SimpleUriInfo;
34 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
36 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
37 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeJsonBodyWriter;
38 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonToPatchBodyReader;
39 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchJsonBodyWriter;
40 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.JSONRestconfService;
41 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.TransactionServicesWrapper;
42 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
43 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
44 import org.opendaylight.yangtools.yang.common.OperationFailedException;
45 import org.opendaylight.yangtools.yang.common.RpcError;
46 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Implementation of the JSONRestconfService interface using the restconf Draft18 implementation.
54 * @author Thomas Pantelis
56 public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, AutoCloseable {
57 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceRfc8040Impl.class);
59 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
61 private final TransactionServicesWrapper services;
62 private final DOMMountPointServiceHandler mountPointServiceHandler;
64 public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
65 final DOMMountPointServiceHandler mountPointServiceHandler) {
66 this.services = services;
67 this.mountPointServiceHandler = mountPointServiceHandler;
70 @SuppressWarnings("checkstyle:IllegalCatch")
72 public void put(final String uriPath, final String payload) throws OperationFailedException {
73 Preconditions.checkNotNull(payload, "payload can't be null");
75 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
77 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
79 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
80 LOG.debug("Parsed NormalizedNode: {}", context.getData());
83 services.putData(uriPath, context, new SimpleUriInfo(uriPath));
84 } catch (final Exception e) {
85 propagateExceptionAs(uriPath, e, "PUT");
89 @SuppressWarnings("checkstyle:IllegalCatch")
91 public void post(final String uriPath, final String payload)
92 throws OperationFailedException {
93 Preconditions.checkNotNull(payload, "payload can't be null");
95 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
97 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
99 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
100 LOG.debug("Parsed NormalizedNode: {}", context.getData());
103 services.postData(uriPath, context, new SimpleUriInfo(uriPath));
104 } catch (final Exception e) {
105 propagateExceptionAs(uriPath, e, "POST");
109 @SuppressWarnings("checkstyle:IllegalCatch")
111 public void delete(final String uriPath) throws OperationFailedException {
112 LOG.debug("delete: uriPath: {}", uriPath);
115 services.deleteData(uriPath);
116 } catch (final Exception e) {
117 propagateExceptionAs(uriPath, e, "DELETE");
121 @SuppressWarnings("checkstyle:IllegalCatch")
123 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
124 throws OperationFailedException {
125 LOG.debug("get: uriPath: {}", uriPath);
128 final MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
129 queryParams.putSingle(RestconfDataServiceConstant.ReadData.CONTENT,
130 datastoreType == LogicalDatastoreType.CONFIGURATION ? RestconfDataServiceConstant.ReadData.CONFIG :
131 RestconfDataServiceConstant.ReadData.NONCONFIG);
133 final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
134 final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
136 final Optional<String> result = Optional.of(toJson(readData));
138 LOG.debug("get returning: {}", result.get());
141 } catch (final Exception e) {
142 if (!isDataMissing(e)) {
143 propagateExceptionAs(uriPath, e, "GET");
146 LOG.debug("Data missing - returning absent");
147 return Optional.absent();
151 @SuppressWarnings("checkstyle:IllegalCatch")
152 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
154 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
155 throws OperationFailedException {
156 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
158 final String actualInput = input.isPresent() ? input.get() : null;
160 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
162 String output = null;
164 final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
166 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
167 .getInstanceIdentifier());
168 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
170 final NormalizedNodeContext outputContext =
171 services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
173 if (outputContext.getData() != null) {
174 output = toJson(outputContext);
176 } catch (RuntimeException | IOException e) {
177 propagateExceptionAs(uriPath, e, "RPC");
180 return Optional.fromNullable(output);
183 @SuppressWarnings("checkstyle:IllegalCatch")
185 public Optional<String> patch(final String uriPath, final String payload)
186 throws OperationFailedException {
188 String output = null;
189 Preconditions.checkNotNull(payload, "payload can't be null");
191 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
193 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
195 JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader();
196 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
198 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
199 LOG.debug("Parsed NormalizedNode: {}", context.getData());
202 PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
203 output = toJson(patchStatusContext);
204 } catch (final Exception e) {
205 propagateExceptionAs(uriPath, e, "PATCH");
207 return Optional.fromNullable(output);
211 public void close() {
214 private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
215 final boolean isPost) throws OperationFailedException {
216 final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
217 uriPath, SchemaContextHandler.getSchemaContext(),
218 Optional.of(mountPointServiceHandler.get()));
220 if (payload == null) {
221 return new NormalizedNodeContext(instanceIdentifierContext, null);
224 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
226 return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
227 } catch (final IOException e) {
228 propagateExceptionAs(uriPath, e, "GET");
233 private String toJson(final PatchStatusContext patchStatusContext) throws IOException {
234 final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
235 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
236 writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
237 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
238 return outputStream.toString(StandardCharsets.UTF_8.name());
241 private static String toJson(final NormalizedNodeContext readData) throws IOException {
242 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
243 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
244 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
245 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
246 return outputStream.toString(StandardCharsets.UTF_8.name());
249 private static boolean isDataMissing(final Exception exception) {
250 if (exception instanceof RestconfDocumentedException) {
251 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
252 return !rde.getErrors().isEmpty() && rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING;
258 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
259 throws OperationFailedException {
260 LOG.debug("Error for uriPath: {}", uriPath, exception);
262 if (exception instanceof RestconfDocumentedException) {
263 throw new OperationFailedException(String.format(
264 "%s failed for URI %s", operation, uriPath), exception.getCause(),
265 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
268 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
271 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
272 final RpcError[] to = new RpcError[from.size()];
274 for (final RestconfError e: from) {
275 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
276 e.getErrorMessage());
282 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
285 return ErrorType.TRANSPORT;
287 return ErrorType.RPC;
289 return ErrorType.PROTOCOL;
291 return ErrorType.APPLICATION;