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.collect.Maps;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.List;
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;
37 * Util class for put data to DS
40 public final class PutDataTransactionUtil {
43 * Valid input data with {@link SchemaNode}
46 * - {@link SchemaNode}
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);
59 * Valid top level node name
66 public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodeContext payload) {
67 final String payloadName = payload.getData().getNodeType().getLocalName();
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);
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);
85 * Validates whether keys in {@code payload} are equal to values of keys in
86 * {@code iiWithData} for list schema node
88 * @throws RestconfDocumentedException
89 * if key values or key count in payload and URI isn't equal
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)
102 isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
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.");
115 final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
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);
127 * Check mount point and prepare variables for put data to DS
131 * @param schemaCtxRef
132 * - reference to {@link SchemaContext}
133 * @param transactionNode
134 * @return {@link CheckedFuture}
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, RestconfDataServiceConstant.PutData.PUT_TX_TYPE, responseFactory);
144 return responseFactory.build();
152 * @param schemaContext
153 * - {@link SchemaContext}
155 * - write transaction
158 * @return {@link CheckedFuture}
160 private static CheckedFuture<Void, TransactionCommitFailedException> submitData(final YangInstanceIdentifier path,
161 final SchemaContext schemaContext,
162 final DOMDataWriteTransaction writeTx, final NormalizedNode<?, ?> data) {
163 TransactionUtil.ensureParentsByMerge(path, schemaContext, writeTx);
164 writeTx.put(LogicalDatastoreType.CONFIGURATION, path, data);
165 return writeTx.submit();