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.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;
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;
42 * Util class for put data to DS
45 public final class PutDataTransactionUtil {
48 * Valid input data with {@link SchemaNode}
51 * - {@link SchemaNode}
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);
64 * Valid top level node name
71 public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodeContext payload) {
72 final String payloadName = payload.getData().getNodeType().getLocalName();
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);
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);
90 * Validates whether keys in {@code payload} are equal to values of keys in
91 * {@code iiWithData} for list schema node
93 * @throws RestconfDocumentedException
94 * if key values or key count in payload and URI isn't equal
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)
107 isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions);
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.");
120 final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition);
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);
132 * Check mount point and prepare variables for put data to DS
136 * @param schemaCtxRef
137 * - reference to {@link SchemaContext}
138 * @param transactionNode
139 * @return {@link CheckedFuture}
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();
158 * @param schemaContext
159 * - {@link SchemaContext}
161 * - write transaction
164 * @return {@link CheckedFuture}
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();
175 * Merged parents of data
179 * @param schemaContext
180 * - {@link SchemaContext}
182 * - write transaction
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;
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());
195 while (it.hasNext()) {
196 final PathArgument pathArgument = it.next();
197 if (rootNormalizedPath == null) {
198 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
201 normalizedPathWithoutChildArgs.add(pathArgument);
202 if (module.getDataChildByName(pathArgument.getNodeType()) instanceof ListSchemaNode) {
207 if (normalizedPathWithoutChildArgs.isEmpty()) {
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);