2 * Copyright (c) 2013 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.parser.builder.impl;
10 import com.google.common.base.Splitter;
12 import java.util.Comparator;
13 import java.util.Date;
16 import java.util.TreeMap;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.RefineBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
27 public final class GroupingUtils {
28 private static final Splitter SLASH_SPLITTER = Splitter.on('/');
30 private GroupingUtils() {
34 * Search given modules for grouping by name defined in uses node.
37 * builder of uses statement
42 * @return grouping with given name if found, null otherwise
44 public static GroupingBuilder getTargetGroupingFromModules(final UsesNodeBuilder usesBuilder,
45 final Map<URI, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
46 final int line = usesBuilder.getLine();
48 SchemaPath groupingPath = usesBuilder.getTargetGroupingPath();
49 QName groupingName = groupingPath.getPathFromRoot().iterator().next();
50 ModuleBuilder dependentModule = BuilderUtils.findModule(groupingName, modules);
52 Set<GroupingBuilder> groupings = dependentModule.getGroupingBuilders();
53 GroupingBuilder result = findGroupingBuilder(groupings, groupingName.getLocalName());
58 Builder parent = usesBuilder.getParent();
59 while (parent != null) {
60 if (parent instanceof DataNodeContainerBuilder) {
61 groupings = ((DataNodeContainerBuilder) parent).getGroupingBuilders();
62 } else if (parent instanceof RpcDefinitionBuilder) {
63 groupings = ((RpcDefinitionBuilder) parent).getGroupings();
65 result = findGroupingBuilder(groupings, groupingName.getLocalName());
67 parent = parent.getParent();
74 throw new YangParseException(module.getName(), line, "Grouping '" + groupingName + "' not found.");
80 * Find grouping by name.
83 * collection of grouping builders to search
86 * @return grouping with given name if present in collection, null otherwise
88 private static GroupingBuilder findGroupingBuilder(final Set<GroupingBuilder> groupings, final String name) {
89 for (GroupingBuilder grouping : groupings) {
90 if (grouping.getQName().getLocalName().equals(name)) {
98 * Perform refinement of uses target grouping nodes. Uses process has to be
102 * uses node containing refine statements
104 public static void performRefine(final UsesNodeBuilder usesNode) {
105 for (RefineBuilder refine : usesNode.getRefines()) {
106 String refineTargetPath = refine.getTargetPathString();
108 Builder currentNode = usesNode.getParent();
109 for (String pathElement : SLASH_SPLITTER.split(refineTargetPath)) {
110 if (currentNode instanceof DataNodeContainerBuilder) {
111 currentNode = ((DataNodeContainerBuilder) currentNode).getDataChildByName(pathElement);
112 } else if (currentNode instanceof ChoiceBuilder) {
113 currentNode = ((ChoiceBuilder) currentNode).getCaseNodeByName(pathElement);
117 DataSchemaNodeBuilder nodeToRefine = (DataSchemaNodeBuilder) currentNode;
118 if (nodeToRefine == null) {
119 throw new YangParseException(refine.getModuleName(), refine.getLine(), "Refine target node '"
120 + refine.getTargetPathString() + "' not found");
122 RefineUtils.performRefine(nodeToRefine, refine);
123 usesNode.addRefineNode(nodeToRefine);
127 public static class UsesComparator implements Comparator<UsesNodeBuilder> {
129 public int compare(final UsesNodeBuilder o1, final UsesNodeBuilder o2) {
130 return getElementPosition(o2) - getElementPosition(o1);
134 private static int getElementPosition(final UsesNodeBuilder usesNode) {
136 Builder parent = usesNode.getParent();
137 while (!(parent instanceof ModuleBuilder)) {
138 parent = parent.getParent();