Added tests for yang.model.util
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / helpers / SchemaContextUtils.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.yangtools.yang.data.codec.gson.helpers;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Function;
12 import com.google.common.base.Objects;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Predicate;
15 import com.google.common.collect.Iterables;
16
17 import java.net.URI;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 /**
35  * This class is implementation-internal and subject to change. Please do not use it.
36  */
37 @Beta
38 public final class SchemaContextUtils {
39     private final SchemaContext schemaContext;
40
41     private SchemaContextUtils(final SchemaContext schemaContext) {
42         this.schemaContext = Preconditions.checkNotNull(schemaContext);
43     }
44
45     public static SchemaContextUtils create(final SchemaContext schemaContext) {
46         return new SchemaContextUtils(schemaContext);
47     }
48
49     public URI findNamespaceByModuleName(final String moduleName) {
50         final Module module = this.findModuleByName(moduleName);
51         return module == null ? null : module.getNamespace();
52     }
53
54
55     public Module findModuleByName(final String moduleName) {
56         checkPreconditions();
57         Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
58         return schemaContext.findModuleByName(moduleName, null);
59     }
60
61     public Module findModuleByNamespace(final URI namespace) {
62         this.checkPreconditions();
63         Preconditions.checkArgument(namespace != null);
64         return schemaContext.findModuleByNamespaceAndRevision(namespace, null);
65     }
66
67     private void checkPreconditions() {
68         if (schemaContext == null) {
69             throw new IllegalStateException("Schema context isn't set.");
70         }
71     }
72
73     public DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name,
74             final URI namespace) {
75         Preconditions.<URI> checkNotNull(namespace);
76
77         final List<DataSchemaNode> potentialSchemaNodes = findInstanceDataChildrenByName(container, name);
78
79         Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
80             @Override
81             public boolean apply(final DataSchemaNode node) {
82                 return Objects.equal(node.getQName().getNamespace(), namespace);
83             }
84         };
85
86         Iterable<DataSchemaNode> result = Iterables.filter(potentialSchemaNodes, filter);
87         return Iterables.getFirst(result, null);
88     }
89
90     public List<DataSchemaNode> findInstanceDataChildrenByName(final DataNodeContainer container, final String name) {
91         Preconditions.<DataNodeContainer> checkNotNull(container);
92         Preconditions.<String> checkNotNull(name);
93
94         List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<DataSchemaNode>();
95         collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name);
96         return instantiatedDataNodeContainers;
97     }
98
99     private void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
100             final DataNodeContainer container, final String name) {
101
102         Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
103             @Override
104             public boolean apply(final DataSchemaNode node) {
105                 return Objects.equal(node.getQName().getLocalName(), name);
106             }
107         };
108
109         Iterable<DataSchemaNode> nodes = Iterables.filter(container.getChildNodes(), filter);
110
111         // Can't combine this loop with the filter above because the filter is
112         // lazily-applied by Iterables.filter.
113         for (final DataSchemaNode potentialNode : nodes) {
114             if (isInstantiatedDataSchema(potentialNode)) {
115                 potentialSchemaNodes.add(potentialNode);
116             }
117         }
118
119         Iterable<ChoiceNode> choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceNode.class);
120         Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION);
121
122         final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat(map);
123         for (final ChoiceCaseNode caze : allCases) {
124             collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name);
125         }
126     }
127
128     public boolean isInstantiatedDataSchema(final DataSchemaNode node) {
129         return node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode
130                 || node instanceof ContainerSchemaNode || node instanceof ListSchemaNode
131                 || node instanceof AnyXmlSchemaNode;
132     }
133
134     private final Function<ChoiceNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
135         @Override
136         public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
137             return node.getCases();
138         }
139     };
140
141 }