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