2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.gson.helpers;
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;
18 import java.util.ArrayList;
19 import java.util.List;
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;
35 * This class is implementation-internal and subject to change. Please do not use it.
38 public final class SchemaContextUtils {
39 private final SchemaContext schemaContext;
41 private SchemaContextUtils(final SchemaContext schemaContext) {
42 this.schemaContext = Preconditions.checkNotNull(schemaContext);
45 public static SchemaContextUtils create(final SchemaContext schemaContext) {
46 return new SchemaContextUtils(schemaContext);
49 public URI findNamespaceByModuleName(final String moduleName) {
50 final Module module = this.findModuleByName(moduleName);
51 return module == null ? null : module.getNamespace();
55 public Module findModuleByName(final String moduleName) {
57 Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
58 return schemaContext.findModuleByName(moduleName, null);
61 public Module findModuleByNamespace(final URI namespace) {
62 this.checkPreconditions();
63 Preconditions.checkArgument(namespace != null);
64 return schemaContext.findModuleByNamespaceAndRevision(namespace, null);
67 private void checkPreconditions() {
68 if (schemaContext == null) {
69 throw new IllegalStateException("Schema context isn't set.");
73 public DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name,
74 final URI namespace) {
75 Preconditions.<URI> checkNotNull(namespace);
77 final List<DataSchemaNode> potentialSchemaNodes = findInstanceDataChildrenByName(container, name);
79 Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
81 public boolean apply(final DataSchemaNode node) {
82 return Objects.equal(node.getQName().getNamespace(), namespace);
86 Iterable<DataSchemaNode> result = Iterables.filter(potentialSchemaNodes, filter);
87 return Iterables.getFirst(result, null);
90 public List<DataSchemaNode> findInstanceDataChildrenByName(final DataNodeContainer container, final String name) {
91 Preconditions.<DataNodeContainer> checkNotNull(container);
92 Preconditions.<String> checkNotNull(name);
94 List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<DataSchemaNode>();
95 collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name);
96 return instantiatedDataNodeContainers;
99 private void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
100 final DataNodeContainer container, final String name) {
102 Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
104 public boolean apply(final DataSchemaNode node) {
105 return Objects.equal(node.getQName().getLocalName(), name);
109 Iterable<DataSchemaNode> nodes = Iterables.filter(container.getChildNodes(), filter);
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);
119 Iterable<ChoiceNode> choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceNode.class);
120 Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION);
122 final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat(map);
123 for (final ChoiceCaseNode caze : allCases) {
124 collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name);
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;
134 private final Function<ChoiceNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
136 public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
137 return node.getCases();