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;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Collections;
20 import java.util.List;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedHashMap;
23 import javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.core.PathSegment;
25 import javax.ws.rs.core.UriBuilder;
26 import javax.ws.rs.core.UriInfo;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
30 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
32 import org.opendaylight.yangtools.yang.common.OperationFailedException;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
42 * @author Thomas Pantelis
43 * @deprecated Replaced by {@link JSONRestconfServiceDraft18}
46 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
49 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
51 @SuppressWarnings("checkstyle:IllegalCatch")
53 public void put(final String uriPath, final String payload) throws OperationFailedException {
54 Preconditions.checkNotNull(payload, "payload can't be null");
56 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
58 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
59 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
61 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
62 LOG.debug("Parsed NormalizedNode: {}", context.getData());
65 RestconfImpl.getInstance().updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
66 } catch (final Exception e) {
67 propagateExceptionAs(uriPath, e, "PUT");
71 @SuppressWarnings("checkstyle:IllegalCatch")
73 public void post(final String uriPath, final String payload)
74 throws OperationFailedException {
75 Preconditions.checkNotNull(payload, "payload can't be null");
77 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
79 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
80 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
82 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
83 LOG.debug("Parsed NormalizedNode: {}", context.getData());
86 RestconfImpl.getInstance().createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
87 } catch (final Exception e) {
88 propagateExceptionAs(uriPath, e, "POST");
92 @SuppressWarnings("checkstyle:IllegalCatch")
94 public void delete(final String uriPath) throws OperationFailedException {
95 LOG.debug("delete: uriPath: {}", uriPath);
98 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
99 } catch (final Exception e) {
100 propagateExceptionAs(uriPath, e, "DELETE");
104 @SuppressWarnings("checkstyle:IllegalCatch")
106 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
107 throws OperationFailedException {
108 LOG.debug("get: uriPath: {}", uriPath);
111 NormalizedNodeContext readData;
112 final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
113 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
114 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
116 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
119 final Optional<String> result = Optional.of(toJson(readData));
121 LOG.debug("get returning: {}", result.get());
124 } catch (final Exception e) {
125 if (!isDataMissing(e)) {
126 propagateExceptionAs(uriPath, e, "GET");
129 LOG.debug("Data missing - returning absent");
130 return Optional.absent();
134 @SuppressWarnings("checkstyle:IllegalCatch")
136 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
137 throws OperationFailedException {
138 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
140 final String actualInput = input.isPresent() ? input.get() : null;
142 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
144 String output = null;
146 NormalizedNodeContext outputContext;
147 if (actualInput != null) {
148 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
149 final NormalizedNodeContext inputContext =
150 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
152 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
153 .getInstanceIdentifier());
154 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
156 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
158 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
161 if (outputContext.getData() != null) {
162 output = toJson(outputContext);
164 } catch (final Exception e) {
165 propagateExceptionAs(uriPath, e, "RPC");
168 return Optional.fromNullable(output);
172 public void close() {
175 private static String toJson(final NormalizedNodeContext readData) throws IOException {
176 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
177 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
178 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
179 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
180 return outputStream.toString(StandardCharsets.UTF_8.name());
183 private static boolean isDataMissing(final Exception exception) {
184 boolean dataMissing = false;
185 if (exception instanceof RestconfDocumentedException) {
186 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
187 if (!rde.getErrors().isEmpty()) {
188 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
197 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
198 throws OperationFailedException {
199 LOG.debug("Error for uriPath: {}", uriPath, exception);
201 if (exception instanceof RestconfDocumentedException) {
202 throw new OperationFailedException(String.format(
203 "%s failed for URI %s", operation, uriPath), exception.getCause(),
204 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
207 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
210 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
211 final RpcError[] to = new RpcError[from.size()];
213 for (final RestconfError e: from) {
214 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
215 e.getErrorMessage());
221 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
224 return ErrorType.TRANSPORT;
227 return ErrorType.RPC;
230 return ErrorType.PROTOCOL;
233 return ErrorType.APPLICATION;
238 private static class SimpleUriInfo implements UriInfo {
239 private final String path;
240 private final MultivaluedMap<String, String> queryParams;
242 SimpleUriInfo(String path) {
243 this(path, new MultivaluedHashMap<>());
246 SimpleUriInfo(String path, MultivaluedMap<String, String> queryParams) {
248 this.queryParams = queryParams;
252 public String getPath() {
257 public String getPath(boolean decode) {
262 public List<PathSegment> getPathSegments() {
263 throw new UnsupportedOperationException();
267 public List<PathSegment> getPathSegments(boolean decode) {
268 throw new UnsupportedOperationException();
272 public URI getRequestUri() {
273 return URI.create(path);
277 public UriBuilder getRequestUriBuilder() {
278 return UriBuilder.fromUri(getRequestUri());
282 public URI getAbsolutePath() {
283 return getRequestUri();
287 public UriBuilder getAbsolutePathBuilder() {
288 return UriBuilder.fromUri(getAbsolutePath());
292 public URI getBaseUri() {
293 return URI.create("");
297 public UriBuilder getBaseUriBuilder() {
298 return UriBuilder.fromUri(getBaseUri());
302 public MultivaluedMap<String, String> getPathParameters() {
303 return new MultivaluedHashMap<>();
307 public MultivaluedMap<String, String> getPathParameters(boolean decode) {
308 return getPathParameters();
312 public MultivaluedMap<String, String> getQueryParameters() {
317 public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
318 return getQueryParameters();
322 public List<String> getMatchedURIs() {
323 return Collections.emptyList();
327 public List<String> getMatchedURIs(boolean decode) {
328 return getMatchedURIs();
332 public List<Object> getMatchedResources() {
333 return Collections.emptyList();
337 public URI resolve(URI uri) {
342 public URI relativize(URI uri) {