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