From 0082b632a043ef891f906b367e38132484685bb3 Mon Sep 17 00:00:00 2001 From: Martin Vitez Date: Thu, 28 Mar 2013 14:19:31 +0100 Subject: [PATCH] Added YANG typedef support in the YANG parser Added javadoc to builders Added JUnit test Fixed hashCode and equals implementation (now only based on QName) for schema Node TOs. Change-Id: I0d064391ac8de2c77258d3817ce4f7f50ea90350 Signed-off-by: Tony Tkacik --- .../parser/builder/api/SchemaNodeBuilder.java | 58 +- .../builder/api/TypeDefinitionBuilder.java | 52 +- .../impl/ContainerSchemaNodeBuilder.java | 31 +- .../parser/builder/impl/ExtensionBuilder.java | 9 +- .../parser/builder/impl/FeatureBuilder.java | 52 +- .../builder/impl/GroupingBuilderImpl.java | 95 +-- .../impl/LeafListSchemaNodeBuilder.java | 72 +-- .../builder/impl/LeafSchemaNodeBuilder.java | 75 +-- .../builder/impl/ListSchemaNodeBuilder.java | 123 +--- .../builder/impl/NotificationBuilder.java | 84 +-- .../builder/impl/RpcDefinitionBuilder.java | 91 +-- .../parser/builder/impl/TypedefBuilder.java | 20 +- .../impl/UnknownSchemaNodeBuilder.java | 13 + .../parser/impl/YangModelParserImpl.java | 554 +++++++++++++++--- .../parser/util/YangModelBuilderUtil.java | 9 +- .../parser/impl/YangModelParserTest.java | 148 ++++- .../src/test/resources/model/testfile1.yang | 96 +-- .../src/test/resources/model/testfile2.yang | 143 +++-- .../model/api/type/UnknownTypeDefinition.java | 11 +- .../controller/model/util/UnknownType.java | 40 +- .../model/util/YangTypesConverter.java | 20 +- 21 files changed, 1091 insertions(+), 705 deletions(-) diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/SchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/SchemaNodeBuilder.java index 06855cffac..b96347ef18 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/SchemaNodeBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/SchemaNodeBuilder.java @@ -1,26 +1,32 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.yang.model.parser.builder.api; - -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.SchemaPath; -import org.opendaylight.controller.yang.model.api.Status; - - -/** - * Interface for all builders of SchemaNode nodes. - */ -public interface SchemaNodeBuilder extends Builder { - - QName getQName(); - void setPath(SchemaPath schemaPath); - void setDescription(String description); - void setReference(String reference); - void setStatus(Status status); - -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.yang.model.parser.builder.api; + +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.Status; +import org.opendaylight.controller.yang.model.parser.builder.impl.UnknownSchemaNodeBuilder; + +/** + * Interface for all builders of SchemaNode nodes. + */ +public interface SchemaNodeBuilder extends Builder { + + QName getQName(); + + void setPath(SchemaPath schemaPath); + + void setDescription(String description); + + void setReference(String reference); + + void setStatus(Status status); + + void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder); + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/TypeDefinitionBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/TypeDefinitionBuilder.java index fc7ef22b80..a303b969a6 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/TypeDefinitionBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/api/TypeDefinitionBuilder.java @@ -1,24 +1,28 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.yang.model.parser.builder.api; - -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.TypeDefinition; - - -/** - * Interface for builders of 'typedef' statement. - */ -public interface TypeDefinitionBuilder { - - QName getQName(); - TypeDefinition getBaseType(); - TypeDefinition build(); - void setUnits(String units); - -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.yang.model.parser.builder.api; + +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.TypeDefinition; + +/** + * Interface for builders of 'typedef' statement. + */ +public interface TypeDefinitionBuilder { + + QName getQName(); + + TypeDefinition getBaseType(); + + TypeDefinition build(); + + void setUnits(String units); + + void setDefaultValue(Object defaultValue); + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ContainerSchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ContainerSchemaNodeBuilder.java index 953b22e174..9b965daae6 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ContainerSchemaNodeBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ContainerSchemaNodeBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -43,6 +44,7 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder private final Set addedTypedefs = new HashSet(); private final Set augmentations = new HashSet(); private final Set addedUsesNodes = new HashSet(); + private final List addedUnknownNodes = new ArrayList(); ContainerSchemaNodeBuilder(QName qname) { super(qname); @@ -80,6 +82,13 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder } instance.setUses(uses); + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + instance.setConstraints(constraintsBuilder.build()); instance.setAvailableAugmentations(augmentations); @@ -140,6 +149,11 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder instance.setPresenceContainer(presence); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private class ContainerSchemaNodeImpl implements ContainerSchemaNode { private final QName qname; @@ -155,6 +169,7 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder private Set groupings = Collections.emptySet(); private Set> typeDefinitions = Collections.emptySet(); private Set uses = Collections.emptySet(); + private List unknownSchemaNodes = Collections.emptyList(); private boolean presence; private ContainerSchemaNodeImpl(QName qname) { @@ -315,7 +330,13 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder @Override public List getUnknownSchemaNodes() { - return Collections.emptyList(); + return unknownSchemaNodes; + } + + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } } @Override @@ -323,6 +344,7 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder final int prime = 31; int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); + result = prime * result + ((path == null) ? 0 : path.hashCode()); return result; } @@ -345,6 +367,13 @@ public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder } else if (!qname.equals(other.qname)) { return false; } + if (path == null) { + if (other.path != null) { + return false; + } + } else if (!path.equals(other.path)) { + return false; + } return true; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ExtensionBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ExtensionBuilder.java index 735feb1ab7..fa5c4954ad 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ExtensionBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ExtensionBuilder.java @@ -22,12 +22,12 @@ public class ExtensionBuilder implements SchemaNodeBuilder { private final ExtensionDefinitionImpl instance; private final QName qname; - private final List addedExtensions; + private final List addedExtensions = new ArrayList(); + private final List addedUnknownNodes = new ArrayList(); ExtensionBuilder(QName qname) { this.qname = qname; instance = new ExtensionDefinitionImpl(qname); - addedExtensions = new ArrayList(); } @Override @@ -73,6 +73,11 @@ public class ExtensionBuilder implements SchemaNodeBuilder { instance.setStatus(status); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private static class ExtensionDefinitionImpl implements ExtensionDefinition { private final QName qname; private SchemaPath schemaPath; diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/FeatureBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/FeatureBuilder.java index 23c74fd0bc..b861700350 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/FeatureBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/FeatureBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -21,6 +22,7 @@ public class FeatureBuilder implements SchemaNodeBuilder { private final FeatureDefinitionImpl instance; private final QName qname; + private final List addedUnknownNodes = new ArrayList(); FeatureBuilder(QName qname) { this.qname = qname; @@ -29,6 +31,13 @@ public class FeatureBuilder implements SchemaNodeBuilder { @Override public FeatureDefinitionImpl build() { + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + return instance; } @@ -57,6 +66,11 @@ public class FeatureBuilder implements SchemaNodeBuilder { instance.setStatus(status); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private static class FeatureDefinitionImpl implements FeatureDefinition { private final QName qname; private SchemaPath path; @@ -117,18 +131,18 @@ public class FeatureBuilder implements SchemaNodeBuilder { return unknownSchemaNodes; } + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } + } + @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.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()); return result; } @@ -158,27 +172,6 @@ public class FeatureBuilder implements SchemaNodeBuilder { } else if (!path.equals(other.path)) { 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) { - return false; - } - } else if (!status.equals(other.status)) { - return false; - } return true; } @@ -187,10 +180,7 @@ public class FeatureBuilder implements SchemaNodeBuilder { StringBuilder sb = new StringBuilder( FeatureDefinitionImpl.class.getSimpleName()); sb.append("[name=" + qname); - sb.append(", path=" + path); - sb.append(", description=" + description); - sb.append(", reference=" + reference); - sb.append(", status=" + status + "]"); + sb.append(", path=" + path + "]"); return sb.toString(); } } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/GroupingBuilderImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/GroupingBuilderImpl.java index 1bacca8d5b..03dd15b504 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/GroupingBuilderImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/GroupingBuilderImpl.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -34,6 +35,7 @@ public class GroupingBuilderImpl implements GroupingBuilder { private final Set groupings = new HashSet(); private final Set addedTypedefs = new HashSet(); private final Set usesNodes = new HashSet(); + private final List addedUnknownNodes = new ArrayList(); GroupingBuilderImpl(QName qname) { this.instance = new GroupingDefinitionImpl(qname); @@ -69,6 +71,13 @@ public class GroupingBuilderImpl implements GroupingBuilder { } instance.setUses(usesNodeDefinitions); + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + return instance; } @@ -120,16 +129,22 @@ public class GroupingBuilderImpl implements GroupingBuilder { usesNodes.add(usesBuilder); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private static class GroupingDefinitionImpl implements GroupingDefinition { private final QName qname; private SchemaPath path; private String description; private String reference; private Status status; - private Map childNodes; - private Set groupings; - private Set> typeDefinitions; - private Set uses; + private Map childNodes = Collections.emptyMap(); + private Set groupings = Collections.emptySet(); + private Set> typeDefinitions = Collections.emptySet(); + private Set uses = Collections.emptySet(); + private List unknownSchemaNodes = Collections.emptyList(); private GroupingDefinitionImpl(QName qname) { this.qname = qname; @@ -214,7 +229,13 @@ public class GroupingBuilderImpl implements GroupingBuilder { @Override public List getUnknownSchemaNodes() { - return Collections.emptyList(); + return unknownSchemaNodes; + } + + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } } @Override @@ -240,21 +261,6 @@ public class GroupingBuilderImpl implements GroupingBuilder { int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.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()); - result = prime * result - + ((childNodes == null) ? 0 : childNodes.hashCode()); - result = prime * result - + ((groupings == null) ? 0 : groupings.hashCode()); - result = prime - * result - + ((typeDefinitions == null) ? 0 : typeDefinitions - .hashCode()); - result = prime * result + ((uses == null) ? 0 : uses.hashCode()); return result; } @@ -284,55 +290,6 @@ public class GroupingBuilderImpl implements GroupingBuilder { } else if (!path.equals(other.path)) { 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) { - return false; - } - } else if (!status.equals(other.status)) { - 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 (typeDefinitions == null) { - if (other.typeDefinitions != null) { - return false; - } - } else if (!typeDefinitions.equals(other.typeDefinitions)) { - return false; - } - if (uses == null) { - if (other.uses != null) { - return false; - } - } else if (!uses.equals(other.uses)) { - return false; - } return true; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafListSchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafListSchemaNodeBuilder.java index 99d3e64794..5a9fdb5815 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafListSchemaNodeBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafListSchemaNodeBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -26,13 +27,13 @@ public class LeafListSchemaNodeBuilder implements SchemaNodeBuilder, private final LeafListSchemaNodeImpl instance; private final QName qname; - private final ConstraintsBuilder constraintsBuilder; + private final ConstraintsBuilder constraintsBuilder = new ConstraintsBuilder(); + private final List addedUnknownNodes = new ArrayList(); private TypeDefinition type; LeafListSchemaNodeBuilder(QName qname) { this.qname = qname; instance = new LeafListSchemaNodeImpl(qname); - constraintsBuilder = new ConstraintsBuilder(); } @Override @@ -98,6 +99,11 @@ public class LeafListSchemaNodeBuilder implements SchemaNodeBuilder, instance.setUserOrdered(userOrdered); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private class LeafListSchemaNodeImpl implements LeafListSchemaNode { private final QName qname; private SchemaPath path; @@ -206,24 +212,18 @@ public class LeafListSchemaNodeBuilder implements SchemaNodeBuilder, return unknownSchemaNodes; } + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } + } + @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.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()); - result = prime * result + (augmenting ? 1231 : 1237); - result = prime * result + (configuration ? 1231 : 1237); - result = prime * result - + ((constraints == null) ? 0 : constraints.hashCode()); - result = prime * result + ((type == null) ? 0 : type.hashCode()); - result = prime * result + (userOrdered ? 1231 : 1237); return result; } @@ -253,50 +253,6 @@ public class LeafListSchemaNodeBuilder implements SchemaNodeBuilder, } else if (!path.equals(other.path)) { 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) { - return false; - } - } else if (!status.equals(other.status)) { - return false; - } - if (augmenting != other.augmenting) { - return false; - } - if (configuration != other.configuration) { - return false; - } - if (constraints == null) { - if (other.constraints != null) { - return false; - } - } else if (!constraints.equals(other.constraints)) { - return false; - } - if (type == null) { - if (other.type != null) { - return false; - } - } else if (!type.equals(other.type)) { - return false; - } - if (userOrdered != other.userOrdered) { - return false; - } return true; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafSchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafSchemaNodeBuilder.java index 5c28e1ba2c..bd18ba50a3 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafSchemaNodeBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/LeafSchemaNodeBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -26,17 +27,24 @@ public class LeafSchemaNodeBuilder implements DataSchemaNodeBuilder, private final QName qname; private final LeafSchemaNodeImpl instance; - private final ConstraintsBuilder constraintsBuilder; + private final ConstraintsBuilder constraintsBuilder = new ConstraintsBuilder(); + private final List addedUnknownNodes = new ArrayList(); private TypeDefinition type; LeafSchemaNodeBuilder(QName qname) { this.qname = qname; instance = new LeafSchemaNodeImpl(qname); - constraintsBuilder = new ConstraintsBuilder(); } @Override public LeafSchemaNode build() { + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + instance.setConstraints(constraintsBuilder.build()); return instance; } @@ -94,6 +102,11 @@ public class LeafSchemaNodeBuilder implements DataSchemaNodeBuilder, instance.setType(type); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private class LeafSchemaNodeImpl implements LeafSchemaNode { private final QName qname; private SchemaPath path; @@ -194,23 +207,18 @@ public class LeafSchemaNodeBuilder implements DataSchemaNodeBuilder, return unknownSchemaNodes; } + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } + } + @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.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()); - result = prime * result + (augmenting ? 1231 : 1237); - result = prime * result + (configuration ? 1231 : 1237); - result = prime * result - + ((constraints == null) ? 0 : constraints.hashCode()); - result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; } @@ -240,47 +248,6 @@ public class LeafSchemaNodeBuilder implements DataSchemaNodeBuilder, } else if (!path.equals(other.path)) { 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) { - return false; - } - } else if (!status.equals(other.status)) { - return false; - } - if (augmenting != other.augmenting) { - return false; - } - if (configuration != other.configuration) { - return false; - } - if (constraints == null) { - if (other.constraints != null) { - return false; - } - } else if (!constraints.equals(other.constraints)) { - return false; - } - if (type == null) { - if (other.type != null) { - return false; - } - } else if (!type.equals(other.type)) { - return false; - } return true; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ListSchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ListSchemaNodeBuilder.java index 2a4befe1e3..ccca99c2bc 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ListSchemaNodeBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ListSchemaNodeBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -44,6 +45,7 @@ public class ListSchemaNodeBuilder extends AbstractChildNodeBuilder implements private final Set addedTypedefs = new HashSet(); private final Set augmentations = new HashSet(); private final Set usesNodes = new HashSet(); + private final List addedUnknownNodes = new ArrayList(); ListSchemaNodeBuilder(QName qname) { super(qname); @@ -81,6 +83,13 @@ public class ListSchemaNodeBuilder extends AbstractChildNodeBuilder implements } instance.setGroupings(groupingDefinitions); + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + instance.setConstraints(constraintsBuilder.build()); instance.setAvailableAugmentations(augmentations); @@ -147,6 +156,11 @@ public class ListSchemaNodeBuilder extends AbstractChildNodeBuilder implements instance.setUserOrdered(userOrdered); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private class ListSchemaNodeImpl implements ListSchemaNode { private final QName qname; private SchemaPath path; @@ -339,36 +353,18 @@ public class ListSchemaNodeBuilder extends AbstractChildNodeBuilder implements return unknownSchemaNodes; } + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } + } + @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.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()); - result = prime * result - + ((keyDefinition == null) ? 0 : keyDefinition.hashCode()); - result = prime * result + (augmenting ? 1231 : 1237); - result = prime * result + (configuration ? 1231 : 1237); - result = prime * result - + ((constraints == null) ? 0 : constraints.hashCode()); - result = prime * result - + ((augmentations == null) ? 0 : augmentations.hashCode()); - result = prime * result - + ((childNodes == null) ? 0 : childNodes.hashCode()); - result = prime - * result - + ((typeDefinitions == null) ? 0 : typeDefinitions - .hashCode()); - result = prime * result - + ((groupings == null) ? 0 : groupings.hashCode()); - result = prime * result + ((uses == null) ? 0 : uses.hashCode()); - result = prime * result + (userOrdered ? 1231 : 1237); return result; } @@ -398,85 +394,6 @@ public class ListSchemaNodeBuilder extends AbstractChildNodeBuilder implements } else if (!path.equals(other.path)) { 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) { - return false; - } - } else if (!status.equals(other.status)) { - return false; - } - if (keyDefinition == null) { - if (other.keyDefinition != null) { - return false; - } - } else if (!keyDefinition.equals(other.keyDefinition)) { - return false; - } - if (augmenting != other.augmenting) { - return false; - } - if (configuration != other.configuration) { - return false; - } - if (constraints == null) { - if (other.constraints != null) { - return false; - } - } else if (!constraints.equals(other.constraints)) { - return false; - } - if (augmentations == null) { - if (other.augmentations != null) { - return false; - } - } else if (!augmentations.equals(other.augmentations)) { - return false; - } - if (childNodes == null) { - if (other.childNodes != null) { - return false; - } - } else if (!childNodes.equals(other.childNodes)) { - return false; - } - if (typeDefinitions == null) { - if (other.typeDefinitions != null) { - return false; - } - } else if (!typeDefinitions.equals(other.typeDefinitions)) { - return false; - } - if (groupings == null) { - if (other.groupings != null) { - return false; - } - } else if (!groupings.equals(other.groupings)) { - return false; - } - if (uses == null) { - if (other.uses != null) { - return false; - } - } else if (!uses.equals(other.uses)) { - return false; - } - if (userOrdered != other.userOrdered) { - return false; - } return true; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/NotificationBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/NotificationBuilder.java index 783b21e04b..d2010c6578 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/NotificationBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/NotificationBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -38,6 +39,7 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements private final NotificationDefinitionImpl instance; private final Set addedTypedefs = new HashSet(); private final Set addedUsesNodes = new HashSet(); + private final List addedUnknownNodes = new ArrayList(); NotificationBuilder(QName qname) { super(qname); @@ -74,6 +76,13 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements } instance.setUses(uses); + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + return instance; } @@ -107,6 +116,11 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements instance.setStatus(status); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private class NotificationDefinitionImpl implements NotificationDefinition { private final QName qname; @@ -214,6 +228,12 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements return unknownSchemaNodes; } + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } + } + @Override public DataSchemaNode getDataChildByName(QName name) { return childNodes.get(name); @@ -237,19 +257,6 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); result = prime * result + ((path == null) ? 0 : path.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()); - result = prime * result - + ((childNodes == null) ? 0 : childNodes.hashCode()); - result = prime * result - + ((groupings == null) ? 0 : groupings.hashCode()); - result = prime * result - + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode()); - result = prime * result + ((uses == null) ? 0 : uses.hashCode()); return result; } @@ -279,55 +286,6 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements } else if (!path.equals(other.path)) { 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) { - return false; - } - } else if (!status.equals(other.status)) { - 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 (typeDefinitions == null) { - if (other.typeDefinitions != null) { - return false; - } - } else if (!typeDefinitions.equals(other.typeDefinitions)) { - return false; - } - if (uses == null) { - if (other.uses != null) { - return false; - } - } else if (!uses.equals(other.uses)) { - return false; - } return true; } @@ -335,7 +293,7 @@ public class NotificationBuilder extends AbstractChildNodeBuilder implements public String toString() { StringBuilder sb = new StringBuilder( NotificationDefinitionImpl.class.getSimpleName()); - sb.append("[qname=" + qname + "]"); + sb.append("[qname=" + qname + ", path="+ path +"]"); return sb.toString(); } } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/RpcDefinitionBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/RpcDefinitionBuilder.java index 822a8359c4..28ee66751e 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/RpcDefinitionBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/RpcDefinitionBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -37,6 +38,7 @@ public class RpcDefinitionBuilder implements ChildNodeBuilder, private ContainerSchemaNodeBuilder outputBuilder; private final Set addedTypedefs = new HashSet(); private final Set addedGroupings = new HashSet(); + private final List addedUnknownNodes = new ArrayList(); RpcDefinitionBuilder(QName qname) { this.qname = qname; @@ -64,6 +66,13 @@ public class RpcDefinitionBuilder implements ChildNodeBuilder, } instance.setGroupings(groupings); + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); + return instance; } @@ -122,6 +131,11 @@ public class RpcDefinitionBuilder implements ChildNodeBuilder, "Can not add uses node to rpc definition: rpc can not contains uses nodes."); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + @Override public int hashCode() { return qname.hashCode(); @@ -245,26 +259,17 @@ public class RpcDefinitionBuilder implements ChildNodeBuilder, return unknownSchemaNodes; } + private void setUnknownSchemaNodes(List unknownSchemaNodes) { + if(unknownSchemaNodes != null) { + this.unknownSchemaNodes = unknownSchemaNodes; + } + } + @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((qname == null) ? 0 : qname.hashCode()); - result = prime * result + ((path == null) ? 0 : path.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()); - result = prime * result - + ((input == null) ? 0 : input.hashCode()); - result = prime * result - + ((output == null) ? 0 : output.hashCode()); - result = prime * result - + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode()); - result = prime * result - + ((groupings == null) ? 0 : groupings.hashCode()); return result; } @@ -287,62 +292,6 @@ public class RpcDefinitionBuilder implements ChildNodeBuilder, } else if (!qname.equals(other.qname)) { return false; } - if (path == null) { - if (other.path != null) { - return false; - } - } else if (!path.equals(other.path)) { - 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) { - return false; - } - } else if (!status.equals(other.status)) { - return false; - } - if (input == null) { - if (other.input != null) { - return false; - } - } else if (!input.equals(other.input)) { - return false; - } - if (output == null) { - if (other.output != null) { - return false; - } - } else if (!output.equals(other.output)) { - return false; - } - if (typeDefinitions == null) { - if (other.typeDefinitions != null) { - return false; - } - } else if (!typeDefinitions.equals(other.typeDefinitions)) { - return false; - } - if (groupings == null) { - if (other.groupings != null) { - return false; - } - } else if (!groupings.equals(other.groupings)) { - return false; - } return true; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/TypedefBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/TypedefBuilder.java index 1a2c43d148..3a8813fd65 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/TypedefBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/TypedefBuilder.java @@ -32,6 +32,7 @@ public class TypedefBuilder implements TypeDefinitionBuilder, private String reference; private Status status; private String units; + private Object defaultValue; TypedefBuilder(QName qname) { this.qname = qname; @@ -53,6 +54,7 @@ public class TypedefBuilder implements TypeDefinitionBuilder, instance.setPath(schemaPath); instance.setBaseType(baseType); instance.setUnits(units); + instance.setDefaultValue(defaultValue); return instance; } else { // type @@ -96,6 +98,11 @@ public class TypedefBuilder implements TypeDefinitionBuilder, this.units = units; } + @Override + public void setDefaultValue(Object defaultValue) { + this.defaultValue = defaultValue; + } + @Override public TypeDefinition getType() { return baseType; @@ -111,6 +118,11 @@ public class TypedefBuilder implements TypeDefinitionBuilder, return baseType; } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + // TODO + } + private static class TypeDefinitionImpl> implements TypeDefinition { @@ -119,6 +131,7 @@ public class TypedefBuilder implements TypeDefinitionBuilder, private String description; private String reference; private Status status = Status.CURRENT; + private Object defaultValue; private T baseType; private String units; private List unknownSchemaNodes = Collections.emptyList(); @@ -188,8 +201,11 @@ public class TypedefBuilder implements TypeDefinitionBuilder, @Override public Object getDefaultValue() { - // TODO Auto-generated method stub - return null; + return defaultValue; + } + + private void setDefaultValue(Object defaultValue) { + this.defaultValue = defaultValue; } @Override diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/UnknownSchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/UnknownSchemaNodeBuilder.java index d0bb01cf34..91c6ed197e 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/UnknownSchemaNodeBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/UnknownSchemaNodeBuilder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.builder.impl; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -20,6 +21,7 @@ public class UnknownSchemaNodeBuilder implements SchemaNodeBuilder { private final QName qname; private final UnknownSchemaNodeImpl instance; + private final List addedUnknownNodes = new ArrayList(); UnknownSchemaNodeBuilder(final QName qname) { this.qname = qname; @@ -29,6 +31,12 @@ public class UnknownSchemaNodeBuilder implements SchemaNodeBuilder { @Override public UnknownSchemaNode build() { + // UNKNOWN NODES + final List unknownNodes = new ArrayList(); + for(UnknownSchemaNodeBuilder b : addedUnknownNodes) { + unknownNodes.add(b.build()); + } + instance.setUnknownSchemaNodes(unknownNodes); return instance; } @@ -57,6 +65,11 @@ public class UnknownSchemaNodeBuilder implements SchemaNodeBuilder { instance.setStatus(status); } + @Override + public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + addedUnknownNodes.add(unknownSchemaNodeBuilder); + } + private static class UnknownSchemaNodeImpl implements UnknownSchemaNode { private final QName qname; private SchemaPath path; diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java index d86b49d78b..d7c6711aff 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserImpl.java @@ -26,7 +26,22 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.opendaylight.controller.antlrv4.code.gen.YangLexer; import org.opendaylight.controller.antlrv4.code.gen.YangParser; +import org.opendaylight.controller.model.api.type.BinaryTypeDefinition; +import org.opendaylight.controller.model.api.type.BitsTypeDefinition; +import org.opendaylight.controller.model.api.type.BitsTypeDefinition.Bit; +import org.opendaylight.controller.model.api.type.DecimalTypeDefinition; +import org.opendaylight.controller.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.controller.model.api.type.IntegerTypeDefinition; +import org.opendaylight.controller.model.api.type.LengthConstraint; +import org.opendaylight.controller.model.api.type.PatternConstraint; +import org.opendaylight.controller.model.api.type.RangeConstraint; +import org.opendaylight.controller.model.api.type.StringTypeDefinition; +import org.opendaylight.controller.model.util.BaseConstraints; +import org.opendaylight.controller.model.util.BinaryType; +import org.opendaylight.controller.model.util.BitsType; +import org.opendaylight.controller.model.util.StringType; import org.opendaylight.controller.model.util.UnknownType; +import org.opendaylight.controller.model.util.YangTypesConverter; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.AugmentationSchema; import org.opendaylight.controller.yang.model.api.DataSchemaNode; @@ -37,6 +52,7 @@ import org.opendaylight.controller.yang.model.api.NotificationDefinition; import org.opendaylight.controller.yang.model.api.RpcDefinition; import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder; @@ -72,22 +88,85 @@ public class YangModelParserImpl implements YangModelParser { return new SchemaContextImpl(modules); } + private Map> loadFiles( + String... yangFiles) { + final Map> modules = new HashMap>(); + + final YangModelParserListenerImpl yangModelParser = new YangModelParserListenerImpl(); + final ParseTreeWalker walker = new ParseTreeWalker(); + + List trees = parseFiles(yangFiles); + + ModuleBuilder[] builders = new ModuleBuilder[trees.size()]; + + for (int i = 0; i < trees.size(); i++) { + walker.walk(yangModelParser, trees.get(i)); + builders[i] = yangModelParser.getModuleBuilder(); + } + + for (ModuleBuilder builder : builders) { + final String builderName = builder.getName(); + Date builderRevision = builder.getRevision(); + if (builderRevision == null) { + builderRevision = createEpochTime(); + } + + TreeMap builderByRevision = modules + .get(builderName); + if (builderByRevision == null) { + builderByRevision = new TreeMap(); + } + builderByRevision.put(builderRevision, builder); + + modules.put(builderName, builderByRevision); + } + return modules; + } + + private List parseFiles(String... yangFileNames) { + List trees = new ArrayList(); + for (String fileName : yangFileNames) { + trees.add(parseFile(fileName)); + } + return trees; + } + + private ParseTree parseFile(String yangFileName) { + ParseTree result = null; + try { + final File yangFile = new File(yangFileName); + final FileInputStream inStream = new FileInputStream(yangFile); + final ANTLRInputStream input = new ANTLRInputStream(inStream); + final YangLexer lexer = new YangLexer(input); + final CommonTokenStream tokens = new CommonTokenStream(lexer); + final YangParser parser = new YangParser(tokens); + result = parser.yang(); + } catch (IOException e) { + logger.warn("Exception while reading yang file: " + yangFileName, e); + } + return result; + } + private Set build(Map> modules) { // first validate - for (Map.Entry> entry : modules.entrySet()) { - for (Map.Entry childEntry : entry.getValue().entrySet()) { + for (Map.Entry> entry : modules + .entrySet()) { + for (Map.Entry childEntry : entry.getValue() + .entrySet()) { ModuleBuilder moduleBuilder = childEntry.getValue(); validateBuilder(modules, moduleBuilder); } } - // then build final Set result = new HashSet(); - for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry> entry : modules + .entrySet()) { final Map modulesByRevision = new HashMap(); - for (Map.Entry childEntry : entry.getValue().entrySet()) { + for (Map.Entry childEntry : entry.getValue() + .entrySet()) { ModuleBuilder moduleBuilder = childEntry.getValue(); - modulesByRevision.put(childEntry.getKey(),moduleBuilder.build()); + modulesByRevision.put(childEntry.getKey(), + moduleBuilder.build()); result.add(moduleBuilder.build()); } } @@ -95,47 +174,175 @@ public class YangModelParserImpl implements YangModelParser { return result; } - private void validateBuilder(Map> modules, ModuleBuilder builder) { + private void validateBuilder( + Map> modules, + ModuleBuilder builder) { resolveTypedefs(modules, builder); resolveAugments(modules, builder); } - private void resolveTypedefs(Map> modules, ModuleBuilder builder) { - Map, TypeAwareBuilder> dirtyNodes = builder.getDirtyNodes(); + /** + * Search for dirty nodes (node which contains UnknownType) and resolve + * unknown types. + * + * @param modules + * all available modules + * @param builder + * current module + */ + private void resolveTypedefs( + Map> modules, + ModuleBuilder builder) { + Map, TypeAwareBuilder> dirtyNodes = builder + .getDirtyNodes(); if (dirtyNodes.size() == 0) { return; } else { - for (Map.Entry, TypeAwareBuilder> entry : dirtyNodes.entrySet()) { - TypeAwareBuilder tab = entry.getValue(); - TypeDefinitionBuilder tdb = findTypeDefinitionBuilder(modules,entry.getValue(), builder); - tab.setType(tdb.build()); + for (Map.Entry, TypeAwareBuilder> entry : dirtyNodes + .entrySet()) { + TypeAwareBuilder typeToResolve = entry.getValue(); + Map foundedTypeDefinitionBuilder = findTypeDefinitionBuilderWithConstraints( + modules, entry.getValue(), builder); + TypeDefinitionBuilder targetType = foundedTypeDefinitionBuilder + .entrySet().iterator().next().getKey(); + TypeConstraints constraints = foundedTypeDefinitionBuilder + .entrySet().iterator().next().getValue(); + + UnknownType ut = (UnknownType) typeToResolve.getType(); + + // RANGE + List ranges = ut.getRangeStatements(); + resolveRanges(ranges, typeToResolve, targetType, modules, + builder); + + // LENGTH + List lengths = ut.getLengthStatements(); + resolveLengths(lengths, typeToResolve, targetType, modules, + builder); + + // PATTERN + List patterns = ut.getPatterns(); + + // Fraction Digits + Integer fractionDigits = ut.getFractionDigits(); + + TypeDefinition type = targetType.getBaseType(); + String typeName = type.getQName().getLocalName(); + + // MERGE CONSTRAINTS (enumeration and leafref omitted because + // they have no restrictions) + if (type instanceof DecimalTypeDefinition) { + List fullRanges = new ArrayList(); + fullRanges.addAll(constraints.getRanges()); + fullRanges.addAll(ranges); + Integer fd = fractionDigits == null ? constraints + .getFractionDigits() : fractionDigits; + type = YangTypesConverter.javaTypeForBaseYangDecimal64Type( + fullRanges, fd); + } else if (type instanceof IntegerTypeDefinition) { + List fullRanges = new ArrayList(); + fullRanges.addAll(constraints.getRanges()); + fullRanges.addAll(ranges); + if (typeName.startsWith("int")) { + type = YangTypesConverter + .javaTypeForBaseYangSignedIntegerType(typeName, + fullRanges); + } else { + type = YangTypesConverter + .javaTypeForBaseYangUnsignedIntegerType( + typeName, fullRanges); + } + } else if (type instanceof StringTypeDefinition) { + List fullLengths = new ArrayList(); + fullLengths.addAll(constraints.getLengths()); + fullLengths.addAll(lengths); + List fullPatterns = new ArrayList(); + fullPatterns.addAll(constraints.getPatterns()); + fullPatterns.addAll(patterns); + type = new StringType(fullLengths, fullPatterns); + } else if (type instanceof BitsTypeDefinition) { + // TODO: add 'length' restriction to BitsType + BitsTypeDefinition bitsType = (BitsTypeDefinition) type; + List bits = bitsType.getBits(); + type = new BitsType(bits); + } else if (type instanceof BinaryTypeDefinition) { + type = new BinaryType(null, lengths, null); + } else if (typeName.equals("instance-identifier")) { + // TODO: instance-identifier + /* + * boolean requireInstance = isRequireInstance(typeBody); + * type = new InstanceIdentifier(null, requireInstance); + */ + } + typeToResolve.setType(type); } } } - private TypeDefinitionBuilder findTypeDefinitionBuilder(Map> modules, TypeAwareBuilder typeBuilder, ModuleBuilder builder) { + private TypeDefinitionBuilder findTypeDefinitionBuilder( + Map> modules, + TypeAwareBuilder typeBuilder, ModuleBuilder builder) { + Map result = findTypeDefinitionBuilderWithConstraints( + modules, typeBuilder, builder); + return result.entrySet().iterator().next().getKey(); + } + + private Map findTypeDefinitionBuilderWithConstraints( + Map> modules, + TypeAwareBuilder typeBuilder, ModuleBuilder builder) { + return findTypeDefinitionBuilderWithConstraints(new TypeConstraints(), + modules, typeBuilder, builder); + } + + /** + * Traverse through all referenced types chain until base YANG type is + * founded. + * + * @param constraints + * current type constraints + * @param modules + * all available modules + * @param typeBuilder + * type builder which contains type + * @param builder + * current module + * @return map, where key is type referenced and value is its constraints + */ + private Map findTypeDefinitionBuilderWithConstraints( + TypeConstraints constraints, + Map> modules, + TypeAwareBuilder typeBuilder, ModuleBuilder builder) { + Map result = new HashMap(); + UnknownType type = (UnknownType) typeBuilder.getType(); QName typeQName = type.getQName(); String typeName = type.getQName().getLocalName(); String prefix = typeQName.getPrefix(); + // search for module which contains referenced typedef ModuleBuilder dependentModuleBuilder; if (prefix.equals(builder.getPrefix())) { dependentModuleBuilder = builder; } else { - ModuleImport dependentModuleImport = getDependentModuleImport(builder, prefix); + ModuleImport dependentModuleImport = getModuleImport(builder, + prefix); String dependentModuleName = dependentModuleImport.getModuleName(); Date dependentModuleRevision = dependentModuleImport.getRevision(); - TreeMap moduleBuildersByRevision = modules.get(dependentModuleName); - if(dependentModuleRevision == null) { - dependentModuleBuilder = moduleBuildersByRevision.lastEntry().getValue(); + TreeMap moduleBuildersByRevision = modules + .get(dependentModuleName); + if (dependentModuleRevision == null) { + dependentModuleBuilder = moduleBuildersByRevision.lastEntry() + .getValue(); } else { - dependentModuleBuilder = moduleBuildersByRevision.get(dependentModuleRevision); + dependentModuleBuilder = moduleBuildersByRevision + .get(dependentModuleRevision); } } - final Set typedefs = dependentModuleBuilder.getModuleTypedefs(); - + // pull all typedef statements from dependent module... + final Set typedefs = dependentModuleBuilder + .getModuleTypedefs(); + // and search for referenced typedef TypeDefinitionBuilder lookedUpBuilder = null; for (TypeDefinitionBuilder tdb : typedefs) { QName qname = tdb.getQName(); @@ -145,15 +352,65 @@ public class YangModelParserImpl implements YangModelParser { } } - if (lookedUpBuilder.getBaseType() instanceof UnknownType) { - return findTypeDefinitionBuilder(modules, (TypeAwareBuilder) lookedUpBuilder, dependentModuleBuilder); + // if referenced type is UnknownType again, search recursively with + // current constraints + TypeDefinition referencedType = lookedUpBuilder.getBaseType(); + if (referencedType instanceof UnknownType) { + UnknownType unknown = (UnknownType) lookedUpBuilder.getBaseType(); + + final List ranges = unknown.getRangeStatements(); + constraints.addRanges(ranges); + final List lengths = unknown + .getLengthStatements(); + constraints.addLengths(lengths); + final List patterns = unknown.getPatterns(); + constraints.addPatterns(patterns); + return findTypeDefinitionBuilderWithConstraints(constraints, + modules, (TypeAwareBuilder) lookedUpBuilder, + dependentModuleBuilder); } else { - return lookedUpBuilder; + // pull restriction from this base type and add them to + // 'constraints' + if (referencedType instanceof DecimalTypeDefinition) { + constraints.addRanges(((DecimalTypeDefinition) referencedType) + .getRangeStatements()); + constraints + .setFractionDigits(((DecimalTypeDefinition) referencedType) + .getFractionDigits()); + } else if (referencedType instanceof IntegerTypeDefinition) { + constraints.addRanges(((IntegerTypeDefinition) referencedType) + .getRangeStatements()); + } else if (referencedType instanceof StringTypeDefinition) { + constraints.addPatterns(((StringTypeDefinition) referencedType) + .getPatterns()); + } else if (referencedType instanceof BitsTypeDefinition) { + // TODO: add 'length' restriction to BitsType + } else if (referencedType instanceof BinaryTypeDefinition) { + // TODO + } else if (referencedType instanceof InstanceIdentifierTypeDefinition) { + // TODO: instance-identifier + } + + result.put(lookedUpBuilder, constraints); + // return lookedUpBuilder; + return result; } } - private void resolveAugments(Map> modules, ModuleBuilder builder) { - Set augmentBuilders = builder.getAddedAugments(); + /** + * Go through all augmentation definitions and resolve them. This means find + * referenced node and add child nodes to it. + * + * @param modules + * all available modules + * @param builder + * current module + */ + private void resolveAugments( + Map> modules, + ModuleBuilder builder) { + Set augmentBuilders = builder + .getAddedAugments(); Set augments = new HashSet(); for (AugmentationSchemaBuilder augmentBuilder : augmentBuilders) { @@ -164,21 +421,26 @@ public class YangModelParserImpl implements YangModelParser { prefix = pathPart.getPrefix(); augmentTargetPath.add(pathPart.getLocalName()); } - ModuleImport dependentModuleImport = getDependentModuleImport(builder, prefix); + ModuleImport dependentModuleImport = getModuleImport(builder, + prefix); String dependentModuleName = dependentModuleImport.getModuleName(); augmentTargetPath.add(0, dependentModuleName); Date dependentModuleRevision = dependentModuleImport.getRevision(); - TreeMap moduleBuildersByRevision = modules.get(dependentModuleName); + TreeMap moduleBuildersByRevision = modules + .get(dependentModuleName); ModuleBuilder dependentModule; - if(dependentModuleRevision == null) { - dependentModule = moduleBuildersByRevision.lastEntry().getValue(); + if (dependentModuleRevision == null) { + dependentModule = moduleBuildersByRevision.lastEntry() + .getValue(); } else { - dependentModule = moduleBuildersByRevision.get(dependentModuleRevision); + dependentModule = moduleBuildersByRevision + .get(dependentModuleRevision); } - AugmentationTargetBuilder augmentTarget = (AugmentationTargetBuilder) dependentModule.getNode(augmentTargetPath); + AugmentationTargetBuilder augmentTarget = (AugmentationTargetBuilder) dependentModule + .getNode(augmentTargetPath); AugmentationSchema result = augmentBuilder.build(); augmentTarget.addAugmentation(result); fillAugmentTarget(augmentBuilder, (ChildNodeBuilder) augmentTarget); @@ -187,6 +449,12 @@ public class YangModelParserImpl implements YangModelParser { builder.setAugmentations(augments); } + /** + * Add all augment's child nodes to given target. + * + * @param augment + * @param target + */ private void fillAugmentTarget(AugmentationSchemaBuilder augment, ChildNodeBuilder target) { for (DataSchemaNodeBuilder builder : augment.getChildNodes()) { @@ -195,72 +463,143 @@ public class YangModelParserImpl implements YangModelParser { } } - private Map> loadFiles(String... yangFiles) { - final Map> modules = new HashMap>(); - - final YangModelParserListenerImpl yangModelParser = new YangModelParserListenerImpl(); - final ParseTreeWalker walker = new ParseTreeWalker(); - - List trees = parseFiles(yangFiles); - - ModuleBuilder[] builders = new ModuleBuilder[trees.size()]; - - for (int i = 0; i < trees.size(); i++) { - walker.walk(yangModelParser, trees.get(i)); - builders[i] = yangModelParser.getModuleBuilder(); - } - - for (ModuleBuilder builder : builders) { - final String builderName = builder.getName(); - Date builderRevision = builder.getRevision(); - if(builderRevision == null) { - builderRevision = createEpochTime(); - } - - TreeMap builderByRevision = modules.get(builderName); - if (builderByRevision == null) { - builderByRevision = new TreeMap(); + /** + * Get module import referenced by given prefix. + * + * @param builder + * module to search + * @param prefix + * prefix associated with import + * @return ModuleImport based on given prefix + */ + private ModuleImport getModuleImport(ModuleBuilder builder, String prefix) { + ModuleImport moduleImport = null; + for (ModuleImport mi : builder.getModuleImports()) { + if (mi.getPrefix().equals(prefix)) { + moduleImport = mi; + break; } - builderByRevision.put(builderRevision, builder); - - modules.put(builderName, builderByRevision); } - return modules; + return moduleImport; } - private List parseFiles(String... yangFileNames) { - List trees = new ArrayList(); - for (String fileName : yangFileNames) { - trees.add(parseFile(fileName)); + /** + * Helper method for resolving special 'min' or 'max' values in range + * constraint + * + * @param ranges + * ranges to resolve + * @param typeToResolve + * type to resolve + * @param targetType + * target type + * @param modules + * all available modules + * @param builder + * current module + */ + private void resolveRanges(List ranges, + TypeAwareBuilder typeToResolve, TypeDefinitionBuilder targetType, + Map> modules, + ModuleBuilder builder) { + if (ranges != null && ranges.size() > 0) { + Long min = (Long) ranges.get(0).getMin(); + Long max = (Long) ranges.get(ranges.size() - 1).getMax(); + // if range contains one of the special values 'min' or 'max' + if (min.equals(Long.MIN_VALUE) || max.equals(Long.MAX_VALUE)) { + Long[] values = parseRangeConstraint(typeToResolve, targetType, + modules, builder); + if (min.equals(Long.MIN_VALUE)) { + min = values[0]; + RangeConstraint oldFirst = ranges.get(0); + RangeConstraint newFirst = BaseConstraints.rangeConstraint( + min, oldFirst.getMax(), oldFirst.getDescription(), + oldFirst.getReference()); + ranges.set(0, newFirst); + } + if (max.equals(Long.MAX_VALUE)) { + max = values[1]; + RangeConstraint oldLast = ranges.get(ranges.size() - 1); + RangeConstraint newLast = BaseConstraints.rangeConstraint( + oldLast.getMin(), max, oldLast.getDescription(), + oldLast.getReference()); + ranges.set(ranges.size() - 1, newLast); + } + } } - return trees; } - private ParseTree parseFile(String yangFileName) { - ParseTree result = null; - try { - final File yangFile = new File(yangFileName); - final FileInputStream inStream = new FileInputStream(yangFile); - final ANTLRInputStream input = new ANTLRInputStream(inStream); - final YangLexer lexer = new YangLexer(input); - final CommonTokenStream tokens = new CommonTokenStream(lexer); - final YangParser parser = new YangParser(tokens); - result = parser.yang(); - } catch (IOException e) { - logger.warn("Exception while reading yang file: " + yangFileName, e); + /** + * Helper method for resolving special 'min' or 'max' values in length + * constraint + * + * @param ranges + * ranges to resolve + * @param typeToResolve + * type to resolve + * @param targetType + * target type + * @param modules + * all available modules + * @param builder + * current module + */ + private void resolveLengths(List lengths, + TypeAwareBuilder typeToResolve, TypeDefinitionBuilder targetType, + Map> modules, + ModuleBuilder builder) { + if (lengths != null && lengths.size() > 0) { + Long min = lengths.get(0).getMin(); + Long max = lengths.get(lengths.size() - 1).getMax(); + // if length contains one of the special values 'min' or 'max' + if (min.equals(Long.MIN_VALUE) || max.equals(Long.MAX_VALUE)) { + Long[] values = parseRangeConstraint(typeToResolve, targetType, + modules, builder); + if (min.equals(Long.MIN_VALUE)) { + min = values[0]; + LengthConstraint oldFirst = lengths.get(0); + LengthConstraint newFirst = BaseConstraints + .lengthConstraint(min, oldFirst.getMax(), + oldFirst.getDescription(), + oldFirst.getReference()); + lengths.set(0, newFirst); + } + if (max.equals(Long.MAX_VALUE)) { + max = values[1]; + LengthConstraint oldLast = lengths.get(lengths.size() - 1); + LengthConstraint newLast = BaseConstraints + .lengthConstraint(oldLast.getMin(), max, + oldLast.getDescription(), + oldLast.getReference()); + lengths.set(lengths.size() - 1, newLast); + } + } } - return result; } - private ModuleImport getDependentModuleImport(ModuleBuilder builder, String prefix) { - ModuleImport moduleImport = null; - for (ModuleImport mi : builder.getModuleImports()) { - if (mi.getPrefix().equals(prefix)) { - moduleImport = mi; - break; - } + private Long[] parseRangeConstraint(TypeAwareBuilder typeToResolve, + TypeDefinitionBuilder targetType, + Map> modules, + ModuleBuilder builder) { + TypeDefinition targetBaseType = targetType.getBaseType(); + + if (targetBaseType instanceof IntegerTypeDefinition) { + IntegerTypeDefinition itd = (IntegerTypeDefinition) targetBaseType; + List ranges = itd.getRangeStatements(); + Long min = (Long) ranges.get(0).getMin(); + Long max = (Long) ranges.get(ranges.size() - 1).getMax(); + return new Long[] { min, max }; + } else if (targetBaseType instanceof DecimalTypeDefinition) { + DecimalTypeDefinition dtd = (DecimalTypeDefinition) targetBaseType; + List ranges = dtd.getRangeStatements(); + Long min = (Long) ranges.get(0).getMin(); + Long max = (Long) ranges.get(ranges.size() - 1).getMax(); + return new Long[] { min, max }; + } else { + return parseRangeConstraint(typeToResolve, + findTypeDefinitionBuilder(modules, typeToResolve, builder), + modules, builder); } - return moduleImport; } private Date createEpochTime() { @@ -318,4 +657,45 @@ public class YangModelParserImpl implements YangModelParser { } } + private static class TypeConstraints { + private final List ranges = new ArrayList(); + private final List lengths = new ArrayList(); + private final List patterns = new ArrayList(); + private Integer fractionDigits; + + public List getRanges() { + return ranges; + } + + public void addRanges(List ranges) { + this.ranges.addAll(0, ranges); + } + + public List getLengths() { + return lengths; + } + + public void addLengths(List lengths) { + this.lengths.addAll(0, lengths); + } + + public List getPatterns() { + return patterns; + } + + public void addPatterns(List patterns) { + this.patterns.addAll(0, patterns); + } + + public Integer getFractionDigits() { + return fractionDigits; + } + + public void setFractionDigits(Integer fractionDigits) { + if (fractionDigits != null) { + this.fractionDigits = fractionDigits; + } + } + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java index 022798e0d9..d5dfa9a215 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/util/YangModelBuilderUtil.java @@ -932,10 +932,12 @@ public class YangModelBuilderUtil { List rangeStatements = getRangeConstraints(ctx); List lengthStatements = getLengthConstraints(ctx); List patternStatements = getPatternConstraint(ctx); + Integer fractionDigits = getFractionDigits(ctx); ut.rangeStatements(rangeStatements); ut.lengthStatements(lengthStatements); ut.patterns(patternStatements); + ut.fractionDigits(fractionDigits); } return ut.build(); @@ -973,7 +975,10 @@ public class YangModelBuilderUtil { type = YangTypesConverter.javaTypeForBaseYangDecimal64Type( rangeStatements, fractionDigits); } else if (typeName.startsWith("int") || typeName.startsWith("uint")) { - type = YangTypesConverter.javaTypeForBaseYangIntegerType(typeName, + type = YangTypesConverter.javaTypeForBaseYangSignedIntegerType(typeName, + rangeStatements); + } else if(typeName.startsWith("uint")) { + type = YangTypesConverter.javaTypeForBaseYangUnsignedIntegerType(typeName, rangeStatements); } else if (typeName.equals("enumeration")) { type = new EnumerationType(enumConstants); @@ -990,7 +995,7 @@ public class YangModelBuilderUtil { type = new Leafref(xpath); } else if (typeName.equals("binary")) { type = new BinaryType(null, lengthStatements, null); - } else if (typeName.equals("instanceidentifier")) { + } else if (typeName.equals("instance-identifier")) { boolean requireInstance = isRequireInstance(typeBody); type = new InstanceIdentifier(null, requireInstance); } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java index 74cfed5fb1..889290f65c 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java @@ -9,17 +9,24 @@ package org.opendaylight.controller.yang.model.parser.impl; import static org.junit.Assert.*; -import java.io.IOException; +import java.util.HashSet; +import java.util.List; import java.util.Set; import org.junit.Before; import org.junit.Test; +import org.opendaylight.controller.model.api.type.IntegerTypeDefinition; +import org.opendaylight.controller.model.api.type.PatternConstraint; +import org.opendaylight.controller.model.api.type.RangeConstraint; +import org.opendaylight.controller.model.util.Decimal64; import org.opendaylight.controller.model.util.Int32; +import org.opendaylight.controller.model.util.StringType; import org.opendaylight.controller.yang.model.api.AugmentationSchema; import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; 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.TypeDefinition; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; public class YangModelParserTest { @@ -29,7 +36,7 @@ public class YangModelParserTest { private YangModelParser tested; @Before - public void init() throws IOException { + public void init() { tested = new YangModelParserImpl(); } @@ -68,13 +75,14 @@ public class YangModelParserTest { ListSchemaNode list = (ListSchemaNode)container.getDataChildByName("ifEntry"); assertNotNull(list); + assertEquals(1, list.getAvailableAugmentations().size()); LeafSchemaNode leaf = (LeafSchemaNode)list.getDataChildByName("ds0ChannelNumber"); assertNotNull(leaf); } @Test - public void testTypeDef() { + public void testTypedefRangesResolving() { Set modules = tested.parseYangModels(testFile1, testFile2); assertEquals(2, modules.size()); @@ -87,7 +95,141 @@ public class YangModelParserTest { assertNotNull(m1); LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("testleaf"); + TypeDefinition baseType = testleaf.getType().getBaseType(); + assertTrue(testleaf.getType().getBaseType() instanceof Int32); + Int32 baseTypeCast = (Int32)baseType; + List ranges = baseTypeCast.getRangeStatements(); + assertEquals(2, ranges.size()); + RangeConstraint range = ranges.get(0); + assertEquals(2L, range.getMin()); + assertEquals(20L, range.getMax()); + } + + @Test + public void testTypedefPatternsResolving() { + Set modules = tested.parseYangModels(testFile1, testFile2); + assertEquals(2, modules.size()); + + Module m1 = null; + for(Module m : modules) { + if(m.getName().equals("types1")) { + m1 = m; + } + } + assertNotNull(m1); + + LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-string-leaf"); + TypeDefinition baseType = testleaf.getType().getBaseType(); + assertTrue(testleaf.getType().getBaseType() instanceof StringType); + StringType baseTypeCast = (StringType)baseType; + + Set expectedRegularExpressions = new HashSet(); + expectedRegularExpressions.add("[a-k]*"); + expectedRegularExpressions.add("[b-u]*"); + expectedRegularExpressions.add("[e-z]*"); + + Set actualRegularExpressions = new HashSet(); + List patterns = baseTypeCast.getPatterns(); + for(PatternConstraint pc : patterns) { + actualRegularExpressions.add(pc.getRegularExpression()); + } + + assertEquals(expectedRegularExpressions, actualRegularExpressions); + } + + @Test + public void testTypedefLengthsResolving() { + Set modules = tested.parseYangModels(testFile1, testFile2); + assertEquals(2, modules.size()); + + Module m1 = null; + for(Module m : modules) { + if(m.getName().equals("types1")) { + m1 = m; + } + } + assertNotNull(m1); + + LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-int-leaf"); + TypeDefinition baseType = testleaf.getType().getBaseType(); + assertTrue(testleaf.getType().getBaseType() instanceof IntegerTypeDefinition); + Int32 baseTypeCast = (Int32)baseType; + + Long[][] expectedRanges = new Long[3][2]; + expectedRanges[0] = new Long[]{10L, 20L}; + expectedRanges[1] = new Long[]{12L, 18L}; + expectedRanges[2] = new Long[]{14L, 16L}; + + List actualRanges = baseTypeCast.getRangeStatements(); + assertEquals(3, actualRanges.size()); + for(int i = 0; i < actualRanges.size(); i++) { + assertEquals(expectedRanges[i][0], actualRanges.get(i).getMin()); + assertEquals(expectedRanges[i][1], actualRanges.get(i).getMax()); + } + } + + @Test + public void testTypeDef() { + Set modules = tested.parseYangModels(testFile1, testFile2); + assertEquals(2, modules.size()); + + Module m2 = null; + for(Module m : modules) { + if(m.getName().equals("types2")) { + m2 = m; + } + } + assertNotNull(m2); + + LeafSchemaNode testleaf = (LeafSchemaNode)m2.getDataChildByName("nested-type-leaf"); + TypeDefinition baseType = testleaf.getType().getBaseType(); assertTrue(testleaf.getType().getBaseType() instanceof Int32); + Int32 baseTypeCast = (Int32)baseType; + List ranges = baseTypeCast.getRangeStatements(); + assertEquals(2, ranges.size()); + RangeConstraint range = ranges.get(0); + assertEquals(2L, range.getMin()); + assertEquals(20L, range.getMax()); + } + + @Test + public void testTypedefDecimal1() { + Set modules = tested.parseYangModels(testFile1, testFile2); + assertEquals(2, modules.size()); + + Module m1 = null; + for(Module m : modules) { + if(m.getName().equals("types1")) { + m1 = m; + } + } + assertNotNull(m1); + + LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf"); + TypeDefinition baseType = testleaf.getType().getBaseType(); + assertTrue(testleaf.getType().getBaseType() instanceof Decimal64); + Decimal64 baseTypeCast = (Decimal64)baseType; + assertEquals(4, (int)baseTypeCast.getFractionDigits()); + } + + @Test + public void testTypedefDecimal2() { + Set modules = tested.parseYangModels(testFile1, testFile2); + assertEquals(2, modules.size()); + + Module m1 = null; + for(Module m : modules) { + if(m.getName().equals("types1")) { + m1 = m; + } + } + assertNotNull(m1); + + LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf2"); + TypeDefinition baseType = testleaf.getType().getBaseType(); + assertTrue(testleaf.getType().getBaseType() instanceof Decimal64); + Decimal64 baseTypeCast = (Decimal64)baseType; + assertEquals(5, (int)baseTypeCast.getFractionDigits()); } } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang index 2c796a9acd..0d5fba7b35 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile1.yang @@ -1,39 +1,57 @@ -module types1 { - yang-version 1; - namespace "urn:simple.container.demo"; - prefix "t1"; - - import types2 { - prefix "data"; - revision-date 2013-02-27; - } - - organization "Cisco"; - contact "WILL-BE-DEFINED-LATER"; - - revision "2013-02-27" { - reference " WILL BE DEFINED LATER"; - } - - container interfaces { - list ifEntry { - key "ifIndex"; - - leaf ifIndex { - type uint32; - units minutes; - } - - leaf ifMtu { - type int32; - } - } - } - - leaf testleaf { - type data:my-base-int32-type { - range "11..max"; - } - } - -} +module types1 { + yang-version 1; + namespace "urn:simple.container.demo"; + prefix "t1"; + + import types2 { + prefix "data"; + revision-date 2013-02-27; + } + + organization "Cisco"; + contact "WILL-BE-DEFINED-LATER"; + + revision "2013-02-27" { + reference " WILL BE DEFINED LATER"; + } + + container interfaces { + list ifEntry { + key "ifIndex"; + + leaf ifIndex { + type uint32; + units minutes; + } + + leaf ifMtu { + type int32; + } + } + } + + leaf testleaf { + type data:my-base-int32-type { + range "min..max"; + } + } + + leaf test-string-leaf { + type data:my-string-type-ext; + } + + leaf test-int-leaf { + type data:my-int-type-ext; + } + + leaf test-decimal-leaf { + type data:my-decimal-type { + fraction-digits 4; + } + } + + leaf test-decimal-leaf2 { + type data:my-decimal-type-ext; + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang index 9d12207179..aea5fb4e4b 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/model/testfile2.yang @@ -1,43 +1,100 @@ -module types2 { - yang-version 1; - namespace "urn:simple.types.data.demo"; - prefix "t2"; - - import types1 { - prefix "if"; - revision-date 2013-02-27; - } - - organization "Cisco"; - contact "WILL-BE-DEFINED-LATER"; - description "This is types-data test description"; - - revision "2013-02-27" { - reference " WILL BE DEFINED LATER"; - } - - - augment "/if:interfaces/if:ifEntry" { - when "if:ifType='ds0'"; - leaf ds0ChannelNumber { - type string; - } - } - - leaf if-name { - type leafref { - path "/interface/name"; - } - } - - leaf name { - type string; - } - - typedef my-base-int32-type { - type int32 { - range "0..32"; - } - } - -} +module types2 { + yang-version 1; + namespace "urn:simple.types.data.demo"; + prefix "t2"; + + import types1 { + prefix "if"; + revision-date 2013-02-27; + } + + organization "Cisco"; + contact "WILL-BE-DEFINED-LATER"; + description "This is types-data test description"; + + revision "2013-02-27" { + reference " WILL BE DEFINED LATER"; + } + + typedef my-base-int32-type { + type int32 { + range "2..20"; + } + } + + typedef my-type1 { + type my-base-int32-type { + range "11..max"; + } + } + + typedef my-string-type { + type string { + pattern "[a-k]*"; + } + } + + typedef my-string-type2 { + type my-string-type { + pattern "[b-u]*"; + } + } + + typedef my-string-type-ext { + type my-string-type2 { + pattern "[e-z]*"; + } + } + + typedef my-int-type { + type int32 { + range "10..20"; + } + } + + typedef my-int-type2 { + type my-int-type { + range "12..18"; + } + } + + typedef my-int-type-ext { + type my-int-type2 { + range "14..16"; + } + } + + typedef my-decimal-type { + type decimal64 { + fraction-digits 6; + } + } + + typedef my-decimal-type-ext { + type decimal64 { + fraction-digits 5; + } + } + + augment "/if:interfaces/if:ifEntry" { + when "if:ifType='ds0'"; + leaf ds0ChannelNumber { + type string; + } + } + + leaf if-name { + type leafref { + path "/interface/name"; + } + } + + leaf name { + type string; + } + + leaf nested-type-leaf { + type my-type1; + } + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/model/api/type/UnknownTypeDefinition.java b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/model/api/type/UnknownTypeDefinition.java index a4da8f7a94..f5f3b17886 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/model/api/type/UnknownTypeDefinition.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/model/api/type/UnknownTypeDefinition.java @@ -14,11 +14,14 @@ import org.opendaylight.controller.yang.model.api.TypeDefinition; public interface UnknownTypeDefinition extends TypeDefinition { - public List getRangeStatements(); + List getRangeStatements(); - public List getLengthStatements(); + List getLengthStatements(); - public List getPatterns(); + List getPatterns(); + + LengthConstraint getLengthConstraint(); + + Integer getFractionDigits(); - public LengthConstraint getLengthConstraint(); } diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/UnknownType.java b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/UnknownType.java index 1553ce0d76..7ad57a7646 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/UnknownType.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/UnknownType.java @@ -31,6 +31,7 @@ public class UnknownType implements UnknownTypeDefinition { private final List rangeStatements; private final List extensions; private final LengthConstraint lengthConstraint; + private final Integer fractionDigits; private final Status status; private final String units; @@ -49,6 +50,7 @@ public class UnknownType implements UnknownTypeDefinition { private List rangeStatements = Collections.emptyList(); private List extensions = Collections.emptyList(); private LengthConstraint lengthConstraint = null; + private Integer fractionDigits = null; private Status status = Status.CURRENT; private String units = ""; @@ -104,6 +106,11 @@ public class UnknownType implements UnknownTypeDefinition { return this; } + public Builder fractionDigits(final Integer fractionDigits) { + this.fractionDigits = fractionDigits; + return this; + } + public Builder status(Status status) { this.status = status; return this; @@ -137,11 +144,12 @@ public class UnknownType implements UnknownTypeDefinition { this.status = builder.status; this.units = builder.units; this.defaultValue = builder.defaultValue; + this.fractionDigits = builder.fractionDigits; } /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.TypeDefinition#getBaseType() */ @@ -152,7 +160,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getUnits() */ @Override @@ -162,7 +170,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.TypeDefinition#getDefaultValue * () @@ -174,7 +182,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see org.opendaylight.controller.yang.model.api.SchemaNode#getQName() */ @Override @@ -184,7 +192,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see org.opendaylight.controller.yang.model.api.SchemaNode#getPath() */ @Override @@ -194,7 +202,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.SchemaNode#getDescription() */ @@ -205,7 +213,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see org.opendaylight.controller.yang.model.api.SchemaNode#getReference() */ @Override @@ -215,7 +223,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see org.opendaylight.controller.yang.model.api.SchemaNode#getStatus() */ @Override @@ -225,7 +233,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.SchemaNode#getExtensionSchemaNodes * () @@ -237,7 +245,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.type.UnknownTypeDefinition * #getRangeStatements() @@ -249,7 +257,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.type.UnknownTypeDefinition * #getLengthStatements() @@ -261,7 +269,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.type.UnknownTypeDefinition * #getPatterns() @@ -273,7 +281,7 @@ public class UnknownType implements UnknownTypeDefinition { /* * (non-Javadoc) - * + * * @see * org.opendaylight.controller.yang.model.api.type.UnknownTypeDefinition * #getLengthConstraint() @@ -283,6 +291,11 @@ public class UnknownType implements UnknownTypeDefinition { return lengthConstraint; } + @Override + public Integer getFractionDigits() { + return fractionDigits; + } + @Override public int hashCode() { final int prime = 31; @@ -437,4 +450,5 @@ public class UnknownType implements UnknownTypeDefinition { builder2.append("]"); return builder2.toString(); } + } diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/YangTypesConverter.java b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/YangTypesConverter.java index 7f485c3c20..1b96adf7cc 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/YangTypesConverter.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/model/util/YangTypesConverter.java @@ -92,29 +92,29 @@ public class YangTypesConverter { } public static TypeDefinition javaTypeForBaseYangSignedIntegerType( - String typeName) { + String typeName, List ranges) { if (typeName.equals("int8")) { - return new Int8(); + return new Int8(ranges, null, null); } else if (typeName.equals("int16")) { - return new Int16(); + return new Int16(ranges, null, null); } else if (typeName.equals("int32")) { - return new Int32(); + return new Int32(ranges, null, null); } else if (typeName.equals("int64")) { - return new Int64(); + return new Int64(ranges, null, null); } return null; } public static TypeDefinition javaTypeForBaseYangUnsignedIntegerType( - final String typeName) { + final String typeName, List ranges) { if (typeName.equals("uint8")) { - return new Uint8(); + return new Uint8(ranges, null, null); } else if (typeName.equals("uint16")) { - return new Uint16(); + return new Uint16(ranges, null, null); } else if (typeName.equals("uint32")) { - return new Uint32(); + return new Uint32(ranges, null, null); } else if (typeName.equals("uint64")) { - return new Uint64(); + return new Uint64(ranges, null, null); } return null; } -- 2.36.6