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.impl.JsonNormalizedNodeBodyReader;
23 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
24 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
25 import org.opendaylight.netconf.sal.rest.impl.PatchJsonBodyWriter;
26 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError;
30 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
31 import org.opendaylight.restconf.common.patch.PatchContext;
32 import org.opendaylight.restconf.common.patch.PatchStatusContext;
33 import org.opendaylight.restconf.common.util.SimpleUriInfo;
34 import org.opendaylight.yangtools.yang.common.OperationFailedException;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
44 * @author Thomas Pantelis
45 * @deprecated Replaced by {JSONRestconfServiceRfc8040Impl from restconf-nb-rfc8040
48 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
49 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
51 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
53 private final ControllerContext controllerContext;
55 public JSONRestconfServiceImpl(ControllerContext controllerContext) {
56 this.controllerContext = controllerContext;
59 @SuppressWarnings("checkstyle:IllegalCatch")
61 public void put(final String uriPath, final String payload) throws OperationFailedException {
62 Preconditions.checkNotNull(payload, "payload can't be null");
64 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
66 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
67 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false,
70 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
71 LOG.debug("Parsed NormalizedNode: {}", context.getData());
74 RestconfImpl.getInstance().updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
75 } catch (final Exception e) {
76 propagateExceptionAs(uriPath, e, "PUT");
80 @SuppressWarnings("checkstyle:IllegalCatch")
82 public void post(final String uriPath, final String payload)
83 throws OperationFailedException {
84 Preconditions.checkNotNull(payload, "payload can't be null");
86 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
88 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
89 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true,
92 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
93 LOG.debug("Parsed NormalizedNode: {}", context.getData());
96 RestconfImpl.getInstance().createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
97 } catch (final Exception e) {
98 propagateExceptionAs(uriPath, e, "POST");
102 @SuppressWarnings("checkstyle:IllegalCatch")
104 public void delete(final String uriPath) throws OperationFailedException {
105 LOG.debug("delete: uriPath: {}", uriPath);
108 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
109 } catch (final Exception e) {
110 propagateExceptionAs(uriPath, e, "DELETE");
114 @SuppressWarnings("checkstyle:IllegalCatch")
116 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
117 throws OperationFailedException {
118 LOG.debug("get: uriPath: {}", uriPath);
121 NormalizedNodeContext readData;
122 final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
123 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
124 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
126 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
129 final Optional<String> result = Optional.of(toJson(readData));
131 LOG.debug("get returning: {}", result.get());
134 } catch (final Exception e) {
135 if (!isDataMissing(e)) {
136 propagateExceptionAs(uriPath, e, "GET");
139 LOG.debug("Data missing - returning absent");
140 return Optional.absent();
144 @SuppressWarnings("checkstyle:IllegalCatch")
145 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
147 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
148 throws OperationFailedException {
149 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
151 final String actualInput = input.isPresent() ? input.get() : null;
153 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
155 String output = null;
157 NormalizedNodeContext outputContext;
158 if (actualInput != null) {
159 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
160 final NormalizedNodeContext inputContext =
161 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, controllerContext);
163 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
164 .getInstanceIdentifier());
165 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
167 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
169 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
172 if (outputContext.getData() != null) {
173 output = toJson(outputContext);
175 } catch (final RuntimeException | IOException e) {
176 propagateExceptionAs(uriPath, e, "RPC");
179 return Optional.fromNullable(output);
182 @SuppressWarnings("checkstyle:IllegalCatch")
184 public Optional<String> patch(final String uriPath, final String payload)
185 throws OperationFailedException {
187 String output = null;
188 Preconditions.checkNotNull(payload, "payload can't be null");
190 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
192 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
194 JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
195 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
197 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
198 LOG.debug("Parsed NormalizedNode: {}", context.getData());
201 PatchStatusContext patchStatusContext = RestconfImpl.getInstance()
202 .patchConfigurationData(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 String toJson(final PatchStatusContext patchStatusContext) throws IOException {
215 final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
216 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
217 writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
218 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
219 return outputStream.toString(StandardCharsets.UTF_8.name());
222 private static String toJson(final NormalizedNodeContext readData) throws IOException {
223 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
224 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
225 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
226 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
227 return outputStream.toString(StandardCharsets.UTF_8.name());
230 private static boolean isDataMissing(final Exception exception) {
231 boolean dataMissing = false;
232 if (exception instanceof RestconfDocumentedException) {
233 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
234 if (!rde.getErrors().isEmpty()) {
235 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
244 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
245 throws OperationFailedException {
246 LOG.debug("Error for uriPath: {}", uriPath, exception);
248 if (exception instanceof RestconfDocumentedException) {
249 throw new OperationFailedException(String.format(
250 "%s failed for URI %s", operation, uriPath), exception.getCause(),
251 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
254 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
257 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
258 final RpcError[] to = new RpcError[from.size()];
260 for (final RestconfError e: from) {
261 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
262 e.getErrorMessage());
268 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
271 return ErrorType.TRANSPORT;
274 return ErrorType.RPC;
277 return ErrorType.PROTOCOL;
280 return ErrorType.APPLICATION;