Merge changes from topic 'rem_web_xml'
[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     private final SchemaContextHandler schemaContextHandler;
64
65     public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
66             final DOMMountPointServiceHandler mountPointServiceHandler,
67             final SchemaContextHandler schemaContextHandler) {
68         this.services = services;
69         this.mountPointServiceHandler = mountPointServiceHandler;
70         this.schemaContextHandler = schemaContextHandler;
71     }
72
73     @SuppressWarnings("checkstyle:IllegalCatch")
74     @Override
75     public void put(final String uriPath, final String payload) throws OperationFailedException {
76         Preconditions.checkNotNull(payload, "payload can't be null");
77
78         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
79
80         final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
81
82         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
83         LOG.debug("Parsed NormalizedNode: {}", context.getData());
84
85         try {
86             services.putData(uriPath, context, new SimpleUriInfo(uriPath));
87         } catch (final Exception e) {
88             propagateExceptionAs(uriPath, e, "PUT");
89         }
90     }
91
92     @SuppressWarnings("checkstyle:IllegalCatch")
93     @Override
94     public void post(final String uriPath, final String payload)
95             throws OperationFailedException {
96         Preconditions.checkNotNull(payload, "payload can't be null");
97
98         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
99
100         final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
101
102         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
103         LOG.debug("Parsed NormalizedNode: {}", context.getData());
104
105         try {
106             services.postData(uriPath, context, new SimpleUriInfo(uriPath));
107         } catch (final Exception e) {
108             propagateExceptionAs(uriPath, e, "POST");
109         }
110     }
111
112     @SuppressWarnings("checkstyle:IllegalCatch")
113     @Override
114     public void delete(final String uriPath) throws OperationFailedException {
115         LOG.debug("delete: uriPath: {}", uriPath);
116
117         try {
118             services.deleteData(uriPath);
119         } catch (final Exception e) {
120             propagateExceptionAs(uriPath, e, "DELETE");
121         }
122     }
123
124     @SuppressWarnings("checkstyle:IllegalCatch")
125     @Override
126     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
127             throws OperationFailedException {
128         LOG.debug("get: uriPath: {}", uriPath);
129
130         try {
131             final MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
132             queryParams.putSingle(RestconfDataServiceConstant.ReadData.CONTENT,
133                     datastoreType == LogicalDatastoreType.CONFIGURATION ? RestconfDataServiceConstant.ReadData.CONFIG :
134                         RestconfDataServiceConstant.ReadData.NONCONFIG);
135
136             final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
137             final NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
138
139             final Optional<String> result = Optional.of(toJson(readData));
140
141             LOG.debug("get returning: {}", result.get());
142
143             return result;
144         } catch (final Exception e) {
145             if (!isDataMissing(e)) {
146                 propagateExceptionAs(uriPath, e, "GET");
147             }
148
149             LOG.debug("Data missing - returning absent");
150             return Optional.absent();
151         }
152     }
153
154     @SuppressWarnings("checkstyle:IllegalCatch")
155     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
156     @Override
157     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
158             throws OperationFailedException {
159         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
160
161         final String actualInput = input.isPresent() ? input.get() : null;
162
163         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
164
165         String output = null;
166         try {
167             final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
168
169             LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
170                     .getInstanceIdentifier());
171             LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
172
173             final NormalizedNodeContext outputContext =
174                     services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
175
176             if (outputContext.getData() != null) {
177                 output = toJson(outputContext);
178             }
179         } catch (RuntimeException | IOException e) {
180             propagateExceptionAs(uriPath, e, "RPC");
181         }
182
183         return Optional.fromNullable(output);
184     }
185
186     @SuppressWarnings("checkstyle:IllegalCatch")
187     @Override
188     public Optional<String> patch(final String uriPath, final String payload)
189             throws OperationFailedException {
190
191         String output = null;
192         Preconditions.checkNotNull(payload, "payload can't be null");
193
194         LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
195
196         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
197
198         JsonToPatchBodyReader jsonToPatchBodyReader =
199                 new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
200         final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
201
202         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
203         LOG.debug("Parsed NormalizedNode: {}", context.getData());
204
205         try {
206             PatchStatusContext patchStatusContext = services.patchData(context, new SimpleUriInfo(uriPath));
207             output = toJson(patchStatusContext);
208         } catch (final Exception e) {
209             propagateExceptionAs(uriPath, e, "PATCH");
210         }
211         return Optional.fromNullable(output);
212     }
213
214     @Override
215     public void close() {
216     }
217
218     private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
219             final boolean isPost) throws OperationFailedException {
220         final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
221                 uriPath, schemaContextHandler.get(), Optional.of(mountPointServiceHandler.get()));
222
223         if (payload == null) {
224             return new NormalizedNodeContext(instanceIdentifierContext, null);
225         }
226
227         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
228         try {
229             return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
230         } catch (final IOException e) {
231             propagateExceptionAs(uriPath, e, "GET");
232             return null;
233         }
234     }
235
236     private  String toJson(final PatchStatusContext patchStatusContext) throws IOException {
237         final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
238         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
239         writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
240                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
241         return outputStream.toString(StandardCharsets.UTF_8.name());
242     }
243
244     private static String toJson(final NormalizedNodeContext readData) throws IOException {
245         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
246         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
247         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
248                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
249         return outputStream.toString(StandardCharsets.UTF_8.name());
250     }
251
252     private static boolean isDataMissing(final Exception exception) {
253         if (exception instanceof RestconfDocumentedException) {
254             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
255             return !rde.getErrors().isEmpty() && rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING;
256         }
257
258         return false;
259     }
260
261     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
262             throws OperationFailedException {
263         LOG.debug("Error for uriPath: {}", uriPath, exception);
264
265         if (exception instanceof RestconfDocumentedException) {
266             throw new OperationFailedException(String.format(
267                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
268                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
269         }
270
271         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
272     }
273
274     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
275         final RpcError[] to = new RpcError[from.size()];
276         int index = 0;
277         for (final RestconfError e: from) {
278             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
279                     e.getErrorMessage());
280         }
281
282         return to;
283     }
284
285     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
286         switch (errorType) {
287             case TRANSPORT:
288                 return ErrorType.TRANSPORT;
289             case RPC:
290                 return ErrorType.RPC;
291             case PROTOCOL:
292                 return ErrorType.PROTOCOL;
293             default:
294                 return ErrorType.APPLICATION;
295         }
296     }
297 }