eb06329eaab9ddd3ab3bbc1c62bf695d284e7ad1
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / GroupingUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
9
10 import com.google.common.base.Splitter;
11 import java.net.URI;
12 import java.util.Comparator;
13 import java.util.Date;
14 import java.util.Map;
15 import java.util.NavigableMap;
16 import java.util.Set;
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;
28
29 public final class GroupingUtils {
30     private static final Logger LOG = LoggerFactory.getLogger(GroupingUtils.class);
31
32     private static final Splitter SLASH_SPLITTER = Splitter.on('/');
33
34     private GroupingUtils() {
35     }
36
37     /**
38      * Search given modules for grouping by name defined in uses node.
39      *
40      * @param usesBuilder
41      *            builder of uses statement
42      * @param modules
43      *            all loaded modules
44      * @param module
45      *            current module
46      * @return grouping with given name, never null
47      * @throws YangParseException
48      *             if no grouping found
49      */
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();
53
54         SchemaPath groupingPath = usesBuilder.getTargetGroupingPath();
55         QName groupingName = groupingPath.getPathFromRoot().iterator().next();
56         ModuleBuilder dependentModule = BuilderUtils.findModule(groupingName, modules);
57
58         Set<GroupingBuilder> groupings = dependentModule.getGroupingBuilders();
59         GroupingBuilder result = findGroupingBuilder(groupings, groupingName.getLocalName());
60         if (result != null) {
61             return result;
62         }
63
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();
70             }
71             result = findGroupingBuilder(groupings, groupingName.getLocalName());
72             if (result == null) {
73                 parent = parent.getParent();
74             } else {
75                 break;
76             }
77         }
78
79         if (result == null) {
80             throw new YangParseException(module.getName(), line, "Grouping '" + groupingName + "' not found.");
81         }
82         return result;
83     }
84
85     /**
86      * Find grouping by name.
87      *
88      * @param groupings
89      *            collection of grouping builders to search
90      * @param name
91      *            name of grouping
92      * @return grouping with given name if present in collection, null otherwise
93      */
94     private static GroupingBuilder findGroupingBuilder(final Set<GroupingBuilder> groupings, final String name) {
95         for (GroupingBuilder grouping : groupings) {
96             if (grouping.getQName().getLocalName().equals(name)) {
97                 return grouping;
98             }
99         }
100         return null;
101     }
102
103     /**
104      * Perform refinement of uses target grouping nodes. Uses process has to be
105      * already performed.
106      *
107      * @param usesNode
108      *            uses node containing refine statements
109      */
110     public static void performRefine(final UsesNodeBuilder usesNode) {
111         for (RefineBuilder refine : usesNode.getRefines()) {
112             String refineTargetPath = refine.getTargetPathString();
113
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);
120                 }
121             }
122
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)
127
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());
133                 continue;
134             }
135             RefineUtils.performRefine(nodeToRefine, refine);
136             usesNode.addRefineNode(nodeToRefine);
137         }
138     }
139
140     public static class UsesComparator implements Comparator<UsesNodeBuilder> {
141         @Override
142         public int compare(final UsesNodeBuilder o1, final UsesNodeBuilder o2) {
143             return getElementPosition(o2) - getElementPosition(o1);
144         }
145     }
146
147     private static int getElementPosition(final UsesNodeBuilder usesNode) {
148         int i = 0;
149         Builder parent = usesNode.getParent();
150         while (!(parent instanceof ModuleBuilder)) {
151             parent = parent.getParent();
152             i++;
153         }
154         return i;
155     }
156
157 }