Bug 6951 - Implement Query parameters - with-defaults
[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 @Deprecated
39 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
40     private final static Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
41
42     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
43
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     @Override
64     public void post(final String uriPath, final String payload, final UriInfo uriInfo)
65             throws OperationFailedException {
66         Preconditions.checkNotNull(payload, "payload can't be null");
67
68         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
69
70         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
71         final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
72
73         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
74         LOG.debug("Parsed NormalizedNode: {}", context.getData());
75
76         try {
77             RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
78         } catch (final Exception e) {
79             propagateExceptionAs(uriPath, e, "POST");
80         }
81     }
82
83     @Override
84     public void delete(final String uriPath) throws OperationFailedException {
85         LOG.debug("delete: uriPath: {}", uriPath);
86
87         try {
88             RestconfImpl.getInstance().deleteConfigurationData(uriPath);
89         } catch (final Exception e) {
90             propagateExceptionAs(uriPath, e, "DELETE");
91         }
92     }
93
94     @Override
95     public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
96             throws OperationFailedException {
97         LOG.debug("get: uriPath: {}", uriPath);
98
99         try {
100             NormalizedNodeContext readData;
101             if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
102                 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
103             } else {
104                 readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
105             }
106
107             final Optional<String> result = Optional.of(toJson(readData));
108
109             LOG.debug("get returning: {}", result.get());
110
111             return result;
112         } catch (final Exception e) {
113             if(!isDataMissing(e)) {
114                 propagateExceptionAs(uriPath, e, "GET");
115             }
116
117             LOG.debug("Data missing - returning absent");
118             return Optional.absent();
119         }
120     }
121
122     @Override
123     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input) throws OperationFailedException {
124         Preconditions.checkNotNull(uriPath, "uriPath can't be null");
125
126         final String actualInput = input.isPresent() ? input.get() : null;
127
128         LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
129
130         String output = null;
131         try {
132             NormalizedNodeContext outputContext;
133             if(actualInput != null) {
134                 final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
135                 final NormalizedNodeContext inputContext = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
136
137                 LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
138                         .getInstanceIdentifier());
139                 LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
140
141                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, inputContext, null);
142             } else {
143                 outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
144             }
145
146             if(outputContext.getData() != null) {
147                 output = toJson(outputContext);
148             }
149         } catch (final Exception e) {
150             propagateExceptionAs(uriPath, e, "RPC");
151         }
152
153         return Optional.fromNullable(output);
154     }
155
156     @Override
157     public void close() {
158     }
159
160     private String toJson(final NormalizedNodeContext readData) throws IOException {
161         final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
162         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
163         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
164                 MediaType.APPLICATION_JSON_TYPE, null, outputStream );
165         return outputStream.toString(StandardCharsets.UTF_8.name());
166     }
167
168     private boolean isDataMissing(final Exception e) {
169         boolean dataMissing = false;
170         if(e instanceof RestconfDocumentedException) {
171             final RestconfDocumentedException rde = (RestconfDocumentedException)e;
172             if(!rde.getErrors().isEmpty()) {
173                 if(rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
174                     dataMissing = true;
175                 }
176             }
177         }
178
179         return dataMissing;
180     }
181
182     private static void propagateExceptionAs(final String uriPath, final Exception e, final String operation) throws OperationFailedException {
183         LOG.debug("Error for uriPath: {}", uriPath, e);
184
185         if(e instanceof RestconfDocumentedException) {
186             throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e.getCause(),
187                     toRpcErrors(((RestconfDocumentedException)e).getErrors()));
188         }
189
190         throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e);
191     }
192
193     private static RpcError[] toRpcErrors(final List<RestconfError> from) {
194         final RpcError[] to = new RpcError[from.size()];
195         int i = 0;
196         for(final RestconfError e: from) {
197             to[i++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
198                     e.getErrorMessage());
199         }
200
201         return to;
202     }
203
204     private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
205         switch(errorType) {
206             case TRANSPORT: {
207                 return ErrorType.TRANSPORT;
208             }
209             case RPC: {
210                 return ErrorType.RPC;
211             }
212             case PROTOCOL: {
213                 return ErrorType.PROTOCOL;
214             }
215             default: {
216                 return ErrorType.APPLICATION;
217             }
218         }
219     }
220 }