/** * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl.leafref; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; public final class LeafRefContextUtils { private LeafRefContextUtils() { throw new UnsupportedOperationException(); } public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node, final LeafRefContext root) { final SchemaPath schemaPath = node.getPath(); return getLeafRefReferencingContext(schemaPath, root); } public static LeafRefContext getLeafRefReferencingContext( final SchemaPath schemaPath, final LeafRefContext root) { final Iterable pathFromRoot = schemaPath.getPathFromRoot(); return getLeafRefReferencingContext(pathFromRoot, root); } public static LeafRefContext getLeafRefReferencingContext( final Iterable pathFromRoot, LeafRefContext root) { LeafRefContext leafRefCtx = null; final Iterator iterator = pathFromRoot.iterator(); while (iterator.hasNext() && root != null) { final QName qname = iterator.next(); leafRefCtx = root.getReferencingChildByName(qname); if (iterator.hasNext()) { root = leafRefCtx; } } return leafRefCtx; } public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node, final LeafRefContext root) { final SchemaPath schemaPath = node.getPath(); return getLeafRefReferencedByContext(schemaPath, root); } public static LeafRefContext getLeafRefReferencedByContext( final SchemaPath schemaPath, final LeafRefContext root) { final Iterable pathFromRoot = schemaPath.getPathFromRoot(); return getLeafRefReferencedByContext(pathFromRoot, root); } public static LeafRefContext getLeafRefReferencedByContext( final Iterable pathFromRoot, LeafRefContext root) { LeafRefContext leafRefCtx = null; final Iterator iterator = pathFromRoot.iterator(); while (iterator.hasNext() && root != null) { final QName qname = iterator.next(); leafRefCtx = root.getReferencedChildByName(qname); if (iterator.hasNext()) { root = leafRefCtx; } } return leafRefCtx; } public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) { if (node == null || root == null) { return false; } final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext( node, root); if (leafRefReferencingContext == null) { return false; } return leafRefReferencingContext.isReferencing(); } public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) { if (node == null || root == null) { return false; } final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext( node, root); if (leafRefReferencingContext == null) { return false; } return leafRefReferencingContext.hasReferencingChild(); } public static boolean isReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) { if (node == null || root == null) { return false; } final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext( node, root); if (leafRefReferencedByContext == null) { return false; } return leafRefReferencedByContext.isReferenced(); } public static boolean hasChildReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) { if ((node == null) || (root == null)) { return false; } final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext( node, root); if (leafRefReferencedByContext == null) { return false; } return leafRefReferencedByContext.hasReferencedChild(); } public static List findAllLeafRefChilds(final SchemaNode node, final LeafRefContext root) { return findAllLeafRefChilds(node.getPath(), root); } public static List findAllLeafRefChilds( final SchemaPath schemaPath, final LeafRefContext root) { return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root); } public static List findAllLeafRefChilds( final Iterable pathFromRoot, final LeafRefContext root) { final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext( pathFromRoot, root); final List allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext); return allLeafRefsChilds; } public static List findAllLeafRefChilds( final LeafRefContext parent) { final LinkedList leafRefChilds = new LinkedList(); if (parent == null) { return leafRefChilds; } if (parent.isReferencing()) { leafRefChilds.add(parent); return leafRefChilds; } else { final Set> childs = parent .getReferencingChilds().entrySet(); for (final Entry child : childs) { leafRefChilds.addAll(findAllLeafRefChilds(child.getValue())); } } return leafRefChilds; } public static List findAllChildsReferencedByLeafRef( final SchemaNode node, final LeafRefContext root) { return findAllChildsReferencedByLeafRef(node.getPath(), root); } public static List findAllChildsReferencedByLeafRef( final SchemaPath schemaPath, final LeafRefContext root) { return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(), root); } public static List findAllChildsReferencedByLeafRef( final Iterable pathFromRoot, final LeafRefContext root) { final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext( pathFromRoot, root); final List allChildsReferencedByLeafRef = findAllChildsReferencedByLeafRef(leafRefReferencedByContext); return allChildsReferencedByLeafRef; } public static List findAllChildsReferencedByLeafRef( final LeafRefContext parent) { final LinkedList childsReferencedByLeafRef = new LinkedList(); if (parent == null) { return childsReferencedByLeafRef; } if (parent.isReferenced()) { childsReferencedByLeafRef.add(parent); return childsReferencedByLeafRef; } else { final Set> childs = parent .getReferencedByChilds().entrySet(); for (final Entry child : childs) { childsReferencedByLeafRef .addAll(findAllChildsReferencedByLeafRef(child .getValue())); } } return childsReferencedByLeafRef; } public static Map getAllLeafRefsReferencingThisNode( final SchemaNode node, final LeafRefContext root) { return getAllLeafRefsReferencingThisNode(node.getPath(), root); } public static Map getAllLeafRefsReferencingThisNode( final SchemaPath path, final LeafRefContext root) { return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root); } public static Map getAllLeafRefsReferencingThisNode( final Iterable pathFromRoot, final LeafRefContext root) { final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext( pathFromRoot, root); if (leafRefReferencedByContext == null) { return new HashMap(); } return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs(); } }