import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
-import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
public final class SchemaUtils {
private SchemaUtils() {
- throw new UnsupportedOperationException();
+ // Hidden on purpose
}
/**
* @return schema node with newest revision or absent if no schema node with matching qname is found
*/
public static Optional<DataSchemaNode> findFirstSchema(final QName qname,
- final Iterable<DataSchemaNode> dataSchemaNode) {
+ final Iterable<? extends DataSchemaNode> dataSchemaNode) {
DataSchemaNode schema = null;
if (dataSchemaNode != null && qname != null) {
for (final DataSchemaNode dsn : dataSchemaNode) {
schema = dsn;
}
} else if (dsn instanceof ChoiceSchemaNode) {
- for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases().values()) {
-
- final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
- if (dataChildByName != null) {
- return Optional.of(dataChildByName);
+ for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
+ final Optional<DataSchemaNode> dataChildByName = choiceCase.findDataChildByName(qname);
+ if (dataChildByName.isPresent()) {
+ return dataChildByName;
}
final Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
if (foundDsn.isPresent()) {
public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
// Try to find child schema node directly, but use a fallback that compares QNames without revisions
// and auto-expands choices
- final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
- return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
+ final Optional<DataSchemaNode> dataChildByName = schema.findDataChildByName(qname);
+ return dataChildByName.isPresent() ? dataChildByName.get()
+ : findSchemaForChild(schema, qname, schema.getChildNodes());
}
- @Nullable
- public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
+ public static @Nullable DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
final boolean strictMode) {
if (strictMode) {
return findSchemaForChild(schema, qname);
}
public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
- final Iterable<DataSchemaNode> childNodes) {
+ final Iterable<? extends DataSchemaNode> childNodes) {
final Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
checkState(childSchema.isPresent(), "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname,
schema);
}
public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
- for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
final Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName,
choiceCaseNode.getChildNodes());
if (childSchema.isPresent()) {
}
public static AugmentationSchemaNode findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qnames) {
- for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
final Optional<AugmentationSchemaNode> schemaForAugment = findAugment(choiceCaseNode, qnames);
if (schemaForAugment.isPresent()) {
return schemaForAugment.get();
final Set<QName> qnames) {
for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
final Set<QName> qNamesFromAugment = ImmutableSet.copyOf(Collections2.transform(augment.getChildNodes(),
- DataSchemaNode::getQName));
-
- if (qNamesFromAugment.equals(qnames)) {
+ DataSchemaNode::getQName));
+ if (qnames.equals(qNamesFromAugment)) {
return Optional.of(augment);
}
}
}
private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema,
- final Iterable<DataSchemaNode> childNodes) {
+ final Iterable<? extends DataSchemaNode> childNodes) {
final Map<QName, ChoiceSchemaNode> mappedChoices = new LinkedHashMap<>();
for (final DataSchemaNode childSchema : childNodes) {
continue;
}
- for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
for (final QName qname : getChildNodesRecursive(choiceCaseNode)) {
mappedChoices.put(qname, (ChoiceSchemaNode) childSchema);
}
}
for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) schema).getAvailableAugmentations()) {
- if (augmentation.getDataChildByName(childSchema.getQName()) != null) {
+ if (augmentation.findDataChildByName(childSchema.getQName()).isPresent()) {
return true;
}
}
childNodesToAugmentation.put(qname, mostTopAugmentation);
}
} else if (child instanceof ChoiceSchemaNode) {
- for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
for (final QName qname : getChildNodesRecursive(choiceCaseNode)) {
childNodesToAugmentation.put(qname, mostTopAugmentation);
}
// Choice Node has to map child nodes from all its cases
if (schema instanceof ChoiceSchemaNode) {
- for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
if (!augments.containsKey(choiceCaseNode.getQName())) {
continue;
}
for (final DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
if (childSchema instanceof ChoiceSchemaNode) {
- for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
}
} else if (childSchema instanceof AugmentationSchemaNode || childSchema instanceof CaseSchemaNode) {
public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema,
final AugmentationSchemaNode augmentSchema) {
if (!targetSchema.getAvailableAugmentations().contains(augmentSchema)) {
- return Collections.emptySet();
+ return ImmutableSet.of();
}
if (targetSchema instanceof DataNodeContainer) {
return getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
final Set<DataSchemaNode> realChildNodes = new HashSet<>();
if (targetSchema instanceof ChoiceSchemaNode) {
for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
- for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
}
public static Optional<CaseSchemaNode> detectCase(final ChoiceSchemaNode schema,
final DataContainerChild<?, ?> child) {
- for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
+ for (final CaseSchemaNode choiceCaseNode : schema.getCases()) {
if (child instanceof AugmentationNode
&& belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) {
return Optional.of(choiceCaseNode);
- } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
+ } else if (choiceCaseNode.findDataChildByName(child.getNodeType()).isPresent()) {
return Optional.of(choiceCaseNode);
}
}
}
for (final AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
- final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
- if (childInAugmentation != null) {
+ final Optional<DataSchemaNode> childInAugmentation = augmentation.findDataChildByName(child.getQName());
+ if (childInAugmentation.isPresent()) {
return augmentation;
}
}
return null;
}
- /**
- * Create AugmentationIdentifier from an AugmentationSchemaNode.
- *
- * @param schema Augmentation schema
- * @return AugmentationIdentifier for the schema
- * @throws NullPointerException if {@code schema} is null
- * @deprecated Use {@link DataSchemaContextNode#augmentationIdentifierFrom(AugmentationSchemaNode)} instead.
- */
- @Deprecated
- public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchemaNode schema) {
- return DataSchemaContextNode.augmentationIdentifierFrom(schema);
- }
-
/**
* Finds schema node for given path in schema context. This method performs
* lookup in the namespace of all leafs, leaf-lists, lists, containers,
* @throws IllegalArgumentException
* if the schema node does not allow children
*/
- @Nullable
- public static SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) {
+ public static @Nullable SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) {
if (node instanceof DataNodeContainer) {
- SchemaNode child = ((DataNodeContainer) node).getDataChildByName(qname);
+ SchemaNode child = ((DataNodeContainer) node).dataChildByName(qname);
if (child == null && node instanceof SchemaContext) {
child = tryFind(((SchemaContext) node).getOperations(), qname).orElse(null);
}
return child;
}
if (node instanceof ChoiceSchemaNode) {
- return ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
+ return ((ChoiceSchemaNode) node).findCase(qname).orElse(null);
}
- if (node instanceof RpcDefinition) {
+ if (node instanceof OperationDefinition) {
switch (qname.getLocalName()) {
case "input":
- return ((RpcDefinition) node).getInput();
+ return ((OperationDefinition) node).getInput();
case "output":
- return ((RpcDefinition) node).getOutput();
+ return ((OperationDefinition) node).getOutput();
default:
return null;
}
*/
public static Collection<SchemaNode> findParentSchemaNodesOnPath(final SchemaContext schemaContext,
final SchemaPath path) {
+ return findParentSchemaNodesOnPath(schemaContext, path.getPathFromRoot());
+ }
+
+ /**
+ * Finds schema node for given path in schema context. This method performs lookup in both the namespace
+ * of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions,
+ * notifications, anydatas and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
+ *
+ * <p>
+ * This method returns collection of SchemaNodes, because name conflicts can occur between the namespace
+ * of groupings and namespace of data nodes. This method finds and collects all schema nodes that matches supplied
+ * SchemaPath and returns them all as collection of schema nodes.
+ *
+ * @param schemaContext schema context
+ * @param path path
+ * @return collection of schema nodes on path
+ */
+ public static Collection<SchemaNode> findParentSchemaNodesOnPath(final SchemaContext schemaContext,
+ final Iterable<QName> path) {
final Collection<SchemaNode> currentNodes = new ArrayList<>();
final Collection<SchemaNode> childNodes = new ArrayList<>();
currentNodes.add(requireNonNull(schemaContext));
- for (final QName qname : path.getPathFromRoot()) {
+ for (final QName qname : path) {
for (final SchemaNode current : currentNodes) {
childNodes.addAll(findChildSchemaNodesByQName(current, qname));
}