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 java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.lang.annotation.Annotation;
17 import java.nio.charset.StandardCharsets;
18 import java.util.List;
19 import javax.ws.rs.core.MediaType;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
22 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
23 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
24 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
25 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
26 import org.opendaylight.restconf.common.errors.RestconfError;
27 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
28 import org.opendaylight.restconf.common.util.SimpleUriInfo;
29 import org.opendaylight.yangtools.yang.common.OperationFailedException;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
39 * @author Thomas Pantelis
40 * @deprecated Replaced by {JSONRestconfServiceRfc8040Impl from restconf-nb-rfc8040
43 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
46 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
48 @SuppressWarnings("checkstyle:IllegalCatch")
50 public void put(final String uriPath, final String payload) throws OperationFailedException {
51 Preconditions.checkNotNull(payload, "payload can't be null");
53 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
55 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
56 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
58 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
59 LOG.debug("Parsed NormalizedNode: {}", context.getData());
62 RestconfImpl.getInstance().updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
63 } catch (final Exception e) {
64 propagateExceptionAs(uriPath, e, "PUT");
68 @SuppressWarnings("checkstyle:IllegalCatch")
70 public void post(final String uriPath, final String payload)
71 throws OperationFailedException {
72 Preconditions.checkNotNull(payload, "payload can't be null");
74 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
76 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
77 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
79 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
80 LOG.debug("Parsed NormalizedNode: {}", context.getData());
83 RestconfImpl.getInstance().createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
84 } catch (final Exception e) {
85 propagateExceptionAs(uriPath, e, "POST");
89 @SuppressWarnings("checkstyle:IllegalCatch")
91 public void delete(final String uriPath) throws OperationFailedException {
92 LOG.debug("delete: uriPath: {}", uriPath);
95 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
96 } catch (final Exception e) {
97 propagateExceptionAs(uriPath, e, "DELETE");
101 @SuppressWarnings("checkstyle:IllegalCatch")
103 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
104 throws OperationFailedException {
105 LOG.debug("get: uriPath: {}", uriPath);
108 NormalizedNodeContext readData;
109 final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
110 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
111 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
113 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
116 final Optional<String> result = Optional.of(toJson(readData));
118 LOG.debug("get returning: {}", result.get());
121 } catch (final Exception e) {
122 if (!isDataMissing(e)) {
123 propagateExceptionAs(uriPath, e, "GET");
126 LOG.debug("Data missing - returning absent");
127 return Optional.absent();
131 @SuppressWarnings("checkstyle:IllegalCatch")
133 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
134 throws OperationFailedException {
135 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
137 final String actualInput = input.isPresent() ? input.get() : null;
139 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
141 String output = null;
143 NormalizedNodeContext outputContext;
144 if (actualInput != null) {
145 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
146 final NormalizedNodeContext inputContext =
147 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
149 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
150 .getInstanceIdentifier());
151 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
153 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
155 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
158 if (outputContext.getData() != null) {
159 output = toJson(outputContext);
161 } catch (final Exception e) {
162 propagateExceptionAs(uriPath, e, "RPC");
165 return Optional.fromNullable(output);
169 public void close() {
172 private static String toJson(final NormalizedNodeContext readData) throws IOException {
173 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
174 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
175 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
176 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
177 return outputStream.toString(StandardCharsets.UTF_8.name());
180 private static boolean isDataMissing(final Exception exception) {
181 boolean dataMissing = false;
182 if (exception instanceof RestconfDocumentedException) {
183 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
184 if (!rde.getErrors().isEmpty()) {
185 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
194 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
195 throws OperationFailedException {
196 LOG.debug("Error for uriPath: {}", uriPath, exception);
198 if (exception instanceof RestconfDocumentedException) {
199 throw new OperationFailedException(String.format(
200 "%s failed for URI %s", operation, uriPath), exception.getCause(),
201 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
204 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
207 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
208 final RpcError[] to = new RpcError[from.size()];
210 for (final RestconfError e: from) {
211 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
212 e.getErrorMessage());
218 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
221 return ErrorType.TRANSPORT;
224 return ErrorType.RPC;
227 return ErrorType.PROTOCOL;
230 return ErrorType.APPLICATION;