f63bee7c047bfd4844ae23f119d8b7ba13b11718
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Optional;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.lang.annotation.Annotation;
19 import java.nio.charset.StandardCharsets;
20 import java.util.List;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.MultivaluedMap;
25 import javax.ws.rs.core.UriInfo;
26 import org.apache.aries.blueprint.annotation.service.Service;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.netconf.sal.rest.api.RestconfService;
29 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
31 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
32 import org.opendaylight.netconf.sal.rest.impl.PatchJsonBodyWriter;
33 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
34 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError;
37 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
38 import org.opendaylight.restconf.common.patch.PatchContext;
39 import org.opendaylight.restconf.common.patch.PatchStatusContext;
40 import org.opendaylight.restconf.common.util.MultivaluedHashMap;
41 import org.opendaylight.restconf.common.util.SimpleUriInfo;
42 import org.opendaylight.yangtools.yang.common.OperationFailedException;
43 import org.opendaylight.yangtools.yang.common.RpcError;
44 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
51  *
52  * @author Thomas Pantelis
53  * @deprecated Replaced by {JSONRestconfServiceRfc8040Impl from restconf-nb-rfc8040
54  */
55 @Singleton
56 @Deprecated
57 @Service(classes = JSONRestconfService.class)
58 public class JSONRestconfServiceImpl implements JSONRestconfService {
59     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
60
61     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
62
63     private final ControllerContext controllerContext;
64     private final RestconfService restconfService;
65
66     @Inject
67     public JSONRestconfServiceImpl(final ControllerContext controllerContext, final RestconfImpl restconfService) {
68         this.controllerContext = controllerContext;
69         this.restconfService = restconfService;
70     }
71
72     @SuppressWarnings("checkstyle:IllegalCatch")
73     @Override
74     public void put(final String uriPath, final String payload) throws OperationFailedException {
75         requireNonNull(payload, "payload can't be null");
76
77         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
78
79         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
80         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false,
81                 controllerContext);
82
83         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
84         LOG.debug("Parsed NormalizedNode: {}", context.getData());
85
86         try {
87             restconfService.updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
88         } catch (final Exception e) {
89             propagateExceptionAs(uriPath, e, "PUT");
90         }
91     }
92
93     @SuppressWarnings("checkstyle:IllegalCatch")
94     @Override
95     public void post(final String uriPath, final String payload) throws OperationFailedException {
96         requireNonNull(payload, "payload can't be null");
97
98         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
99
100         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
101         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true,
102                 controllerContext);
103
104         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
105         LOG.debug("Parsed NormalizedNode: {}", context.getData());
106
107         try {
108             restconfService.createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
109         } catch (final Exception e) {
110             propagateExceptionAs(uriPath, e, "POST");
111         }
112     }
113
114     @SuppressWarnings("checkstyle:IllegalCatch")
115     @Override
116     public void delete(final String uriPath) throws OperationFailedException {
117         LOG.debug("delete: uriPath: {}", uriPath);
118
119         try {
120             restconfService.deleteConfigurationData(uriPath);
121         } catch (final Exception e) {
122             propagateExceptionAs(uriPath, e, "DELETE");
123         }
124     }
125
126     @SuppressWarnings("checkstyle:IllegalCatch")
127     @Override
128     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
129             throws OperationFailedException {
130         LOG.debug("get: uriPath: {}", uriPath);
131
132         try {
133             NormalizedNodeContext readData;
134             final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
135             if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
136                 readData = restconfService.readConfigurationData(uriPath, uriInfo);
137             } else {
138                 readData = restconfService.readOperationalData(uriPath, uriInfo);
139             }
140
141             final Optional<String> result = Optional.of(toJson(readData));
142
143             LOG.debug("get returning: {}", result.get());
144
145             return result;
146         } catch (final Exception e) {
147             if (!isDataMissing(e)) {
148                 propagateExceptionAs(uriPath, e, "GET");
149             }
150
151             LOG.debug("Data missing - returning absent");
152             return Optional.absent();
153         }
154     }
155
156     @SuppressWarnings("checkstyle:IllegalCatch")
157     @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF", justification = "Unrecognised NullableDecl")
158     @Override
159     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
160             throws OperationFailedException {
161         requireNonNull(uriPath, "uriPath can't be null");
162
163         final String actualInput = input.isPresent() ? input.get() : null;
164
165         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
166
167         String output = null;
168         try {
169             NormalizedNodeContext outputContext;
170             if (actualInput != null) {
171                 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
172                 final NormalizedNodeContext inputContext =
173                         JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, controllerContext);
174
175                 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
176                         .getInstanceIdentifier());
177                 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
178
179                 outputContext = restconfService.invokeRpc(uriPath, inputContext, null);
180             } else {
181                 outputContext = restconfService.invokeRpc(uriPath, "", null);
182             }
183
184             if (outputContext.getData() != null) {
185                 output = toJson(outputContext);
186             }
187         } catch (final RuntimeException | IOException e) {
188             propagateExceptionAs(uriPath, e, "RPC");
189         }
190
191         return Optional.fromNullable(output);
192     }
193
194     @SuppressWarnings("checkstyle:IllegalCatch")
195     @Override
196     public Optional<String> patch(final String uriPath, final String payload)
197             throws OperationFailedException {
198
199         String output = null;
200         requireNonNull(payload, "payload can't be null");
201
202         LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
203
204         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
205
206         JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
207         final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
208
209         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
210         LOG.debug("Parsed NormalizedNode: {}", context.getData());
211
212         try {
213             PatchStatusContext patchStatusContext = restconfService
214                 .patchConfigurationData(context, new SimpleUriInfo(uriPath));
215             output = toJson(patchStatusContext);
216         } catch (final Exception e) {
217             propagateExceptionAs(uriPath, e, "PATCH");
218         }
219         return Optional.fromNullable(output);
220     }
221
222     @SuppressWarnings("checkstyle:IllegalCatch")
223     @Override
224     public Optional<String> subscribeToStream(final String identifier, final MultivaluedMap<String, String> params)
225             throws OperationFailedException {
226         //Note: We use http://127.0.0.1 because the Uri parser requires something there though it does nothing
227         String uri = new StringBuilder("http://127.0.0.1:8081/restconf/streams/stream/").append(identifier).toString();
228         MultivaluedMap queryParams = params != null ? params : new MultivaluedHashMap<String, String>();
229         UriInfo uriInfo = new SimpleUriInfo(uri, queryParams);
230
231         String jsonRes = null;
232         try {
233             NormalizedNodeContext res = restconfService.subscribeToStream(identifier, uriInfo);
234             jsonRes = toJson(res);
235         } catch (final Exception e) {
236             propagateExceptionAs(identifier, e, "RPC");
237         }
238
239         return Optional.fromNullable(jsonRes);
240     }
241
242     private static String toJson(final PatchStatusContext patchStatusContext) throws IOException {
243         final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
244         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
245         writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
246                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
247         return outputStream.toString(StandardCharsets.UTF_8.name());
248     }
249
250     private static String toJson(final NormalizedNodeContext readData) throws IOException {
251         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
252         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
253         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
254                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
255         return outputStream.toString(StandardCharsets.UTF_8.name());
256     }
257
258     private static boolean isDataMissing(final Exception exception) {
259         boolean dataMissing = false;
260         if (exception instanceof RestconfDocumentedException) {
261             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
262             if (!rde.getErrors().isEmpty()) {
263                 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
264                     dataMissing = true;
265                 }
266             }
267         }
268
269         return dataMissing;
270     }
271
272     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
273             throws OperationFailedException {
274         LOG.debug("Error for uriPath: {}", uriPath, exception);
275
276         if (exception instanceof RestconfDocumentedException) {
277             throw new OperationFailedException(String.format(
278                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
279                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
280         }
281
282         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
283     }
284
285     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
286         final RpcError[] to = new RpcError[from.size()];
287         int index = 0;
288         for (final RestconfError e: from) {
289             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
290                     e.getErrorMessage());
291         }
292
293         return to;
294     }
295
296     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
297         switch (errorType) {
298             case TRANSPORT: {
299                 return ErrorType.TRANSPORT;
300             }
301             case RPC: {
302                 return ErrorType.RPC;
303             }
304             case PROTOCOL: {
305                 return ErrorType.PROTOCOL;
306             }
307             default: {
308                 return ErrorType.APPLICATION;
309             }
310         }
311     }
312 }