50b23594daf75b983e768956f872967a448011f4
[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 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.ws.rs.core.MediaType;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.netconf.sal.rest.api.RestconfService;
23 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
24 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
25 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
26 import org.opendaylight.netconf.sal.rest.impl.PatchJsonBodyWriter;
27 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
28 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfError;
31 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
32 import org.opendaylight.restconf.common.patch.PatchContext;
33 import org.opendaylight.restconf.common.patch.PatchStatusContext;
34 import org.opendaylight.restconf.common.util.SimpleUriInfo;
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 {JSONRestconfServiceRfc8040Impl from restconf-nb-rfc8040
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     private final ControllerContext controllerContext;
55     private final RestconfService restconfService;
56
57     public JSONRestconfServiceImpl(ControllerContext controllerContext, RestconfService restconfService) {
58         this.controllerContext = controllerContext;
59         this.restconfService = restconfService;
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     @Override
64     public void put(final String uriPath, final String payload) throws OperationFailedException {
65         Preconditions.checkNotNull(payload, "payload can't be null");
66
67         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
68
69         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
70         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false,
71                 controllerContext);
72
73         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
74         LOG.debug("Parsed NormalizedNode: {}", context.getData());
75
76         try {
77             restconfService.updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
78         } catch (final Exception e) {
79             propagateExceptionAs(uriPath, e, "PUT");
80         }
81     }
82
83     @SuppressWarnings("checkstyle:IllegalCatch")
84     @Override
85     public void post(final String uriPath, final String payload)
86             throws OperationFailedException {
87         Preconditions.checkNotNull(payload, "payload can't be null");
88
89         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
90
91         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
92         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true,
93                 controllerContext);
94
95         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
96         LOG.debug("Parsed NormalizedNode: {}", context.getData());
97
98         try {
99             restconfService.createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
100         } catch (final Exception e) {
101             propagateExceptionAs(uriPath, e, "POST");
102         }
103     }
104
105     @SuppressWarnings("checkstyle:IllegalCatch")
106     @Override
107     public void delete(final String uriPath) throws OperationFailedException {
108         LOG.debug("delete: uriPath: {}", uriPath);
109
110         try {
111             restconfService.deleteConfigurationData(uriPath);
112         } catch (final Exception e) {
113             propagateExceptionAs(uriPath, e, "DELETE");
114         }
115     }
116
117     @SuppressWarnings("checkstyle:IllegalCatch")
118     @Override
119     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
120             throws OperationFailedException {
121         LOG.debug("get: uriPath: {}", uriPath);
122
123         try {
124             NormalizedNodeContext readData;
125             final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
126             if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
127                 readData = restconfService.readConfigurationData(uriPath, uriInfo);
128             } else {
129                 readData = restconfService.readOperationalData(uriPath, uriInfo);
130             }
131
132             final Optional<String> result = Optional.of(toJson(readData));
133
134             LOG.debug("get returning: {}", result.get());
135
136             return result;
137         } catch (final Exception e) {
138             if (!isDataMissing(e)) {
139                 propagateExceptionAs(uriPath, e, "GET");
140             }
141
142             LOG.debug("Data missing - returning absent");
143             return Optional.absent();
144         }
145     }
146
147     @SuppressWarnings("checkstyle:IllegalCatch")
148     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
149     @Override
150     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
151             throws OperationFailedException {
152         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
153
154         final String actualInput = input.isPresent() ? input.get() : null;
155
156         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
157
158         String output = null;
159         try {
160             NormalizedNodeContext outputContext;
161             if (actualInput != null) {
162                 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
163                 final NormalizedNodeContext inputContext =
164                         JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, controllerContext);
165
166                 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
167                         .getInstanceIdentifier());
168                 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
169
170                 outputContext = restconfService.invokeRpc(uriPath, inputContext, null);
171             } else {
172                 outputContext = restconfService.invokeRpc(uriPath, "", null);
173             }
174
175             if (outputContext.getData() != null) {
176                 output = toJson(outputContext);
177             }
178         } catch (final RuntimeException | IOException e) {
179             propagateExceptionAs(uriPath, e, "RPC");
180         }
181
182         return Optional.fromNullable(output);
183     }
184
185     @SuppressWarnings("checkstyle:IllegalCatch")
186     @Override
187     public Optional<String> patch(final String uriPath, final String payload)
188             throws OperationFailedException {
189
190         String output = null;
191         Preconditions.checkNotNull(payload, "payload can't be null");
192
193         LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
194
195         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
196
197         JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
198         final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
199
200         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
201         LOG.debug("Parsed NormalizedNode: {}", context.getData());
202
203         try {
204             PatchStatusContext patchStatusContext = restconfService
205                 .patchConfigurationData(context, new SimpleUriInfo(uriPath));
206             output = toJson(patchStatusContext);
207         } catch (final Exception e) {
208             propagateExceptionAs(uriPath, e, "PATCH");
209         }
210         return Optional.fromNullable(output);
211     }
212
213     @Override
214     public void close() {
215     }
216
217     private  String toJson(final PatchStatusContext patchStatusContext) throws IOException {
218         final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
219         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
220         writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
221                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
222         return outputStream.toString(StandardCharsets.UTF_8.name());
223     }
224
225     private static String toJson(final NormalizedNodeContext readData) throws IOException {
226         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
227         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
228         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
229                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
230         return outputStream.toString(StandardCharsets.UTF_8.name());
231     }
232
233     private static boolean isDataMissing(final Exception exception) {
234         boolean dataMissing = false;
235         if (exception instanceof RestconfDocumentedException) {
236             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
237             if (!rde.getErrors().isEmpty()) {
238                 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
239                     dataMissing = true;
240                 }
241             }
242         }
243
244         return dataMissing;
245     }
246
247     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
248             throws OperationFailedException {
249         LOG.debug("Error for uriPath: {}", uriPath, exception);
250
251         if (exception instanceof RestconfDocumentedException) {
252             throw new OperationFailedException(String.format(
253                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
254                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
255         }
256
257         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
258     }
259
260     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
261         final RpcError[] to = new RpcError[from.size()];
262         int index = 0;
263         for (final RestconfError e: from) {
264             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
265                     e.getErrorMessage());
266         }
267
268         return to;
269     }
270
271     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
272         switch (errorType) {
273             case TRANSPORT: {
274                 return ErrorType.TRANSPORT;
275             }
276             case RPC: {
277                 return ErrorType.RPC;
278             }
279             case PROTOCOL: {
280                 return ErrorType.PROTOCOL;
281             }
282             default: {
283                 return ErrorType.APPLICATION;
284             }
285         }
286     }
287 }