Run NormalizedNode parsing inline if possible
[netconf.git] / restconf / sal-rest-connector / 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 java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.lang.annotation.Annotation;
17 import java.nio.charset.StandardCharsets;
18 import java.util.List;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.UriInfo;
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.NormalizedNodeJsonBodyWriter;
24 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
26 import org.opendaylight.yangtools.yang.common.OperationFailedException;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Implementation of the JSONRestconfService interface.
35  *
36  * @author Thomas Pantelis
37  */
38 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
39     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
40
41     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
42
43     @SuppressWarnings("checkstyle:IllegalCatch")
44     @Override
45     public void put(final String uriPath, final String payload, final UriInfo uriInfo) throws OperationFailedException {
46         Preconditions.checkNotNull(payload, "payload can't be null");
47
48         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
49
50         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
51         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
52
53         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
54         LOG.debug("Parsed NormalizedNode: {}", context.getData());
55
56         try {
57             RestconfImpl.getInstance().updateConfigurationData(uriPath, context, uriInfo);
58         } catch (final Exception e) {
59             propagateExceptionAs(uriPath, e, "PUT");
60         }
61     }
62
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     @Override
65     public void post(final String uriPath, final String payload, final UriInfo uriInfo)
66             throws OperationFailedException {
67         Preconditions.checkNotNull(payload, "payload can't be null");
68
69         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
70
71         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
72         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
73
74         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
75         LOG.debug("Parsed NormalizedNode: {}", context.getData());
76
77         try {
78             RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
79         } catch (final Exception e) {
80             propagateExceptionAs(uriPath, e, "POST");
81         }
82     }
83
84     @SuppressWarnings("checkstyle:IllegalCatch")
85     @Override
86     public void delete(final String uriPath) throws OperationFailedException {
87         LOG.debug("delete: uriPath: {}", uriPath);
88
89         try {
90             RestconfImpl.getInstance().deleteConfigurationData(uriPath);
91         } catch (final Exception e) {
92             propagateExceptionAs(uriPath, e, "DELETE");
93         }
94     }
95
96     @SuppressWarnings("checkstyle:IllegalCatch")
97     @Override
98     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
99             throws OperationFailedException {
100         LOG.debug("get: uriPath: {}", uriPath);
101
102         try {
103             NormalizedNodeContext readData;
104             if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
105                 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
106             } else {
107                 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
108             }
109
110             final Optional<String> result = Optional.of(toJson(readData));
111
112             LOG.debug("get returning: {}", result.get());
113
114             return result;
115         } catch (final Exception e) {
116             if (!isDataMissing(e)) {
117                 propagateExceptionAs(uriPath, e, "GET");
118             }
119
120             LOG.debug("Data missing - returning absent");
121             return Optional.absent();
122         }
123     }
124
125     @SuppressWarnings("checkstyle:IllegalCatch")
126     @Override
127     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
128             throws OperationFailedException {
129         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
130
131         final String actualInput = input.isPresent() ? input.get() : null;
132
133         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
134
135         String output = null;
136         try {
137             NormalizedNodeContext outputContext;
138             if (actualInput != null) {
139                 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
140                 final NormalizedNodeContext inputContext =
141                         JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
142
143                 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
144                         .getInstanceIdentifier());
145                 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
146
147                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
148             } else {
149                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
150             }
151
152             if (outputContext.getData() != null) {
153                 output = toJson(outputContext);
154             }
155         } catch (final Exception e) {
156             propagateExceptionAs(uriPath, e, "RPC");
157         }
158
159         return Optional.fromNullable(output);
160     }
161
162     @Override
163     public void close() {
164     }
165
166     private static String toJson(final NormalizedNodeContext readData) throws IOException {
167         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
168         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
169         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
170                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
171         return outputStream.toString(StandardCharsets.UTF_8.name());
172     }
173
174     private static boolean isDataMissing(final Exception exception) {
175         boolean dataMissing = false;
176         if (exception instanceof RestconfDocumentedException) {
177             final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
178             if (!rde.getErrors().isEmpty()) {
179                 if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
180                     dataMissing = true;
181                 }
182             }
183         }
184
185         return dataMissing;
186     }
187
188     private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
189             throws OperationFailedException {
190         LOG.debug("Error for uriPath: {}", uriPath, exception);
191
192         if (exception instanceof RestconfDocumentedException) {
193             throw new OperationFailedException(String.format(
194                     "%s failed for URI %s", operation, uriPath), exception.getCause(),
195                     toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
196         }
197
198         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
199     }
200
201     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
202         final RpcError[] to = new RpcError[from.size()];
203         int index = 0;
204         for (final RestconfError e: from) {
205             to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
206                     e.getErrorMessage());
207         }
208
209         return to;
210     }
211
212     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
213         switch (errorType) {
214             case TRANSPORT: {
215                 return ErrorType.TRANSPORT;
216             }
217             case RPC: {
218                 return ErrorType.RPC;
219             }
220             case PROTOCOL: {
221                 return ErrorType.PROTOCOL;
222             }
223             default: {
224                 return ErrorType.APPLICATION;
225             }
226         }
227     }
228 }