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.netconf.sal.restconf.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.ws.rs.core.MediaType;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.netconf.sal.rest.api.RestconfService;
23 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
24 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
25 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
26 import org.opendaylight.netconf.sal.rest.impl.PatchJsonBodyWriter;
27 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
28 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfError;
31 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
32 import org.opendaylight.restconf.common.patch.PatchContext;
33 import org.opendaylight.restconf.common.patch.PatchStatusContext;
34 import org.opendaylight.restconf.common.util.SimpleUriInfo;
35 import org.opendaylight.yangtools.yang.common.OperationFailedException;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
45 * @author Thomas Pantelis
46 * @deprecated Replaced by {JSONRestconfServiceRfc8040Impl from restconf-nb-rfc8040
49 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
50 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
52 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
54 private final ControllerContext controllerContext;
55 private final RestconfService restconfService;
57 public JSONRestconfServiceImpl(ControllerContext controllerContext, RestconfService restconfService) {
58 this.controllerContext = controllerContext;
59 this.restconfService = restconfService;
62 @SuppressWarnings("checkstyle:IllegalCatch")
64 public void put(final String uriPath, final String payload) throws OperationFailedException {
65 Preconditions.checkNotNull(payload, "payload can't be null");
67 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
69 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
70 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false,
73 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
74 LOG.debug("Parsed NormalizedNode: {}", context.getData());
77 restconfService.updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
78 } catch (final Exception e) {
79 propagateExceptionAs(uriPath, e, "PUT");
83 @SuppressWarnings("checkstyle:IllegalCatch")
85 public void post(final String uriPath, final String payload)
86 throws OperationFailedException {
87 Preconditions.checkNotNull(payload, "payload can't be null");
89 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
91 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
92 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true,
95 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
96 LOG.debug("Parsed NormalizedNode: {}", context.getData());
99 restconfService.createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
100 } catch (final Exception e) {
101 propagateExceptionAs(uriPath, e, "POST");
105 @SuppressWarnings("checkstyle:IllegalCatch")
107 public void delete(final String uriPath) throws OperationFailedException {
108 LOG.debug("delete: uriPath: {}", uriPath);
111 restconfService.deleteConfigurationData(uriPath);
112 } catch (final Exception e) {
113 propagateExceptionAs(uriPath, e, "DELETE");
117 @SuppressWarnings("checkstyle:IllegalCatch")
119 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
120 throws OperationFailedException {
121 LOG.debug("get: uriPath: {}", uriPath);
124 NormalizedNodeContext readData;
125 final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
126 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
127 readData = restconfService.readConfigurationData(uriPath, uriInfo);
129 readData = restconfService.readOperationalData(uriPath, uriInfo);
132 final Optional<String> result = Optional.of(toJson(readData));
134 LOG.debug("get returning: {}", result.get());
137 } catch (final Exception e) {
138 if (!isDataMissing(e)) {
139 propagateExceptionAs(uriPath, e, "GET");
142 LOG.debug("Data missing - returning absent");
143 return Optional.absent();
147 @SuppressWarnings("checkstyle:IllegalCatch")
148 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
150 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
151 throws OperationFailedException {
152 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
154 final String actualInput = input.isPresent() ? input.get() : null;
156 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
158 String output = null;
160 NormalizedNodeContext outputContext;
161 if (actualInput != null) {
162 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
163 final NormalizedNodeContext inputContext =
164 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, controllerContext);
166 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
167 .getInstanceIdentifier());
168 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
170 outputContext = restconfService.invokeRpc(uriPath, inputContext, null);
172 outputContext = restconfService.invokeRpc(uriPath, "", null);
175 if (outputContext.getData() != null) {
176 output = toJson(outputContext);
178 } catch (final RuntimeException | IOException e) {
179 propagateExceptionAs(uriPath, e, "RPC");
182 return Optional.fromNullable(output);
185 @SuppressWarnings("checkstyle:IllegalCatch")
187 public Optional<String> patch(final String uriPath, final String payload)
188 throws OperationFailedException {
190 String output = null;
191 Preconditions.checkNotNull(payload, "payload can't be null");
193 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
195 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
197 JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
198 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
200 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
201 LOG.debug("Parsed NormalizedNode: {}", context.getData());
204 PatchStatusContext patchStatusContext = restconfService
205 .patchConfigurationData(context, new SimpleUriInfo(uriPath));
206 output = toJson(patchStatusContext);
207 } catch (final Exception e) {
208 propagateExceptionAs(uriPath, e, "PATCH");
210 return Optional.fromNullable(output);
214 public void close() {
217 private String toJson(final PatchStatusContext patchStatusContext) throws IOException {
218 final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
219 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
220 writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
221 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
222 return outputStream.toString(StandardCharsets.UTF_8.name());
225 private static String toJson(final NormalizedNodeContext readData) throws IOException {
226 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
227 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
228 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
229 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
230 return outputStream.toString(StandardCharsets.UTF_8.name());
233 private static boolean isDataMissing(final Exception exception) {
234 boolean dataMissing = false;
235 if (exception instanceof RestconfDocumentedException) {
236 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
237 if (!rde.getErrors().isEmpty()) {
238 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
247 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
248 throws OperationFailedException {
249 LOG.debug("Error for uriPath: {}", uriPath, exception);
251 if (exception instanceof RestconfDocumentedException) {
252 throw new OperationFailedException(String.format(
253 "%s failed for URI %s", operation, uriPath), exception.getCause(),
254 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
257 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
260 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
261 final RpcError[] to = new RpcError[from.size()];
263 for (final RestconfError e: from) {
264 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
265 e.getErrorMessage());
271 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
274 return ErrorType.TRANSPORT;
277 return ErrorType.RPC;
280 return ErrorType.PROTOCOL;
283 return ErrorType.APPLICATION;