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 javax.ws.rs.core.UriInfo;
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.NormalizedNodeJsonBodyWriter;
24 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
26 import org.opendaylight.yangtools.yang.common.OperationFailedException;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Implementation of the JSONRestconfService interface.
36 * @author Thomas Pantelis
39 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
42 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
44 @SuppressWarnings("checkstyle:IllegalCatch")
46 public void put(final String uriPath, final String payload, final UriInfo uriInfo) throws OperationFailedException {
47 Preconditions.checkNotNull(payload, "payload can't be null");
49 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
51 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
52 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
54 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
55 LOG.debug("Parsed NormalizedNode: {}", context.getData());
58 RestconfImpl.getInstance().updateConfigurationData(uriPath, context, uriInfo);
59 } catch (final Exception e) {
60 propagateExceptionAs(uriPath, e, "PUT");
64 @SuppressWarnings("checkstyle:IllegalCatch")
66 public void post(final String uriPath, final String payload, final UriInfo uriInfo)
67 throws OperationFailedException {
68 Preconditions.checkNotNull(payload, "payload can't be null");
70 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
72 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
73 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
75 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
76 LOG.debug("Parsed NormalizedNode: {}", context.getData());
79 RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
80 } catch (final Exception e) {
81 propagateExceptionAs(uriPath, e, "POST");
85 @SuppressWarnings("checkstyle:IllegalCatch")
87 public void delete(final String uriPath) throws OperationFailedException {
88 LOG.debug("delete: uriPath: {}", uriPath);
91 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
92 } catch (final Exception e) {
93 propagateExceptionAs(uriPath, e, "DELETE");
97 @SuppressWarnings("checkstyle:IllegalCatch")
99 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
100 throws OperationFailedException {
101 LOG.debug("get: uriPath: {}", uriPath);
104 NormalizedNodeContext readData;
105 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
106 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
108 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
111 final Optional<String> result = Optional.of(toJson(readData));
113 LOG.debug("get returning: {}", result.get());
116 } catch (final Exception e) {
117 if (!isDataMissing(e)) {
118 propagateExceptionAs(uriPath, e, "GET");
121 LOG.debug("Data missing - returning absent");
122 return Optional.absent();
126 @SuppressWarnings("checkstyle:IllegalCatch")
128 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
129 throws OperationFailedException {
130 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
132 final String actualInput = input.isPresent() ? input.get() : null;
134 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
136 String output = null;
138 NormalizedNodeContext outputContext;
139 if (actualInput != null) {
140 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
141 final NormalizedNodeContext inputContext =
142 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
144 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
145 .getInstanceIdentifier());
146 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
148 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
150 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
153 if (outputContext.getData() != null) {
154 output = toJson(outputContext);
156 } catch (final Exception e) {
157 propagateExceptionAs(uriPath, e, "RPC");
160 return Optional.fromNullable(output);
164 public void close() {
167 private static String toJson(final NormalizedNodeContext readData) throws IOException {
168 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
169 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
170 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
171 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
172 return outputStream.toString(StandardCharsets.UTF_8.name());
175 private static boolean isDataMissing(final Exception exception) {
176 boolean dataMissing = false;
177 if (exception instanceof RestconfDocumentedException) {
178 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
179 if (!rde.getErrors().isEmpty()) {
180 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
189 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
190 throws OperationFailedException {
191 LOG.debug("Error for uriPath: {}", uriPath, exception);
193 if (exception instanceof RestconfDocumentedException) {
194 throw new OperationFailedException(String.format(
195 "%s failed for URI %s", operation, uriPath), exception.getCause(),
196 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
199 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
202 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
203 final RpcError[] to = new RpcError[from.size()];
205 for (final RestconfError e: from) {
206 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
207 e.getErrorMessage());
213 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
216 return ErrorType.TRANSPORT;
219 return ErrorType.RPC;
222 return ErrorType.PROTOCOL;
225 return ErrorType.APPLICATION;