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.nb.rfc8040.rests.services.impl;
10 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.CREATE_NOTIFICATION_STREAM;
11 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_ACCESS_PATH_PART;
12 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_LOCATION_PATH_PART;
13 import static org.opendaylight.restconf.nb.rfc8040.rests.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.restconf.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
30 import org.opendaylight.restconf.common.context.WriterParameters;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.common.errors.RestconfError;
33 import org.opendaylight.restconf.common.patch.PatchContext;
34 import org.opendaylight.restconf.common.patch.PatchStatusContext;
35 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
36 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
37 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
38 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
39 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
40 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataService;
41 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
42 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
43 import org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil;
44 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PatchDataTransactionUtil;
45 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil;
46 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PutDataTransactionUtil;
47 import org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil;
48 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
49 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
50 import org.opendaylight.restconf.nb.rfc8040.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 withDefaUsed = 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.");
105 LOG.info("Unknown key : {}.", entry.getKey());
109 boolean tagged = false;
111 if ("report-all-tagged".equals(withDefa)) {
115 if ("report-all".equals(withDefa)) {
120 final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(
121 instanceIdentifier, uriInfo, tagged);
123 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
124 final DOMTransactionChain transactionChain;
125 if (mountPoint == null) {
126 transactionChain = this.transactionChainHandler.get();
128 transactionChain = transactionChainOfMountPoint(mountPoint);
131 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
132 instanceIdentifier, mountPoint, transactionChain);
133 final NormalizedNode<?, ?> node =
134 ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
135 schemaContextRef, uriInfo);
136 if (identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART)
137 && identifier.contains(STREAM_LOCATION_PATH_PART)) {
138 final String value = (String) node.getValue();
139 final String streamName = value.substring(
140 value.indexOf(CREATE_NOTIFICATION_STREAM.toString() + RestconfConstants.SLASH),
142 this.delegRestconfSubscrService.subscribeToStream(streamName, uriInfo);
145 throw new RestconfDocumentedException(
146 "Request could not be completed because the relevant data model content does not exist",
147 RestconfError.ErrorType.PROTOCOL,
148 RestconfError.ErrorTag.DATA_MISSING);
151 if ((parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL))
152 || parameters.getContent().equals(RestconfDataServiceConstant.ReadData.CONFIG)) {
153 return Response.status(200)
154 .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
155 .header("ETag", '"' + node.getNodeType().getModule().getFormattedRevision()
156 + node.getNodeType().getLocalName() + '"')
157 .header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC())))
161 return Response.status(200).entity(new NormalizedNodeContext(instanceIdentifier, node, parameters)).build();
165 public Response putData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
166 Preconditions.checkNotNull(payload);
168 boolean insertUsed = false;
169 boolean pointUsed = false;
170 String insert = null;
173 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
174 switch (entry.getKey()) {
178 insert = entry.getValue().iterator().next();
180 throw new RestconfDocumentedException("Insert parameter can be used only once.");
186 point = entry.getValue().iterator().next();
188 throw new RestconfDocumentedException("Point parameter can be used only once.");
192 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
196 checkQueryParams(insertUsed, pointUsed, insert);
198 final InstanceIdentifierContext<? extends SchemaNode> iid = payload
199 .getInstanceIdentifierContext();
201 PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
202 PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
203 PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
205 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
206 final DOMTransactionChain transactionChain;
207 final SchemaContextRef ref;
208 if (mountPoint == null) {
209 transactionChain = this.transactionChainHandler.get();
210 ref = new SchemaContextRef(this.schemaContextHandler.get());
212 transactionChain = transactionChainOfMountPoint(mountPoint);
213 ref = new SchemaContextRef(mountPoint.getSchemaContext());
216 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
217 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
218 return PutDataTransactionUtil.putData(payload, ref, transactionNode, insert, point);
221 private static void checkQueryParams(final boolean insertUsed, final boolean pointUsed, final String insert) {
222 if (pointUsed && !insertUsed) {
223 throw new RestconfDocumentedException("Point parameter can't be used without Insert parameter.");
225 if (pointUsed && (insert.equals("first") || insert.equals("last"))) {
226 throw new RestconfDocumentedException(
227 "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
232 public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
233 return postData(payload, uriInfo);
237 public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
238 Preconditions.checkNotNull(payload);
240 boolean insertUsed = false;
241 boolean pointUsed = false;
242 String insert = null;
245 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
246 switch (entry.getKey()) {
250 insert = entry.getValue().iterator().next();
252 throw new RestconfDocumentedException("Insert parameter can be used only once.");
258 point = entry.getValue().iterator().next();
260 throw new RestconfDocumentedException("Point parameter can be used only once.");
264 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
268 checkQueryParams(insertUsed, pointUsed, insert);
270 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
271 final DOMTransactionChain transactionChain;
272 final SchemaContextRef ref;
273 if (mountPoint == null) {
274 transactionChain = this.transactionChainHandler.get();
275 ref = new SchemaContextRef(this.schemaContextHandler.get());
277 transactionChain = transactionChainOfMountPoint(mountPoint);
278 ref = new SchemaContextRef(mountPoint.getSchemaContext());
280 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
281 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
282 return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref, insert, point);
286 public Response deleteData(final String identifier) {
287 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
288 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
289 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
291 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
292 final DOMTransactionChain transactionChain;
293 if (mountPoint == null) {
294 transactionChain = this.transactionChainHandler.get();
296 transactionChain = transactionChainOfMountPoint(mountPoint);
299 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
301 return DeleteDataTransactionUtil.deleteData(transactionNode);
305 public PatchStatusContext patchData(final String identifier, final PatchContext context, final UriInfo uriInfo) {
306 return patchData(context, uriInfo);
310 public PatchStatusContext patchData(final PatchContext context, final UriInfo uriInfo) {
311 Preconditions.checkNotNull(context);
312 final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
314 final DOMTransactionChain transactionChain;
315 final SchemaContextRef ref;
316 if (mountPoint == null) {
317 transactionChain = this.transactionChainHandler.get();
318 ref = new SchemaContextRef(this.schemaContextHandler.get());
320 transactionChain = transactionChainOfMountPoint(mountPoint);
321 ref = new SchemaContextRef(mountPoint.getSchemaContext());
324 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
325 context.getInstanceIdentifierContext(), mountPoint, transactionChain);
327 return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
331 * Prepare transaction chain to access data of mount point.
333 * mount point reference
334 * @return {@link DOMTransactionChain}
336 private static DOMTransactionChain transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
337 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
338 if (domDataBrokerService.isPresent()) {
339 return domDataBrokerService.get().createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
342 final String errMsg = "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier();
344 throw new RestconfDocumentedException(errMsg);