import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+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.model.api.AugmentationSchema;
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+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 static final Function<DataSchemaNode, QName> QNAME_FUNCTION = new Function<DataSchemaNode, QName>() {
+ @Override
+ public QName apply(@Nonnull final DataSchemaNode input) {
+ return input.getQName();
+ }
+ };
+
private SchemaUtils() {
throw new UnsupportedOperationException();
}
* @param dataSchemaNode - iterable of schemaNodes to look through
* @return - schema node with newest revision or absent if no schema node with matching qname is found
*/
- public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+ public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
DataSchemaNode sNode = null;
if (dataSchemaNode != null && qname != null) {
for (DataSchemaNode dsn : dataSchemaNode) {
for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
schemaForAugment = findAugment(choiceCaseNode, qNames);
- if(schemaForAugment.isPresent()) {
+ if (schemaForAugment.isPresent()) {
break;
}
}
HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
@Override
- public QName apply(final @Nonnull DataSchemaNode input) {
+ public QName apply(@Nonnull final DataSchemaNode input) {
Preconditions.checkNotNull(input);
return input.getQName();
}
}));
- if(qNamesFromAugment.equals(qNames)) {
+ if (qNamesFromAugment.equals(qNames)) {
return Optional.of(augment);
}
}
/**
* Recursively find all child nodes that come from choices.
*
+ * @param schema schema
* @return Map with all child nodes, to their most top augmentation
*/
public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
}
for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
- if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
+ if (augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
return true;
}
}
/**
* Recursively find all child nodes that come from augmentations.
*
+ * @param schema schema
* @return Map with all child nodes, to their most top augmentation
*/
public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
* Recursively list all child nodes.
*
* In case of choice, augment and cases, step in.
+ *
+ * @param nodeContainer node container
+ * @return set of QNames
*/
public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
Set<QName> allChildNodes = Sets.newHashSet();
for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
- if(childSchema instanceof ChoiceSchemaNode) {
+ if (childSchema instanceof ChoiceSchemaNode) {
for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
}
- } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
+ } else if (childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
}
else {
* Schema of the same child node from augment, and directly from target is not the same.
* Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
*
+ * @param targetSchema target schema
+ * @param augmentSchema augment schema
+ * @return set of nodes
*/
public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
- if(targetSchema instanceof DataNodeContainer) {
+ if (targetSchema instanceof DataNodeContainer) {
realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
- } else if(targetSchema instanceof ChoiceSchemaNode) {
+ } else if (targetSchema instanceof ChoiceSchemaNode) {
for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
for (ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
- if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
+ if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
}
}
public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
if (child instanceof AugmentationNode
- && belongsToCaseAugment(choiceCaseNode,
- (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
+ && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) {
return Optional.of(choiceCaseNode);
} else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
return Optional.of(choiceCaseNode);
return Optional.absent();
}
- public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
+ public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final AugmentationIdentifier childToProcess) {
for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
Set<QName> currentAugmentChildNodes = Sets.newHashSet();
currentAugmentChildNodes.add(dataSchemaNode.getQName());
}
- if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
+ if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
return true;
}
}
return false;
}
- public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
- return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
+ /**
+ * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
+ * node is found then it is returned, else null.
+ *
+ * @param parent parent node
+ * @param child child node
+ * @return augmentation schema
+ */
+ public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
+ if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
+ for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
+ DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
+ if (childInAugmentation != null) {
+ return augmentation;
+ }
+ }
+ }
+ return null;
}
- public static Set<QName> getChildQNames(final AugmentationSchema schema) {
- Set<QName> qnames = Sets.newHashSet();
+ public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
+ final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION);
+ return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
+ }
- for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
- qnames.add(dataSchemaNode.getQName());
+ /**
+ * Finds schema node for given path in schema context.
+ * @param schemaContext schema context
+ * @param path path
+ * @return schema node on path
+ */
+ 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;
+ }
- return qnames;
+ /**
+ * Find child schema node identified by its QName within a provided schema node.
+ * @param node schema node
+ * @param qname QName
+ * @return child schema node
+ * @throws java.lang.IllegalArgumentException if the schema node does not allow children
+ */
+ public static SchemaNode findChildSchemaByQName(final SchemaNode node, final QName qname) {
+ SchemaNode child = null;
+
+ if (node instanceof DataNodeContainer) {
+ child = ((DataNodeContainer) node).getDataChildByName(qname);
+
+ if (child == null && node instanceof SchemaContext) {
+ child = tryFindGroupings((SchemaContext) node, qname).orNull();
+ }
+
+ if (child == null && node instanceof SchemaContext) {
+ child = tryFindNotification((SchemaContext) node, qname)
+ .or(tryFindRpc(((SchemaContext) node), qname)).orNull();
+ }
+ } else if (node instanceof ChoiceSchemaNode) {
+ child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
+ } else if (node instanceof RpcDefinition) {
+ switch (qname.getLocalName()) {
+ case "input":
+ child = ((RpcDefinition) node).getInput();
+ break;
+ case "output":
+ child = ((RpcDefinition) node).getOutput();
+ break;
+ default:
+ child = null;
+ break;
+ }
+ } else {
+ throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
+ }
+
+ return child;
+ }
+
+ private static Optional<SchemaNode> tryFindGroupings(final SchemaContext ctx, final QName qname) {
+ return Optional.fromNullable(Iterables.find(ctx.getGroupings(), new SchemaNodePredicate(qname), null));
+ }
+
+ private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
+ return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
}
+
+ private static Optional<SchemaNode> tryFindNotification(final SchemaContext ctx, final QName qname) {
+ return Optional.fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null));
+ }
+
+ private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
+ private final QName qname;
+
+ public SchemaNodePredicate(final QName qname) {
+ this.qname = qname;
+ }
+
+ @Override
+ public boolean apply(final SchemaNode input) {
+ return input.getQName().equals(qname);
+ }
+ }
+
}