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.utils;
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.HashMap;
12 import java.util.List;
14 import java.util.Optional;
15 import javax.ws.rs.core.Response;
16 import javax.ws.rs.core.Response.Status;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
20 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
21 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.common.errors.RestconfError;
24 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
25 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
26 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
27 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
40 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
43 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
49 * Util class for put data to DS.
52 public final class PutDataTransactionUtil {
54 private PutDataTransactionUtil() {
58 * Valid input data with {@link SchemaNode}.
60 * @param schemaNode {@link SchemaNode}
61 * @param payload input data
63 public static void validInputData(final SchemaNode schemaNode, final NormalizedNodeContext payload) {
64 if (schemaNode != null && payload.getData() == null) {
65 throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
66 } else if (schemaNode == null && payload.getData() != null) {
67 throw new RestconfDocumentedException("No input expected.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
72 * Valid top level node name.
74 * @param path path of node
77 public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodeContext payload) {
78 final String payloadName = payload.getData().getNodeType().getLocalName();
81 if (!payload.getData().getNodeType().equals(RestconfDataServiceConstant.NETCONF_BASE_QNAME)) {
82 throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument",
83 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
86 final String identifierName = path.getLastPathArgument().getNodeType().getLocalName();
87 if (!payloadName.equals(identifierName)) {
88 throw new RestconfDocumentedException(
89 "Payload name (" + payloadName + ") is different from identifier name (" + identifierName + ")",
90 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
96 * Validates whether keys in {@code payload} are equal to values of keys in
97 * {@code iiWithData} for list schema node.
99 * @throws RestconfDocumentedException if key values or key count in payload and URI isn't equal
101 public static void validateListKeysEqualityInPayloadAndUri(final NormalizedNodeContext payload) {
102 final InstanceIdentifierContext<?> iiWithData = payload.getInstanceIdentifierContext();
103 final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument();
104 final SchemaNode schemaNode = iiWithData.getSchemaNode();
105 final NormalizedNode<?, ?> data = payload.getData();
106 if (schemaNode instanceof ListSchemaNode) {
107 final List<QName> keyDefinitions = ((ListSchemaNode) schemaNode).getKeyDefinition();
108 if (lastPathArgument instanceof NodeIdentifierWithPredicates && data instanceof MapEntryNode) {
109 final Map<QName, Object> uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument).asMap();
110 isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
115 private static void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues, final MapEntryNode payload,
116 final List<QName> keyDefinitions) {
117 final Map<QName, Object> mutableCopyUriKeyValues = new HashMap<>(uriKeyValues);
118 for (final QName keyDefinition : keyDefinitions) {
119 final Object uriKeyValue = RestconfDocumentedException.throwIfNull(
120 mutableCopyUriKeyValues.remove(keyDefinition), ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
121 "Missing key %s in URI.", keyDefinition);
123 final Object dataKeyValue = payload.getIdentifier().getValue(keyDefinition);
125 if (!uriKeyValue.equals(dataKeyValue)) {
126 final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName()
127 + "' specified in the URI doesn't match the value '" + dataKeyValue
128 + "' specified in the message body. ";
129 throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
135 * Check mount point and prepare variables for put data to DS. Close {@link DOMTransactionChain} if any
136 * inside of object {@link RestconfStrategy} provided as a parameter if any.
138 * @param payload data to put
139 * @param schemaContext reference to {@link EffectiveModelContext}
140 * @param strategy object that perform the actual DS operations
141 * @param point query parameter
142 * @param insert query parameter
143 * @return {@link Response}
145 public static Response putData(final NormalizedNodeContext payload, final EffectiveModelContext schemaContext,
146 final RestconfStrategy strategy, final String insert, final String point) {
147 final YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
149 strategy.prepareReadWriteExecution();
150 final FluentFuture<Boolean> existsFuture = strategy.exists(LogicalDatastoreType.CONFIGURATION, path);
151 final FutureDataFactory<Boolean> existsResponse = new FutureDataFactory<>();
152 FutureCallbackTx.addCallback(existsFuture, RestconfDataServiceConstant.PutData.PUT_TX_TYPE, existsResponse);
154 final ResponseFactory responseFactory =
155 new ResponseFactory(existsResponse.result ? Status.NO_CONTENT : Status.CREATED);
156 final FluentFuture<? extends CommitInfo> submitData = submitData(path, schemaContext, strategy,
157 payload.getData(), insert, point, existsResponse.result);
158 //This method will close transactionChain if any
159 FutureCallbackTx.addCallback(submitData, RestconfDataServiceConstant.PutData.PUT_TX_TYPE, responseFactory,
160 strategy.getTransactionChain());
161 return responseFactory.build();
167 * @param path path of data
168 * @param schemaContext {@link SchemaContext}
169 * @param strategy object that perform the actual DS operations
171 * @param point query parameter
172 * @param insert query parameter
173 * @return {@link FluentFuture}
175 private static FluentFuture<? extends CommitInfo> submitData(
176 final YangInstanceIdentifier path,
177 final EffectiveModelContext schemaContext,
178 final RestconfStrategy strategy,
179 final NormalizedNode<?, ?> data, final String insert, final String point, final boolean exists) {
180 if (insert == null) {
181 return makePut(path, schemaContext, strategy, data, exists);
184 final DataSchemaNode schemaNode = checkListAndOrderedType(schemaContext, path);
187 if (schemaNode instanceof ListSchemaNode) {
188 final NormalizedNode<?, ?> readData = readList(strategy, path);
189 final OrderedMapNode readList = (OrderedMapNode) readData;
190 if (readList == null || readList.getValue().isEmpty()) {
191 return makePut(path, schemaContext, strategy, data, exists);
193 strategy.delete(LogicalDatastoreType.CONFIGURATION, path.getParent());
194 simplePut(LogicalDatastoreType.CONFIGURATION, path, strategy, schemaContext, data, exists);
195 listPut(LogicalDatastoreType.CONFIGURATION, path.getParent(), strategy,
196 schemaContext, readList, exists);
197 return strategy.commit();
200 final NormalizedNode<?, ?> readData = readList(strategy, path);
202 final OrderedLeafSetNode<?> readLeafList = (OrderedLeafSetNode<?>) readData;
203 if (readLeafList == null || readLeafList.getValue().isEmpty()) {
204 return makePut(path, schemaContext, strategy, data, exists);
206 strategy.delete(LogicalDatastoreType.CONFIGURATION, path.getParent());
207 simplePut(LogicalDatastoreType.CONFIGURATION, path, strategy,
208 schemaContext, data, exists);
209 listPut(LogicalDatastoreType.CONFIGURATION, path.getParent(), strategy,
210 schemaContext, readLeafList, exists);
211 return strategy.commit();
215 return makePut(path, schemaContext, strategy, data, exists);
217 if (schemaNode instanceof ListSchemaNode) {
218 final NormalizedNode<?, ?> readData = readList(strategy, path);
219 final OrderedMapNode readList = (OrderedMapNode) readData;
220 if (readList == null || readList.getValue().isEmpty()) {
221 return makePut(path, schemaContext, strategy, data, exists);
223 insertWithPointListPut(strategy, LogicalDatastoreType.CONFIGURATION, path,
224 data, schemaContext, point, readList, true, exists);
225 return strategy.commit();
228 final NormalizedNode<?, ?> readData = readList(strategy, path);
230 final OrderedLeafSetNode<?> readLeafList = (OrderedLeafSetNode<?>) readData;
231 if (readLeafList == null || readLeafList.getValue().isEmpty()) {
232 return makePut(path, schemaContext, strategy, data, exists);
234 insertWithPointLeafListPut(strategy, LogicalDatastoreType.CONFIGURATION,
235 path, data, schemaContext, point, readLeafList, true, exists);
236 return strategy.commit();
240 if (schemaNode instanceof ListSchemaNode) {
241 final NormalizedNode<?, ?> readData = readList(strategy, path);
242 final OrderedMapNode readList = (OrderedMapNode) readData;
243 if (readList == null || readList.getValue().isEmpty()) {
244 return makePut(path, schemaContext, strategy, data, exists);
246 insertWithPointListPut(strategy, LogicalDatastoreType.CONFIGURATION,
247 path, data, schemaContext, point, readList, false, exists);
248 return strategy.commit();
251 final NormalizedNode<?, ?> readData = readList(strategy, path);
253 final OrderedLeafSetNode<?> readLeafList = (OrderedLeafSetNode<?>) readData;
254 if (readLeafList == null || readLeafList.getValue().isEmpty()) {
255 return makePut(path, schemaContext, strategy, data, exists);
257 insertWithPointLeafListPut(strategy, LogicalDatastoreType.CONFIGURATION,
258 path, data, schemaContext, point, readLeafList, true, exists);
259 return strategy.commit();
263 throw new RestconfDocumentedException(
264 "Used bad value of insert parameter. Possible values are first, last, before or after, "
265 + "but was: " + insert, RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE);
269 // FIXME: this method is only called from a context where we are modifying data. This should be part of strategy,
270 // requiring an already-open transaction. It also must return a future, so it can be properly composed.
271 static NormalizedNode<?, ?> readList(final RestconfStrategy strategy, final YangInstanceIdentifier path) {
272 return ReadDataTransactionUtil.readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, true);
275 private static void insertWithPointLeafListPut(final RestconfStrategy strategy,
276 final LogicalDatastoreType datastore,
277 final YangInstanceIdentifier path,
278 final NormalizedNode<?, ?> data,
279 final EffectiveModelContext schemaContext, final String point,
280 final OrderedLeafSetNode<?> readLeafList, final boolean before,
281 final boolean exists) {
282 strategy.delete(datastore, path.getParent());
283 final InstanceIdentifierContext<?> instanceIdentifier =
284 ParserIdentifier.toInstanceIdentifier(point, schemaContext, Optional.empty());
285 int lastItemPosition = 0;
286 for (final LeafSetEntryNode<?> nodeChild : readLeafList.getValue()) {
287 if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
295 int lastInsertedPosition = 0;
296 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path.getParent());
297 strategy.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
298 for (final LeafSetEntryNode<?> nodeChild : readLeafList.getValue()) {
299 if (lastInsertedPosition == lastItemPosition) {
300 simplePut(datastore, path, strategy, schemaContext, data, exists);
302 final YangInstanceIdentifier childPath = path.getParent().node(nodeChild.getIdentifier());
304 strategy.replace(datastore, childPath, nodeChild);
306 strategy.create(datastore, childPath, nodeChild);
308 lastInsertedPosition++;
312 private static void insertWithPointListPut(final RestconfStrategy strategy,
313 final LogicalDatastoreType datastore, final YangInstanceIdentifier path,
314 final NormalizedNode<?, ?> data,
315 final EffectiveModelContext schemaContext, final String point,
316 final OrderedMapNode readList, final boolean before,
317 final boolean exists) {
318 strategy.delete(datastore, path.getParent());
319 final InstanceIdentifierContext<?> instanceIdentifier =
320 ParserIdentifier.toInstanceIdentifier(point, schemaContext, Optional.empty());
321 int lastItemPosition = 0;
322 for (final MapEntryNode mapEntryNode : readList.getValue()) {
323 if (mapEntryNode.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
331 int lastInsertedPosition = 0;
332 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path.getParent());
333 strategy.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
334 for (final MapEntryNode mapEntryNode : readList.getValue()) {
335 if (lastInsertedPosition == lastItemPosition) {
336 simplePut(datastore, path, strategy, schemaContext, data, exists);
338 final YangInstanceIdentifier childPath = path.getParent().node(mapEntryNode.getIdentifier());
340 strategy.replace(datastore, childPath, mapEntryNode);
342 strategy.create(datastore, childPath, mapEntryNode);
344 lastInsertedPosition++;
348 private static void listPut(final LogicalDatastoreType datastore, final YangInstanceIdentifier path,
349 final RestconfStrategy strategy, final SchemaContext schemaContext,
350 final OrderedLeafSetNode<?> payload, final boolean exists) {
351 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
352 strategy.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
353 TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
354 for (final LeafSetEntryNode<?> child : ((LeafSetNode<?>) payload).getValue()) {
355 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
357 strategy.replace(datastore, childPath, child);
359 strategy.create(datastore, childPath, child);
364 private static void listPut(final LogicalDatastoreType datastore, final YangInstanceIdentifier path,
365 final RestconfStrategy strategy, final SchemaContext schemaContext,
366 final OrderedMapNode payload, final boolean exists) {
367 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
368 strategy.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
369 TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
370 for (final MapEntryNode child : payload.getValue()) {
371 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
373 strategy.replace(datastore, childPath, child);
375 strategy.create(datastore, childPath, child);
380 private static void simplePut(final LogicalDatastoreType configuration, final YangInstanceIdentifier path,
381 final RestconfStrategy strategy, final SchemaContext schemaContext,
382 final NormalizedNode<?, ?> data, final boolean exists) {
383 TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
385 strategy.replace(LogicalDatastoreType.CONFIGURATION, path, data);
387 strategy.create(LogicalDatastoreType.CONFIGURATION, path, data);
391 private static FluentFuture<? extends CommitInfo> makePut(final YangInstanceIdentifier path,
392 final SchemaContext schemaContext,
393 final RestconfStrategy strategy,
394 final NormalizedNode<?, ?> data,
395 final boolean exists) {
396 TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
398 strategy.replace(LogicalDatastoreType.CONFIGURATION, path, data);
400 strategy.create(LogicalDatastoreType.CONFIGURATION, path, data);
402 return strategy.commit();
405 public static DataSchemaNode checkListAndOrderedType(final SchemaContext ctx, final YangInstanceIdentifier path) {
406 final YangInstanceIdentifier parent = path.getParent();
407 final DataSchemaContextNode<?> node = DataSchemaContextTree.from(ctx).getChild(parent);
408 final DataSchemaNode dataSchemaNode = node.getDataSchemaNode();
410 if (dataSchemaNode instanceof ListSchemaNode) {
411 if (!((ListSchemaNode) dataSchemaNode).isUserOrdered()) {
412 throw new RestconfDocumentedException("Insert parameter can be used only with ordered-by user list.",
413 RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
415 return dataSchemaNode;
417 if (dataSchemaNode instanceof LeafListSchemaNode) {
418 if (!((LeafListSchemaNode) dataSchemaNode).isUserOrdered()) {
419 throw new RestconfDocumentedException(
420 "Insert parameter can be used only with ordered-by user leaf-list.",
421 RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
423 return dataSchemaNode;
425 throw new RestconfDocumentedException("Insert parameter can be used only with list or leaf-list",
426 RestconfError.ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);