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