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