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 java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 * Util class for common methods of transactions
28 public final class TransactionUtil {
30 private TransactionUtil() {
31 throw new UnsupportedOperationException("Util class");
35 * Merged parents of data
39 * @param schemaContext
40 * - {@link SchemaContext}
44 public static void ensureParentsByMerge(final YangInstanceIdentifier path, final SchemaContext schemaContext,
45 final DOMDataWriteTransaction writeTx) {
46 final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
47 boolean hasList = false;
48 YangInstanceIdentifier rootNormalizedPath = null;
50 final Iterator<PathArgument> it = path.getPathArguments().iterator();
51 final Module module = schemaContext.findModuleByNamespaceAndRevision(
52 path.getLastPathArgument().getNodeType().getModule().getNamespace(),
53 path.getLastPathArgument().getNodeType().getModule().getRevision());
55 while (it.hasNext()) {
56 final PathArgument pathArgument = it.next();
57 if (rootNormalizedPath == null) {
58 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
61 normalizedPathWithoutChildArgs.add(pathArgument);
62 if (module.getDataChildByName(pathArgument.getNodeType()) instanceof ListSchemaNode) {
67 if (normalizedPathWithoutChildArgs.isEmpty()) {
71 Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
72 final NormalizedNode<?, ?> parentStructure = ImmutableNodes.fromInstanceId(schemaContext,
73 YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
74 writeTx.merge(LogicalDatastoreType.CONFIGURATION, rootNormalizedPath, parentStructure);