Bump to odlparent 3.1.0 and yangtools 2.0.3
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / JSONRestconfServiceRfc8040Impl.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.restconf.nb.rfc8040.rests.services.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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.nio.charset.StandardCharsets;
19 import java.util.List;
20 import javax.annotation.Nullable;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.Response;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
30 import org.opendaylight.restconf.common.patch.PatchContext;
31 import org.opendaylight.restconf.common.patch.PatchStatusContext;
32 import org.opendaylight.restconf.common.util.MultivaluedHashMap;
33 import org.opendaylight.restconf.common.util.SimpleUriInfo;
34 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
36 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
37 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeJsonBodyWriter;
38 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonToPatchBodyReader;
39 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchJsonBodyWriter;
40 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.JSONRestconfService;
41 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.TransactionServicesWrapper;
42 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
43 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
44 import org.opendaylight.yangtools.yang.common.OperationFailedException;
45 import org.opendaylight.yangtools.yang.common.RpcError;
46 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * Implementation of the JSONRestconfService interface using the restconf Draft18 implementation.
53  *
54  * @author Thomas Pantelis
55  */
56 public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, AutoCloseable {
57     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceRfc8040Impl.class);
58
59     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
60
61     private final TransactionServicesWrapper services;
62     private final DOMMountPointServiceHandler mountPointServiceHandler;
63
64     public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
65             final DOMMountPointServiceHandler mountPointServiceHandler) {
66         this.services = services;
67         this.mountPointServiceHandler = mountPointServiceHandler;
68     }
69
70     @SuppressWarnings("checkstyle:IllegalCatch")
71     @Override
72     public void put(final String uriPath, final String payload) throws OperationFailedException {
73         Preconditions.checkNotNull(payload, "payload can't be null");
74
75         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
76
77         final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
78
79         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
80         LOG.debug("Parsed NormalizedNode: {}", context.getData());
81
82         try {
83             services.putData(uriPath, context, new SimpleUriInfo(uriPath));
84         } catch (final Exception e) {
85             propagateExceptionAs(uriPath, e, "PUT");
86         }
87     }
88
89     @SuppressWarnings("checkstyle:IllegalCatch")
90     @Override
91     public void post(final String uriPath, final String payload)
92             throws OperationFailedException {
93         Preconditions.checkNotNull(payload, "payload can't be null");
94
95         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
96
97         final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
98
99         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
100         LOG.debug("Parsed NormalizedNode: {}", context.getData());
101
102         try {
103             services.postData(uriPath, context, new SimpleUriInfo(uriPath));
104         } catch (final Exception e) {
105             propagateExceptionAs(uriPath, e, "POST");
106         }
107     }
108
109     @SuppressWarnings("checkstyle:IllegalCatch")
110     @Override
111     public void delete(final String uriPath) throws OperationFailedException {
112         LOG.debug("delete: uriPath: {}", uriPath);
113
114         try {
115             services.deleteData(uriPath);
116         } catch (final Exception e) {
117             propagateExceptionAs(uriPath, e, "DELETE");
118         }
119     }
120
121     @SuppressWarnings("checkstyle:IllegalCatch")
122     @Override
123     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
124             throws OperationFailedException {
125         LOG.debug("get: uriPath: {}", uriPath);
126
127         try {
128             final MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
129             queryParams.putSingle(RestconfDataServiceConstant.ReadData.CONTENT,
130                     datastoreType == LogicalDatastoreType.CONFIGURATION ? RestconfDataServiceConstant.ReadData.CONFIG :
131                         RestconfDataServiceConstant.ReadData.NONCONFIG);
132
133             final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
134             final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
135
136             final Optional<String> result = Optional.of(toJson(readData));
137
138             LOG.debug("get returning: {}", result.get());
139
140             return result;
141         } catch (final Exception e) {
142             if (!isDataMissing(e)) {
143                 propagateExceptionAs(uriPath, e, "GET");
144             }
145
146             LOG.debug("Data missing - returning absent");
147             return Optional.absent();
148         }
149     }
150
151     @SuppressWarnings("checkstyle:IllegalCatch")
152     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
153     @Override
154     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
155             throws OperationFailedException {
156         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
157
158         final String actualInput = input.isPresent() ? input.get() : null;
159
160         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
161
162         String output = null;
163         try {
164             final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
165
166             LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
167                     .getInstanceIdentifier());
168             LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
169
170             final NormalizedNodeContext outputContext =
171                     services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
172
173             if (outputContext.getData() != null) {
174                 output = toJson(outputContext);
175             }
176         } catch (RuntimeException | IOException e) {
177             propagateExceptionAs(uriPath, e, "RPC");
178         }
179
180         return Optional.fromNullable(output);
181     }
182
183     @SuppressWarnings("checkstyle:IllegalCatch")
184     @Override
185     public Optional<String> patch(final String uriPath, final String payload)
186             throws OperationFailedException {
187
188         String output = null;
189         Preconditions.checkNotNull(payload, "payload can't be null");
190
191         LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
192
193         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
194
195         JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader();
196         final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
197
198         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
199         LOG.debug("Parsed NormalizedNode: {}", context.getData());
200
201         try {
202             PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
203             output = toJson(patchStatusContext);
204         } catch (final Exception e) {
205             propagateExceptionAs(uriPath, e, "PATCH");
206         }
207         return Optional.fromNullable(output);
208     }
209
210     @Override
211     public void close() {
212     }
213
214     private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
215             final boolean isPost) throws OperationFailedException {
216         final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
217                 uriPath, SchemaContextHandler.getSchemaContext(),
218                 Optional.of(mountPointServiceHandler.get()));
219
220         if (payload == null) {
221             return new NormalizedNodeContext(instanceIdentifierContext, null);
222         }
223
224         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
225         try {
226             return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
227         } catch (final IOException e) {
228             propagateExceptionAs(uriPath, e, "GET");
229             return null;
230         }
231     }
232
233     private  String toJson(final PatchStatusContext patchStatusContext) throws IOException {
234         final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
235         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
236         writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
237                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
238         return outputStream.toString(StandardCharsets.UTF_8.name());
239     }
240
241     private static String toJson(final NormalizedNodeContext readData) throws IOException {
242         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
243         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
244         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
245                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
246         return outputStream.toString(StandardCharsets.UTF_8.name());
247     }
248
249     private static boolean isDataMissing(final Exception exception) {
250         if (exception instanceof RestconfDocumentedException) {
251             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
252             return !rde.getErrors().isEmpty() && rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING;
253         }
254
255         return false;
256     }
257
258     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
259             throws OperationFailedException {
260         LOG.debug("Error for uriPath: {}", uriPath, exception);
261
262         if (exception instanceof RestconfDocumentedException) {
263             throw new OperationFailedException(String.format(
264                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
265                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
266         }
267
268         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
269     }
270
271     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
272         final RpcError[] to = new RpcError[from.size()];
273         int index = 0;
274         for (final RestconfError e: from) {
275             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
276                     e.getErrorMessage());
277         }
278
279         return to;
280     }
281
282     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
283         switch (errorType) {
284             case TRANSPORT:
285                 return ErrorType.TRANSPORT;
286             case RPC:
287                 return ErrorType.RPC;
288             case PROTOCOL:
289                 return ErrorType.PROTOCOL;
290             default:
291                 return ErrorType.APPLICATION;
292         }
293     }
294 }