fc9c676c4ea36a4bcaeb60bd591765d019a57813
[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 java.net.URI;
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;
41
42 /**
43  * Util class to post data to DS
44  *
45  */
46 public final class PostDataTransactionUtil {
47
48     private static final Logger LOG = LoggerFactory.getLogger(PostDataTransactionUtil.class);
49
50     private PostDataTransactionUtil() {
51         throw new UnsupportedOperationException("Util class.");
52     }
53
54     /**
55      * Check mount point and prepare variables for post data
56      *
57      * @param uriInfo
58      *
59      * @param payload
60      *            - data
61      * @param transactionNode
62      *            - wrapper for transaction data
63      * @param schemaContextRef
64      *            - reference to actual {@link SchemaContext}
65      * @return {@link CheckedFuture}
66      */
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();
76     }
77
78     /**
79      * Post data by type
80      *
81      * @param path
82      *            - path
83      * @param data
84      *            - data
85      * @param transactionNode
86      *            - wrapper for data to transaction
87      * @param schemaContext
88      *            - schema context of data
89      * @return {@link CheckedFuture}
90      */
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);
99
100         if (data instanceof MapNode) {
101             for (final MapEntryNode child : ((MapNode) data).getValue()) {
102                 putChild(child, transactionChain, transaction, path);
103             }
104         } else if (data instanceof AugmentationNode) {
105             for (final DataContainerChild<? extends PathArgument, ?> child : ((AugmentationNode) data).getValue()) {
106                 putChild(child, transactionChain, transaction, path);
107             }
108         } else if (data instanceof ChoiceNode) {
109             for (final DataContainerChild<? extends PathArgument, ?> child : ((ChoiceNode) data).getValue()) {
110                 putChild(child, transactionChain, transaction, path);
111             }
112         } else if (data instanceof LeafSetNode<?>) {
113             for (final LeafSetEntryNode<?> child : ((LeafSetNode<?>) data).getValue()) {
114                 putChild(child, transactionChain, transaction, path);
115             }
116         } else if (data instanceof ContainerNode) {
117             for (final DataContainerChild<? extends PathArgument, ?> child : ((ContainerNode) data).getValue()) {
118                 putChild(child, transactionChain, transaction, path);
119             }
120         } else {
121             transaction.cancel();
122             RestConnectorProvider.resetTransactionChainForAdapaters(transactionChain);
123
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);
128         }
129
130         return transaction.submit();
131     }
132
133     /**
134      * Prepare data for submit
135      *
136      * @param child
137      *            - data
138      * @param transactionChain
139      *            - transaction chain
140      * @param readWriteTx
141      *            - transaction
142      * @param path
143      *            - path to data
144      */
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);
152     }
153
154     /**
155      * Get location from {@link YangInstanceIdentifier} and {@link UriInfo}
156      *
157      * @param uriInfo
158      *            - uri info
159      * @param transactionNode
160      *            - wrapper for data of transaction
161      * @param schemaContextRef
162      *            -reference to {@link SchemaContext}
163      * @return {@link URI}
164      */
165     private static URI resolveLocation(final UriInfo uriInfo, final TransactionVarsWrapper transactionNode,
166             final SchemaContextRef schemaContextRef) {
167         if (uriInfo == null) {
168             return null;
169         }
170
171         final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
172         uriBuilder.path("data");
173         uriBuilder.path(ParserIdentifier.stringFromYangInstanceIdentifier(transactionNode.getInstanceIdentifier().getInstanceIdentifier(),
174                 schemaContextRef.get()));
175
176         return uriBuilder.build();
177     }
178 }