Bug 5528 - Impl Post data
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / 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.restful.utils;
9
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;
23
24 /**
25  * Util class for common methods of transactions
26  *
27  */
28 public final class TransactionUtil {
29
30     private TransactionUtil() {
31         throw new UnsupportedOperationException("Util class");
32     }
33
34     /**
35      * Merged parents of data
36      *
37      * @param path
38      *            - path of data
39      * @param schemaContext
40      *            - {@link SchemaContext}
41      * @param writeTx
42      *            - write transaction
43      */
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;
49
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());
54
55         while (it.hasNext()) {
56             final PathArgument pathArgument = it.next();
57             if (rootNormalizedPath == null) {
58                 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
59             }
60             if (it.hasNext()) {
61                 normalizedPathWithoutChildArgs.add(pathArgument);
62                 if (module.getDataChildByName(pathArgument.getNodeType()) instanceof ListSchemaNode) {
63                     hasList = true;
64                 }
65             }
66         }
67         if (normalizedPathWithoutChildArgs.isEmpty()) {
68             return;
69         }
70         if (hasList) {
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);
75         }
76     }
77 }