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.nb.rfc8040.rests.utils;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 * Util class for common methods of transactions.
24 public final class TransactionUtil {
25 private TransactionUtil() {
26 throw new UnsupportedOperationException("Util class");
30 * Merged parents of data.
32 * @param path path of data
33 * @param schemaContext {@link SchemaContext}
34 * @param transaction A handle to a set of DS operations
36 // FIXME: this method should only be invoked in MdsalRestconfStrategy, and even then only if we are crossing
38 public static void ensureParentsByMerge(final YangInstanceIdentifier path, final SchemaContext schemaContext,
39 final RestconfTransaction transaction) {
40 final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
41 YangInstanceIdentifier rootNormalizedPath = null;
43 final Iterator<PathArgument> it = path.getPathArguments().iterator();
45 while (it.hasNext()) {
46 final PathArgument pathArgument = it.next();
47 if (rootNormalizedPath == null) {
48 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
52 normalizedPathWithoutChildArgs.add(pathArgument);
56 if (normalizedPathWithoutChildArgs.isEmpty()) {
60 final NormalizedNode parentStructure = ImmutableNodes.fromInstanceId(schemaContext,
61 YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
62 transaction.merge(LogicalDatastoreType.CONFIGURATION, rootNormalizedPath, parentStructure);