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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.Optional;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.lang.annotation.Annotation;
19 import java.nio.charset.StandardCharsets;
20 import java.util.List;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.MultivaluedMap;
25 import javax.ws.rs.core.UriInfo;
26 import org.apache.aries.blueprint.annotation.service.Service;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.netconf.sal.rest.api.RestconfService;
29 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
31 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
32 import org.opendaylight.netconf.sal.rest.impl.PatchJsonBodyWriter;
33 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
34 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError;
37 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
38 import org.opendaylight.restconf.common.patch.PatchContext;
39 import org.opendaylight.restconf.common.patch.PatchStatusContext;
40 import org.opendaylight.restconf.common.util.MultivaluedHashMap;
41 import org.opendaylight.restconf.common.util.SimpleUriInfo;
42 import org.opendaylight.yangtools.yang.common.OperationFailedException;
43 import org.opendaylight.yangtools.yang.common.RpcError;
44 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
52 * @author Thomas Pantelis
53 * @deprecated Replaced by {JSONRestconfServiceRfc8040Impl from restconf-nb-rfc8040
57 @Service(classes = JSONRestconfService.class)
58 public class JSONRestconfServiceImpl implements JSONRestconfService {
59 private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
61 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
63 private final ControllerContext controllerContext;
64 private final RestconfService restconfService;
67 public JSONRestconfServiceImpl(final ControllerContext controllerContext, final RestconfImpl restconfService) {
68 this.controllerContext = controllerContext;
69 this.restconfService = restconfService;
72 @SuppressWarnings("checkstyle:IllegalCatch")
74 public void put(final String uriPath, final String payload) throws OperationFailedException {
75 requireNonNull(payload, "payload can't be null");
77 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
79 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
80 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false,
83 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
84 LOG.debug("Parsed NormalizedNode: {}", context.getData());
87 restconfService.updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
88 } catch (final Exception e) {
89 propagateExceptionAs(uriPath, e, "PUT");
93 @SuppressWarnings("checkstyle:IllegalCatch")
95 public void post(final String uriPath, final String payload) throws OperationFailedException {
96 requireNonNull(payload, "payload can't be null");
98 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
100 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
101 final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true,
104 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
105 LOG.debug("Parsed NormalizedNode: {}", context.getData());
108 restconfService.createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
109 } catch (final Exception e) {
110 propagateExceptionAs(uriPath, e, "POST");
114 @SuppressWarnings("checkstyle:IllegalCatch")
116 public void delete(final String uriPath) throws OperationFailedException {
117 LOG.debug("delete: uriPath: {}", uriPath);
120 restconfService.deleteConfigurationData(uriPath);
121 } catch (final Exception e) {
122 propagateExceptionAs(uriPath, e, "DELETE");
126 @SuppressWarnings("checkstyle:IllegalCatch")
128 public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
129 throws OperationFailedException {
130 LOG.debug("get: uriPath: {}", uriPath);
133 NormalizedNodeContext readData;
134 final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
135 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
136 readData = restconfService.readConfigurationData(uriPath, uriInfo);
138 readData = restconfService.readOperationalData(uriPath, uriInfo);
141 final Optional<String> result = Optional.of(toJson(readData));
143 LOG.debug("get returning: {}", result.get());
146 } catch (final Exception e) {
147 if (!isDataMissing(e)) {
148 propagateExceptionAs(uriPath, e, "GET");
151 LOG.debug("Data missing - returning absent");
152 return Optional.absent();
156 @SuppressWarnings("checkstyle:IllegalCatch")
157 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
159 public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
160 throws OperationFailedException {
161 requireNonNull(uriPath, "uriPath can't be null");
163 final String actualInput = input.isPresent() ? input.get() : null;
165 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
167 String output = null;
169 NormalizedNodeContext outputContext;
170 if (actualInput != null) {
171 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
172 final NormalizedNodeContext inputContext =
173 JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, controllerContext);
175 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
176 .getInstanceIdentifier());
177 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
179 outputContext = restconfService.invokeRpc(uriPath, inputContext, null);
181 outputContext = restconfService.invokeRpc(uriPath, "", null);
184 if (outputContext.getData() != null) {
185 output = toJson(outputContext);
187 } catch (final RuntimeException | IOException e) {
188 propagateExceptionAs(uriPath, e, "RPC");
191 return Optional.fromNullable(output);
194 @SuppressWarnings("checkstyle:IllegalCatch")
196 public Optional<String> patch(final String uriPath, final String payload)
197 throws OperationFailedException {
199 String output = null;
200 requireNonNull(payload, "payload can't be null");
202 LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
204 final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
206 JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
207 final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
209 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
210 LOG.debug("Parsed NormalizedNode: {}", context.getData());
213 PatchStatusContext patchStatusContext = restconfService
214 .patchConfigurationData(context, new SimpleUriInfo(uriPath));
215 output = toJson(patchStatusContext);
216 } catch (final Exception e) {
217 propagateExceptionAs(uriPath, e, "PATCH");
219 return Optional.fromNullable(output);
222 @SuppressWarnings("checkstyle:IllegalCatch")
224 public Optional<String> subscribeToStream(final String identifier, final MultivaluedMap<String, String> params)
225 throws OperationFailedException {
226 //Note: We use http://127.0.0.1 because the Uri parser requires something there though it does nothing
227 String uri = new StringBuilder("http://127.0.0.1:8081/restconf/streams/stream/").append(identifier).toString();
228 MultivaluedMap queryParams = params != null ? params : new MultivaluedHashMap<String, String>();
229 UriInfo uriInfo = new SimpleUriInfo(uri, queryParams);
231 String jsonRes = null;
233 NormalizedNodeContext res = restconfService.subscribeToStream(identifier, uriInfo);
234 jsonRes = toJson(res);
235 } catch (final Exception e) {
236 propagateExceptionAs(identifier, e, "RPC");
239 return Optional.fromNullable(jsonRes);
242 private static String toJson(final PatchStatusContext patchStatusContext) throws IOException {
243 final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
244 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
245 writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
246 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
247 return outputStream.toString(StandardCharsets.UTF_8.name());
250 private static String toJson(final NormalizedNodeContext readData) throws IOException {
251 final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
252 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
253 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
254 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
255 return outputStream.toString(StandardCharsets.UTF_8.name());
258 private static boolean isDataMissing(final Exception exception) {
259 boolean dataMissing = false;
260 if (exception instanceof RestconfDocumentedException) {
261 final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
262 if (!rde.getErrors().isEmpty()) {
263 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
272 private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
273 throws OperationFailedException {
274 LOG.debug("Error for uriPath: {}", uriPath, exception);
276 if (exception instanceof RestconfDocumentedException) {
277 throw new OperationFailedException(String.format(
278 "%s failed for URI %s", operation, uriPath), exception.getCause(),
279 toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
282 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
285 private static RpcError[] toRpcErrors(final List<RestconfError> from) {
286 final RpcError[] to = new RpcError[from.size()];
288 for (final RestconfError e: from) {
289 to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
290 e.getErrorMessage());
296 private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
299 return ErrorType.TRANSPORT;
302 return ErrorType.RPC;
305 return ErrorType.PROTOCOL;
308 return ErrorType.APPLICATION;