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