Merge "Distributed Datastore integration with config subsystem Updated with the usage...
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / reader / impl / SeparatedNodes.java
1 /*
2  * Copyright (c) 2014 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.controller.netconf.cli.reader.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20
21 class SeparatedNodes {
22     private final Set<DataSchemaNode> keyNodes;
23     private final Set<DataSchemaNode> mandatoryNotKey;
24     private final Set<DataSchemaNode> otherNodes;
25
26     public SeparatedNodes(final Set<DataSchemaNode> keyNodes, final Set<DataSchemaNode> mandatoryNotKey,
27             final Set<DataSchemaNode> otherNodes) {
28         this.keyNodes = keyNodes;
29         this.mandatoryNotKey = mandatoryNotKey;
30         this.otherNodes = otherNodes;
31     }
32
33     public Set<DataSchemaNode> getKeyNodes() {
34         return keyNodes;
35     }
36
37     public Set<DataSchemaNode> getMandatoryNotKey() {
38         return mandatoryNotKey;
39     }
40
41     public Set<DataSchemaNode> getOthers() {
42         return otherNodes;
43     }
44
45     static SeparatedNodes separateNodes(final DataNodeContainer dataNodeContainer) {
46         return separateNodes(dataNodeContainer, false);
47     }
48
49     static SeparatedNodes separateNodes(final DataNodeContainer dataNodeContainer, final boolean removeConfigFalseNodes) {
50         final Set<DataSchemaNode> keys = new HashSet<>();
51         final Set<DataSchemaNode> mandatoryNotKeys = new HashSet<>();
52         final Set<DataSchemaNode> others = new HashSet<>();
53
54         List<QName> keyQNames = Collections.emptyList();
55         if (dataNodeContainer instanceof ListSchemaNode) {
56             keyQNames = ((ListSchemaNode) dataNodeContainer).getKeyDefinition();
57         }
58
59         for (final DataSchemaNode dataSchemaNode : dataNodeContainer.getChildNodes()) {
60             if (removeConfigFalseNodes) {
61                 if (!dataSchemaNode.isConfiguration()) {
62                     continue;
63                 }
64             }
65             if (keyQNames.contains(dataSchemaNode.getQName())) {
66                 Preconditions.checkArgument(dataSchemaNode instanceof LeafSchemaNode);
67                 keys.add(dataSchemaNode);
68             } else if (dataSchemaNode.getConstraints().isMandatory()) {
69                 mandatoryNotKeys.add(dataSchemaNode);
70             } else {
71                 others.add(dataSchemaNode);
72             }
73         }
74
75         return new SeparatedNodes(keys, mandatoryNotKeys, others);
76     }
77 }