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.common.QName;
52 import org.opendaylight.yangtools.yang.common.Revision;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * Implementation of {@link RestconfDataService}.
61 public class RestconfDataServiceImpl implements RestconfDataService {
63 private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
64 private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
66 private SchemaContextHandler schemaContextHandler;
67 private TransactionChainHandler transactionChainHandler;
68 private DOMMountPointServiceHandler mountPointServiceHandler;
70 private final RestconfStreamsSubscriptionService delegRestconfSubscrService;
72 public RestconfDataServiceImpl(final SchemaContextHandler schemaContextHandler,
73 final TransactionChainHandler transactionChainHandler,
74 final DOMMountPointServiceHandler mountPointServiceHandler,
75 final RestconfStreamsSubscriptionService delegRestconfSubscrService) {
76 this.schemaContextHandler = schemaContextHandler;
77 this.transactionChainHandler = transactionChainHandler;
78 this.mountPointServiceHandler = mountPointServiceHandler;
79 this.delegRestconfSubscrService = delegRestconfSubscrService;
83 public synchronized void updateHandlers(final Object... handlers) {
84 for (final Object object : handlers) {
85 if (object instanceof SchemaContextHandler) {
86 schemaContextHandler = (SchemaContextHandler) object;
87 } else if (object instanceof DOMMountPointServiceHandler) {
88 mountPointServiceHandler = (DOMMountPointServiceHandler) object;
89 } else if (object instanceof TransactionChainHandler) {
90 transactionChainHandler = (TransactionChainHandler) object;
96 public Response readData(final UriInfo uriInfo) {
97 return readData(null, uriInfo);
101 public Response readData(final String identifier, final UriInfo uriInfo) {
102 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
103 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
104 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
106 boolean withDefaUsed = false;
107 String withDefa = null;
109 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
110 switch (entry.getKey()) {
111 case "with-defaults":
114 withDefa = entry.getValue().iterator().next();
116 throw new RestconfDocumentedException("With-defaults parameter can be used only once.");
120 LOG.info("Unknown key : {}.", entry.getKey());
124 boolean tagged = false;
126 if ("report-all-tagged".equals(withDefa)) {
130 if ("report-all".equals(withDefa)) {
135 final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(
136 instanceIdentifier, uriInfo, tagged);
138 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
139 final DOMTransactionChain transactionChain;
140 if (mountPoint == null) {
141 transactionChain = this.transactionChainHandler.get();
143 transactionChain = transactionChainOfMountPoint(mountPoint);
146 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
147 instanceIdentifier, mountPoint, transactionChain);
148 final NormalizedNode<?, ?> node =
149 ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
150 schemaContextRef, uriInfo);
151 if (identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART)
152 && identifier.contains(STREAM_LOCATION_PATH_PART)) {
153 final String value = (String) node.getValue();
154 final String streamName = value.substring(
155 value.indexOf(CREATE_NOTIFICATION_STREAM.toString() + RestconfConstants.SLASH),
157 this.delegRestconfSubscrService.subscribeToStream(streamName, uriInfo);
160 throw new RestconfDocumentedException(
161 "Request could not be completed because the relevant data model content does not exist",
162 RestconfError.ErrorType.PROTOCOL,
163 RestconfError.ErrorTag.DATA_MISSING);
166 if (parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL)
167 || parameters.getContent().equals(RestconfDataServiceConstant.ReadData.CONFIG)) {
168 final QName type = node.getNodeType();
169 return Response.status(200)
170 .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
171 .header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null)
172 + type.getLocalName() + '"')
173 .header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC())))
177 return Response.status(200).entity(new NormalizedNodeContext(instanceIdentifier, node, parameters)).build();
181 public Response putData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
182 Preconditions.checkNotNull(payload);
184 boolean insertUsed = false;
185 boolean pointUsed = false;
186 String insert = null;
189 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
190 switch (entry.getKey()) {
194 insert = entry.getValue().iterator().next();
196 throw new RestconfDocumentedException("Insert parameter can be used only once.");
202 point = entry.getValue().iterator().next();
204 throw new RestconfDocumentedException("Point parameter can be used only once.");
208 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
212 checkQueryParams(insertUsed, pointUsed, insert);
214 final InstanceIdentifierContext<? extends SchemaNode> iid = payload
215 .getInstanceIdentifierContext();
217 PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
218 PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
219 PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
221 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
222 final DOMTransactionChain transactionChain;
223 final SchemaContextRef ref;
224 if (mountPoint == null) {
225 transactionChain = this.transactionChainHandler.get();
226 ref = new SchemaContextRef(this.schemaContextHandler.get());
228 transactionChain = transactionChainOfMountPoint(mountPoint);
229 ref = new SchemaContextRef(mountPoint.getSchemaContext());
232 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
233 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
234 return PutDataTransactionUtil.putData(payload, ref, transactionNode, insert, point);
237 private static void checkQueryParams(final boolean insertUsed, final boolean pointUsed, final String insert) {
238 if (pointUsed && !insertUsed) {
239 throw new RestconfDocumentedException("Point parameter can't be used without Insert parameter.");
241 if (pointUsed && (insert.equals("first") || insert.equals("last"))) {
242 throw new RestconfDocumentedException(
243 "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
248 public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
249 return postData(payload, uriInfo);
253 public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
254 Preconditions.checkNotNull(payload);
256 boolean insertUsed = false;
257 boolean pointUsed = false;
258 String insert = null;
261 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
262 switch (entry.getKey()) {
266 insert = entry.getValue().iterator().next();
268 throw new RestconfDocumentedException("Insert parameter can be used only once.");
274 point = entry.getValue().iterator().next();
276 throw new RestconfDocumentedException("Point parameter can be used only once.");
280 throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
284 checkQueryParams(insertUsed, pointUsed, insert);
286 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
287 final DOMTransactionChain transactionChain;
288 final SchemaContextRef ref;
289 if (mountPoint == null) {
290 transactionChain = this.transactionChainHandler.get();
291 ref = new SchemaContextRef(this.schemaContextHandler.get());
293 transactionChain = transactionChainOfMountPoint(mountPoint);
294 ref = new SchemaContextRef(mountPoint.getSchemaContext());
296 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
297 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
298 return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref, insert, point);
302 public Response deleteData(final String identifier) {
303 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
304 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
305 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
307 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
308 final DOMTransactionChain transactionChain;
309 if (mountPoint == null) {
310 transactionChain = this.transactionChainHandler.get();
312 transactionChain = transactionChainOfMountPoint(mountPoint);
315 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
317 return DeleteDataTransactionUtil.deleteData(transactionNode);
321 public PatchStatusContext patchData(final String identifier, final PatchContext context, final UriInfo uriInfo) {
322 return patchData(context, uriInfo);
326 public PatchStatusContext patchData(final PatchContext context, final UriInfo uriInfo) {
327 Preconditions.checkNotNull(context);
328 final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
330 final DOMTransactionChain transactionChain;
331 final SchemaContextRef ref;
332 if (mountPoint == null) {
333 transactionChain = this.transactionChainHandler.get();
334 ref = new SchemaContextRef(this.schemaContextHandler.get());
336 transactionChain = transactionChainOfMountPoint(mountPoint);
337 ref = new SchemaContextRef(mountPoint.getSchemaContext());
340 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
341 context.getInstanceIdentifierContext(), mountPoint, transactionChain);
343 return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
347 * Prepare transaction chain to access data of mount point.
349 * mount point reference
350 * @return {@link DOMTransactionChain}
352 private static DOMTransactionChain transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
353 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
354 if (domDataBrokerService.isPresent()) {
355 return domDataBrokerService.get().createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
358 final String errMsg = "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier();
360 throw new RestconfDocumentedException(errMsg);