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.Path;
24 import javax.ws.rs.core.Response;
25 import javax.ws.rs.core.UriInfo;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
28 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
29 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
30 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
31 import org.opendaylight.restconf.common.context.WriterParameters;
32 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
33 import org.opendaylight.restconf.common.errors.RestconfError;
34 import org.opendaylight.restconf.common.patch.PatchContext;
35 import org.opendaylight.restconf.common.patch.PatchStatusContext;
36 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
37 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
38 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
39 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
40 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
41 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataService;
42 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
43 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
44 import org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil;
45 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PatchDataTransactionUtil;
46 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil;
47 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PutDataTransactionUtil;
48 import org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil;
49 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
50 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
51 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.common.Revision;
54 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
55 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Implementation of {@link RestconfDataService}.
63 public class RestconfDataServiceImpl implements RestconfDataService {
65 private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
66 private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
68 private SchemaContextHandler schemaContextHandler;
69 private TransactionChainHandler transactionChainHandler;
70 private DOMMountPointServiceHandler mountPointServiceHandler;
72 private final RestconfStreamsSubscriptionService delegRestconfSubscrService;
74 public RestconfDataServiceImpl(final SchemaContextHandler schemaContextHandler,
75 final TransactionChainHandler transactionChainHandler,
76 final DOMMountPointServiceHandler mountPointServiceHandler,
77 final RestconfStreamsSubscriptionService delegRestconfSubscrService) {
78 this.schemaContextHandler = schemaContextHandler;
79 this.transactionChainHandler = transactionChainHandler;
80 this.mountPointServiceHandler = mountPointServiceHandler;
81 this.delegRestconfSubscrService = delegRestconfSubscrService;
85 public synchronized void updateHandlers(final Object... handlers) {
86 for (final Object object : handlers) {
87 if (object instanceof SchemaContextHandler) {
88 schemaContextHandler = (SchemaContextHandler) object;
89 } else if (object instanceof DOMMountPointServiceHandler) {
90 mountPointServiceHandler = (DOMMountPointServiceHandler) object;
91 } else if (object instanceof TransactionChainHandler) {
92 transactionChainHandler = (TransactionChainHandler) object;
98 public Response readData(final UriInfo uriInfo) {
99 return readData(null, uriInfo);
103 public Response readData(final String identifier, final UriInfo uriInfo) {
104 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
105 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
106 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
108 boolean withDefaUsed = false;
109 String withDefa = null;
111 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
112 switch (entry.getKey()) {
113 case "with-defaults":
116 withDefa = entry.getValue().iterator().next();
118 throw new RestconfDocumentedException("With-defaults parameter can be used only once.");
122 LOG.info("Unknown key : {}.", entry.getKey());
126 boolean tagged = false;
128 if ("report-all-tagged".equals(withDefa)) {
132 if ("report-all".equals(withDefa)) {
137 final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(
138 instanceIdentifier, uriInfo, tagged);
140 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
141 final DOMTransactionChain transactionChain;
142 if (mountPoint == null) {
143 transactionChain = this.transactionChainHandler.get();
145 transactionChain = transactionChainOfMountPoint(mountPoint);
148 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
149 instanceIdentifier, mountPoint, transactionChain);
150 final NormalizedNode<?, ?> node =
151 ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
152 schemaContextRef, uriInfo);
153 if (identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART)
154 && identifier.contains(STREAM_LOCATION_PATH_PART)) {
155 final String value = (String) node.getValue();
156 final String streamName = value.substring(
157 value.indexOf(CREATE_NOTIFICATION_STREAM.toString() + RestconfConstants.SLASH),
159 this.delegRestconfSubscrService.subscribeToStream(streamName, uriInfo);
162 throw new RestconfDocumentedException(
163 "Request could not be completed because the relevant data model content does not exist",
164 RestconfError.ErrorType.PROTOCOL,
165 RestconfError.ErrorTag.DATA_MISSING);
168 if (parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL)
169 || parameters.getContent().equals(RestconfDataServiceConstant.ReadData.CONFIG)) {
170 final QName type = node.getNodeType();
171 return Response.status(200)
172 .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
173 .header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null)
174 + type.getLocalName() + '"')
175 .header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC())))
179 return Response.status(200).entity(new NormalizedNodeContext(instanceIdentifier, node, parameters)).build();
183 public Response putData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
184 Preconditions.checkNotNull(payload);
186 boolean insertUsed = false;
187 boolean pointUsed = false;
188 String insert = null;
191 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
192 switch (entry.getKey()) {
196 insert = entry.getValue().iterator().next();
198 throw new RestconfDocumentedException("Insert parameter can be used only once.");
204 point = entry.getValue().iterator().next();
206 throw new RestconfDocumentedException("Point parameter can be used only once.");
210 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
214 checkQueryParams(insertUsed, pointUsed, insert);
216 final InstanceIdentifierContext<? extends SchemaNode> iid = payload
217 .getInstanceIdentifierContext();
219 PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
220 PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
221 PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
223 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
224 final DOMTransactionChain transactionChain;
225 final SchemaContextRef ref;
226 if (mountPoint == null) {
227 transactionChain = this.transactionChainHandler.get();
228 ref = new SchemaContextRef(this.schemaContextHandler.get());
230 transactionChain = transactionChainOfMountPoint(mountPoint);
231 ref = new SchemaContextRef(mountPoint.getSchemaContext());
234 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
235 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
236 return PutDataTransactionUtil.putData(payload, ref, transactionNode, insert, point);
239 private static void checkQueryParams(final boolean insertUsed, final boolean pointUsed, final String insert) {
240 if (pointUsed && !insertUsed) {
241 throw new RestconfDocumentedException("Point parameter can't be used without Insert parameter.");
243 if (pointUsed && (insert.equals("first") || insert.equals("last"))) {
244 throw new RestconfDocumentedException(
245 "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
250 public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
251 return postData(payload, uriInfo);
255 public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
256 Preconditions.checkNotNull(payload);
258 boolean insertUsed = false;
259 boolean pointUsed = false;
260 String insert = null;
263 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
264 switch (entry.getKey()) {
268 insert = entry.getValue().iterator().next();
270 throw new RestconfDocumentedException("Insert parameter can be used only once.");
276 point = entry.getValue().iterator().next();
278 throw new RestconfDocumentedException("Point parameter can be used only once.");
282 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
286 checkQueryParams(insertUsed, pointUsed, insert);
288 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
289 final DOMTransactionChain transactionChain;
290 final SchemaContextRef ref;
291 if (mountPoint == null) {
292 transactionChain = this.transactionChainHandler.get();
293 ref = new SchemaContextRef(this.schemaContextHandler.get());
295 transactionChain = transactionChainOfMountPoint(mountPoint);
296 ref = new SchemaContextRef(mountPoint.getSchemaContext());
298 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
299 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
300 return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref, insert, point);
304 public Response deleteData(final String identifier) {
305 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
306 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
307 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
309 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
310 final DOMTransactionChain transactionChain;
311 if (mountPoint == null) {
312 transactionChain = this.transactionChainHandler.get();
314 transactionChain = transactionChainOfMountPoint(mountPoint);
317 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
319 return DeleteDataTransactionUtil.deleteData(transactionNode);
323 public PatchStatusContext patchData(final String identifier, final PatchContext context, final UriInfo uriInfo) {
324 return patchData(context, uriInfo);
328 public PatchStatusContext patchData(final PatchContext context, final UriInfo uriInfo) {
329 Preconditions.checkNotNull(context);
330 final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
332 final DOMTransactionChain transactionChain;
333 final SchemaContextRef ref;
334 if (mountPoint == null) {
335 transactionChain = this.transactionChainHandler.get();
336 ref = new SchemaContextRef(this.schemaContextHandler.get());
338 transactionChain = transactionChainOfMountPoint(mountPoint);
339 ref = new SchemaContextRef(mountPoint.getSchemaContext());
342 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
343 context.getInstanceIdentifierContext(), mountPoint, transactionChain);
345 return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
349 * Prepare transaction chain to access data of mount point.
351 * mount point reference
352 * @return {@link DOMTransactionChain}
354 private static DOMTransactionChain transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
355 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
356 if (domDataBrokerService.isPresent()) {
357 return domDataBrokerService.get().createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
360 final String errMsg = "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier();
362 throw new RestconfDocumentedException(errMsg);