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 final static Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
42 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
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");
64 public void post(final String uriPath, final String payload, final UriInfo uriInfo)
65 throws OperationFailedException {
66 Preconditions.checkNotNull(payload, "payload can't be null");
68 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
70 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
71 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
73 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
74 LOG.debug("Parsed NormalizedNode: {}", context.getData());
77 RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
78 } catch (final Exception e) {
79 propagateExceptionAs(uriPath, e, "POST");
84 public void delete(final String uriPath) throws OperationFailedException {
85 LOG.debug("delete: uriPath: {}", uriPath);
88 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
89 } catch (final Exception e) {
90 propagateExceptionAs(uriPath, e, "DELETE");
95 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
96 throws OperationFailedException {
97 LOG.debug("get: uriPath: {}", uriPath);
100 NormalizedNodeContext readData;
101 if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
102 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
104 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
107 final Optional<String> result = Optional.of(toJson(readData));
109 LOG.debug("get returning: {}", result.get());
112 } catch (final Exception e) {
113 if(!isDataMissing(e)) {
114 propagateExceptionAs(uriPath, e, "GET");
117 LOG.debug("Data missing - returning absent");
118 return Optional.absent();
123 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input) throws OperationFailedException {
124 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
126 final String actualInput = input.isPresent() ? input.get() : null;
128 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
130 String output = null;
132 NormalizedNodeContext outputContext;
133 if(actualInput != null) {
134 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
135 final NormalizedNodeContext inputContext = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
137 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
138 .getInstanceIdentifier());
139 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
141 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
143 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
146 if(outputContext.getData() != null) {
147 output = toJson(outputContext);
149 } catch (final Exception e) {
150 propagateExceptionAs(uriPath, e, "RPC");
153 return Optional.fromNullable(output);
157 public void close() {
160 private String toJson(final NormalizedNodeContext readData) throws IOException {
161 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
162 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
163 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
164 MediaType.APPLICATION_JSON_TYPE, null, outputStream );
165 return outputStream.toString(StandardCharsets.UTF_8.name());
168 private boolean isDataMissing(final Exception e) {
169 boolean dataMissing = false;
170 if(e instanceof RestconfDocumentedException) {
171 final RestconfDocumentedException rde = (RestconfDocumentedException)e;
172 if(!rde.getErrors().isEmpty()) {
173 if(rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
182 private static void propagateExceptionAs(final String uriPath, final Exception e, final String operation) throws OperationFailedException {
183 LOG.debug("Error for uriPath: {}", uriPath, e);
185 if(e instanceof RestconfDocumentedException) {
186 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e.getCause(),
187 toRpcErrors(((RestconfDocumentedException)e).getErrors()));
190 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e);
193 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
194 final RpcError[] to = new RpcError[from.size()];
196 for(final RestconfError e: from) {
197 to[i++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
198 e.getErrorMessage());
204 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
207 return ErrorType.TRANSPORT;
210 return ErrorType.RPC;
213 return ErrorType.PROTOCOL;
216 return ErrorType.APPLICATION;