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;
15 import java.util.NavigableMap;
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;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public final class GroupingUtils {
30 private static final Logger LOG = LoggerFactory.getLogger(GroupingUtils.class);
32 private static final Splitter SLASH_SPLITTER = Splitter.on('/');
34 private GroupingUtils() {
38 * Search given modules for grouping by name defined in uses node.
41 * builder of uses statement
46 * @return grouping with given name, never null
47 * @throws YangParseException
48 * if no grouping found
50 public static GroupingBuilder getTargetGroupingFromModules(final UsesNodeBuilder usesBuilder,
51 final Map<URI, NavigableMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
52 final int line = usesBuilder.getLine();
54 SchemaPath groupingPath = usesBuilder.getTargetGroupingPath();
55 QName groupingName = groupingPath.getPathFromRoot().iterator().next();
56 ModuleBuilder dependentModule = BuilderUtils.findModule(groupingName, modules);
58 Set<GroupingBuilder> groupings = dependentModule.getGroupingBuilders();
59 GroupingBuilder result = findGroupingBuilder(groupings, groupingName.getLocalName());
64 Builder parent = usesBuilder.getParent();
65 while (parent != null) {
66 if (parent instanceof DataNodeContainerBuilder) {
67 groupings = ((DataNodeContainerBuilder) parent).getGroupingBuilders();
68 } else if (parent instanceof RpcDefinitionBuilder) {
69 groupings = ((RpcDefinitionBuilder) parent).getGroupings();
71 result = findGroupingBuilder(groupings, groupingName.getLocalName());
73 parent = parent.getParent();
80 throw new YangParseException(module.getName(), line, "Grouping '" + groupingName + "' not found.");
86 * Find grouping by name.
89 * collection of grouping builders to search
92 * @return grouping with given name if present in collection, null otherwise
94 private static GroupingBuilder findGroupingBuilder(final Set<GroupingBuilder> groupings, final String name) {
95 for (GroupingBuilder grouping : groupings) {
96 if (grouping.getQName().getLocalName().equals(name)) {
104 * Perform refinement of uses target grouping nodes. Uses process has to be
108 * uses node containing refine statements
110 public static void performRefine(final UsesNodeBuilder usesNode) {
111 for (RefineBuilder refine : usesNode.getRefines()) {
112 String refineTargetPath = refine.getTargetPathString();
114 Builder currentNode = usesNode.getParent();
115 for (String pathElement : SLASH_SPLITTER.split(refineTargetPath)) {
116 if (currentNode instanceof DataNodeContainerBuilder) {
117 currentNode = ((DataNodeContainerBuilder) currentNode).getDataChildByName(pathElement);
118 } else if (currentNode instanceof ChoiceBuilder) {
119 currentNode = ((ChoiceBuilder) currentNode).getCaseNodeByName(pathElement);
123 DataSchemaNodeBuilder nodeToRefine = (DataSchemaNodeBuilder) currentNode;
124 if (nodeToRefine == null) {
125 // FIXME: exception replaced with log to avoid breakage when
126 // user tries to refine instance of extension (unknown node)
128 // throw new YangParseException(refine.getModuleName(),
129 // refine.getLine(), "Refine target node '" +
130 // refine.getTargetPathString() + "' not found");
131 LOG.warn("Error in module {} at line {}: Refine target node {} not found.", refine.getModuleName(),
132 refine.getLine(), refine.getTargetPathString());
135 RefineUtils.performRefine(nodeToRefine, refine);
136 usesNode.addRefineNode(nodeToRefine);
140 public static class UsesComparator implements Comparator<UsesNodeBuilder> {
142 public int compare(final UsesNodeBuilder o1, final UsesNodeBuilder o2) {
143 return getElementPosition(o2) - getElementPosition(o1);
147 private static int getElementPosition(final UsesNodeBuilder usesNode) {
149 Builder parent = usesNode.getParent();
150 while (!(parent instanceof ModuleBuilder)) {
151 parent = parent.getParent();