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.utils;
10 import com.google.common.util.concurrent.CheckedFuture;
12 import javax.ws.rs.core.Response;
13 import javax.ws.rs.core.UriBuilder;
14 import javax.ws.rs.core.UriInfo;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
19 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
22 import org.opendaylight.restconf.RestConnectorProvider;
23 import org.opendaylight.restconf.common.references.SchemaContextRef;
24 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
25 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
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.MapNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Util class to post data to DS
46 public final class PostDataTransactionUtil {
48 private static final Logger LOG = LoggerFactory.getLogger(PostDataTransactionUtil.class);
50 private PostDataTransactionUtil() {
51 throw new UnsupportedOperationException("Util class.");
55 * Check mount point and prepare variables for post data
61 * @param transactionNode
62 * - wrapper for transaction data
63 * @param schemaContextRef
64 * - reference to actual {@link SchemaContext}
65 * @return {@link CheckedFuture}
67 public static Response postData(final UriInfo uriInfo, final NormalizedNodeContext payload,
68 final TransactionVarsWrapper transactionNode, final SchemaContextRef schemaContextRef) {
69 final CheckedFuture<Void, TransactionCommitFailedException> future = submitData(
70 payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(),
71 transactionNode, schemaContextRef.get());
72 final URI location = PostDataTransactionUtil.resolveLocation(uriInfo, transactionNode, schemaContextRef);
73 final ResponseFactory dataFactory = new ResponseFactory(null, location);
74 FutureCallbackTx.addCallback(future, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
75 return dataFactory.build();
85 * @param transactionNode
86 * - wrapper for data to transaction
87 * @param schemaContext
88 * - schema context of data
89 * @return {@link CheckedFuture}
91 private static CheckedFuture<Void, TransactionCommitFailedException> submitData(final YangInstanceIdentifier path,
92 final NormalizedNode<?, ?> data, final TransactionVarsWrapper transactionNode,
93 final SchemaContext schemaContext) {
94 final DOMTransactionChain transactionChain = transactionNode.getTransactionChain();
95 final DOMDataReadWriteTransaction transaction = transactionChain.newReadWriteTransaction();
96 final NormalizedNode<?, ?> node = ImmutableNodes.fromInstanceId(schemaContext, path);
97 transaction.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(node.getIdentifier()), node);
98 TransactionUtil.ensureParentsByMerge(path, schemaContext, transaction);
100 if (data instanceof MapNode) {
101 for (final MapEntryNode child : ((MapNode) data).getValue()) {
102 putChild(child, transactionChain, transaction, path);
104 } else if (data instanceof AugmentationNode) {
105 for (final DataContainerChild<? extends PathArgument, ?> child : ((AugmentationNode) data).getValue()) {
106 putChild(child, transactionChain, transaction, path);
108 } else if (data instanceof ChoiceNode) {
109 for (final DataContainerChild<? extends PathArgument, ?> child : ((ChoiceNode) data).getValue()) {
110 putChild(child, transactionChain, transaction, path);
112 } else if (data instanceof LeafSetNode<?>) {
113 for (final LeafSetEntryNode<?> child : ((LeafSetNode<?>) data).getValue()) {
114 putChild(child, transactionChain, transaction, path);
116 } else if (data instanceof ContainerNode) {
117 for (final DataContainerChild<? extends PathArgument, ?> child : ((ContainerNode) data).getValue()) {
118 putChild(child, transactionChain, transaction, path);
121 transaction.cancel();
122 RestConnectorProvider.resetTransactionChainForAdapaters(transactionChain);
124 final String errMsg = "Only Map, Choice, Augmentation, LeafSet and Container nodes are supported";
125 LOG.trace("{}:{}", errMsg, path);
126 throw new RestconfDocumentedException(
127 "Node not supported", RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT, path);
130 return transaction.submit();
134 * Prepare data for submit
138 * @param transactionChain
139 * - transaction chain
145 private static void putChild(final NormalizedNode<?, ?> child, final DOMTransactionChain transactionChain,
146 final DOMDataReadWriteTransaction readWriteTx, final YangInstanceIdentifier path) {
147 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
148 TransactionUtil.checkItemDoesNotExists(
149 transactionChain, readWriteTx, LogicalDatastoreType.CONFIGURATION, childPath,
150 RestconfDataServiceConstant.PostData.POST_TX_TYPE);
151 readWriteTx.put(LogicalDatastoreType.CONFIGURATION, childPath, child);
155 * Get location from {@link YangInstanceIdentifier} and {@link UriInfo}
159 * @param transactionNode
160 * - wrapper for data of transaction
161 * @param schemaContextRef
162 * -reference to {@link SchemaContext}
163 * @return {@link URI}
165 private static URI resolveLocation(final UriInfo uriInfo, final TransactionVarsWrapper transactionNode,
166 final SchemaContextRef schemaContextRef) {
167 if (uriInfo == null) {
171 final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
172 uriBuilder.path("data");
173 uriBuilder.path(ParserIdentifier.stringFromYangInstanceIdentifier(transactionNode.getInstanceIdentifier().getInstanceIdentifier(),
174 schemaContextRef.get()));
176 return uriBuilder.build();