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;
11 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.ExecutionException;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriBuilder;
16 import javax.ws.rs.core.UriInfo;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
20 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
24 import org.opendaylight.restconf.common.references.SchemaContextRef;
25 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
26 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Util class to post data to DS
47 public final class PostDataTransactionUtil {
49 private static final Logger LOG = LoggerFactory.getLogger(PostDataTransactionUtil.class);
51 private PostDataTransactionUtil() {
52 throw new UnsupportedOperationException("Util class.");
56 * Check mount point and prepare variables for post data
62 * @param transactionNode
63 * - wrapper for transaction data
64 * @param schemaContextRef
65 * - reference to actual {@link SchemaContext}
66 * @return {@link CheckedFuture}
68 public static Response postData(final UriInfo uriInfo, final NormalizedNodeContext payload,
69 final TransactionVarsWrapper transactionNode, final SchemaContextRef schemaContextRef) {
70 final CheckedFuture<Void, TransactionCommitFailedException> future = submitData(
71 payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(),
72 transactionNode, schemaContextRef.get());
73 final URI location = PostDataTransactionUtil.resolveLocation(uriInfo, transactionNode, schemaContextRef);
74 final ResponseFactory dataFactory = new ResponseFactory(
75 ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.CONFIG, transactionNode),
77 FutureCallbackTx.addCallback(future, transactionNode.getTransaction(),
78 RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
79 return dataFactory.build();
89 * @param transactionNode
90 * - wrapper for data to transaction
91 * @param schemaContext
92 * - schema context of data
93 * @return {@link CheckedFuture}
95 private static CheckedFuture<Void, TransactionCommitFailedException> submitData(final YangInstanceIdentifier path,
96 final NormalizedNode<?, ?> data, final TransactionVarsWrapper transactionNode,
97 final SchemaContext schemaContext) {
98 final DOMDataReadWriteTransaction transaction = transactionNode.getTransaction();
99 final NormalizedNode<?, ?> node = ImmutableNodes.fromInstanceId(schemaContext, path);
100 transaction.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(node.getIdentifier()), node);
101 TransactionUtil.ensureParentsByMerge(path, schemaContext, transaction);
103 if (data instanceof MapNode) {
104 for (final MapEntryNode child : ((MapNode) data).getValue()) {
105 putChild(child, transaction, path);
107 } else if (data instanceof AugmentationNode) {
108 for (final DataContainerChild<? extends PathArgument, ?> child : ((AugmentationNode) data).getValue()) {
109 putChild(child, transaction, path);
111 } else if (data instanceof ChoiceNode) {
112 for (final DataContainerChild<? extends PathArgument, ?> child : ((ChoiceNode) data).getValue()) {
113 putChild(child, transaction, path);
115 } else if (data instanceof LeafSetNode<?>) {
116 for (final LeafSetEntryNode<?> child : ((LeafSetNode<?>) data).getValue()) {
117 putChild(child, transaction, path);
119 } else if (data instanceof ContainerNode) {
120 for (final DataContainerChild<? extends PathArgument, ?> child : ((ContainerNode) data).getValue()) {
121 putChild(child, transaction, path);
124 return transaction.submit();
128 * Prepare data for submit
137 private static void putChild(final NormalizedNode<?, ?> child, final DOMDataReadWriteTransaction readWriteTx,
138 final YangInstanceIdentifier path) {
139 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
140 checkItemDesNotExits(childPath, readWriteTx);
141 readWriteTx.put(LogicalDatastoreType.CONFIGURATION, childPath, child);
145 * Check if data posted to create doesn't exits.
150 * - read write transaction
152 private static void checkItemDesNotExits(final YangInstanceIdentifier path,
153 final DOMDataReadWriteTransaction readWriteTx) {
154 final ListenableFuture<Boolean> existData = readWriteTx.exists(LogicalDatastoreType.CONFIGURATION, path);
156 if (existData.get()) {
157 readWriteTx.cancel();
158 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
159 ErrorTag.DATA_EXISTS);
161 } catch (InterruptedException | ExecutionException e) {
162 LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
167 * Get location from {@link YangInstanceIdentifier} and {@link UriInfo}
171 * @param transactionNode
172 * - wrapper for data of transaction
173 * @param schemaContextRef
174 * -reference to {@link SchemaContext}
175 * @return {@link URI}
177 private static URI resolveLocation(final UriInfo uriInfo, final TransactionVarsWrapper transactionNode,
178 final SchemaContextRef schemaContextRef) {
179 if (uriInfo == null) {
183 final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
184 uriBuilder.path("data");
185 uriBuilder.path(ParserIdentifier.stringFromYangInstanceIdentifier(transactionNode.getInstanceIdentifier().getInstanceIdentifier(),
186 schemaContextRef.get()));
188 return uriBuilder.build();