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.Charsets;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.lang.annotation.Annotation;
18 import java.util.List;
19 import javax.ws.rs.core.MediaType;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
22 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
23 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
25 import org.opendaylight.yangtools.yang.common.OperationFailedException;
26 import org.opendaylight.yangtools.yang.common.RpcError;
27 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
28 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Implementation of the JSONRestconfService interface.
35 * @author Thomas Pantelis
37 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
38 private final static Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
40 private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
43 public void put(String uriPath, String payload) throws OperationFailedException {
44 Preconditions.checkNotNull(payload, "payload can't be null");
46 LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
48 InputStream entityStream = new ByteArrayInputStream(payload.getBytes(Charsets.UTF_8));
49 NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
51 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
52 LOG.debug("Parsed NormalizedNode: {}", context.getData());
55 RestconfImpl.getInstance().updateConfigurationData(uriPath, context);
56 } catch (Exception e) {
57 propagateExceptionAs(uriPath, e, "PUT");
62 public void post(String uriPath, String payload) throws OperationFailedException {
63 Preconditions.checkNotNull(payload, "payload can't be null");
65 LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
67 InputStream entityStream = new ByteArrayInputStream(payload.getBytes(Charsets.UTF_8));
68 NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
70 LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
71 LOG.debug("Parsed NormalizedNode: {}", context.getData());
74 RestconfImpl.getInstance().createConfigurationData(uriPath, context, null);
75 } catch (Exception e) {
76 propagateExceptionAs(uriPath, e, "POST");
81 public void delete(String uriPath) throws OperationFailedException {
82 LOG.debug("delete: uriPath: {}", uriPath);
85 RestconfImpl.getInstance().deleteConfigurationData(uriPath);
86 } catch (Exception e) {
87 propagateExceptionAs(uriPath, e, "DELETE");
92 public Optional<String> get(String uriPath, LogicalDatastoreType datastoreType) throws OperationFailedException {
93 LOG.debug("get: uriPath: {}", uriPath);
96 NormalizedNodeContext readData;
97 if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
98 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, null);
100 readData = RestconfImpl.getInstance().readOperationalData(uriPath, null);
103 Optional<String> result = Optional.of(toJson(readData));
105 LOG.debug("get returning: {}", result.get());
108 } catch (Exception e) {
109 if(!isDataMissing(e)) {
110 propagateExceptionAs(uriPath, e, "GET");
113 LOG.debug("Data missing - returning absent");
114 return Optional.absent();
119 public Optional<String> invokeRpc(String uriPath, Optional<String> input) throws OperationFailedException {
120 Preconditions.checkNotNull(uriPath, "uriPath can't be null");
122 String actualInput = input.isPresent() ? input.get() : null;
124 LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
126 String output = null;
128 NormalizedNodeContext outputContext;
129 if(actualInput != null) {
130 InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(Charsets.UTF_8));
131 NormalizedNodeContext inputContext = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
133 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
134 .getInstanceIdentifier());
135 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
137 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
139 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
142 if(outputContext.getData() != null) {
143 output = toJson(outputContext);
145 } catch (Exception e) {
146 propagateExceptionAs(uriPath, e, "RPC");
149 return Optional.fromNullable(output);
153 public void close() {
156 private String toJson(NormalizedNodeContext readData) throws IOException {
157 NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
158 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
159 writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
160 MediaType.APPLICATION_JSON_TYPE, null, outputStream );
161 return outputStream.toString(Charsets.UTF_8.name());
164 private boolean isDataMissing(Exception e) {
165 boolean dataMissing = false;
166 if(e instanceof RestconfDocumentedException) {
167 RestconfDocumentedException rde = (RestconfDocumentedException)e;
168 if(!rde.getErrors().isEmpty()) {
169 if(rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
178 private static void propagateExceptionAs(String uriPath, Exception e, String operation) throws OperationFailedException {
179 LOG.debug("Error for uriPath: {}", uriPath, e);
181 if(e instanceof RestconfDocumentedException) {
182 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e.getCause(),
183 toRpcErrors(((RestconfDocumentedException)e).getErrors()));
186 throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e);
189 private static RpcError[] toRpcErrors(List<RestconfError> from) {
190 RpcError[] to = new RpcError[from.size()];
192 for(RestconfError e: from) {
193 to[i++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
194 e.getErrorMessage());
200 private static ErrorType toRpcErrorType(RestconfError.ErrorType errorType) {
203 return ErrorType.TRANSPORT;
206 return ErrorType.RPC;
209 return ErrorType.PROTOCOL;
212 return ErrorType.APPLICATION;