2dbb374cdefbc219514063b6de93d9bb4602f0bf
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / restconf / impl / JSONRestconfServiceImpl.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.restconf.impl;
9
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;
32
33 /**
34  * Implementation of the JSONRestconfService interface.
35  *
36  * @author Thomas Pantelis
37  */
38 @Deprecated
39 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
40     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
41
42     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
43
44     @SuppressWarnings("checkstyle:IllegalCatch")
45     @Override
46     public void put(final String uriPath, final String payload, final UriInfo uriInfo) throws OperationFailedException {
47         Preconditions.checkNotNull(payload, "payload can't be null");
48
49         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
50
51         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
52         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
53
54         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
55         LOG.debug("Parsed NormalizedNode: {}", context.getData());
56
57         try {
58             RestconfImpl.getInstance().updateConfigurationData(uriPath, context, uriInfo);
59         } catch (final Exception e) {
60             propagateExceptionAs(uriPath, e, "PUT");
61         }
62     }
63
64     @SuppressWarnings("checkstyle:IllegalCatch")
65     @Override
66     public void post(final String uriPath, final String payload, final UriInfo uriInfo)
67             throws OperationFailedException {
68         Preconditions.checkNotNull(payload, "payload can't be null");
69
70         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
71
72         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
73         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
74
75         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
76         LOG.debug("Parsed NormalizedNode: {}", context.getData());
77
78         try {
79             RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
80         } catch (final Exception e) {
81             propagateExceptionAs(uriPath, e, "POST");
82         }
83     }
84
85     @SuppressWarnings("checkstyle:IllegalCatch")
86     @Override
87     public void delete(final String uriPath) throws OperationFailedException {
88         LOG.debug("delete: uriPath: {}", uriPath);
89
90         try {
91             RestconfImpl.getInstance().deleteConfigurationData(uriPath);
92         } catch (final Exception e) {
93             propagateExceptionAs(uriPath, e, "DELETE");
94         }
95     }
96
97     @SuppressWarnings("checkstyle:IllegalCatch")
98     @Override
99     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
100             throws OperationFailedException {
101         LOG.debug("get: uriPath: {}", uriPath);
102
103         try {
104             NormalizedNodeContext readData;
105             if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
106                 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
107             } else {
108                 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
109             }
110
111             final Optional<String> result = Optional.of(toJson(readData));
112
113             LOG.debug("get returning: {}", result.get());
114
115             return result;
116         } catch (final Exception e) {
117             if (!isDataMissing(e)) {
118                 propagateExceptionAs(uriPath, e, "GET");
119             }
120
121             LOG.debug("Data missing - returning absent");
122             return Optional.absent();
123         }
124     }
125
126     @SuppressWarnings("checkstyle:IllegalCatch")
127     @Override
128     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
129             throws OperationFailedException {
130         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
131
132         final String actualInput = input.isPresent() ? input.get() : null;
133
134         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
135
136         String output = null;
137         try {
138             NormalizedNodeContext outputContext;
139             if (actualInput != null) {
140                 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
141                 final NormalizedNodeContext inputContext =
142                         JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
143
144                 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
145                         .getInstanceIdentifier());
146                 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
147
148                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
149             } else {
150                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
151             }
152
153             if (outputContext.getData() != null) {
154                 output = toJson(outputContext);
155             }
156         } catch (final Exception e) {
157             propagateExceptionAs(uriPath, e, "RPC");
158         }
159
160         return Optional.fromNullable(output);
161     }
162
163     @Override
164     public void close() {
165     }
166
167     private static String toJson(final NormalizedNodeContext readData) throws IOException {
168         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
169         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
170         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
171                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
172         return outputStream.toString(StandardCharsets.UTF_8.name());
173     }
174
175     private static boolean isDataMissing(final Exception exception) {
176         boolean dataMissing = false;
177         if (exception instanceof RestconfDocumentedException) {
178             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
179             if (!rde.getErrors().isEmpty()) {
180                 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
181                     dataMissing = true;
182                 }
183             }
184         }
185
186         return dataMissing;
187     }
188
189     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
190             throws OperationFailedException {
191         LOG.debug("Error for uriPath: {}", uriPath, exception);
192
193         if (exception instanceof RestconfDocumentedException) {
194             throw new OperationFailedException(String.format(
195                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
196                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
197         }
198
199         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
200     }
201
202     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
203         final RpcError[] to = new RpcError[from.size()];
204         int index = 0;
205         for (final RestconfError e: from) {
206             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
207                     e.getErrorMessage());
208         }
209
210         return to;
211     }
212
213     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
214         switch (errorType) {
215             case TRANSPORT: {
216                 return ErrorType.TRANSPORT;
217             }
218             case RPC: {
219                 return ErrorType.RPC;
220             }
221             case PROTOCOL: {
222                 return ErrorType.PROTOCOL;
223             }
224             default: {
225                 return ErrorType.APPLICATION;
226             }
227         }
228     }
229 }