Merge "Bug 1446: Add new concurrent classes for tracking stats"
[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.Set;
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;
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 COLON_SPLITTER = Splitter.on(':');
33     private static final Splitter SLASH_SPLITTER = Splitter.on('/');
34
35     private GroupingUtils() {
36     }
37
38     /**
39      * Search given modules for grouping by name defined in uses node.
40      *
41      * @param usesBuilder
42      *            builder of uses statement
43      * @param modules
44      *            all loaded modules
45      * @param module
46      *            current module
47      * @return grouping with given name, never null
48      * @throws YangParseException
49      *             if no grouping found
50      */
51     public static GroupingBuilder getTargetGroupingFromModules(final UsesNodeBuilder usesBuilder,
52             final Map<URI, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
53         final int line = usesBuilder.getLine();
54
55         SchemaPath groupingPath = usesBuilder.getTargetGroupingPath();
56         QName groupingName = groupingPath.getPathFromRoot().iterator().next();
57         ModuleBuilder dependentModule = BuilderUtils.findModule(groupingName, modules);
58
59         Set<GroupingBuilder> groupings = dependentModule.getGroupingBuilders();
60         GroupingBuilder result = findGroupingBuilder(groupings, groupingName.getLocalName());
61         if (result != null) {
62             return result;
63         }
64
65         Builder parent = usesBuilder.getParent();
66         while (parent != null) {
67             if (parent instanceof DataNodeContainerBuilder) {
68                 groupings = ((DataNodeContainerBuilder) parent).getGroupingBuilders();
69             } else if (parent instanceof RpcDefinitionBuilder) {
70                 groupings = ((RpcDefinitionBuilder) parent).getGroupings();
71             }
72             result = findGroupingBuilder(groupings, groupingName.getLocalName());
73             if (result == null) {
74                 parent = parent.getParent();
75             } else {
76                 break;
77             }
78         }
79
80         if (result == null) {
81             throw new YangParseException(module.getName(), line, "Grouping '" + groupingName + "' not found.");
82         }
83         return result;
84     }
85
86     /**
87      * Find grouping by name.
88      *
89      * @param groupings
90      *            collection of grouping builders to search
91      * @param name
92      *            name of grouping
93      * @return grouping with given name if present in collection, null otherwise
94      */
95     private static GroupingBuilder findGroupingBuilder(final Set<GroupingBuilder> groupings, final String name) {
96         for (GroupingBuilder grouping : groupings) {
97             if (grouping.getQName().getLocalName().equals(name)) {
98                 return grouping;
99             }
100         }
101         return null;
102     }
103
104     /**
105      * Perform refinement of uses target grouping nodes. Uses process has to be
106      * already performed.
107      *
108      * @param usesNode
109      *            uses node containing refine statements
110      */
111     public static void performRefine(final UsesNodeBuilder usesNode) {
112         for (RefineBuilder refine : usesNode.getRefines()) {
113             String refineTargetPath = refine.getTargetPathString();
114
115             Builder currentNode = usesNode.getParent();
116             for (String pathElement : SLASH_SPLITTER.split(refineTargetPath)) {
117                 if (currentNode instanceof DataNodeContainerBuilder) {
118                     currentNode = ((DataNodeContainerBuilder) currentNode).getDataChildByName(pathElement);
119                 } else if (currentNode instanceof ChoiceBuilder) {
120                     currentNode = ((ChoiceBuilder) currentNode).getCaseNodeByName(pathElement);
121                 }
122             }
123
124             DataSchemaNodeBuilder nodeToRefine = (DataSchemaNodeBuilder) currentNode;
125             if (nodeToRefine == null) {
126                 // FIXME: exception replaced with log to avoid breakage when
127                 // user tries to refine instance of extension (unknown node)
128
129                 // throw new YangParseException(refine.getModuleName(),
130                 // refine.getLine(), "Refine target node '" +
131                 // refine.getTargetPathString() + "' not found");
132                 LOG.warn("Error in module {} at line {}: Refine target node {} not found.", refine.getModuleName(),
133                         refine.getLine(), refine.getTargetPathString());
134                 continue;
135             }
136             RefineUtils.performRefine(nodeToRefine, refine);
137             usesNode.addRefineNode(nodeToRefine);
138         }
139     }
140
141     public static class UsesComparator implements Comparator<UsesNodeBuilder> {
142         @Override
143         public int compare(final UsesNodeBuilder o1, final UsesNodeBuilder o2) {
144             return getElementPosition(o2) - getElementPosition(o1);
145         }
146     }
147
148     private static int getElementPosition(final UsesNodeBuilder usesNode) {
149         int i = 0;
150         Builder parent = usesNode.getParent();
151         while (!(parent instanceof ModuleBuilder)) {
152             parent = parent.getParent();
153             i++;
154         }
155         return i;
156     }
157
158 }