Fixed augment schema path resolving.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / AugmentationSchemaBuilderImpl.java
index 1027cb1fc3fda89a21d45a127ae178eefe632b77..c27bf683fc3b5557c6875b6747e4ff37df784367 100644 (file)
@@ -17,6 +17,7 @@ import org.opendaylight.controller.yang.common.QName;
 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
+import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.controller.yang.model.api.SchemaPath;
 import org.opendaylight.controller.yang.model.api.Status;
 import org.opendaylight.controller.yang.model.api.TypeDefinition;
@@ -27,21 +28,34 @@ import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder
 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
 import org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil;
+import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
 
 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
-
     private final AugmentationSchemaImpl instance;
-    private final SchemaPath augmentTarget;
-    final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
-    final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
+    private final int line;
+    private final String augmentTargetStr;
+    private SchemaPath augmentTarget;
+    private SchemaPath finalAugmentTarget;
+    private String whenCondition;
+    private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
+    private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
+    private boolean resolved;
 
-    AugmentationSchemaBuilderImpl(String augmentPath) {
-        SchemaPath targetPath = YangModelBuilderUtil.parseAugmentPath(augmentPath);
+    AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
+        this.augmentTargetStr = augmentTargetStr;
+        this.line = line;
+        final SchemaPath targetPath = YangModelBuilderUtil
+                .parseAugmentPath(augmentTargetStr);
         augmentTarget = targetPath;
         instance = new AugmentationSchemaImpl(targetPath);
     }
 
+    @Override
+    public int getLine() {
+        return line;
+    }
+
     @Override
     public void addChildNode(DataSchemaNodeBuilder childNode) {
         childNodes.add(childNode);
@@ -72,23 +86,32 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
 
     @Override
     public AugmentationSchema build() {
+        instance.setTargetPath(finalAugmentTarget);
+
+        RevisionAwareXPath whenStmt;
+        if (whenCondition == null) {
+            whenStmt = null;
+        } else {
+            whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
+        }
+        instance.setWhenCondition(whenStmt);
 
         // CHILD NODES
-        Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
+        final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
         for (DataSchemaNodeBuilder node : childNodes) {
             childs.put(node.getQName(), node.build());
         }
         instance.setChildNodes(childs);
 
         // GROUPINGS
-        Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
+        final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
         for (GroupingBuilder builder : groupings) {
             groupingDefinitions.add(builder.build());
         }
         instance.setGroupings(groupingDefinitions);
 
         // USES
-        Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
+        final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
         for (UsesNodeBuilder builder : usesNodes) {
             usesNodeDefinitions.add(builder.build());
         }
@@ -97,6 +120,24 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
         return instance;
     }
 
+    @Override
+    public boolean isResolved() {
+        return resolved;
+    }
+
+    @Override
+    public void setResolved(boolean resolved) {
+        this.resolved = resolved;
+    }
+
+    public String getWhenCondition() {
+        return whenCondition;
+    }
+
+    public void addWhenCondition(String whenCondition) {
+        this.whenCondition = whenCondition;
+    }
+
     @Override
     public void addTypedef(TypeDefinitionBuilder type) {
         throw new UnsupportedOperationException(
@@ -123,12 +164,89 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
         return augmentTarget;
     }
 
-    private static class AugmentationSchemaImpl implements AugmentationSchema {
+    @Override
+    public void setTargetPath(SchemaPath path) {
+        this.finalAugmentTarget = path;
+    }
 
-        private final SchemaPath targetPath;
-        private Map<QName, DataSchemaNode> childNodes;
-        private Set<GroupingDefinition> groupings;
-        private Set<UsesNode> uses;
+    @Override
+    public String getTargetPathAsString() {
+        return augmentTargetStr;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 17;
+        int result = 1;
+        result = prime * result
+                + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
+        result = prime * result
+                + ((childNodes == null) ? 0 : childNodes.hashCode());
+        result = prime * result
+                + ((groupings == null) ? 0 : groupings.hashCode());
+        result = prime * result + ((usesNodes == null) ? 0 : usesNodes.hashCode());
+        result = prime * result
+                + ((whenCondition == null) ? 0 : whenCondition.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
+        if (augmentTargetStr == null) {
+            if (other.augmentTargetStr != null) {
+                return false;
+            }
+        } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
+            return false;
+        }
+        if (childNodes == null) {
+            if (other.childNodes != null) {
+                return false;
+            }
+        } else if (!childNodes.equals(other.childNodes)) {
+            return false;
+        }
+        if (groupings == null) {
+            if (other.groupings != null) {
+                return false;
+            }
+        } else if (!groupings.equals(other.groupings)) {
+            return false;
+        }
+        if (usesNodes == null) {
+            if (other.usesNodes != null) {
+                return false;
+            }
+        } else if (!usesNodes.equals(other.usesNodes)) {
+            return false;
+        }
+        if (whenCondition == null) {
+            if (other.whenCondition != null) {
+                return false;
+            }
+        } else if (!whenCondition.equals(other.whenCondition)) {
+            return false;
+        }
+        return true;
+    }
+
+
+    private static class AugmentationSchemaImpl implements AugmentationSchema {
+        private SchemaPath targetPath;
+        private RevisionAwareXPath whenCondition;
+        private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
+        private Set<GroupingDefinition> groupings = Collections.emptySet();
+        private Set<UsesNode> uses = Collections.emptySet();
 
         private String description;
         private String reference;
@@ -143,13 +261,28 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
             return targetPath;
         }
 
+        private void setTargetPath(SchemaPath path) {
+            this.targetPath = path;
+        }
+
+        @Override
+        public RevisionAwareXPath getWhenCondition() {
+            return whenCondition;
+        }
+
+        private void setWhenCondition(RevisionAwareXPath whenCondition) {
+            this.whenCondition = whenCondition;
+        }
+
         @Override
         public Set<DataSchemaNode> getChildNodes() {
             return new HashSet<DataSchemaNode>(childNodes.values());
         }
 
         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
-            this.childNodes = childNodes;
+            if (childNodes != null) {
+                this.childNodes = childNodes;
+            }
         }
 
         @Override
@@ -158,7 +291,9 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
         }
 
         private void setGroupings(Set<GroupingDefinition> groupings) {
-            this.groupings = groupings;
+            if (groupings != null) {
+                this.groupings = groupings;
+            }
         }
 
         @Override
@@ -167,7 +302,9 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
         }
 
         private void setUses(Set<UsesNode> uses) {
-            this.uses = uses;
+            if (uses != null) {
+                this.uses = uses;
+            }
         }
 
         /**
@@ -235,11 +372,7 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
                     + ((groupings == null) ? 0 : groupings.hashCode());
             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
             result = prime * result
-                    + ((description == null) ? 0 : description.hashCode());
-            result = prime * result
-                    + ((reference == null) ? 0 : reference.hashCode());
-            result = prime * result
-                    + ((status == null) ? 0 : status.hashCode());
+                    + ((whenCondition == null) ? 0 : whenCondition.hashCode());
             return result;
         }
 
@@ -283,25 +416,11 @@ public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder
             } else if (!uses.equals(other.uses)) {
                 return false;
             }
-            if (description == null) {
-                if (other.description != null) {
-                    return false;
-                }
-            } else if (!description.equals(other.description)) {
-                return false;
-            }
-            if (reference == null) {
-                if (other.reference != null) {
-                    return false;
-                }
-            } else if (!reference.equals(other.reference)) {
-                return false;
-            }
-            if (status == null) {
-                if (other.status != null) {
+            if (whenCondition == null) {
+                if (other.whenCondition != null) {
                     return false;
                 }
-            } else if (!status.equals(other.status)) {
+            } else if (!whenCondition.equals(other.whenCondition)) {
                 return false;
             }
             return true;