Use DepthParameter in QueryParameters
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / TransactionUtil.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.nb.rfc8040.rests.utils;
9
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19
20 /**
21  * Util class for common methods of transactions.
22  */
23 public final class TransactionUtil {
24     private TransactionUtil() {
25         // Hidden on purpose
26     }
27
28     /**
29      * Merged parents of data.
30      *
31      * @param path          path of data
32      * @param schemaContext {@link SchemaContext}
33      * @param transaction   A handle to a set of DS operations
34      */
35     // FIXME: this method should only be invoked in MdsalRestconfStrategy, and even then only if we are crossing
36     //        an implicit list.
37     public static void ensureParentsByMerge(final YangInstanceIdentifier path, final SchemaContext schemaContext,
38                                             final RestconfTransaction transaction) {
39         final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
40         YangInstanceIdentifier rootNormalizedPath = null;
41
42         final Iterator<PathArgument> it = path.getPathArguments().iterator();
43
44         while (it.hasNext()) {
45             final PathArgument pathArgument = it.next();
46             if (rootNormalizedPath == null) {
47                 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
48             }
49
50             if (it.hasNext()) {
51                 normalizedPathWithoutChildArgs.add(pathArgument);
52             }
53         }
54
55         if (normalizedPathWithoutChildArgs.isEmpty()) {
56             return;
57         }
58
59         final NormalizedNode parentStructure = ImmutableNodes.fromInstanceId(schemaContext,
60                 YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
61         transaction.merge(rootNormalizedPath, parentStructure);
62     }
63 }