Split Restconf implementations (draft02 and RFC) - Prepare modules
[netconf.git] / restconf / restconf-nb-bierman02 / 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.net.URI;
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;
38
39 /**
40  * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
41  *
42  * @author Thomas Pantelis
43  * @deprecated Replaced by {@link JSONRestconfServiceDraft18}
44  */
45 @Deprecated
46 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
47     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
48
49     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
50
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     @Override
53     public void put(final String uriPath, final String payload) throws OperationFailedException {
54         Preconditions.checkNotNull(payload, "payload can't be null");
55
56         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
57
58         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
59         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
60
61         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
62         LOG.debug("Parsed NormalizedNode: {}", context.getData());
63
64         try {
65             RestconfImpl.getInstance().updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
66         } catch (final Exception e) {
67             propagateExceptionAs(uriPath, e, "PUT");
68         }
69     }
70
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     @Override
73     public void post(final String uriPath, final String payload)
74             throws OperationFailedException {
75         Preconditions.checkNotNull(payload, "payload can't be null");
76
77         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
78
79         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
80         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
81
82         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
83         LOG.debug("Parsed NormalizedNode: {}", context.getData());
84
85         try {
86             RestconfImpl.getInstance().createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
87         } catch (final Exception e) {
88             propagateExceptionAs(uriPath, e, "POST");
89         }
90     }
91
92     @SuppressWarnings("checkstyle:IllegalCatch")
93     @Override
94     public void delete(final String uriPath) throws OperationFailedException {
95         LOG.debug("delete: uriPath: {}", uriPath);
96
97         try {
98             RestconfImpl.getInstance().deleteConfigurationData(uriPath);
99         } catch (final Exception e) {
100             propagateExceptionAs(uriPath, e, "DELETE");
101         }
102     }
103
104     @SuppressWarnings("checkstyle:IllegalCatch")
105     @Override
106     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
107             throws OperationFailedException {
108         LOG.debug("get: uriPath: {}", uriPath);
109
110         try {
111             NormalizedNodeContext readData;
112             final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
113             if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
114                 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
115             } else {
116                 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
117             }
118
119             final Optional<String> result = Optional.of(toJson(readData));
120
121             LOG.debug("get returning: {}", result.get());
122
123             return result;
124         } catch (final Exception e) {
125             if (!isDataMissing(e)) {
126                 propagateExceptionAs(uriPath, e, "GET");
127             }
128
129             LOG.debug("Data missing - returning absent");
130             return Optional.absent();
131         }
132     }
133
134     @SuppressWarnings("checkstyle:IllegalCatch")
135     @Override
136     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
137             throws OperationFailedException {
138         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
139
140         final String actualInput = input.isPresent() ? input.get() : null;
141
142         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
143
144         String output = null;
145         try {
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);
151
152                 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
153                         .getInstanceIdentifier());
154                 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
155
156                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
157             } else {
158                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
159             }
160
161             if (outputContext.getData() != null) {
162                 output = toJson(outputContext);
163             }
164         } catch (final Exception e) {
165             propagateExceptionAs(uriPath, e, "RPC");
166         }
167
168         return Optional.fromNullable(output);
169     }
170
171     @Override
172     public void close() {
173     }
174
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());
181     }
182
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) {
189                     dataMissing = true;
190                 }
191             }
192         }
193
194         return dataMissing;
195     }
196
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);
200
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()));
205         }
206
207         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
208     }
209
210     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
211         final RpcError[] to = new RpcError[from.size()];
212         int index = 0;
213         for (final RestconfError e: from) {
214             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
215                     e.getErrorMessage());
216         }
217
218         return to;
219     }
220
221     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
222         switch (errorType) {
223             case TRANSPORT: {
224                 return ErrorType.TRANSPORT;
225             }
226             case RPC: {
227                 return ErrorType.RPC;
228             }
229             case PROTOCOL: {
230                 return ErrorType.PROTOCOL;
231             }
232             default: {
233                 return ErrorType.APPLICATION;
234             }
235         }
236     }
237
238     private static class SimpleUriInfo implements UriInfo {
239         private final String path;
240         private final MultivaluedMap<String, String> queryParams;
241
242         SimpleUriInfo(String path) {
243             this(path, new MultivaluedHashMap<>());
244         }
245
246         SimpleUriInfo(String path, MultivaluedMap<String, String> queryParams) {
247             this.path = path;
248             this.queryParams = queryParams;
249         }
250
251         @Override
252         public String getPath() {
253             return path;
254         }
255
256         @Override
257         public String getPath(boolean decode) {
258             return path;
259         }
260
261         @Override
262         public List<PathSegment> getPathSegments() {
263             throw new UnsupportedOperationException();
264         }
265
266         @Override
267         public List<PathSegment> getPathSegments(boolean decode) {
268             throw new UnsupportedOperationException();
269         }
270
271         @Override
272         public URI getRequestUri() {
273             return URI.create(path);
274         }
275
276         @Override
277         public UriBuilder getRequestUriBuilder() {
278             return UriBuilder.fromUri(getRequestUri());
279         }
280
281         @Override
282         public URI getAbsolutePath() {
283             return getRequestUri();
284         }
285
286         @Override
287         public UriBuilder getAbsolutePathBuilder() {
288             return UriBuilder.fromUri(getAbsolutePath());
289         }
290
291         @Override
292         public URI getBaseUri() {
293             return URI.create("");
294         }
295
296         @Override
297         public UriBuilder getBaseUriBuilder() {
298             return UriBuilder.fromUri(getBaseUri());
299         }
300
301         @Override
302         public MultivaluedMap<String, String> getPathParameters() {
303             return new MultivaluedHashMap<>();
304         }
305
306         @Override
307         public MultivaluedMap<String, String> getPathParameters(boolean decode) {
308             return getPathParameters();
309         }
310
311         @Override
312         public MultivaluedMap<String, String> getQueryParameters() {
313             return queryParams;
314         }
315
316         @Override
317         public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
318             return getQueryParameters();
319         }
320
321         @Override
322         public List<String> getMatchedURIs() {
323             return Collections.emptyList();
324         }
325
326         @Override
327         public List<String> getMatchedURIs(boolean decode) {
328             return getMatchedURIs();
329         }
330
331         @Override
332         public List<Object> getMatchedResources() {
333             return Collections.emptyList();
334         }
335
336         @Override
337         public URI resolve(URI uri) {
338             return uri;
339         }
340
341         @Override
342         public URI relativize(URI uri) {
343             return uri;
344         }
345     }
346 }