Bug 8153: enforce check-style rules for netconf
[netconf.git] / netconf / tools / netconf-cli / src / main / java / org / opendaylight / 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.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     private 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,
50                                         final boolean removeConfigFalseNodes) {
51         final Set<DataSchemaNode> keys = new HashSet<>();
52         final Set<DataSchemaNode> mandatoryNotKeys = new HashSet<>();
53         final Set<DataSchemaNode> others = new HashSet<>();
54
55         List<QName> keyQNames = Collections.emptyList();
56         if (dataNodeContainer instanceof ListSchemaNode) {
57             keyQNames = ((ListSchemaNode) dataNodeContainer).getKeyDefinition();
58         }
59
60         for (final DataSchemaNode dataSchemaNode : dataNodeContainer.getChildNodes()) {
61             if (removeConfigFalseNodes) {
62                 if (!dataSchemaNode.isConfiguration()) {
63                     continue;
64                 }
65             }
66             if (keyQNames.contains(dataSchemaNode.getQName())) {
67                 Preconditions.checkArgument(dataSchemaNode instanceof LeafSchemaNode);
68                 keys.add(dataSchemaNode);
69             } else if (dataSchemaNode.getConstraints().isMandatory()) {
70                 mandatoryNotKeys.add(dataSchemaNode);
71             } else {
72                 others.add(dataSchemaNode);
73             }
74         }
75
76         return new SeparatedNodes(keys, mandatoryNotKeys, others);
77     }
78 }