*/
package org.opendaylight.yangtools.yang.data.impl.schema;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
schema = dsn;
}
} else if (dsn instanceof ChoiceSchemaNode) {
- for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
+ for (final CaseSchemaNode choiceCase : ((ChoiceSchemaNode) dsn).getCases().values()) {
final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
if (dataChildByName != null) {
public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
final Iterable<DataSchemaNode> childNodes) {
final Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
- Preconditions.checkState(childSchema.isPresent(),
- "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
+ checkState(childSchema.isPresent(), "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname,
+ schema);
return childSchema.get();
}
public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
- for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
final Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName,
choiceCaseNode.getChildNodes());
if (childSchema.isPresent()) {
public static AugmentationSchemaNode findSchemaForAugment(final AugmentationTarget schema,
final Set<QName> qnames) {
final Optional<AugmentationSchemaNode> schemaForAugment = findAugment(schema, qnames);
- Preconditions.checkState(schemaForAugment.isPresent(),
- "Unknown augmentation node detected, identified by: %s, in: %s", qnames, schema);
+ checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
+ qnames, schema);
return schemaForAugment.get();
}
public static AugmentationSchemaNode findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qnames) {
- Optional<AugmentationSchemaNode> schemaForAugment = Optional.empty();
-
- for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
- schemaForAugment = findAugment(choiceCaseNode, qnames);
+ for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
+ final Optional<AugmentationSchemaNode> schemaForAugment = findAugment(choiceCaseNode, qnames);
if (schemaForAugment.isPresent()) {
- break;
+ return schemaForAugment.get();
}
}
- Preconditions.checkState(schemaForAugment.isPresent(),
- "Unknown augmentation node detected, identified by: %s, in: %s", qnames, schema);
- return schemaForAugment.get();
+ throw new IllegalStateException(String.format("Unknown augmentation node detected, identified by: %s, in: %s",
+ qnames, schema));
}
private static Optional<AugmentationSchemaNode> findAugment(final AugmentationTarget schema,
continue;
}
- for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) {
for (final QName qname : getChildNodesRecursive(choiceCaseNode)) {
mappedChoices.put(qname, (ChoiceSchemaNode) childSchema);
}
final AugmentationSchemaNode mostTopAugmentation = augments.get(child.getQName());
// recursively add all child nodes in case of augment, case and choice
- if (child instanceof AugmentationSchemaNode || child instanceof ChoiceCaseNode) {
+ if (child instanceof AugmentationSchemaNode || child instanceof CaseSchemaNode) {
for (final QName qname : getChildNodesRecursive((DataNodeContainer) child)) {
childNodesToAugmentation.put(qname, mostTopAugmentation);
}
} else if (child instanceof ChoiceSchemaNode) {
- for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases().values()) {
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 ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases().values()) {
if (!augments.containsKey(choiceCaseNode.getQName())) {
continue;
}
for (final DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
if (childSchema instanceof ChoiceSchemaNode) {
- for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases().values()) {
allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
}
- } else if (childSchema instanceof AugmentationSchemaNode || childSchema instanceof ChoiceCaseNode) {
+ } else if (childSchema instanceof AugmentationSchemaNode || childSchema instanceof CaseSchemaNode) {
allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
} else {
allChildNodes.add(childSchema.getQName());
final Set<DataSchemaNode> realChildNodes = new HashSet<>();
if (targetSchema instanceof ChoiceSchemaNode) {
for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
- for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases().values()) {
if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
}
return realChildNodes;
}
- public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema,
+ public static Optional<CaseSchemaNode> detectCase(final ChoiceSchemaNode schema,
final DataContainerChild<?, ?> child) {
- for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
+ for (final CaseSchemaNode choiceCaseNode : schema.getCases().values()) {
if (child instanceof AugmentationNode
&& belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) {
return Optional.of(choiceCaseNode);
return Optional.empty();
}
- public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode,
+ public static boolean belongsToCaseAugment(final CaseSchemaNode caseNode,
final AugmentationIdentifier childToProcess) {
for (final AugmentationSchemaNode augmentationSchema : caseNode.getAvailableAugmentations()) {
}
public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchemaNode schema) {
- final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), DataSchemaNode::getQName);
- return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
+ return new AugmentationIdentifier(ImmutableSet.copyOf(Collections2.transform(schema.getChildNodes(),
+ DataSchemaNode::getQName)));
}
/**
* @return schema node on path
*/
public static SchemaNode findDataParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
- SchemaNode current = Preconditions.checkNotNull(schemaContext);
+ SchemaNode current = requireNonNull(schemaContext);
for (final QName qname : path.getPathFromRoot()) {
current = findDataChildSchemaByQName(current, qname);
}
return current;
}
- /**
- * 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 is deprecated, because name conflicts can occur between the namespace of groupings and namespace
- * of data nodes and in consequence lookup could be ambiguous.
- *
- * @param schemaContext
- * schema context
- * @param path
- * path
- * @return schema node on path
- *
- * @deprecated Use {@link #findParentSchemaNodesOnPath(SchemaContext, SchemaPath)} instead.
- */
- @Deprecated
- public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
- SchemaNode current = Preconditions.checkNotNull(schemaContext);
- for (final QName qname : path.getPathFromRoot()) {
- current = findChildSchemaByQName(current, qname);
- }
- return current;
- }
-
/**
* Find child data schema node identified by its QName within a provided schema node. This method performs lookup
* in the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions, notifications, anydatas
- * and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
+ * and anyxmls according to RFC6050/RFC7950 section 6.2.1.
*
* @param node
* schema node
*/
@Nullable
public static SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) {
- SchemaNode child = null;
if (node instanceof DataNodeContainer) {
- child = ((DataNodeContainer) node).getDataChildByName(qname);
+ SchemaNode child = ((DataNodeContainer) node).getDataChildByName(qname);
if (child == null && node instanceof SchemaContext) {
- child = tryFindRpc((SchemaContext) node, qname).orElse(null);
+ child = tryFind(((SchemaContext) node).getOperations(), qname).orElse(null);
}
if (child == null && node instanceof NotificationNodeContainer) {
- child = tryFindNotification((NotificationNodeContainer) node, qname).orElse(null);
+ child = tryFind(((NotificationNodeContainer) node).getNotifications(), qname).orElse(null);
}
if (child == null && node instanceof ActionNodeContainer) {
- child = tryFindAction((ActionNodeContainer) node, qname).orElse(null);
+ child = tryFind(((ActionNodeContainer) node).getActions(), qname).orElse(null);
}
- } else if (node instanceof ChoiceSchemaNode) {
- child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
- } else if (node instanceof RpcDefinition) {
+
+ return child;
+ }
+ if (node instanceof ChoiceSchemaNode) {
+ return ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
+ }
+ if (node instanceof RpcDefinition) {
switch (qname.getLocalName()) {
case "input":
- child = ((RpcDefinition) node).getInput();
- break;
+ return ((RpcDefinition) node).getInput();
case "output":
- child = ((RpcDefinition) node).getOutput();
- break;
+ return ((RpcDefinition) node).getOutput();
default:
- child = null;
- break;
+ return null;
}
- } else {
- throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
- }
-
- return child;
- }
-
- /**
- * Find child schema node identified by its QName within a provided schema node. 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 is deprecated, because name conflicts can occur between the namespace of groupings and namespace
- * of data nodes and in consequence lookup could be ambiguous.
- *
- * @param node
- * schema node
- * @param qname
- * QName
- * @return child schema node
- * @throws IllegalArgumentException
- * if the schema node does not allow children
- *
- * @deprecated Use {@link #findChildSchemaNodesByQName(SchemaNode, QName)} instead.
- */
- @Deprecated
- public static SchemaNode findChildSchemaByQName(final SchemaNode node, final QName qname) {
- SchemaNode child = findDataChildSchemaByQName(node, qname);
- if (child == null && node instanceof DataNodeContainer) {
- child = tryFindGroupings((DataNodeContainer) node, qname).orElse(null);
}
- return child;
+ throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
}
/**
final SchemaPath path) {
final Collection<SchemaNode> currentNodes = new ArrayList<>();
final Collection<SchemaNode> childNodes = new ArrayList<>();
- currentNodes.add(Preconditions.checkNotNull(schemaContext));
+ currentNodes.add(requireNonNull(schemaContext));
for (final QName qname : path.getPathFromRoot()) {
for (final SchemaNode current : currentNodes) {
childNodes.addAll(findChildSchemaNodesByQName(current, qname));
childNodes.add(dataNode);
}
if (node instanceof DataNodeContainer) {
- tryFindGroupings((DataNodeContainer) node, qname).ifPresent(childNodes::add);
+ tryFind(((DataNodeContainer) node).getGroupings(), qname).ifPresent(childNodes::add);
}
- return childNodes.isEmpty() ? Collections.emptyList() : ImmutableList.copyOf(childNodes);
- }
-
- private static Optional<SchemaNode> tryFindGroupings(final DataNodeContainer dataNodeContainer, final QName qname) {
- return Optional
- .ofNullable(Iterables.find(dataNodeContainer.getGroupings(), new SchemaNodePredicate(qname), null));
- }
-
- private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
- return Optional.ofNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
- }
-
- private static Optional<SchemaNode> tryFindNotification(final NotificationNodeContainer notificationContanier,
- final QName qname) {
- return Optional.ofNullable(
- Iterables.find(notificationContanier.getNotifications(), new SchemaNodePredicate(qname), null));
+ return childNodes.isEmpty() ? ImmutableList.of() : ImmutableList.copyOf(childNodes);
}
- private static Optional<SchemaNode> tryFindAction(final ActionNodeContainer actionContanier, final QName qname) {
- return Optional.ofNullable(Iterables.find(actionContanier.getActions(), new SchemaNodePredicate(qname), null));
- }
-
- private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
- private final QName qname;
-
- SchemaNodePredicate(final QName qname) {
- this.qname = qname;
- }
-
- @Override
- public boolean apply(final SchemaNode input) {
- return input.getQName().equals(qname);
- }
+ private static <T extends SchemaNode> Optional<T> tryFind(final Collection<T> nodes, final QName qname) {
+ return nodes.stream().filter(node -> qname.equals(node.getQName())).findFirst();
}
}