Merge "Moved parsing of unknown nodes from implementation to abstract classes."
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / impl / YangParserImpl.java
index 6adc29b2271ce956fed15035084b1a3f9f7f77b0..7d64b944219d122ad84d157cd383eb054bb02b10 100644 (file)
@@ -39,6 +39,7 @@ import org.opendaylight.controller.yang.common.QName;
 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.controller.yang.model.api.ChoiceNode;
 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.controller.yang.model.api.DataNodeContainer;
 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
@@ -47,6 +48,7 @@ import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
 import org.opendaylight.controller.yang.model.api.Module;
 import org.opendaylight.controller.yang.model.api.SchemaContext;
+import org.opendaylight.controller.yang.model.api.SchemaNode;
 import org.opendaylight.controller.yang.model.api.SchemaPath;
 import org.opendaylight.controller.yang.model.api.TypeDefinition;
 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
@@ -68,6 +70,7 @@ import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
 import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder;
 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder;
 import org.opendaylight.controller.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
+import org.opendaylight.controller.yang.parser.builder.impl.DeviationBuilder;
 import org.opendaylight.controller.yang.parser.builder.impl.GroupingBuilderImpl;
 import org.opendaylight.controller.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
 import org.opendaylight.controller.yang.parser.builder.impl.IdentityrefTypeBuilder;
@@ -95,7 +98,7 @@ import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 
 public final class YangParserImpl implements YangModelParser {
-    private static final Logger logger = LoggerFactory.getLogger(YangParserImpl.class);
+    private static final Logger LOG = LoggerFactory.getLogger(YangParserImpl.class);
 
     @Override
     public Set<Module> parseYangModels(final List<File> yangFiles) {
@@ -111,7 +114,7 @@ public final class YangParserImpl implements YangModelParser {
                 try {
                     inputStreams.put(new FileInputStream(yangFile), yangFile);
                 } catch (FileNotFoundException e) {
-                    logger.warn("Exception while reading yang file: " + yangFile.getName(), e);
+                    LOG.warn("Exception while reading yang file: " + yangFile.getName(), e);
                 }
             }
 
@@ -124,7 +127,7 @@ public final class YangParserImpl implements YangModelParser {
                 try {
                     is.close();
                 } catch (IOException e) {
-                    logger.debug("Failed to close stream.");
+                    LOG.debug("Failed to close stream.");
                 }
             }
 
@@ -158,7 +161,7 @@ public final class YangParserImpl implements YangModelParser {
                 try {
                     inputStreams.put(new FileInputStream(yangFile), yangFile);
                 } catch (FileNotFoundException e) {
-                    logger.warn("Exception while reading yang file: " + yangFile.getName(), e);
+                    LOG.warn("Exception while reading yang file: " + yangFile.getName(), e);
                 }
             }
 
@@ -170,7 +173,7 @@ public final class YangParserImpl implements YangModelParser {
                 try {
                     is.close();
                 } catch (IOException e) {
-                    logger.debug("Failed to close stream.");
+                    LOG.debug("Failed to close stream.");
                 }
             }
 
@@ -291,7 +294,7 @@ public final class YangParserImpl implements YangModelParser {
 
             result = parser.yang();
         } catch (IOException e) {
-            logger.warn("Exception while reading yang file: " + yangStream, e);
+            LOG.warn("Exception while reading yang file: " + yangStream, e);
         }
         return result;
     }
@@ -305,6 +308,7 @@ public final class YangParserImpl implements YangModelParser {
             }
         }
         resolveAugments(modules);
+        resolveDeviations(modules);
 
         // build
         // LinkedHashMap MUST be used otherwise the values will not maintain
@@ -333,6 +337,7 @@ public final class YangParserImpl implements YangModelParser {
             }
         }
         resolveAugmentsWithContext(modules, context);
+        resolveDeviationsWithContext(modules, context);
 
         // build
         // LinkedHashMap MUST be used otherwise the values will not maintain
@@ -376,11 +381,9 @@ public final class YangParserImpl implements YangModelParser {
      *            current module
      */
     private void resolveDirtyNodes(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
-        final Map<List<String>, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
+        final Set<TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
         if (!dirtyNodes.isEmpty()) {
-            for (Map.Entry<List<String>, TypeAwareBuilder> entry : dirtyNodes.entrySet()) {
-                final TypeAwareBuilder nodeToResolve = entry.getValue();
-
+            for (TypeAwareBuilder nodeToResolve : dirtyNodes) {
                 if (nodeToResolve instanceof UnionTypeBuilder) {
                     // special handling for union types
                     resolveTypeUnion((UnionTypeBuilder) nodeToResolve, modules, module);
@@ -397,11 +400,9 @@ public final class YangParserImpl implements YangModelParser {
 
     private void resolveDirtyNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
             final ModuleBuilder module, SchemaContext context) {
-        final Map<List<String>, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
+        final Set<TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
         if (!dirtyNodes.isEmpty()) {
-            for (Map.Entry<List<String>, TypeAwareBuilder> entry : dirtyNodes.entrySet()) {
-                final TypeAwareBuilder nodeToResolve = entry.getValue();
-
+            for (TypeAwareBuilder nodeToResolve : dirtyNodes) {
                 if (nodeToResolve instanceof UnionTypeBuilder) {
                     // special handling for union types
                     resolveTypeUnionWithContext((UnionTypeBuilder) nodeToResolve, modules, module, context);
@@ -1062,34 +1063,42 @@ public final class YangParserImpl implements YangModelParser {
         DataNodeContainerBuilder parent = usesNode.getParent();
         SchemaPath parentPath = parent.getPath();
         for (DataSchemaNodeBuilder child : targetGrouping.getChildNodeBuilders()) {
-            // if node is refined, take it from refined nodes and continue
-            SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes);
-            if (refined != null) {
-                refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName()));
-                parent.addChildNode((DataSchemaNodeBuilder) refined);
-                continue;
-            }
+            if (child != null) {
+                // if node is refined, take it from refined nodes and continue
+                SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes);
+                if (refined != null) {
+                    refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName()));
+                    parent.addChildNode((DataSchemaNodeBuilder) refined);
+                    continue;
+                }
 
-            DataSchemaNodeBuilder newChild = null;
-            if (child instanceof AnyXmlBuilder) {
-                newChild = new AnyXmlBuilder((AnyXmlBuilder) child);
-            } else if (child instanceof ChoiceBuilder) {
-                newChild = new ChoiceBuilder((ChoiceBuilder) child);
-            } else if (child instanceof ContainerSchemaNodeBuilder) {
-                newChild = new ContainerSchemaNodeBuilder((ContainerSchemaNodeBuilder) child);
-            } else if (child instanceof LeafListSchemaNodeBuilder) {
-                newChild = new LeafListSchemaNodeBuilder((LeafListSchemaNodeBuilder) child);
-            } else if (child instanceof LeafSchemaNodeBuilder) {
-                newChild = new LeafSchemaNodeBuilder((LeafSchemaNodeBuilder) child);
-            } else if (child instanceof ListSchemaNodeBuilder) {
-                newChild = new ListSchemaNodeBuilder((ListSchemaNodeBuilder) child);
-            }
+                DataSchemaNodeBuilder newChild = null;
+                if (child instanceof AnyXmlBuilder) {
+                    newChild = new AnyXmlBuilder((AnyXmlBuilder) child);
+                } else if (child instanceof ChoiceBuilder) {
+                    newChild = new ChoiceBuilder((ChoiceBuilder) child);
+                } else if (child instanceof ContainerSchemaNodeBuilder) {
+                    newChild = new ContainerSchemaNodeBuilder((ContainerSchemaNodeBuilder) child);
+                } else if (child instanceof LeafListSchemaNodeBuilder) {
+                    newChild = new LeafListSchemaNodeBuilder((LeafListSchemaNodeBuilder) child);
+                } else if (child instanceof LeafSchemaNodeBuilder) {
+                    newChild = new LeafSchemaNodeBuilder((LeafSchemaNodeBuilder) child);
+                } else if (child instanceof ListSchemaNodeBuilder) {
+                    newChild = new ListSchemaNodeBuilder((ListSchemaNodeBuilder) child);
+                }
+
+                if (newChild == null) {
+                    throw new YangParseException(usesNode.getLine(),
+                            "Unknown member of target grouping while resolving uses node.");
+                }
 
-            if (newChild instanceof GroupingMember) {
-                ((GroupingMember) newChild).setAddedByUses(true);
+                if (newChild instanceof GroupingMember) {
+                    ((GroupingMember) newChild).setAddedByUses(true);
+                }
+
+                newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName()));
+                parent.addChildNode(newChild);
             }
-            newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName()));
-            parent.addChildNode(newChild);
         }
         for (GroupingBuilder g : targetGrouping.getGroupingBuilders()) {
             GroupingBuilder newGrouping = new GroupingBuilderImpl(g);
@@ -1109,11 +1118,11 @@ public final class YangParserImpl implements YangModelParser {
             // uses has not path
             parent.addUsesNode(newUses);
         }
-        for (UnknownSchemaNodeBuilder un : targetGrouping.getUnknownNodes()) {
+        for (UnknownSchemaNodeBuilder un : targetGrouping.getUnknownNodeBuilders()) {
             UnknownSchemaNodeBuilder newUn = new UnknownSchemaNodeBuilder(un);
             newUn.setAddedByUses(true);
             newUn.setPath(createSchemaPath(parentPath, un.getQName().getLocalName()));
-            parent.addUnknownSchemaNode(newUn);
+            parent.addUnknownNodeBuilder(newUn);
         }
     }
 
@@ -1123,34 +1132,41 @@ public final class YangParserImpl implements YangModelParser {
         DataNodeContainerBuilder parent = usesNode.getParent();
         SchemaPath parentPath = parent.getPath();
         for (DataSchemaNode child : targetGrouping.getChildNodes()) {
-            // if node is refined, take it from refined nodes and continue
-            SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes);
-            if (refined != null) {
-                refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName()));
-                parent.addChildNode((DataSchemaNodeBuilder) refined);
-                continue;
-            }
+            if (child != null) {
+                // if node is refined, take it from refined nodes and continue
+                SchemaNodeBuilder refined = getRefined(child.getQName(), refineNodes);
+                if (refined != null) {
+                    refined.setPath(createSchemaPath(parentPath, refined.getQName().getLocalName()));
+                    parent.addChildNode((DataSchemaNodeBuilder) refined);
+                    continue;
+                }
 
-            DataSchemaNodeBuilder newChild = null;
-            if (child instanceof AnyXmlSchemaNode) {
-                newChild = createAnyXml((AnyXmlSchemaNode) child, line);
-            } else if (child instanceof ChoiceNode) {
-                newChild = createChoice((ChoiceNode) child, line);
-            } else if (child instanceof ContainerSchemaNode) {
-                newChild = createContainer((ContainerSchemaNode) child, line);
-            } else if (child instanceof LeafListSchemaNode) {
-                newChild = createLeafList((LeafListSchemaNode) child, line);
-            } else if (child instanceof LeafSchemaNode) {
-                newChild = createLeafBuilder((LeafSchemaNode) child, line);
-            } else if (child instanceof ListSchemaNode) {
-                newChild = createList((ListSchemaNode) child, line);
-            }
+                DataSchemaNodeBuilder newChild = null;
+                if (child instanceof AnyXmlSchemaNode) {
+                    newChild = createAnyXml((AnyXmlSchemaNode) child, line);
+                } else if (child instanceof ChoiceNode) {
+                    newChild = createChoice((ChoiceNode) child, line);
+                } else if (child instanceof ContainerSchemaNode) {
+                    newChild = createContainer((ContainerSchemaNode) child, line);
+                } else if (child instanceof LeafListSchemaNode) {
+                    newChild = createLeafList((LeafListSchemaNode) child, line);
+                } else if (child instanceof LeafSchemaNode) {
+                    newChild = createLeafBuilder((LeafSchemaNode) child, line);
+                } else if (child instanceof ListSchemaNode) {
+                    newChild = createList((ListSchemaNode) child, line);
+                }
+
+                if (newChild == null) {
+                    throw new YangParseException(usesNode.getLine(),
+                            "Unknown member of target grouping while resolving uses node.");
+                }
 
-            if (newChild instanceof GroupingMember) {
-                ((GroupingMember) newChild).setAddedByUses(true);
+                if (newChild instanceof GroupingMember) {
+                    ((GroupingMember) newChild).setAddedByUses(true);
+                }
+                newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName()));
+                parent.addChildNode(newChild);
             }
-            newChild.setPath(createSchemaPath(parentPath, newChild.getQName().getLocalName()));
-            parent.addChildNode(newChild);
         }
         for (GroupingDefinition g : targetGrouping.getGroupings()) {
             GroupingBuilder newGrouping = createGrouping(g, line);
@@ -1176,7 +1192,7 @@ public final class YangParserImpl implements YangModelParser {
             UnknownSchemaNodeBuilder newNode = createUnknownSchemaNode(un, line);
             newNode.setAddedByUses(true);
             newNode.setPath(createSchemaPath(parentPath, un.getQName().getLocalName()));
-            parent.addUnknownSchemaNode(newNode);
+            parent.addUnknownNodeBuilder(newNode);
         }
     }
 
@@ -1201,7 +1217,7 @@ public final class YangParserImpl implements YangModelParser {
     }
 
     private void resolveUnknownNodes(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
-        for (UnknownSchemaNodeBuilder usnb : module.getUnknownNodes()) {
+        for (UnknownSchemaNodeBuilder usnb : module.getAllUnknownNodes()) {
             QName nodeType = usnb.getNodeType();
             if (nodeType.getNamespace() == null || nodeType.getRevision() == null) {
                 try {
@@ -1211,15 +1227,15 @@ public final class YangParserImpl implements YangModelParser {
                             nodeType.getPrefix(), nodeType.getLocalName());
                     usnb.setNodeType(newNodeType);
                 } catch (YangParseException e) {
-                    logger.debug(module.getName(), usnb.getLine(), "Failed to find unknown node type: " + nodeType);
+                    LOG.debug(module.getName(), usnb.getLine(), "Failed to find unknown node type: " + nodeType);
                 }
             }
         }
     }
 
     private void resolveUnknownNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, SchemaContext context) {
-        for (UnknownSchemaNodeBuilder unknownNodeBuilder : module.getUnknownNodes()) {
+            final ModuleBuilder module, final SchemaContext context) {
+        for (UnknownSchemaNodeBuilder unknownNodeBuilder : module.getAllUnknownNodes()) {
             QName nodeType = unknownNodeBuilder.getNodeType();
             if (nodeType.getNamespace() == null || nodeType.getRevision() == null) {
                 try {
@@ -1239,11 +1255,122 @@ public final class YangParserImpl implements YangModelParser {
 
                     unknownNodeBuilder.setNodeType(newNodeType);
                 } catch (YangParseException e) {
-                    logger.debug(module.getName(), unknownNodeBuilder.getLine(), "Failed to find unknown node type: "
+                    LOG.debug(module.getName(), unknownNodeBuilder.getLine(), "Failed to find unknown node type: "
                             + nodeType);
                 }
             }
         }
     }
 
+    private void resolveDeviations(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                ModuleBuilder b = inner.getValue();
+                resolveDeviation(modules, b);
+            }
+        }
+    }
+
+    private void resolveDeviation(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
+        for (DeviationBuilder dev : module.getDeviations()) {
+            int line = dev.getLine();
+            SchemaPath targetPath = dev.getTargetPath();
+            List<QName> path = targetPath.getPath();
+            QName q0 = path.get(0);
+            String prefix = q0.getPrefix();
+            if (prefix == null) {
+                prefix = module.getPrefix();
+            }
+
+            ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line);
+            processDeviation(dev, dependentModuleBuilder, path, module);
+        }
+    }
+
+    private void resolveDeviationsWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+            final SchemaContext context) {
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                ModuleBuilder b = inner.getValue();
+                resolveDeviationWithContext(modules, b, context);
+            }
+        }
+    }
+
+    private void resolveDeviationWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+            final ModuleBuilder module, final SchemaContext context) {
+        for (DeviationBuilder dev : module.getDeviations()) {
+            int line = dev.getLine();
+            SchemaPath targetPath = dev.getTargetPath();
+            List<QName> path = targetPath.getPath();
+            QName q0 = path.get(0);
+            String prefix = q0.getPrefix();
+            if (prefix == null) {
+                prefix = module.getPrefix();
+            }
+            String name = null;
+
+            ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line);
+            if (dependentModuleBuilder == null) {
+                Module dependentModule = findModuleFromContext(context, module, prefix, line);
+                Object currentParent = dependentModule;
+
+                for (int i = 0; i < path.size(); i++) {
+                    if (currentParent == null) {
+                        throw new YangParseException(module.getName(), line, "Failed to find deviation target.");
+                    }
+                    QName q = path.get(i);
+                    name = q.getLocalName();
+                    if (currentParent instanceof DataNodeContainer) {
+                        currentParent = ((DataNodeContainer) currentParent).getDataChildByName(name);
+                    }
+                }
+
+                if (currentParent == null) {
+                    throw new YangParseException(module.getName(), line, "Failed to find deviation target.");
+                }
+                if (currentParent instanceof SchemaNode) {
+                    dev.setTargetPath(((SchemaNode) currentParent).getPath());
+                }
+
+            } else {
+                processDeviation(dev, dependentModuleBuilder, path, module);
+            }
+        }
+    }
+
+    /**
+     * Correct deviation target path in deviation builder.
+     *
+     * @param dev
+     *            deviation
+     * @param dependentModuleBuilder
+     *            module containing deviation target
+     * @param path
+     *            current deviation target path
+     * @param module
+     *            current module
+     */
+    private void processDeviation(final DeviationBuilder dev, final ModuleBuilder dependentModuleBuilder,
+            final List<QName> path, final ModuleBuilder module) {
+        final int line = dev.getLine();
+        Builder currentParent = dependentModuleBuilder;
+
+        for (int i = 0; i < path.size(); i++) {
+            if (currentParent == null) {
+                throw new YangParseException(module.getName(), line, "Failed to find deviation target.");
+            }
+            QName q = path.get(i);
+            String name = q.getLocalName();
+            if (currentParent instanceof DataNodeContainerBuilder) {
+                currentParent = ((DataNodeContainerBuilder) currentParent).getDataChildByName(name);
+            }
+        }
+
+        if (currentParent == null || !(currentParent instanceof SchemaNodeBuilder)) {
+            throw new YangParseException(module.getName(), line, "Failed to find deviation target.");
+        }
+        dev.setTargetPath(((SchemaNodeBuilder) currentParent).getPath());
+    }
+
 }