Bug 5528 - Impl Post data
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / utils / PostDataTransactionUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.restful.utils;
9
10 import com.google.common.util.concurrent.CheckedFuture;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.net.URI;
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;
42
43 /**
44  * Util class to post data to DS
45  *
46  */
47 public final class PostDataTransactionUtil {
48
49     private static final Logger LOG = LoggerFactory.getLogger(PostDataTransactionUtil.class);
50
51     private PostDataTransactionUtil() {
52         throw new UnsupportedOperationException("Util class.");
53     }
54
55     /**
56      * Check mount point and prepare variables for post data
57      *
58      * @param uriInfo
59      *
60      * @param payload
61      *            - data
62      * @param transactionNode
63      *            - wrapper for transaction data
64      * @param schemaContextRef
65      *            - reference to actual {@link SchemaContext}
66      * @return {@link CheckedFuture}
67      */
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),
76                 location);
77         FutureCallbackTx.addCallback(future, transactionNode.getTransaction(),
78                 RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
79         return dataFactory.build();
80     }
81
82     /**
83      * Post data by type
84      *
85      * @param path
86      *            - path
87      * @param data
88      *            - data
89      * @param transactionNode
90      *            - wrapper for data to transaction
91      * @param schemaContext
92      *            - schema context of data
93      * @return {@link CheckedFuture}
94      */
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);
102
103         if (data instanceof MapNode) {
104             for (final MapEntryNode child : ((MapNode) data).getValue()) {
105                 putChild(child, transaction, path);
106             }
107         } else if (data instanceof AugmentationNode) {
108             for (final DataContainerChild<? extends PathArgument, ?> child : ((AugmentationNode) data).getValue()) {
109                 putChild(child, transaction, path);
110             }
111         } else if (data instanceof ChoiceNode) {
112             for (final DataContainerChild<? extends PathArgument, ?> child : ((ChoiceNode) data).getValue()) {
113                 putChild(child, transaction, path);
114             }
115         } else if (data instanceof LeafSetNode<?>) {
116             for (final LeafSetEntryNode<?> child : ((LeafSetNode<?>) data).getValue()) {
117                 putChild(child, transaction, path);
118             }
119         } else if (data instanceof ContainerNode) {
120             for (final DataContainerChild<? extends PathArgument, ?> child : ((ContainerNode) data).getValue()) {
121                 putChild(child, transaction, path);
122             }
123         }
124         return transaction.submit();
125     }
126
127     /**
128      * Prepare data for submit
129      *
130      * @param child
131      *            - data
132      * @param readWriteTx
133      *            - transaction
134      * @param path
135      *            - path to data
136      */
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);
142     }
143
144     /**
145      * Check if data posted to create doesn't exits.
146      *
147      * @param path
148      *            - path to data
149      * @param readWriteTx
150      *            - read write transaction
151      */
152     private static void checkItemDesNotExits(final YangInstanceIdentifier path,
153             final DOMDataReadWriteTransaction readWriteTx) {
154         final ListenableFuture<Boolean> existData = readWriteTx.exists(LogicalDatastoreType.CONFIGURATION, path);
155         try {
156             if (existData.get()) {
157                 readWriteTx.cancel();
158                 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
159                         ErrorTag.DATA_EXISTS);
160             }
161         } catch (InterruptedException | ExecutionException e) {
162             LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
163         }
164     }
165
166     /**
167      * Get location from {@link YangInstanceIdentifier} and {@link UriInfo}
168      *
169      * @param uriInfo
170      *            - uri info
171      * @param transactionNode
172      *            - wrapper for data of transaction
173      * @param schemaContextRef
174      *            -reference to {@link SchemaContext}
175      * @return {@link URI}
176      */
177     private static URI resolveLocation(final UriInfo uriInfo, final TransactionVarsWrapper transactionNode,
178             final SchemaContextRef schemaContextRef) {
179         if (uriInfo == null) {
180             return null;
181         }
182
183         final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
184         uriBuilder.path("data");
185         uriBuilder.path(ParserIdentifier.stringFromYangInstanceIdentifier(transactionNode.getInstanceIdentifier().getInstanceIdentifier(),
186                 schemaContextRef.get()));
187
188         return uriBuilder.build();
189     }
190 }
191