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
38 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
41 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
43 @SuppressWarnings("checkstyle:IllegalCatch")
45 public void put(final String uriPath, final String payload, final UriInfo uriInfo) throws OperationFailedException {
46 Preconditions.checkNotNull(payload, "payload can't be null");
48 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
50 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
51 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
53 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
54 LOG.debug("Parsed NormalizedNode: {}", context.getData());
57 RestconfImpl.getInstance().updateConfigurationData(uriPath, context, uriInfo);
58 } catch (final Exception e) {
59 propagateExceptionAs(uriPath, e, "PUT");
63 @SuppressWarnings("checkstyle:IllegalCatch")
65 public void post(final String uriPath, final String payload, final UriInfo uriInfo)
66 throws OperationFailedException {
67 Preconditions.checkNotNull(payload, "payload can't be null");
69 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
71 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
72 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
74 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
75 LOG.debug("Parsed NormalizedNode: {}", context.getData());
78 RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
79 } catch (final Exception e) {
80 propagateExceptionAs(uriPath, e, "POST");
84 @SuppressWarnings("checkstyle:IllegalCatch")
86 public void delete(final String uriPath) throws OperationFailedException {
87 LOG.debug("delete: uriPath: {}", uriPath);
90 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
91 } catch (final Exception e) {
92 propagateExceptionAs(uriPath, e, "DELETE");
96 @SuppressWarnings("checkstyle:IllegalCatch")
98 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
99 throws OperationFailedException {
100 LOG.debug("get: uriPath: {}", uriPath);
103 NormalizedNodeContext readData;
104 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
105 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
107 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
110 final Optional<String> result = Optional.of(toJson(readData));
112 LOG.debug("get returning: {}", result.get());
115 } catch (final Exception e) {
116 if (!isDataMissing(e)) {
117 propagateExceptionAs(uriPath, e, "GET");
120 LOG.debug("Data missing - returning absent");
121 return Optional.absent();
125 @SuppressWarnings("checkstyle:IllegalCatch")
127 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
128 throws OperationFailedException {
129 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
131 final String actualInput = input.isPresent() ? input.get() : null;
133 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
135 String output = null;
137 NormalizedNodeContext outputContext;
138 if (actualInput != null) {
139 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
140 final NormalizedNodeContext inputContext =
141 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
143 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
144 .getInstanceIdentifier());
145 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
147 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
149 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
152 if (outputContext.getData() != null) {
153 output = toJson(outputContext);
155 } catch (final Exception e) {
156 propagateExceptionAs(uriPath, e, "RPC");
159 return Optional.fromNullable(output);
163 public void close() {
166 private static String toJson(final NormalizedNodeContext readData) throws IOException {
167 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
168 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
169 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
170 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
171 return outputStream.toString(StandardCharsets.UTF_8.name());
174 private static boolean isDataMissing(final Exception exception) {
175 boolean dataMissing = false;
176 if (exception instanceof RestconfDocumentedException) {
177 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
178 if (!rde.getErrors().isEmpty()) {
179 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
188 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
189 throws OperationFailedException {
190 LOG.debug("Error for uriPath: {}", uriPath, exception);
192 if (exception instanceof RestconfDocumentedException) {
193 throw new OperationFailedException(String.format(
194 "%s failed for URI %s", operation, uriPath), exception.getCause(),
195 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
198 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
201 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
202 final RpcError[] to = new RpcError[from.size()];
204 for (final RestconfError e: from) {
205 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
206 e.getErrorMessage());
212 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
215 return ErrorType.TRANSPORT;
218 return ErrorType.RPC;
221 return ErrorType.PROTOCOL;
224 return ErrorType.APPLICATION;