2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.restful.services.impl;
10 import static org.opendaylight.restconf.restful.utils.RestconfStreamsConstants.CREATE_NOTIFICATION_STREAM;
11 import static org.opendaylight.restconf.restful.utils.RestconfStreamsConstants.STREAM_ACCESS_PATH_PART;
12 import static org.opendaylight.restconf.restful.utils.RestconfStreamsConstants.STREAM_LOCATION_PATH_PART;
13 import static org.opendaylight.restconf.restful.utils.RestconfStreamsConstants.STREAM_PATH;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import java.time.Clock;
18 import java.time.LocalDateTime;
19 import java.time.format.DateTimeFormatter;
20 import java.util.List;
21 import java.util.Map.Entry;
22 import javax.annotation.Nonnull;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.UriInfo;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
27 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
28 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
31 import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusContext;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
34 import org.opendaylight.netconf.sal.restconf.impl.WriterParameters;
35 import org.opendaylight.restconf.RestConnectorProvider;
36 import org.opendaylight.restconf.common.references.SchemaContextRef;
37 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
38 import org.opendaylight.restconf.handlers.SchemaContextHandler;
39 import org.opendaylight.restconf.handlers.TransactionChainHandler;
40 import org.opendaylight.restconf.restful.services.api.RestconfDataService;
41 import org.opendaylight.restconf.restful.services.api.RestconfStreamsSubscriptionService;
42 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
43 import org.opendaylight.restconf.restful.utils.DeleteDataTransactionUtil;
44 import org.opendaylight.restconf.restful.utils.PatchDataTransactionUtil;
45 import org.opendaylight.restconf.restful.utils.PostDataTransactionUtil;
46 import org.opendaylight.restconf.restful.utils.PutDataTransactionUtil;
47 import org.opendaylight.restconf.restful.utils.ReadDataTransactionUtil;
48 import org.opendaylight.restconf.restful.utils.RestconfDataServiceConstant;
49 import org.opendaylight.restconf.utils.RestconfConstants;
50 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * Implementation of {@link RestconfDataService}
59 public class RestconfDataServiceImpl implements RestconfDataService {
61 private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
62 private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
64 private final SchemaContextHandler schemaContextHandler;
65 private final TransactionChainHandler transactionChainHandler;
66 private final DOMMountPointServiceHandler mountPointServiceHandler;
68 private final RestconfStreamsSubscriptionService delegRestconfSubscrService;
70 public RestconfDataServiceImpl(final SchemaContextHandler schemaContextHandler,
71 final TransactionChainHandler transactionChainHandler,
72 final DOMMountPointServiceHandler mountPointServiceHandler,
73 final RestconfStreamsSubscriptionService delegRestconfSubscrService) {
74 this.schemaContextHandler = schemaContextHandler;
75 this.transactionChainHandler = transactionChainHandler;
76 this.mountPointServiceHandler = mountPointServiceHandler;
77 this.delegRestconfSubscrService = delegRestconfSubscrService;
81 public Response readData(final UriInfo uriInfo) {
82 return readData(null, uriInfo);
86 public Response readData(final String identifier, final UriInfo uriInfo) {
87 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
88 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
89 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
91 boolean withDefa_used = false;
92 String withDefa = null;
94 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
95 switch (entry.getKey()) {
99 withDefa = entry.getValue().iterator().next();
101 throw new RestconfDocumentedException("With-defaults parameter can be used only once.");
106 boolean tagged = false;
108 if ("report-all-tagged".equals(withDefa)) {
112 if ("report-all".equals(withDefa)) {
117 final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(
118 instanceIdentifier, uriInfo, tagged);
120 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
121 final DOMTransactionChain transactionChain;
122 if (mountPoint == null) {
123 transactionChain = this.transactionChainHandler.get();
125 transactionChain = transactionChainOfMountPoint(mountPoint);
128 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
129 instanceIdentifier, mountPoint, transactionChain);
130 final NormalizedNode<?, ?> node =
131 ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
132 schemaContextRef, uriInfo);
133 if (identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART)
134 && identifier.contains(STREAM_LOCATION_PATH_PART)) {
135 final String value = (String) node.getValue();
136 final String streamName = value.substring(
137 value.indexOf(CREATE_NOTIFICATION_STREAM.toString() + RestconfConstants.SLASH),
139 this.delegRestconfSubscrService.subscribeToStream(streamName, uriInfo);
142 throw new RestconfDocumentedException(
143 "Request could not be completed because the relevant data model content does not exist",
144 RestconfError.ErrorType.PROTOCOL,
145 RestconfError.ErrorTag.DATA_MISSING);
148 if ((parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL))
149 || parameters.getContent().equals(RestconfDataServiceConstant.ReadData.CONFIG)) {
150 return Response.status(200)
151 .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
152 .header("ETag", '"' + node.getNodeType().getModule().getFormattedRevision()
153 + node.getNodeType().getLocalName() + '"')
154 .header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC())))
158 return Response.status(200).entity(new NormalizedNodeContext(instanceIdentifier, node, parameters)).build();
162 public Response putData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
163 Preconditions.checkNotNull(payload);
165 boolean insert_used = false;
166 boolean point_used = false;
167 String insert = null;
170 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
171 switch (entry.getKey()) {
175 insert = entry.getValue().iterator().next();
177 throw new RestconfDocumentedException("Insert parameter can be used only once.");
183 point = entry.getValue().iterator().next();
185 throw new RestconfDocumentedException("Point parameter can be used only once.");
189 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
193 checkQueryParams(insert_used, point_used, insert);
195 final InstanceIdentifierContext<? extends SchemaNode> iid = payload
196 .getInstanceIdentifierContext();
198 PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
199 PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
200 PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
202 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
203 final DOMTransactionChain transactionChain;
204 final SchemaContextRef ref;
205 if (mountPoint == null) {
206 transactionChain = this.transactionChainHandler.get();
207 ref = new SchemaContextRef(this.schemaContextHandler.get());
209 transactionChain = transactionChainOfMountPoint(mountPoint);
210 ref = new SchemaContextRef(mountPoint.getSchemaContext());
213 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
214 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
215 return PutDataTransactionUtil.putData(payload, ref, transactionNode, insert, point);
218 private static void checkQueryParams(final boolean insert_used, final boolean point_used, final String insert) {
219 if (point_used && !insert_used) {
220 throw new RestconfDocumentedException("Point parameter can't be used without Insert parameter.");
222 if (point_used && (insert.equals("first") || insert.equals("last"))) {
223 throw new RestconfDocumentedException(
224 "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
229 public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
230 return postData(payload, uriInfo);
234 public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
235 Preconditions.checkNotNull(payload);
237 boolean insert_used = false;
238 boolean point_used = false;
239 String insert = null;
242 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
243 switch (entry.getKey()) {
247 insert = entry.getValue().iterator().next();
249 throw new RestconfDocumentedException("Insert parameter can be used only once.");
255 point = entry.getValue().iterator().next();
257 throw new RestconfDocumentedException("Point parameter can be used only once.");
261 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
265 checkQueryParams(insert_used, point_used, insert);
267 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
268 final DOMTransactionChain transactionChain;
269 final SchemaContextRef ref;
270 if (mountPoint == null) {
271 transactionChain = this.transactionChainHandler.get();
272 ref = new SchemaContextRef(this.schemaContextHandler.get());
274 transactionChain = transactionChainOfMountPoint(mountPoint);
275 ref = new SchemaContextRef(mountPoint.getSchemaContext());
277 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
278 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
279 return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref, insert, point);
283 public Response deleteData(final String identifier) {
284 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
285 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
286 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
288 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
289 final DOMTransactionChain transactionChain;
290 if (mountPoint == null) {
291 transactionChain = this.transactionChainHandler.get();
293 transactionChain = transactionChainOfMountPoint(mountPoint);
296 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
298 return DeleteDataTransactionUtil.deleteData(transactionNode);
302 public PATCHStatusContext patchData(final String identifier, final PATCHContext context, final UriInfo uriInfo) {
303 return patchData(context, uriInfo);
307 public PATCHStatusContext patchData(final PATCHContext context, final UriInfo uriInfo) {
308 Preconditions.checkNotNull(context);
309 final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
311 final DOMTransactionChain transactionChain;
312 final SchemaContextRef ref;
313 if (mountPoint == null) {
314 transactionChain = this.transactionChainHandler.get();
315 ref = new SchemaContextRef(this.schemaContextHandler.get());
317 transactionChain = transactionChainOfMountPoint(mountPoint);
318 ref = new SchemaContextRef(mountPoint.getSchemaContext());
321 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
322 context.getInstanceIdentifierContext(), mountPoint, transactionChain);
324 return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
328 * Prepare transaction chain to access data of mount point
330 * - mount point reference
331 * @return {@link DOMTransactionChain}
333 private static DOMTransactionChain transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
334 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
335 if (domDataBrokerService.isPresent()) {
336 return domDataBrokerService.get().createTransactionChain(RestConnectorProvider.transactionListener);
339 final String errMsg = "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier();
341 throw new RestconfDocumentedException(errMsg);