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;
63 private final SchemaContextHandler schemaContextHandler;
65 public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
66 final DOMMountPointServiceHandler mountPointServiceHandler,
67 final SchemaContextHandler schemaContextHandler) {
68 this.services = services;
69 this.mountPointServiceHandler = mountPointServiceHandler;
70 this.schemaContextHandler = schemaContextHandler;
73 @SuppressWarnings("checkstyle:IllegalCatch")
75 public void put(final String uriPath, final String payload) throws OperationFailedException {
76 Preconditions.checkNotNull(payload, "payload can't be null");
78 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
80 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
82 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
83 LOG.debug("Parsed NormalizedNode: {}", context.getData());
86 services.putData(uriPath, context, new SimpleUriInfo(uriPath));
87 } catch (final Exception e) {
88 propagateExceptionAs(uriPath, e, "PUT");
92 @SuppressWarnings("checkstyle:IllegalCatch")
94 public void post(final String uriPath, final String payload)
95 throws OperationFailedException {
96 Preconditions.checkNotNull(payload, "payload can't be null");
98 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
100 final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
102 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
103 LOG.debug("Parsed NormalizedNode: {}", context.getData());
106 services.postData(uriPath, context, new SimpleUriInfo(uriPath));
107 } catch (final Exception e) {
108 propagateExceptionAs(uriPath, e, "POST");
112 @SuppressWarnings("checkstyle:IllegalCatch")
114 public void delete(final String uriPath) throws OperationFailedException {
115 LOG.debug("delete: uriPath: {}", uriPath);
118 services.deleteData(uriPath);
119 } catch (final Exception e) {
120 propagateExceptionAs(uriPath, e, "DELETE");
124 @SuppressWarnings("checkstyle:IllegalCatch")
126 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
127 throws OperationFailedException {
128 LOG.debug("get: uriPath: {}", uriPath);
131 final MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
132 queryParams.putSingle(RestconfDataServiceConstant.ReadData.CONTENT,
133 datastoreType == LogicalDatastoreType.CONFIGURATION ? RestconfDataServiceConstant.ReadData.CONFIG :
134 RestconfDataServiceConstant.ReadData.NONCONFIG);
136 final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
137 final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
139 final Optional<String> result = Optional.of(toJson(readData));
141 LOG.debug("get returning: {}", result.get());
144 } catch (final Exception e) {
145 if (!isDataMissing(e)) {
146 propagateExceptionAs(uriPath, e, "GET");
149 LOG.debug("Data missing - returning absent");
150 return Optional.absent();
154 @SuppressWarnings("checkstyle:IllegalCatch")
155 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
157 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
158 throws OperationFailedException {
159 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
161 final String actualInput = input.isPresent() ? input.get() : null;
163 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
165 String output = null;
167 final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
169 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
170 .getInstanceIdentifier());
171 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
173 final NormalizedNodeContext outputContext =
174 services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
176 if (outputContext.getData() != null) {
177 output = toJson(outputContext);
179 } catch (RuntimeException | IOException e) {
180 propagateExceptionAs(uriPath, e, "RPC");
183 return Optional.fromNullable(output);
186 @SuppressWarnings("checkstyle:IllegalCatch")
188 public Optional<String> patch(final String uriPath, final String payload)
189 throws OperationFailedException {
191 String output = null;
192 Preconditions.checkNotNull(payload, "payload can't be null");
194 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
196 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
198 JsonToPatchBodyReader jsonToPatchBodyReader =
199 new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
200 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
202 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
203 LOG.debug("Parsed NormalizedNode: {}", context.getData());
206 PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
207 output = toJson(patchStatusContext);
208 } catch (final Exception e) {
209 propagateExceptionAs(uriPath, e, "PATCH");
211 return Optional.fromNullable(output);
215 public void close() {
218 private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
219 final boolean isPost) throws OperationFailedException {
220 final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
221 uriPath, schemaContextHandler.get(), Optional.of(mountPointServiceHandler.get()));
223 if (payload == null) {
224 return new NormalizedNodeContext(instanceIdentifierContext, null);
227 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
229 return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
230 } catch (final IOException e) {
231 propagateExceptionAs(uriPath, e, "GET");
236 private String toJson(final PatchStatusContext patchStatusContext) throws IOException {
237 final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
238 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
239 writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
240 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
241 return outputStream.toString(StandardCharsets.UTF_8.name());
244 private static String toJson(final NormalizedNodeContext readData) throws IOException {
245 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
246 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
247 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
248 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
249 return outputStream.toString(StandardCharsets.UTF_8.name());
252 private static boolean isDataMissing(final Exception exception) {
253 if (exception instanceof RestconfDocumentedException) {
254 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
255 return !rde.getErrors().isEmpty() && rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING;
261 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
262 throws OperationFailedException {
263 LOG.debug("Error for uriPath: {}", uriPath, exception);
265 if (exception instanceof RestconfDocumentedException) {
266 throw new OperationFailedException(String.format(
267 "%s failed for URI %s", operation, uriPath), exception.getCause(),
268 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
271 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
274 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
275 final RpcError[] to = new RpcError[from.size()];
277 for (final RestconfError e: from) {
278 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
279 e.getErrorMessage());
285 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
288 return ErrorType.TRANSPORT;
290 return ErrorType.RPC;
292 return ErrorType.PROTOCOL;
294 return ErrorType.APPLICATION;