Bug 5528 - Put data impl
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / utils / PutDataTransactionUtil.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.base.Preconditions;
11 import com.google.common.collect.Maps;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map;
17 import javax.ws.rs.core.Response;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.netconf.md.sal.rest.common.RestconfValidationUtils;
22 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
23 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
26 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
27 import org.opendaylight.restconf.common.references.SchemaContextRef;
28 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40
41 /**
42  * Util class for put data to DS
43  *
44  */
45 public final class PutDataTransactionUtil {
46
47     /**
48      * Valid input data with {@link SchemaNode}
49      *
50      * @param schemaNode
51      *            - {@link SchemaNode}
52      * @param payload
53      *            - input data
54      */
55     public static void validInputData(final SchemaNode schemaNode, final NormalizedNodeContext payload) {
56         if ((schemaNode != null) && (payload.getData() == null)) {
57             throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
58         } else if ((schemaNode == null) && (payload.getData() != null)) {
59             throw new RestconfDocumentedException("No input expected.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
60         }
61     }
62
63     /**
64      * Valid top level node name
65      *
66      * @param path
67      *            - path of node
68      * @param payload
69      *            - data
70      */
71     public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodeContext payload) {
72         final String payloadName = payload.getData().getNodeType().getLocalName();
73
74         if (path.isEmpty()) {
75             if (!payload.getData().getNodeType().equals(RestconfDataServiceConstant.NETCONF_BASE_QNAME)) {
76                 throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument",
77                         ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
78             }
79         } else {
80             final String identifierName = path.getLastPathArgument().getNodeType().getLocalName();
81             if (!payloadName.equals(identifierName)) {
82                 throw new RestconfDocumentedException(
83                         "Payload name (" + payloadName + ") is different from identifier name (" + identifierName + ")",
84                         ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
85             }
86         }
87     }
88
89     /**
90      * Validates whether keys in {@code payload} are equal to values of keys in
91      * {@code iiWithData} for list schema node
92      *
93      * @throws RestconfDocumentedException
94      *             if key values or key count in payload and URI isn't equal
95      *
96      */
97     public static void validateListKeysEqualityInPayloadAndUri(final NormalizedNodeContext payload) {
98         final InstanceIdentifierContext<?> iiWithData = payload.getInstanceIdentifierContext();
99         final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument();
100         final SchemaNode schemaNode = iiWithData.getSchemaNode();
101         final NormalizedNode<?, ?> data = payload.getData();
102         if (schemaNode instanceof ListSchemaNode) {
103             final List<QName> keyDefinitions = ((ListSchemaNode) schemaNode).getKeyDefinition();
104             if ((lastPathArgument instanceof NodeIdentifierWithPredicates) && (data instanceof MapEntryNode)) {
105                 final Map<QName, Object> uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument)
106                         .getKeyValues();
107                 isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
108             }
109         }
110     }
111
112     private static void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues, final MapEntryNode payload,
113             final List<QName> keyDefinitions) {
114         final Map<QName, Object> mutableCopyUriKeyValues = Maps.newHashMap(uriKeyValues);
115         for (final QName keyDefinition : keyDefinitions) {
116             final Object uriKeyValue = mutableCopyUriKeyValues.remove(keyDefinition);
117             RestconfValidationUtils.checkDocumentedError(uriKeyValue != null, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
118                     "Missing key " + keyDefinition + " in URI.");
119
120             final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
121
122             if (!uriKeyValue.equals(dataKeyValue)) {
123                 final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName()
124                         + "' specified in the URI doesn't match the value '" + dataKeyValue
125                         + "' specified in the message body. ";
126                 throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
127             }
128         }
129     }
130
131     /**
132      * Check mount point and prepare variables for put data to DS
133      *
134      * @param payload
135      *            - data to put
136      * @param schemaCtxRef
137      *            - reference to {@link SchemaContext}
138      * @param transactionNode
139      * @return {@link CheckedFuture}
140      */
141     public static Response putData(final NormalizedNodeContext payload,
142             final SchemaContextRef schemaCtxRef, final TransactionVarsWrapper transactionNode) {
143         final YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
144         final ResponseFactory responseFactory = new ResponseFactory(
145                 ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.CONFIG, transactionNode));
146         final CheckedFuture<Void, TransactionCommitFailedException> submitData = submitData(path, schemaCtxRef.get(),
147                 transactionNode.getTransaction(), payload.getData());
148         FutureCallbackTx.addCallback(submitData, transactionNode.getTransaction(),
149                 RestconfDataServiceConstant.PutData.PUT_TX_TYPE, responseFactory);
150         return responseFactory.build();
151     }
152
153     /**
154      * Put data to DS
155      *
156      * @param path
157      *            - path of data
158      * @param schemaContext
159      *            - {@link SchemaContext}
160      * @param writeTx
161      *            - write transaction
162      * @param data
163      *            - data
164      * @return {@link CheckedFuture}
165      */
166     private static CheckedFuture<Void, TransactionCommitFailedException> submitData(final YangInstanceIdentifier path,
167             final SchemaContext schemaContext,
168             final DOMDataWriteTransaction writeTx, final NormalizedNode<?, ?> data) {
169         ensureParentsByMerge(path, schemaContext, writeTx);
170         writeTx.put(LogicalDatastoreType.CONFIGURATION, path, data);
171         return writeTx.submit();
172     }
173
174     /**
175      * Merged parents of data
176      *
177      * @param path
178      *            - path of data
179      * @param schemaContext
180      *            - {@link SchemaContext}
181      * @param writeTx
182      *            - write transaction
183      */
184     private static void ensureParentsByMerge(final YangInstanceIdentifier path, final SchemaContext schemaContext,
185             final DOMDataWriteTransaction writeTx) {
186         final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
187         boolean hasList = false;
188         YangInstanceIdentifier rootNormalizedPath = null;
189
190         final Iterator<PathArgument> it = path.getPathArguments().iterator();
191         final Module module = schemaContext.findModuleByNamespaceAndRevision(
192                 path.getLastPathArgument().getNodeType().getModule().getNamespace(),
193                 path.getLastPathArgument().getNodeType().getModule().getRevision());
194
195         while (it.hasNext()) {
196             final PathArgument pathArgument = it.next();
197             if (rootNormalizedPath == null) {
198                 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
199             }
200             if (it.hasNext()) {
201                 normalizedPathWithoutChildArgs.add(pathArgument);
202                 if (module.getDataChildByName(pathArgument.getNodeType()) instanceof ListSchemaNode) {
203                     hasList = true;
204                 }
205             }
206         }
207         if (normalizedPathWithoutChildArgs.isEmpty()) {
208             return;
209         }
210         if (hasList) {
211             Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
212             final NormalizedNode<?, ?> parentStructure = ImmutableNodes.fromInstanceId(schemaContext,
213                     YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
214             writeTx.merge(LogicalDatastoreType.CONFIGURATION, rootNormalizedPath, parentStructure);
215         }
216     }
217 }