Bug 5528 - Impl Post data
[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.collect.Maps;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.List;
13 import java.util.Map;
14 import javax.ws.rs.core.Response;
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.DOMDataWriteTransaction;
18 import org.opendaylight.netconf.md.sal.rest.common.RestconfValidationUtils;
19 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
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.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35
36 /**
37  * Util class for put data to DS
38  *
39  */
40 public final class PutDataTransactionUtil {
41
42     /**
43      * Valid input data with {@link SchemaNode}
44      *
45      * @param schemaNode
46      *            - {@link SchemaNode}
47      * @param payload
48      *            - input data
49      */
50     public static void validInputData(final SchemaNode schemaNode, final NormalizedNodeContext payload) {
51         if ((schemaNode != null) && (payload.getData() == null)) {
52             throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
53         } else if ((schemaNode == null) && (payload.getData() != null)) {
54             throw new RestconfDocumentedException("No input expected.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
55         }
56     }
57
58     /**
59      * Valid top level node name
60      *
61      * @param path
62      *            - path of node
63      * @param payload
64      *            - data
65      */
66     public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodeContext payload) {
67         final String payloadName = payload.getData().getNodeType().getLocalName();
68
69         if (path.isEmpty()) {
70             if (!payload.getData().getNodeType().equals(RestconfDataServiceConstant.NETCONF_BASE_QNAME)) {
71                 throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument",
72                         ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
73             }
74         } else {
75             final String identifierName = path.getLastPathArgument().getNodeType().getLocalName();
76             if (!payloadName.equals(identifierName)) {
77                 throw new RestconfDocumentedException(
78                         "Payload name (" + payloadName + ") is different from identifier name (" + identifierName + ")",
79                         ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
80             }
81         }
82     }
83
84     /**
85      * Validates whether keys in {@code payload} are equal to values of keys in
86      * {@code iiWithData} for list schema node
87      *
88      * @throws RestconfDocumentedException
89      *             if key values or key count in payload and URI isn't equal
90      *
91      */
92     public static void validateListKeysEqualityInPayloadAndUri(final NormalizedNodeContext payload) {
93         final InstanceIdentifierContext<?> iiWithData = payload.getInstanceIdentifierContext();
94         final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument();
95         final SchemaNode schemaNode = iiWithData.getSchemaNode();
96         final NormalizedNode<?, ?> data = payload.getData();
97         if (schemaNode instanceof ListSchemaNode) {
98             final List<QName> keyDefinitions = ((ListSchemaNode) schemaNode).getKeyDefinition();
99             if ((lastPathArgument instanceof NodeIdentifierWithPredicates) && (data instanceof MapEntryNode)) {
100                 final Map<QName, Object> uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument)
101                         .getKeyValues();
102                 isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
103             }
104         }
105     }
106
107     private static void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues, final MapEntryNode payload,
108             final List<QName> keyDefinitions) {
109         final Map<QName, Object> mutableCopyUriKeyValues = Maps.newHashMap(uriKeyValues);
110         for (final QName keyDefinition : keyDefinitions) {
111             final Object uriKeyValue = mutableCopyUriKeyValues.remove(keyDefinition);
112             RestconfValidationUtils.checkDocumentedError(uriKeyValue != null, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING,
113                     "Missing key " + keyDefinition + " in URI.");
114
115             final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
116
117             if (!uriKeyValue.equals(dataKeyValue)) {
118                 final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName()
119                         + "' specified in the URI doesn't match the value '" + dataKeyValue
120                         + "' specified in the message body. ";
121                 throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
122             }
123         }
124     }
125
126     /**
127      * Check mount point and prepare variables for put data to DS
128      *
129      * @param payload
130      *            - data to put
131      * @param schemaCtxRef
132      *            - reference to {@link SchemaContext}
133      * @param transactionNode
134      * @return {@link CheckedFuture}
135      */
136     public static Response putData(final NormalizedNodeContext payload,
137             final SchemaContextRef schemaCtxRef, final TransactionVarsWrapper transactionNode) {
138         final YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
139         final ResponseFactory responseFactory = new ResponseFactory(
140                 ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.CONFIG, transactionNode));
141         final CheckedFuture<Void, TransactionCommitFailedException> submitData = submitData(path, schemaCtxRef.get(),
142                 transactionNode.getTransaction(), payload.getData());
143         FutureCallbackTx.addCallback(submitData, transactionNode.getTransaction(),
144                 RestconfDataServiceConstant.PutData.PUT_TX_TYPE, responseFactory);
145         return responseFactory.build();
146     }
147
148     /**
149      * Put data to DS
150      *
151      * @param path
152      *            - path of data
153      * @param schemaContext
154      *            - {@link SchemaContext}
155      * @param writeTx
156      *            - write transaction
157      * @param data
158      *            - data
159      * @return {@link CheckedFuture}
160      */
161     private static CheckedFuture<Void, TransactionCommitFailedException> submitData(final YangInstanceIdentifier path,
162             final SchemaContext schemaContext,
163             final DOMDataWriteTransaction writeTx, final NormalizedNode<?, ?> data) {
164         TransactionUtil.ensureParentsByMerge(path, schemaContext, writeTx);
165         writeTx.put(LogicalDatastoreType.CONFIGURATION, path, data);
166         return writeTx.submit();
167     }
168 }