From 5384703a0df855832553100d29a82008dcfd92cb Mon Sep 17 00:00:00 2001 From: mvitez Date: Thu, 4 Apr 2013 15:48:56 +0200 Subject: [PATCH] Refactored YangModelParserImpl Renamed Identityref to IdentityrefType Renamed IdentityType to IdentitySchemaNode and moved from yang-model-util to yang-model-api + refactored as interface Added tests Signed-off-by: mvitez --- .../impl/IdentitySchemaNodeBuilder.java | 201 +++ .../parser/builder/impl/ModuleBuilder.java | 41 +- .../parser/impl/YangModelParserImpl.java | 211 ++- .../impl/YangModelParserListenerImpl.java | 1353 +++++++++-------- .../parser/util/YangModelBuilderUtil.java | 20 +- .../parser/impl/TypesResolutionTest.java | 104 +- .../impl/YangModelParserListenerTest.java | 2 +- .../src/test/resources/abstract-topology.yang | 2 +- .../types/custom-types-test@2012-4-4.yang | 29 + .../yang/model/api/IdentitySchemaNode.java | 14 + .../controller/yang/model/api/Module.java | 2 + .../api/type/IdentityrefTypeDefinition.java | 35 +- ...{Identityref.java => IdentityrefType.java} | 242 ++- .../yang/model/util/YangTypesConverter.java | 255 ++-- 14 files changed, 1463 insertions(+), 1048 deletions(-) create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/IdentitySchemaNodeBuilder.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/types/custom-types-test@2012-4-4.yang create mode 100644 opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/IdentitySchemaNode.java rename opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/{Identityref.java => IdentityrefType.java} (50%) diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/IdentitySchemaNodeBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/IdentitySchemaNodeBuilder.java new file mode 100644 index 0000000000..0abc9598dc --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/IdentitySchemaNodeBuilder.java @@ -0,0 +1,201 @@ +/* + * 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.impl; + +import java.util.Collections; +import java.util.List; + +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; +import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.Status; +import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; +import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder; + +public class IdentitySchemaNodeBuilder implements SchemaNodeBuilder { + + private final QName qname; + private final IdentitySchemaNodeImpl instance; + private IdentitySchemaNodeBuilder baseIdentity; + private String baseIdentityName; + + IdentitySchemaNodeBuilder(QName qname) { + this.qname = qname; + instance = new IdentitySchemaNodeImpl(qname); + } + + @Override + public IdentitySchemaNode build() { + if(baseIdentity != null) { + IdentitySchemaNode base = baseIdentity.build(); + instance.setBaseIdentity(base); + } + return instance; + } + + @Override + public QName getQName() { + return qname; + } + + @Override + public void setPath(SchemaPath path) { + instance.setPath(path); + } + + @Override + public void setDescription(String description) { + instance.setDescription(description); + } + + @Override + public void setReference(String reference) { + instance.setReference(reference); + } + + @Override + public void setStatus(Status status) { + if (status != null) { + instance.setStatus(status); + } + } + + @Override + public void addUnknownSchemaNode( + UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) { + throw new IllegalStateException( + "Can not add schema node to identity statement"); + } + + public String getBaseIdentityName() { + return baseIdentityName; + } + + public void setBaseIdentityName(String baseIdentityName) { + this.baseIdentityName = baseIdentityName; + } + + public void setBaseIdentity(IdentitySchemaNodeBuilder baseType) { + this.baseIdentity = baseType; + } + + private class IdentitySchemaNodeImpl implements IdentitySchemaNode { + private final QName qname; + private IdentitySchemaNode baseIdentity; + private String description; + private String reference; + private Status status = Status.CURRENT; + private SchemaPath path; + + private IdentitySchemaNodeImpl(QName qname) { + this.qname = qname; + } + + @Override + public QName getQName() { + return qname; + } + + @Override + public IdentitySchemaNode getBaseIdentity() { + return baseIdentity; + } + + private void setBaseIdentity(IdentitySchemaNode baseIdentity) { + this.baseIdentity = baseIdentity; + } + + @Override + public String getDescription() { + return description; + } + + private void setDescription(String description) { + this.description = description; + } + + @Override + public String getReference() { + return reference; + } + + private void setReference(String reference) { + this.reference = reference; + } + + @Override + public Status getStatus() { + return status; + } + + private void setStatus(Status status) { + if (status != null) { + this.status = status; + } + } + + @Override + public SchemaPath getPath() { + return path; + } + + private void setPath(SchemaPath path) { + this.path = path; + } + + @Override + public List getUnknownSchemaNodes() { + return Collections.emptyList(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((qname == null) ? 0 : qname.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + IdentitySchemaNodeImpl other = (IdentitySchemaNodeImpl) obj; + if (qname == null) { + if (other.qname != null) { + return false; + } + } else if (!qname.equals(other.qname)) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder( + IdentitySchemaNodeImpl.class.getSimpleName()); + sb.append("["); + sb.append("base=" + baseIdentity); + sb.append(", qname=" + qname); + sb.append(", description=" + description); + sb.append(", reference=" + reference); + sb.append(", status=" + status); + sb.append("]"); + 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/ModuleBuilder.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ModuleBuilder.java index 90a4547f38..d22295ae66 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ModuleBuilder.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/builder/impl/ModuleBuilder.java @@ -24,6 +24,7 @@ import org.opendaylight.controller.yang.model.api.Deviation; import org.opendaylight.controller.yang.model.api.ExtensionDefinition; import org.opendaylight.controller.yang.model.api.FeatureDefinition; import org.opendaylight.controller.yang.model.api.GroupingDefinition; +import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.ModuleImport; import org.opendaylight.controller.yang.model.api.NotificationDefinition; @@ -71,6 +72,7 @@ public class ModuleBuilder implements Builder { private final Map, UsesNodeBuilder> addedUsesNodes = new HashMap, UsesNodeBuilder>(); private final Map, RpcDefinitionBuilder> addedRpcs = new HashMap, RpcDefinitionBuilder>(); private final Set addedNotifications = new HashSet(); + private final Set addedIdentities = new HashSet(); private final Map, FeatureBuilder> addedFeatures = new HashMap, FeatureBuilder>(); private final Map addedDeviations = new HashMap(); private final Map, TypeDefinitionBuilder> addedTypedefs = new HashMap, TypeDefinitionBuilder>(); @@ -85,7 +87,6 @@ public class ModuleBuilder implements Builder { } - /** * Build new Module object based on this builder. */ @@ -142,6 +143,13 @@ public class ModuleBuilder implements Builder { } instance.setExtensionSchemaNodes(extensions); + // IDENTITIES + final Set identities = new HashSet(); + for(IdentitySchemaNodeBuilder idBuilder : addedIdentities) { + identities.add(idBuilder.build()); + } + instance.setIdentities(identities); + return instance; } @@ -153,6 +161,14 @@ public class ModuleBuilder implements Builder { return dirtyNodes; } + public Set getAddedAugments() { + return addedAugments; + } + + public Set getAddedIdentities() { + return addedIdentities; + } + public String getName() { return name; } @@ -165,10 +181,6 @@ public class ModuleBuilder implements Builder { return revision; } - public Set getAddedAugments() { - return addedAugments; - } - public void addDirtyNode(List path) { List dirtyNodePath = new ArrayList(path); TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) moduleNodes @@ -468,6 +480,13 @@ public class ModuleBuilder implements Builder { return builder; } + public IdentitySchemaNodeBuilder addIdentity(QName qname) { + IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(qname); + addedIdentities.add(builder); + + return builder; + } + public void addConfiguration(boolean configuration, List parentPath) { Builder builder = moduleNodes.get(parentPath); if (builder instanceof DeviationBuilder) { @@ -506,6 +525,7 @@ public class ModuleBuilder implements Builder { private Set groupings = Collections.emptySet(); private Set uses = Collections.emptySet(); private List extensionSchemaNodes = Collections.emptyList(); + private Set identities = Collections.emptySet(); private ModuleImpl(String name) { this.name = name; @@ -709,6 +729,17 @@ public class ModuleBuilder implements Builder { } } + @Override + public Set getIdentities() { + return identities; + } + + private void setIdentities(Set identities) { + if(identities != null) { + this.identities = identities; + } + } + @Override public DataSchemaNode getDataChildByName(QName name) { return childNodes.get(name); 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 fce723edff..f30780f30c 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 @@ -41,14 +41,13 @@ import org.opendaylight.controller.yang.model.api.SchemaPath; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition; import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit; import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.InstanceIdentifierTypeDefinition; import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition; import org.opendaylight.controller.yang.model.api.type.LengthConstraint; import org.opendaylight.controller.yang.model.api.type.PatternConstraint; import org.opendaylight.controller.yang.model.api.type.RangeConstraint; import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit; 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; @@ -56,6 +55,7 @@ import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilde import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder; import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.IdentitySchemaNodeBuilder; import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder; import org.opendaylight.controller.yang.model.parser.builder.impl.UnionTypeBuilder; import org.opendaylight.controller.yang.model.util.BaseConstraints; @@ -102,14 +102,15 @@ public class YangModelParserImpl implements YangModelParser { private Map> resolveModuleBuildersFromStreams( String... yangFiles) { InputStream[] streams = new InputStream[yangFiles.length]; - for(int i = 0; i < yangFiles.length; i++) { + for (int i = 0; i < yangFiles.length; i++) { final String yangFileName = yangFiles[i]; final File yangFile = new File(yangFileName); FileInputStream inStream = null; try { inStream = new FileInputStream(yangFile); - } catch(FileNotFoundException e) { - logger.warn("Exception while reading yang stream: " + inStream, e); + } catch (FileNotFoundException e) { + logger.warn("Exception while reading yang stream: " + inStream, + e); } streams[i] = inStream; } @@ -138,14 +139,12 @@ public class YangModelParserImpl implements YangModelParser { 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; @@ -205,6 +204,7 @@ public class YangModelParserImpl implements YangModelParser { ModuleBuilder builder) { resolveTypedefs(modules, builder); resolveAugments(modules, builder); + resolveIdentities(modules, builder); } /** @@ -219,8 +219,7 @@ public class YangModelParserImpl implements YangModelParser { private void resolveTypedefs( Map> modules, ModuleBuilder module) { - Map, TypeAwareBuilder> dirtyNodes = module - .getDirtyNodes(); + Map, TypeAwareBuilder> dirtyNodes = module.getDirtyNodes(); if (dirtyNodes.size() == 0) { return; } else { @@ -244,7 +243,6 @@ public class YangModelParserImpl implements YangModelParser { private UnionTypeBuilder resolveUnionTypeBuilder( Map> modules, ModuleBuilder builder, UnionTypeBuilder unionTypeBuilderToResolve) { - List> resolvedTypes = new ArrayList>(); List> typesToRemove = new ArrayList>(); @@ -277,26 +275,42 @@ public class YangModelParserImpl implements YangModelParser { .iterator().next().getValue(); TypeDefinition targetTypeBaseType = targetType.getBaseType(); - String targetTypeBaseTypeName = targetTypeBaseType.getQName() - .getLocalName(); // RANGE List ranges = ut.getRangeStatements(); resolveRanges(ranges, targetType, modules, builder); - // LENGTH List lengths = ut.getLengthStatements(); resolveLengths(lengths, targetType, modules, builder); - // PATTERN List patterns = ut.getPatterns(); - // Fraction Digits Integer fractionDigits = ut.getFractionDigits(); - // MERGE CONSTRAINTS (enumeration and leafref omitted - // because - // they have no restrictions) + targetTypeBaseType = mergeConstraints(targetTypeBaseType, constraints, ranges, lengths, + patterns, fractionDigits); + + return targetTypeBaseType; + } + + /** + * Merge curent constraints with founded type constraints + * + * @param targetTypeBaseType + * @param constraints + * @param ranges + * @param lengths + * @param patterns + * @param fractionDigits + */ + private TypeDefinition mergeConstraints(TypeDefinition targetTypeBaseType, + TypeConstraints constraints, List ranges, + List lengths, List patterns, + Integer fractionDigits) { + String targetTypeBaseTypeName = targetTypeBaseType.getQName() + .getLocalName(); + // enumeration, leafref and identityref omitted because they have no + // restrictions if (targetTypeBaseType instanceof DecimalTypeDefinition) { List fullRanges = new ArrayList(); fullRanges.addAll(constraints.getRanges()); @@ -332,14 +346,7 @@ public class YangModelParserImpl implements YangModelParser { targetTypeBaseType = new BitsType(bits); } else if (targetTypeBaseType instanceof BinaryTypeDefinition) { targetTypeBaseType = new BinaryType(null, lengths, null); - } else if (targetTypeBaseTypeName.equals("instance-identifier")) { - // TODO: instance-identifier - /* - * boolean requireInstance = isRequireInstance(typeBody); type = new - * InstanceIdentifier(null, requireInstance); - */ } - return targetTypeBaseType; } @@ -362,10 +369,14 @@ public class YangModelParserImpl implements YangModelParser { * Traverse through all referenced types chain until base YANG type is * founded. * - * @param constraints current type constraints - * @param modules all available modules - * @param unknownType unknown type - * @param builder current module + * @param constraints + * current type constraints + * @param modules + * all available modules + * @param unknownType + * unknown type + * @param builder + * current module * @return map, where key is type referenced and value is its constraints */ private Map findTypeDefinitionBuilderWithConstraints( @@ -373,8 +384,6 @@ public class YangModelParserImpl implements YangModelParser { Map> modules, UnknownType unknownType, ModuleBuilder builder) { Map result = new HashMap(); - - // TypeDefinition unknownType = typeBuilder.getType(); QName unknownTypeQName = unknownType.getQName(); String unknownTypeName = unknownTypeQName.getLocalName(); String unknownTypePrefix = unknownTypeQName.getPrefix(); @@ -384,19 +393,8 @@ public class YangModelParserImpl implements YangModelParser { if (unknownTypePrefix.equals(builder.getPrefix())) { dependentModuleBuilder = builder; } else { - ModuleImport dependentModuleImport = getModuleImport(builder, + dependentModuleBuilder = findDependentModule(modules, builder, unknownTypePrefix); - String dependentModuleName = dependentModuleImport.getModuleName(); - Date dependentModuleRevision = dependentModuleImport.getRevision(); - TreeMap moduleBuildersByRevision = modules - .get(dependentModuleName); - if (dependentModuleRevision == null) { - dependentModuleBuilder = moduleBuildersByRevision.lastEntry() - .getValue(); - } else { - dependentModuleBuilder = moduleBuildersByRevision - .get(dependentModuleRevision); - } } // pull all typedef statements from dependent module... @@ -445,10 +443,7 @@ public class YangModelParserImpl implements YangModelParser { } else if (referencedType instanceof BinaryTypeDefinition) { constraints.addLengths(((BinaryTypeDefinition) referencedType) .getLengthConstraints()); - } else if (referencedType instanceof InstanceIdentifierTypeDefinition) { - // TODO: instance-identifier } - result.put(lookedUpBuilder, constraints); return result; } @@ -474,27 +469,17 @@ public class YangModelParserImpl implements YangModelParser { SchemaPath augmentTargetSchemaPath = augmentBuilder.getTargetPath(); String prefix = null; List augmentTargetPath = new ArrayList(); + for (QName pathPart : augmentTargetSchemaPath.getPath()) { prefix = pathPart.getPrefix(); augmentTargetPath.add(pathPart.getLocalName()); } - ModuleImport dependentModuleImport = getModuleImport(module, - prefix); - String dependentModuleName = dependentModuleImport.getModuleName(); - augmentTargetPath.add(0, dependentModuleName); - - Date dependentModuleRevision = dependentModuleImport.getRevision(); - - TreeMap moduleBuildersByRevision = modules - .get(dependentModuleName); - ModuleBuilder dependentModule; - if (dependentModuleRevision == null) { - dependentModule = moduleBuildersByRevision.lastEntry() - .getValue(); - } else { - dependentModule = moduleBuildersByRevision - .get(dependentModuleRevision); - } + ModuleBuilder dependentModule = findDependentModule(modules, + module, prefix); + // + augmentTargetPath.add(0, dependentModule.getName()); + // + AugmentationTargetBuilder augmentTarget = (AugmentationTargetBuilder) dependentModule .getNode(augmentTargetPath); @@ -520,6 +505,82 @@ public class YangModelParserImpl implements YangModelParser { } } + /** + * Go through identity statements defined in current module and resolve + * their 'base' statement if present. + * + * @param modules + * all modules + * @param module + * module being resolved + */ + private void resolveIdentities( + Map> modules, + ModuleBuilder module) { + Set identities = module.getAddedIdentities(); + for (IdentitySchemaNodeBuilder identity : identities) { + String baseIdentityName = identity.getBaseIdentityName(); + if (baseIdentityName != null) { + String baseIdentityPrefix = null; + String baseIdentityLocalName = null; + if (baseIdentityName.contains(":")) { + String[] splitted = baseIdentityName.split(":"); + baseIdentityPrefix = splitted[0]; + baseIdentityLocalName = splitted[1]; + } else { + baseIdentityPrefix = module.getPrefix(); + baseIdentityLocalName = baseIdentityName; + } + ModuleBuilder dependentModule; + if (baseIdentityPrefix.equals(module.getPrefix())) { + dependentModule = module; + } else { + dependentModule = findDependentModule(modules, module, + baseIdentityPrefix); + } + + Set dependentModuleIdentities = dependentModule + .getAddedIdentities(); + for (IdentitySchemaNodeBuilder idBuilder : dependentModuleIdentities) { + if (idBuilder.getQName().getLocalName() + .equals(baseIdentityLocalName)) { + identity.setBaseIdentity(idBuilder); + } + } + } + } + } + + /** + * Find dependent module based on given prefix + * + * @param modules + * all available modules + * @param module + * current module + * @param prefix + * target module prefix + * @return dependent module builder + */ + private ModuleBuilder findDependentModule( + Map> modules, + ModuleBuilder module, String prefix) { + ModuleImport dependentModuleImport = getModuleImport(module, prefix); + String dependentModuleName = dependentModuleImport.getModuleName(); + Date dependentModuleRevision = dependentModuleImport.getRevision(); + + TreeMap moduleBuildersByRevision = modules + .get(dependentModuleName); + ModuleBuilder dependentModule; + if (dependentModuleRevision == null) { + dependentModule = moduleBuildersByRevision.lastEntry().getValue(); + } else { + dependentModule = moduleBuildersByRevision + .get(dependentModuleRevision); + } + return dependentModule; + } + /** * Get module import referenced by given prefix. * @@ -544,10 +605,14 @@ public class YangModelParserImpl implements YangModelParser { * Helper method for resolving special 'min' or 'max' values in range * constraint * - * @param ranges ranges to resolve - * @param targetType target type - * @param modules all available modules - * @param builder current module + * @param ranges + * ranges to resolve + * @param targetType + * target type + * @param modules + * all available modules + * @param builder + * current module */ private void resolveRanges(List ranges, TypeDefinitionBuilder targetType, @@ -584,10 +649,14 @@ public class YangModelParserImpl implements YangModelParser { * Helper method for resolving special 'min' or 'max' values in length * constraint * - * @param lengths lengths to resolve - * @param targetType target type - * @param modules all available modules - * @param builder current module + * @param lengths + * lengths to resolve + * @param targetType + * target type + * @param modules + * all available modules + * @param builder + * current module */ private void resolveLengths(List lengths, TypeDefinitionBuilder targetType, diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java index 24f6b381b6..7e64c6e41c 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerImpl.java @@ -1,665 +1,692 @@ -/* - * 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.impl; - -import static org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil.*; - -import java.net.URI; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Stack; -import java.util.TreeMap; - -import org.antlr.v4.runtime.tree.ParseTree; -import org.opendaylight.controller.antlrv4.code.gen.YangParser; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Contact_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Container_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Description_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_add_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Import_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Key_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_list_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.List_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_header_stmtsContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Namespace_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Organization_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Prefix_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Presence_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Reference_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_date_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Union_specificationContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext; -import org.opendaylight.controller.antlrv4.code.gen.YangParserBaseListener; -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.Status; -import org.opendaylight.controller.yang.model.api.TypeDefinition; -import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder; -import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.ContainerSchemaNodeBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.DeviationBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.ExtensionBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.FeatureBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.LeafListSchemaNodeBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.LeafSchemaNodeBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.ListSchemaNodeBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.NotificationBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.RpcDefinitionBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.TypedefBuilder; -import org.opendaylight.controller.yang.model.parser.builder.impl.UnknownSchemaNodeBuilder; -import org.opendaylight.controller.yang.model.util.YangTypesConverter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -final class YangModelParserListenerImpl extends YangParserBaseListener { - - private static final Logger logger = LoggerFactory - .getLogger(YangModelParserListenerImpl.class); - - private ModuleBuilder moduleBuilder; - - private String moduleName; - private URI namespace; - private String yangModelPrefix; - private Date revision; - - private final DateFormat simpleDateFormat = new SimpleDateFormat( - "yyyy-mm-dd"); - private final Stack actualPath = new Stack(); - - - @Override - public void enterModule_stmt(YangParser.Module_stmtContext ctx) { - moduleName = stringFromNode(ctx); - actualPath.push(moduleName); - moduleBuilder = new ModuleBuilder(moduleName); - - String description = null; - String reference = null; - - for (int i = 0; i < ctx.getChildCount(); i++) { - ParseTree child = ctx.getChild(i); - if (child instanceof Description_stmtContext) { - description = stringFromNode(child); - } else if (child instanceof Reference_stmtContext) { - reference = stringFromNode(child); - } else { - if (description != null && reference != null) { - break; - } - } - } - moduleBuilder.setDescription(description); - moduleBuilder.setReference(reference); - } - - @Override - public void exitModule_stmt(YangParser.Module_stmtContext ctx) { - final String moduleName = actualPath.pop(); - logger.debug("Exiting module " + moduleName); - } - - @Override - public void enterModule_header_stmts(final Module_header_stmtsContext ctx) { - super.enterModule_header_stmts(ctx); - - for (int i = 0; i < ctx.getChildCount(); ++i) { - final ParseTree treeNode = ctx.getChild(i); - if (treeNode instanceof Namespace_stmtContext) { - final String namespaceStr = stringFromNode(treeNode); - namespace = URI.create(namespaceStr); - moduleBuilder.setNamespace(namespace); - } else if (treeNode instanceof Prefix_stmtContext) { - yangModelPrefix = stringFromNode(treeNode); - moduleBuilder.setPrefix(yangModelPrefix); - } else if (treeNode instanceof Yang_version_stmtContext) { - final String yangVersion = stringFromNode(treeNode); - moduleBuilder.setYangVersion(yangVersion); - } - } - } - - @Override - public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) { - for (int i = 0; i < ctx.getChildCount(); i++) { - ParseTree child = ctx.getChild(i); - if (child instanceof Organization_stmtContext) { - final String organization = stringFromNode(child); - moduleBuilder.setOrganization(organization); - } else if (child instanceof Contact_stmtContext) { - final String contact = stringFromNode(child); - moduleBuilder.setContact(contact); - } else if (child instanceof Description_stmtContext) { - final String description = stringFromNode(child); - moduleBuilder.setDescription(description); - } else if (child instanceof Reference_stmtContext) { - final String reference = stringFromNode(child); - moduleBuilder.setReference(reference); - } - } - } - - @Override - public void exitSubmodule_header_stmts( - YangParser.Submodule_header_stmtsContext ctx) { - final String submodule = actualPath.pop(); - logger.debug("exiting submodule " + submodule); - } - - @Override - public void enterRevision_stmts(Revision_stmtsContext ctx) { - TreeMap revisions = new TreeMap(); - - for (int i = 0; i < ctx.getChildCount(); ++i) { - final ParseTree treeNode = ctx.getChild(i); - if (treeNode instanceof Revision_stmtContext) { - final String revisionDateStr = stringFromNode(treeNode); - try { - Date revision = simpleDateFormat.parse(revisionDateStr); - revisions.put(revision, (Revision_stmtContext)treeNode); - - } catch (ParseException e) { - final String message = "Failed to parse revision string: "+ revisionDateStr; - logger.warn(message); - } - } - } - if(revisions.size() > 0) { - Revision_stmtContext revisionCtx = revisions.firstEntry().getValue(); - moduleBuilder.setRevision(revisions.firstKey()); - - for(int i = 0; i < revisionCtx.getChildCount(); i++) { - ParseTree child = revisionCtx.getChild(i); - if(child instanceof Reference_stmtContext) { - moduleBuilder.setReference(stringFromNode(child)); - } - } - } - } - - @Override - public void enterImport_stmt(Import_stmtContext ctx) { - super.enterImport_stmt(ctx); - - final String importName = stringFromNode(ctx); - String importPrefix = null; - Date importRevision = null; - - for (int i = 0; i < ctx.getChildCount(); ++i) { - final ParseTree treeNode = ctx.getChild(i); - if (treeNode instanceof Prefix_stmtContext) { - importPrefix = stringFromNode(treeNode); - } - if (treeNode instanceof Revision_date_stmtContext) { - String importRevisionStr = stringFromNode(treeNode); - try { - importRevision = simpleDateFormat.parse(importRevisionStr); - } catch(ParseException e) { - logger.warn("Failed to parse import revision-date: "+ importRevisionStr); - } - } - } - moduleBuilder.addModuleImport(importName, importRevision, importPrefix); - } - - @Override - public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) { - final String augmentPath = stringFromNode(ctx); - AugmentationSchemaBuilder builder = moduleBuilder.addAugment( - augmentPath, getActualPath()); - updatePath(augmentPath); - - for (int i = 0; i < ctx.getChildCount(); i++) { - ParseTree child = ctx.getChild(i); - if (child instanceof Description_stmtContext) { - String desc = stringFromNode(child); - builder.setDescription(desc); - } else if (child instanceof Reference_stmtContext) { - String ref = stringFromNode(child); - builder.setReference(ref); - } else if (child instanceof Status_stmtContext) { - Status status = parseStatus((Status_stmtContext) child); - builder.setStatus(status); - } - } - } - - @Override - public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) { - final String augment = actualPath.pop(); - logger.debug("exiting augment " + augment); - } - - @Override - public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) { - String argument = stringFromNode(ctx); - QName qname = new QName(namespace, revision, yangModelPrefix, argument); - ExtensionBuilder builder = moduleBuilder.addExtension(qname); - parseSchemaNodeArgs(ctx, builder); - } - - @Override - public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) { - String typedefName = stringFromNode(ctx); - QName typedefQName = new QName(namespace, revision, yangModelPrefix, - typedefName); - TypedefBuilder builder = moduleBuilder.addTypedef(typedefQName, - getActualPath()); - updatePath(typedefName); - - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, - yangModelPrefix)); - parseSchemaNodeArgs(ctx, builder); - builder.setUnits(parseUnits(ctx)); - } - - @Override - public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterType_stmt(YangParser.Type_stmtContext ctx) { - String typeName = stringFromNode(ctx); - QName typeQName; - if (typeName.contains(":")) { - String[] splittedName = typeName.split(":"); - String prefix = splittedName[0]; - String name = splittedName[1]; - if (prefix.equals(yangModelPrefix)) { - typeQName = new QName(namespace, revision, prefix, name); - } else { - typeQName = new QName(null, null, prefix, name); - } - } else { - typeQName = new QName(namespace, revision, yangModelPrefix, - typeName); - } - - TypeDefinition type = null; - Type_body_stmtsContext typeBody = null; - for (int i = 0; i < ctx.getChildCount(); i++) { - if (ctx.getChild(i) instanceof Type_body_stmtsContext) { - typeBody = (Type_body_stmtsContext) ctx.getChild(i); - break; - } - } - - - - // if this is base yang type... - if(YangTypesConverter.isBaseYangType(typeName)) { - if (typeBody == null) { - // if there are no constraints, just grab default base yang type - type = YangTypesConverter.javaTypeForBaseYangType(typeName); - moduleBuilder.setType(type, actualPath); - } else { - if(typeName.equals("union")) { - List types = new ArrayList(); - for(int i = 0; i < typeBody.getChildCount(); i++) { - ParseTree unionSpec = typeBody.getChild(i); - if(unionSpec instanceof Union_specificationContext) { - for(int j = 0; j < unionSpec.getChildCount(); j++) { - ParseTree typeSpec = unionSpec.getChild(j); - types.add(stringFromNode(typeSpec)); - } - } - } - moduleBuilder.addUnionType(actualPath); - } else { - type = parseTypeBody(typeName, typeBody, actualPath, namespace, revision, yangModelPrefix); - moduleBuilder.setType(type, actualPath); - } - } - } else { - type = parseUnknownTypeBody(typeQName, typeBody); - // mark parent node of this type statement as dirty - moduleBuilder.addDirtyNode(actualPath); - moduleBuilder.setType(type, actualPath); - } - - updatePath(typeName); - - } - - @Override - public void exitType_stmt(YangParser.Type_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) { - final String groupName = stringFromNode(ctx); - QName groupQName = new QName(namespace, revision, yangModelPrefix, - groupName); - GroupingBuilder groupBuilder = moduleBuilder.addGrouping(groupQName, - actualPath); - updatePath("grouping"); - updatePath(groupName); - parseSchemaNodeArgs(ctx, groupBuilder); - } - - @Override - public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) { - String actContainer = actualPath.pop(); - actContainer += "-" + actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterContainer_stmt(Container_stmtContext ctx) { - super.enterContainer_stmt(ctx); - String containerName = stringFromNode(ctx); - QName containerQName = new QName(namespace, revision, yangModelPrefix, - containerName); - ContainerSchemaNodeBuilder containerBuilder = moduleBuilder - .addContainerNode(containerQName, actualPath); - updatePath(containerName); - - containerBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); - parseSchemaNodeArgs(ctx, containerBuilder); - parseConstraints(ctx, containerBuilder.getConstraintsBuilder()); - - for (int i = 0; i < ctx.getChildCount(); ++i) { - final ParseTree childNode = ctx.getChild(i); - if (childNode instanceof Presence_stmtContext) { - containerBuilder.setPresenceContainer(true); - break; - } - } - } - - @Override - public void exitContainer_stmt(Container_stmtContext ctx) { - super.exitContainer_stmt(ctx); - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterLeaf_stmt(Leaf_stmtContext ctx) { - super.enterLeaf_stmt(ctx); - - final String leafName = stringFromNode(ctx); - QName leafQName = new QName(namespace, revision, yangModelPrefix, - leafName); - LeafSchemaNodeBuilder leafBuilder = moduleBuilder.addLeafNode( - leafQName, actualPath); - updatePath(leafName); - - leafBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); - parseSchemaNodeArgs(ctx, leafBuilder); - parseConstraints(ctx, leafBuilder.getConstraintsBuilder()); - } - - @Override - public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) { - final String actLeaf = actualPath.pop(); - logger.debug("exiting " + actLeaf); - } - - @Override - public void enterUses_stmt(YangParser.Uses_stmtContext ctx) { - final String groupingPathStr = stringFromNode(ctx); - moduleBuilder.addUsesNode(groupingPathStr, actualPath); - updatePath(groupingPathStr); - } - - @Override - public void exitUses_stmt(YangParser.Uses_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) { - super.enterLeaf_list_stmt(ctx); - - final String leafListName = stringFromNode(ctx); - QName leafListQName = new QName(namespace, revision, yangModelPrefix, - leafListName); - LeafListSchemaNodeBuilder leafListBuilder = moduleBuilder - .addLeafListNode(leafListQName, actualPath); - updatePath(leafListName); - - parseSchemaNodeArgs(ctx, leafListBuilder); - parseConstraints(ctx, leafListBuilder.getConstraintsBuilder()); - - for (int i = 0; i < ctx.getChildCount(); ++i) { - final ParseTree childNode = ctx.getChild(i); - if (childNode instanceof Ordered_by_stmtContext) { - final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode; - final boolean userOrdered = parseUserOrdered(orderedBy); - leafListBuilder.setUserOrdered(userOrdered); - break; - } - } - } - - @Override - public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterList_stmt(List_stmtContext ctx) { - super.enterList_stmt(ctx); - - final String containerName = stringFromNode(ctx); - QName containerQName = new QName(namespace, revision, yangModelPrefix, - containerName); - ListSchemaNodeBuilder listBuilder = moduleBuilder.addListNode( - containerQName, actualPath); - updatePath(containerName); - - listBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); - parseSchemaNodeArgs(ctx, listBuilder); - parseConstraints(ctx, listBuilder.getConstraintsBuilder()); - - String keyDefinition = ""; - for (int i = 0; i < ctx.getChildCount(); ++i) { - ParseTree childNode = ctx.getChild(i); - if (childNode instanceof Ordered_by_stmtContext) { - final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode; - final boolean userOrdered = parseUserOrdered(orderedBy); - listBuilder.setUserOrdered(userOrdered); - } else if (childNode instanceof Key_stmtContext) { - keyDefinition = stringFromNode(childNode); - List key = createListKey(keyDefinition, namespace, - revision, yangModelPrefix); - listBuilder.setKeyDefinition(key); - } - } - } - - @Override - public void exitList_stmt(List_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) { - final String notificationName = stringFromNode(ctx); - QName notificationQName = new QName(namespace, revision, - yangModelPrefix, notificationName); - NotificationBuilder notificationBuilder = moduleBuilder - .addNotification(notificationQName, actualPath); - updatePath(notificationName); - - notificationBuilder.setPath(createActualSchemaPath(actualPath, namespace, - revision, yangModelPrefix)); - parseSchemaNodeArgs(ctx, notificationBuilder); - } - - @Override - public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - // Unknown types - @Override - public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) { - String name = stringFromNode(ctx); - - QName qname; - if(name != null) { - String[] splittedName = name.split(":"); - if(splittedName.length == 2) { - qname = new QName(null, null, splittedName[0], splittedName[1]); - } else { - qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]); - } - } else { - qname = new QName(namespace, revision, yangModelPrefix, name); - } - - UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(qname, getActualPath()); - updatePath(name); - - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); - parseSchemaNodeArgs(ctx, builder); - } - - @Override - public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) { - final String rpcName = stringFromNode(ctx); - QName rpcQName = new QName(namespace, revision, yangModelPrefix, - rpcName); - RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(rpcQName, - actualPath); - updatePath(rpcName); - - rpcBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, - yangModelPrefix)); - parseSchemaNodeArgs(ctx, rpcBuilder); - } - - @Override - public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterInput_stmt(YangParser.Input_stmtContext ctx) { - updatePath("input"); - } - - @Override - public void exitInput_stmt(YangParser.Input_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterOutput_stmt(YangParser.Output_stmtContext ctx) { - updatePath("output"); - } - - @Override - public void exitOutput_stmt(YangParser.Output_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) { - final String featureName = stringFromNode(ctx); - QName featureQName = new QName(namespace, revision, yangModelPrefix, - featureName); - FeatureBuilder featureBuilder = moduleBuilder.addFeature(featureQName, - actualPath); - updatePath(featureName); - - featureBuilder.setPath(createActualSchemaPath(actualPath, namespace, - revision, yangModelPrefix)); - parseSchemaNodeArgs(ctx, featureBuilder); - } - - @Override - public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) { - final String targetPath = stringFromNode(ctx); - String reference = null; - String deviate = null; - DeviationBuilder builder = moduleBuilder.addDeviation(targetPath); - updatePath(targetPath); - - for (int i = 0; i < ctx.getChildCount(); i++) { - ParseTree child = ctx.getChild(i); - if (child instanceof Reference_stmtContext) { - reference = stringFromNode(child); - } else if (child instanceof Deviate_not_supported_stmtContext) { - deviate = stringFromNode(child); - } else if (child instanceof Deviate_add_stmtContext) { - deviate = stringFromNode(child); - } else if (child instanceof Deviate_replace_stmtContext) { - deviate = stringFromNode(child); - } else if (child instanceof Deviate_delete_stmtContext) { - deviate = stringFromNode(child); - } - } - builder.setReference(reference); - builder.setDeviate(deviate); - } - - @Override - public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) { - final String actContainer = actualPath.pop(); - logger.debug("exiting " + actContainer); - } - - @Override - public void enterConfig_stmt(YangParser.Config_stmtContext ctx) { - boolean configuration = parseConfig(ctx); - moduleBuilder.addConfiguration(configuration, actualPath); - } - - public ModuleBuilder getModuleBuilder() { - return moduleBuilder; - } - - private void updatePath(String containerName) { - actualPath.push(containerName); - } - - private List getActualPath() { - return Collections.unmodifiableList(actualPath); - } +/* + * 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.impl; + +import static org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil.*; + +import java.net.URI; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Stack; +import java.util.TreeMap; + +import org.antlr.v4.runtime.tree.ParseTree; +import org.opendaylight.controller.antlrv4.code.gen.YangParser; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Base_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Contact_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Container_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Description_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_add_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Import_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Key_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_list_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.List_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_header_stmtsContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Namespace_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Organization_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Prefix_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Presence_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Reference_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_date_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Union_specificationContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext; +import org.opendaylight.controller.antlrv4.code.gen.YangParserBaseListener; +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.Status; +import org.opendaylight.controller.yang.model.api.TypeDefinition; +import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder; +import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.ContainerSchemaNodeBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.DeviationBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.ExtensionBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.FeatureBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.IdentitySchemaNodeBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.LeafListSchemaNodeBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.LeafSchemaNodeBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.ListSchemaNodeBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.ModuleBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.NotificationBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.RpcDefinitionBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.TypedefBuilder; +import org.opendaylight.controller.yang.model.parser.builder.impl.UnknownSchemaNodeBuilder; +import org.opendaylight.controller.yang.model.util.YangTypesConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class YangModelParserListenerImpl extends YangParserBaseListener { + + private static final Logger logger = LoggerFactory + .getLogger(YangModelParserListenerImpl.class); + + private ModuleBuilder moduleBuilder; + + private String moduleName; + private URI namespace; + private String yangModelPrefix; + private Date revision; + + private final DateFormat simpleDateFormat = new SimpleDateFormat( + "yyyy-mm-dd"); + private final Stack actualPath = new Stack(); + + + @Override + public void enterModule_stmt(YangParser.Module_stmtContext ctx) { + moduleName = stringFromNode(ctx); + actualPath.push(moduleName); + moduleBuilder = new ModuleBuilder(moduleName); + + String description = null; + String reference = null; + + for (int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if (child instanceof Description_stmtContext) { + description = stringFromNode(child); + } else if (child instanceof Reference_stmtContext) { + reference = stringFromNode(child); + } else { + if (description != null && reference != null) { + break; + } + } + } + moduleBuilder.setDescription(description); + moduleBuilder.setReference(reference); + } + + @Override + public void exitModule_stmt(YangParser.Module_stmtContext ctx) { + final String moduleName = actualPath.pop(); + logger.debug("Exiting module " + moduleName); + } + + @Override + public void enterModule_header_stmts(final Module_header_stmtsContext ctx) { + super.enterModule_header_stmts(ctx); + + for (int i = 0; i < ctx.getChildCount(); ++i) { + final ParseTree treeNode = ctx.getChild(i); + if (treeNode instanceof Namespace_stmtContext) { + final String namespaceStr = stringFromNode(treeNode); + namespace = URI.create(namespaceStr); + moduleBuilder.setNamespace(namespace); + } else if (treeNode instanceof Prefix_stmtContext) { + yangModelPrefix = stringFromNode(treeNode); + moduleBuilder.setPrefix(yangModelPrefix); + } else if (treeNode instanceof Yang_version_stmtContext) { + final String yangVersion = stringFromNode(treeNode); + moduleBuilder.setYangVersion(yangVersion); + } + } + } + + @Override + public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) { + for (int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if (child instanceof Organization_stmtContext) { + final String organization = stringFromNode(child); + moduleBuilder.setOrganization(organization); + } else if (child instanceof Contact_stmtContext) { + final String contact = stringFromNode(child); + moduleBuilder.setContact(contact); + } else if (child instanceof Description_stmtContext) { + final String description = stringFromNode(child); + moduleBuilder.setDescription(description); + } else if (child instanceof Reference_stmtContext) { + final String reference = stringFromNode(child); + moduleBuilder.setReference(reference); + } + } + } + + @Override + public void exitSubmodule_header_stmts( + YangParser.Submodule_header_stmtsContext ctx) { + final String submodule = actualPath.pop(); + logger.debug("exiting submodule " + submodule); + } + + @Override + public void enterRevision_stmts(Revision_stmtsContext ctx) { + TreeMap revisions = new TreeMap(); + + for (int i = 0; i < ctx.getChildCount(); ++i) { + final ParseTree treeNode = ctx.getChild(i); + if (treeNode instanceof Revision_stmtContext) { + final String revisionDateStr = stringFromNode(treeNode); + try { + Date revision = simpleDateFormat.parse(revisionDateStr); + revisions.put(revision, (Revision_stmtContext)treeNode); + + } catch (ParseException e) { + final String message = "Failed to parse revision string: "+ revisionDateStr; + logger.warn(message); + } + } + } + if(revisions.size() > 0) { + Revision_stmtContext revisionCtx = revisions.firstEntry().getValue(); + moduleBuilder.setRevision(revisions.firstKey()); + + for(int i = 0; i < revisionCtx.getChildCount(); i++) { + ParseTree child = revisionCtx.getChild(i); + if(child instanceof Reference_stmtContext) { + moduleBuilder.setReference(stringFromNode(child)); + } + } + } + } + + @Override + public void enterImport_stmt(Import_stmtContext ctx) { + super.enterImport_stmt(ctx); + + final String importName = stringFromNode(ctx); + String importPrefix = null; + Date importRevision = null; + + for (int i = 0; i < ctx.getChildCount(); ++i) { + final ParseTree treeNode = ctx.getChild(i); + if (treeNode instanceof Prefix_stmtContext) { + importPrefix = stringFromNode(treeNode); + } + if (treeNode instanceof Revision_date_stmtContext) { + String importRevisionStr = stringFromNode(treeNode); + try { + importRevision = simpleDateFormat.parse(importRevisionStr); + } catch(ParseException e) { + logger.warn("Failed to parse import revision-date: "+ importRevisionStr); + } + } + } + moduleBuilder.addModuleImport(importName, importRevision, importPrefix); + } + + @Override + public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) { + final String augmentPath = stringFromNode(ctx); + AugmentationSchemaBuilder builder = moduleBuilder.addAugment( + augmentPath, getActualPath()); + updatePath(augmentPath); + + for (int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if (child instanceof Description_stmtContext) { + String desc = stringFromNode(child); + builder.setDescription(desc); + } else if (child instanceof Reference_stmtContext) { + String ref = stringFromNode(child); + builder.setReference(ref); + } else if (child instanceof Status_stmtContext) { + Status status = parseStatus((Status_stmtContext) child); + builder.setStatus(status); + } + } + } + + @Override + public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) { + final String augment = actualPath.pop(); + logger.debug("exiting augment " + augment); + } + + @Override + public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) { + String argument = stringFromNode(ctx); + QName qname = new QName(namespace, revision, yangModelPrefix, argument); + ExtensionBuilder builder = moduleBuilder.addExtension(qname); + parseSchemaNodeArgs(ctx, builder); + } + + @Override + public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) { + String typedefName = stringFromNode(ctx); + QName typedefQName = new QName(namespace, revision, yangModelPrefix, + typedefName); + TypedefBuilder builder = moduleBuilder.addTypedef(typedefQName, + getActualPath()); + updatePath(typedefName); + + builder.setPath(createActualSchemaPath(actualPath, namespace, revision, + yangModelPrefix)); + parseSchemaNodeArgs(ctx, builder); + builder.setUnits(parseUnits(ctx)); + } + + @Override + public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterType_stmt(YangParser.Type_stmtContext ctx) { + String typeName = stringFromNode(ctx); + QName typeQName; + if (typeName.contains(":")) { + String[] splittedName = typeName.split(":"); + String prefix = splittedName[0]; + String name = splittedName[1]; + if (prefix.equals(yangModelPrefix)) { + typeQName = new QName(namespace, revision, prefix, name); + } else { + typeQName = new QName(null, null, prefix, name); + } + } else { + typeQName = new QName(namespace, revision, yangModelPrefix, + typeName); + } + + TypeDefinition type = null; + Type_body_stmtsContext typeBody = null; + for (int i = 0; i < ctx.getChildCount(); i++) { + if (ctx.getChild(i) instanceof Type_body_stmtsContext) { + typeBody = (Type_body_stmtsContext) ctx.getChild(i); + break; + } + } + + // if this is base yang type... + if(YangTypesConverter.isBaseYangType(typeName)) { + if (typeBody == null) { + // if there are no constraints, just grab default base yang type + type = YangTypesConverter.javaTypeForBaseYangType(typeName); + moduleBuilder.setType(type, actualPath); + } else { + if(typeName.equals("union")) { + List types = new ArrayList(); + for(int i = 0; i < typeBody.getChildCount(); i++) { + ParseTree unionSpec = typeBody.getChild(i); + if(unionSpec instanceof Union_specificationContext) { + for(int j = 0; j < unionSpec.getChildCount(); j++) { + ParseTree typeSpec = unionSpec.getChild(j); + types.add(stringFromNode(typeSpec)); + } + } + } + moduleBuilder.addUnionType(actualPath); + } else { + type = parseTypeBody(typeName, typeBody, actualPath, namespace, revision, yangModelPrefix); + moduleBuilder.setType(type, actualPath); + } + } + } else { + type = parseUnknownTypeBody(typeQName, typeBody); + // mark parent node of this type statement as dirty + moduleBuilder.addDirtyNode(actualPath); + moduleBuilder.setType(type, actualPath); + } + + updatePath(typeName); + + } + + @Override + public void exitType_stmt(YangParser.Type_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) { + final String groupName = stringFromNode(ctx); + QName groupQName = new QName(namespace, revision, yangModelPrefix, + groupName); + GroupingBuilder groupBuilder = moduleBuilder.addGrouping(groupQName, + actualPath); + updatePath("grouping"); + updatePath(groupName); + parseSchemaNodeArgs(ctx, groupBuilder); + } + + @Override + public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) { + String actContainer = actualPath.pop(); + actContainer += "-" + actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterContainer_stmt(Container_stmtContext ctx) { + super.enterContainer_stmt(ctx); + String containerName = stringFromNode(ctx); + QName containerQName = new QName(namespace, revision, yangModelPrefix, + containerName); + ContainerSchemaNodeBuilder containerBuilder = moduleBuilder + .addContainerNode(containerQName, actualPath); + updatePath(containerName); + + containerBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, containerBuilder); + parseConstraints(ctx, containerBuilder.getConstraintsBuilder()); + + for (int i = 0; i < ctx.getChildCount(); ++i) { + final ParseTree childNode = ctx.getChild(i); + if (childNode instanceof Presence_stmtContext) { + containerBuilder.setPresenceContainer(true); + break; + } + } + } + + @Override + public void exitContainer_stmt(Container_stmtContext ctx) { + super.exitContainer_stmt(ctx); + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterLeaf_stmt(Leaf_stmtContext ctx) { + super.enterLeaf_stmt(ctx); + + final String leafName = stringFromNode(ctx); + QName leafQName = new QName(namespace, revision, yangModelPrefix, + leafName); + LeafSchemaNodeBuilder leafBuilder = moduleBuilder.addLeafNode( + leafQName, actualPath); + updatePath(leafName); + + leafBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, leafBuilder); + parseConstraints(ctx, leafBuilder.getConstraintsBuilder()); + } + + @Override + public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) { + final String actLeaf = actualPath.pop(); + logger.debug("exiting " + actLeaf); + } + + @Override + public void enterUses_stmt(YangParser.Uses_stmtContext ctx) { + final String groupingPathStr = stringFromNode(ctx); + moduleBuilder.addUsesNode(groupingPathStr, actualPath); + updatePath(groupingPathStr); + } + + @Override + public void exitUses_stmt(YangParser.Uses_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) { + super.enterLeaf_list_stmt(ctx); + + final String leafListName = stringFromNode(ctx); + QName leafListQName = new QName(namespace, revision, yangModelPrefix, + leafListName); + LeafListSchemaNodeBuilder leafListBuilder = moduleBuilder + .addLeafListNode(leafListQName, actualPath); + updatePath(leafListName); + + parseSchemaNodeArgs(ctx, leafListBuilder); + parseConstraints(ctx, leafListBuilder.getConstraintsBuilder()); + + for (int i = 0; i < ctx.getChildCount(); ++i) { + final ParseTree childNode = ctx.getChild(i); + if (childNode instanceof Ordered_by_stmtContext) { + final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode; + final boolean userOrdered = parseUserOrdered(orderedBy); + leafListBuilder.setUserOrdered(userOrdered); + break; + } + } + } + + @Override + public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterList_stmt(List_stmtContext ctx) { + super.enterList_stmt(ctx); + + final String containerName = stringFromNode(ctx); + QName containerQName = new QName(namespace, revision, yangModelPrefix, + containerName); + ListSchemaNodeBuilder listBuilder = moduleBuilder.addListNode( + containerQName, actualPath); + updatePath(containerName); + + listBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, listBuilder); + parseConstraints(ctx, listBuilder.getConstraintsBuilder()); + + String keyDefinition = ""; + for (int i = 0; i < ctx.getChildCount(); ++i) { + ParseTree childNode = ctx.getChild(i); + if (childNode instanceof Ordered_by_stmtContext) { + final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode; + final boolean userOrdered = parseUserOrdered(orderedBy); + listBuilder.setUserOrdered(userOrdered); + } else if (childNode instanceof Key_stmtContext) { + keyDefinition = stringFromNode(childNode); + List key = createListKey(keyDefinition, namespace, + revision, yangModelPrefix); + listBuilder.setKeyDefinition(key); + } + } + } + + @Override + public void exitList_stmt(List_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) { + final String notificationName = stringFromNode(ctx); + QName notificationQName = new QName(namespace, revision, + yangModelPrefix, notificationName); + NotificationBuilder notificationBuilder = moduleBuilder + .addNotification(notificationQName, actualPath); + updatePath(notificationName); + + notificationBuilder.setPath(createActualSchemaPath(actualPath, namespace, + revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, notificationBuilder); + } + + @Override + public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + // Unknown types + @Override + public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) { + String name = stringFromNode(ctx); + + QName qname; + if(name != null) { + String[] splittedName = name.split(":"); + if(splittedName.length == 2) { + qname = new QName(null, null, splittedName[0], splittedName[1]); + } else { + qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]); + } + } else { + qname = new QName(namespace, revision, yangModelPrefix, name); + } + + UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(qname, getActualPath()); + updatePath(name); + + builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, builder); + } + + @Override + public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) { + final String rpcName = stringFromNode(ctx); + QName rpcQName = new QName(namespace, revision, yangModelPrefix, + rpcName); + RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(rpcQName, + actualPath); + updatePath(rpcName); + + rpcBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, + yangModelPrefix)); + parseSchemaNodeArgs(ctx, rpcBuilder); + } + + @Override + public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterInput_stmt(YangParser.Input_stmtContext ctx) { + updatePath("input"); + } + + @Override + public void exitInput_stmt(YangParser.Input_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterOutput_stmt(YangParser.Output_stmtContext ctx) { + updatePath("output"); + } + + @Override + public void exitOutput_stmt(YangParser.Output_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) { + final String featureName = stringFromNode(ctx); + QName featureQName = new QName(namespace, revision, yangModelPrefix, + featureName); + FeatureBuilder featureBuilder = moduleBuilder.addFeature(featureQName, + actualPath); + updatePath(featureName); + + featureBuilder.setPath(createActualSchemaPath(actualPath, namespace, + revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, featureBuilder); + } + + @Override + public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) { + final String targetPath = stringFromNode(ctx); + String reference = null; + String deviate = null; + DeviationBuilder builder = moduleBuilder.addDeviation(targetPath); + updatePath(targetPath); + + for (int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if (child instanceof Reference_stmtContext) { + reference = stringFromNode(child); + } else if (child instanceof Deviate_not_supported_stmtContext) { + deviate = stringFromNode(child); + } else if (child instanceof Deviate_add_stmtContext) { + deviate = stringFromNode(child); + } else if (child instanceof Deviate_replace_stmtContext) { + deviate = stringFromNode(child); + } else if (child instanceof Deviate_delete_stmtContext) { + deviate = stringFromNode(child); + } + } + builder.setReference(reference); + builder.setDeviate(deviate); + } + + @Override + public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + @Override + public void enterConfig_stmt(YangParser.Config_stmtContext ctx) { + boolean configuration = parseConfig(ctx); + moduleBuilder.addConfiguration(configuration, actualPath); + } + + @Override + public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) { + final String identityName = stringFromNode(ctx); + final QName identityQName = new QName(namespace, revision, + yangModelPrefix, identityName); + IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName); + updatePath(identityName); + + builder.setPath(createActualSchemaPath(actualPath, namespace, + revision, yangModelPrefix)); + parseSchemaNodeArgs(ctx, builder); + + for(int i = 0; i < ctx.getChildCount(); i++) { + ParseTree child = ctx.getChild(i); + if(child instanceof Base_stmtContext) { + String baseIdentityName = stringFromNode(child); + builder.setBaseIdentityName(baseIdentityName); + } + } + } + + @Override + public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) { + final String actContainer = actualPath.pop(); + logger.debug("exiting " + actContainer); + } + + public ModuleBuilder getModuleBuilder() { + return moduleBuilder; + } + + private void updatePath(String containerName) { + actualPath.push(containerName); + } + + private List getActualPath() { + return Collections.unmodifiableList(actualPath); + } } 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 a27108e471..31ec765423 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 @@ -622,11 +622,27 @@ public class YangModelBuilderUtil { reference = stringFromNode(child); } } - String pattern = stringFromNode(ctx); + String pattern = patternStringFromNode(ctx); return BaseConstraints.patternConstraint(pattern, description, reference); } + public static String patternStringFromNode(final Pattern_stmtContext treeNode) { + String result = ""; + for (int i = 0; i < treeNode.getChildCount(); ++i) { + ParseTree child = treeNode.getChild(i); + if (child instanceof StringContext) { + for(int j = 0; j < child.getChildCount(); j++) { + if(j % 2 == 0) { + String patternToken = child.getChild(j).getText(); + result += patternToken.substring(1, patternToken.length()-1); + } + } + } + } + return result; + } + private static Integer getFractionDigits(Type_body_stmtsContext ctx) { for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree dec64specChild = ctx.getChild(j); @@ -974,7 +990,7 @@ public class YangModelBuilderUtil { if (typeName.equals("decimal64")) { type = YangTypesConverter.javaTypeForBaseYangDecimal64Type( rangeStatements, fractionDigits); - } else if (typeName.startsWith("int") || typeName.startsWith("uint")) { + } else if (typeName.startsWith("int")) { type = YangTypesConverter.javaTypeForBaseYangSignedIntegerType(typeName, rangeStatements); } else if(typeName.startsWith("uint")) { diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/TypesResolutionTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/TypesResolutionTest.java index 1dd7f2dd2c..0c8de22fef 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/TypesResolutionTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/TypesResolutionTest.java @@ -7,7 +7,7 @@ */ package org.opendaylight.controller.yang.model.parser.impl; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import java.io.File; import java.util.List; @@ -15,11 +15,18 @@ import java.util.Set; import org.junit.Before; import org.junit.Test; +import org.opendaylight.controller.yang.model.api.IdentitySchemaNode; +import org.opendaylight.controller.yang.model.api.LeafSchemaNode; import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.TypeDefinition; import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair; +import org.opendaylight.controller.yang.model.api.type.LengthConstraint; +import org.opendaylight.controller.yang.model.api.type.PatternConstraint; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang.model.util.EnumerationType; +import org.opendaylight.controller.yang.model.util.InstanceIdentifier; +import org.opendaylight.controller.yang.model.util.StringType; +import org.opendaylight.controller.yang.model.util.UnionType; public class TypesResolutionTest { @@ -30,30 +37,29 @@ public class TypesResolutionTest { @Before public void init() { parser = new YangModelParserImpl(); - testFiles = new String[5]; - File testDir = new File("src/test/resources/types"); String[] fileList = testDir.list(); + testFiles = new String[fileList.length]; int i = 0; for(String fileName : fileList) { File file = new File(testDir, fileName); testFiles[i] = file.getAbsolutePath(); i++; } - modules = parser.parseYangModels(testFiles); - assertEquals(5, modules.size()); + assertEquals(fileList.length, modules.size()); } @Test - public void testIetfInetTypes() { + public void testIPVersion() { Module tested = findModule(modules, "ietf-inet-types"); Set> typedefs = tested.getTypeDefinitions(); assertEquals(14, typedefs.size()); - TypeDefinition t1 = findTypedef(typedefs, "ip-version"); - EnumerationType en = (EnumerationType)t1.getBaseType(); + TypeDefinition type = findTypedef(typedefs, "ip-version"); + EnumerationType en = (EnumerationType)type.getBaseType(); List values = en.getValues(); + assertEquals(3, values.size()); EnumPair value0 = values.get(0); assertEquals("unknown", value0.getName()); @@ -68,6 +74,88 @@ public class TypesResolutionTest { assertEquals(2, (int)value2.getValue()); } + @Test + public void testIpAddress() { + Module tested = findModule(modules, "ietf-inet-types"); + Set> typedefs = tested.getTypeDefinitions(); + TypeDefinition type = findTypedef(typedefs, "ip-address"); + UnionType baseType = (UnionType)type.getBaseType(); + List> unionTypes = baseType.getTypes(); + + StringType ipv4 = (StringType)unionTypes.get(0); + String expectedPattern = + "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}" + + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + + "(%[\\p{N}\\p{L}]+)?"; + assertEquals(expectedPattern, ipv4.getPatterns().get(0).getRegularExpression()); + + StringType ipv6 = (StringType)unionTypes.get(1); + List ipv6Patterns = ipv6.getPatterns(); + expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}" + + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|" + + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}" + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + + "(%[\\p{N}\\p{L}]+)?"; + assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression()); + + expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + + "(%.+)?"; + assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression()); + } + + @Test + public void testDomainName() { + Module tested = findModule(modules, "ietf-inet-types"); + Set> typedefs = tested.getTypeDefinitions(); + TypeDefinition type = findTypedef(typedefs, "domain-name"); + StringType baseType = (StringType)type.getBaseType(); + List patterns = baseType.getPatterns(); + assertEquals(1, patterns.size()); + String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*" + + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + + "|\\."; + assertEquals(expectedPattern, patterns.get(0).getRegularExpression()); + + List lengths = baseType.getLengthStatements(); + assertEquals(1, lengths.size()); + LengthConstraint length = baseType.getLengthStatements().get(0); + assertEquals(1L, (long)length.getMin()); + assertEquals(253L, (long)length.getMax()); + } + + @Test + public void testInstanceIdentifier1() { + Module tested = findModule(modules, "custom-types-test"); + LeafSchemaNode leaf = (LeafSchemaNode)tested.getDataChildByName("inst-id-leaf1"); + InstanceIdentifier leafType = (InstanceIdentifier)leaf.getType(); + assertFalse(leafType.requireInstance()); + } + + @Test + public void testInstanceIdentifier2() { + Module tested = findModule(modules, "custom-types-test"); + LeafSchemaNode leaf = (LeafSchemaNode)tested.getDataChildByName("inst-id-leaf2"); + InstanceIdentifier leafType = (InstanceIdentifier)leaf.getType(); + assertTrue(leafType.requireInstance()); + } + + @Test + public void testIdentity() { + Module tested = findModule(modules, "custom-types-test"); + Set identities = tested.getIdentities(); + IdentitySchemaNode testedIdentity = null; + for(IdentitySchemaNode id : identities) { + if(id.getQName().getLocalName().equals("crypto-alg")) { + testedIdentity = id; + IdentitySchemaNode baseIdentity = id.getBaseIdentity(); + assertEquals("crypto-base", baseIdentity.getQName().getLocalName()); + assertNull(baseIdentity.getBaseIdentity()); + } + } + assertNotNull(testedIdentity); + } + private Module findModule(Set modules, String name) { for(Module module : modules) { if(module.getName().equals(name)) { diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerTest.java index 4bd54f3364..54561f4fe9 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserListenerTest.java @@ -93,7 +93,7 @@ public class YangModelParserListenerTest { assertEquals(2, typedefs.size()); for(TypeDefinition td : typedefs) { Leafref baseType = (Leafref)td.getBaseType(); - if(td.getQName().getLocalName().equals("network-node-id-ref")) { + if(td.getQName().getLocalName().equals("node-id-ref")) { assertEquals("/tp:topology/tp:network-nodes/tp:network-node/tp:node-id", baseType.getPathStatement().toString()); } else { assertEquals("/tp:topology/tp:network-links/tp:network-link/tp:link-id", baseType.getPathStatement().toString()); diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/abstract-topology.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/abstract-topology.yang index dbece9a997..46aac78789 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/abstract-topology.yang +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/abstract-topology.yang @@ -19,7 +19,7 @@ module abstract-topology { reference "~~~ WILL BE DEFINED LATER"; } - typedef network-node-id-ref { + typedef node-id-ref { type leafref { path "/tp:topology/tp:network-nodes/tp:network-node/tp:node-id"; } diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/types/custom-types-test@2012-4-4.yang b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/types/custom-types-test@2012-4-4.yang new file mode 100644 index 0000000000..4231699b50 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/resources/types/custom-types-test@2012-4-4.yang @@ -0,0 +1,29 @@ +module custom-types-test { + + yang-version 1; + namespace "urn:simple.container.demo"; + prefix "iit"; + + organization "Cisco"; + contact "WILL-BE-DEFINED-LATER"; + + leaf inst-id-leaf1 { + type instance-identifier { + require-instance false; + } + } + + leaf inst-id-leaf2 { + type instance-identifier; + } + + identity crypto-base { + description "crypto-base description"; + } + + identity crypto-alg { + base "crypto-base"; + description "crypto-alg description"; + } + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/IdentitySchemaNode.java b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/IdentitySchemaNode.java new file mode 100644 index 0000000000..d1e68182c3 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/IdentitySchemaNode.java @@ -0,0 +1,14 @@ +/* + * 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.api; + +public interface IdentitySchemaNode extends SchemaNode { + + IdentitySchemaNode getBaseIdentity(); + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/Module.java b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/Module.java index 7dda61ec7e..f2171480a0 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/Module.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/Module.java @@ -44,6 +44,8 @@ public interface Module extends DataNodeContainer { Set getDeviations(); + Set getIdentities(); + List getExtensionSchemaNodes(); } diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/type/IdentityrefTypeDefinition.java b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/type/IdentityrefTypeDefinition.java index b2b78d43a6..e2297d65aa 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/type/IdentityrefTypeDefinition.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-api/src/main/java/org/opendaylight/controller/yang/model/api/type/IdentityrefTypeDefinition.java @@ -1,19 +1,18 @@ -/* - * 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.api.type; - -import org.opendaylight.controller.yang.model.api.RevisionAwareXPath; -import org.opendaylight.controller.yang.model.api.TypeDefinition; - -public interface IdentityrefTypeDefinition extends - TypeDefinition { - - public RevisionAwareXPath getPathStatement(); - - public IdentityTypeDefinition getIdentity(); +/* + * 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.api.type; + +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.TypeDefinition; + +public interface IdentityrefTypeDefinition extends + TypeDefinition { + + public QName getIdentity(); + } diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/Identityref.java b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/IdentityrefType.java similarity index 50% rename from opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/Identityref.java rename to opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/IdentityrefType.java index 27fdfacdfc..3837f72a8a 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/Identityref.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/IdentityrefType.java @@ -1,153 +1,89 @@ -/* - * 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.util; - -import java.util.Collections; -import java.util.List; - -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.RevisionAwareXPath; -import org.opendaylight.controller.yang.model.api.SchemaPath; -import org.opendaylight.controller.yang.model.api.Status; -import org.opendaylight.controller.yang.model.api.UnknownSchemaNode; -import org.opendaylight.controller.yang.model.api.type.IdentityTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition; - -/** - * The default implementation of Identityref Type Definition interface. - * - * @see IdentityrefTypeDefinition - */ -public class Identityref implements IdentityrefTypeDefinition { - - private final QName name = BaseTypes.constructQName("identityref"); - private final SchemaPath path = BaseTypes.schemaPath(name); - private final String description = "The identityref type is used to reference an existing identity."; - private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.10"; - - private final IdentityTypeDefinition identity; - private final RevisionAwareXPath xpath; - - private String units = ""; - - public Identityref(RevisionAwareXPath xpath, IdentityTypeDefinition identity) { - super(); - this.identity = identity; - this.xpath = xpath; - } - - public Identityref(RevisionAwareXPath xpath) { - super(); - this.xpath = xpath; - this.identity = null; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getBaseType() - */ - @Override - public IdentityTypeDefinition getBaseType() { - return identity; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getUnits() - */ - @Override - public String getUnits() { - return units; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getDefaultValue() - */ - @Override - public Object getDefaultValue() { - return identity; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.SchemaNode#getQName() - */ - @Override - public QName getQName() { - return name; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.SchemaNode#getPath() - */ - @Override - public SchemaPath getPath() { - return path; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.SchemaNode#getDescription() - */ - @Override - public String getDescription() { - return description; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.SchemaNode#getReference() - */ - @Override - public String getReference() { - return reference; - } - - /* - * (non-Javadoc) - * - * @see org.opendaylight.controller.yang.model.api.SchemaNode#getStatus() - */ - @Override - public Status getStatus() { - return Status.CURRENT; - } - - @Override - public List getUnknownSchemaNodes() { - return Collections.emptyList(); - } - - /* - * (non-Javadoc) - * - * @see - * org.opendaylight.controller.yang.model.base.type.api.IdentityrefTypeDefinition#getIdentityName - * () - */ - @Override - public IdentityTypeDefinition getIdentity() { - return identity; - } - - @Override - public RevisionAwareXPath getPathStatement() { - return xpath; - } - - -} +/* + * 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.util; + +import java.util.Collections; +import java.util.List; + +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.api.UnknownSchemaNode; +import org.opendaylight.controller.yang.model.api.type.IdentityrefTypeDefinition; + +/** + * The default implementation of Identityref Type Definition interface. + * + * @see IdentityrefTypeDefinition + */ +public class IdentityrefType implements IdentityrefTypeDefinition { + + private final QName name = BaseTypes.constructQName("identityref"); + private final SchemaPath path = BaseTypes.schemaPath(name); + private final String description = "The identityref type is used to reference an existing identity."; + private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.10"; + + private final QName identity; + + private String units = ""; + + public IdentityrefType(QName identity) { + this.identity = identity; + } + + @Override + public String getUnits() { + return units; + } + + @Override + public Object getDefaultValue() { + return identity; + } + + @Override + public QName getQName() { + return name; + } + + @Override + public SchemaPath getPath() { + return path; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public String getReference() { + return reference; + } + + @Override + public Status getStatus() { + return Status.CURRENT; + } + + @Override + public List getUnknownSchemaNodes() { + return Collections.emptyList(); + } + + @Override + public QName getIdentity() { + return identity; + } + + @Override + public IdentityrefTypeDefinition getBaseType() { + return this; + } + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/YangTypesConverter.java b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/YangTypesConverter.java index fdaf57f7b2..f20c6aae6e 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/YangTypesConverter.java +++ b/opendaylight/sal/yang-prototype/yang/yang-model-util/src/main/java/org/opendaylight/controller/yang/model/util/YangTypesConverter.java @@ -1,127 +1,130 @@ -/* - * 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.util; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.TypeDefinition; -import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.BooleanTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.RangeConstraint; -import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition; -import org.opendaylight.controller.yang.model.api.type.UnsignedIntegerTypeDefinition; - -public class YangTypesConverter { - - private static final Map>> baseYangTypeMap = new HashMap>>(); - private static final Set baseYangTypes = new HashSet(); - - private static final TypeDefinition BINARY = new BinaryType(); - private static final TypeDefinition BITS = new BitsType(); - private static final TypeDefinition BOOLEAN_TYPE = new BooleanType(); - private static final TypeDefinition INT8_TYPE = new Int8(); - private static final TypeDefinition INT16_TYPE = new Int16(); - private static final TypeDefinition INT32_TYPE = new Int32(); - private static final TypeDefinition INT64_TYPE = new Int64(); - private static final TypeDefinition STRING_TYPE = new StringType(); - private static final TypeDefinition UINT8_TYPE = new Uint8(); - private static final TypeDefinition UINT16_TYPE = new Uint16(); - private static final TypeDefinition UINT32_TYPE = new Uint32(); - private static final TypeDefinition UINT64_TYPE = new Uint64(); - - static { - baseYangTypeMap.put("binary", BINARY); - baseYangTypeMap.put("bits", BITS); - baseYangTypeMap.put("boolean", BOOLEAN_TYPE); - baseYangTypeMap.put("int8", INT8_TYPE); - baseYangTypeMap.put("int16", INT16_TYPE); - baseYangTypeMap.put("int32", INT32_TYPE); - baseYangTypeMap.put("int64", INT64_TYPE); - baseYangTypeMap.put("string", STRING_TYPE); - baseYangTypeMap.put("uint8", UINT8_TYPE); - baseYangTypeMap.put("uint16", UINT16_TYPE); - baseYangTypeMap.put("uint32", UINT32_TYPE); - baseYangTypeMap.put("uint64", UINT64_TYPE); - - baseYangTypes.add("binary"); - baseYangTypes.add("bits"); - baseYangTypes.add("boolean"); - baseYangTypes.add("decimal64"); - baseYangTypes.add("empty"); - baseYangTypes.add("enumeration"); - baseYangTypes.add("identityref"); - baseYangTypes.add("instance-identifier"); - baseYangTypes.add("int8"); - baseYangTypes.add("int16"); - baseYangTypes.add("int32"); - baseYangTypes.add("int64"); - baseYangTypes.add("leafref"); - baseYangTypes.add("string"); - baseYangTypes.add("uint8"); - baseYangTypes.add("uint16"); - baseYangTypes.add("uint32"); - baseYangTypes.add("uint64"); - baseYangTypes.add("union"); - } - - public static boolean isBaseYangType(String type) { - return baseYangTypes.contains(type); - } - - public static TypeDefinition javaTypeForBaseYangType(QName typeQName) { - TypeDefinition type = baseYangTypeMap.get(typeQName.getLocalName()); - return type; - } - - public static TypeDefinition javaTypeForBaseYangType(String typeName) { - TypeDefinition type = baseYangTypeMap.get(typeName); - return type; - } - - public static TypeDefinition javaTypeForBaseYangSignedIntegerType( - String typeName, List ranges) { - if (typeName.equals("int8")) { - return new Int8(ranges, null, null); - } else if (typeName.equals("int16")) { - return new Int16(ranges, null, null); - } else if (typeName.equals("int32")) { - return new Int32(ranges, null, null); - } else if (typeName.equals("int64")) { - return new Int64(ranges, null, null); - } - return null; - } - - public static TypeDefinition javaTypeForBaseYangUnsignedIntegerType( - final String typeName, List ranges) { - if (typeName.equals("uint8")) { - return new Uint8(ranges, null, null); - } else if (typeName.equals("uint16")) { - return new Uint16(ranges, null, null); - } else if (typeName.equals("uint32")) { - return new Uint32(ranges, null, null); - } else if (typeName.equals("uint64")) { - return new Uint64(ranges, null, null); - } - return null; - } - - public static TypeDefinition javaTypeForBaseYangDecimal64Type( - List rangeStatements, int fractionDigits) { - return new Decimal64(rangeStatements, fractionDigits); - } - +/* + * 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.util; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.model.api.TypeDefinition; +import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.BooleanTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.InstanceIdentifierTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.IntegerTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.RangeConstraint; +import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition; +import org.opendaylight.controller.yang.model.api.type.UnsignedIntegerTypeDefinition; + +public class YangTypesConverter { + + private static final Map>> baseYangTypeMap = new HashMap>>(); + private static final Set baseYangTypes = new HashSet(); + + private static final TypeDefinition BINARY = new BinaryType(); + private static final TypeDefinition BITS = new BitsType(); + private static final TypeDefinition BOOLEAN_TYPE = new BooleanType(); + private static final TypeDefinition INST_ID_TYPE = new InstanceIdentifier(null, true); + private static final TypeDefinition INT8_TYPE = new Int8(); + private static final TypeDefinition INT16_TYPE = new Int16(); + private static final TypeDefinition INT32_TYPE = new Int32(); + private static final TypeDefinition INT64_TYPE = new Int64(); + private static final TypeDefinition STRING_TYPE = new StringType(); + private static final TypeDefinition UINT8_TYPE = new Uint8(); + private static final TypeDefinition UINT16_TYPE = new Uint16(); + private static final TypeDefinition UINT32_TYPE = new Uint32(); + private static final TypeDefinition UINT64_TYPE = new Uint64(); + + static { + baseYangTypeMap.put("binary", BINARY); + baseYangTypeMap.put("bits", BITS); + baseYangTypeMap.put("boolean", BOOLEAN_TYPE); + baseYangTypeMap.put("instance-identifier", INST_ID_TYPE); + baseYangTypeMap.put("int8", INT8_TYPE); + baseYangTypeMap.put("int16", INT16_TYPE); + baseYangTypeMap.put("int32", INT32_TYPE); + baseYangTypeMap.put("int64", INT64_TYPE); + baseYangTypeMap.put("string", STRING_TYPE); + baseYangTypeMap.put("uint8", UINT8_TYPE); + baseYangTypeMap.put("uint16", UINT16_TYPE); + baseYangTypeMap.put("uint32", UINT32_TYPE); + baseYangTypeMap.put("uint64", UINT64_TYPE); + + baseYangTypes.add("binary"); + baseYangTypes.add("bits"); + baseYangTypes.add("boolean"); + baseYangTypes.add("decimal64"); + baseYangTypes.add("empty"); + baseYangTypes.add("enumeration"); + baseYangTypes.add("identityref"); + baseYangTypes.add("instance-identifier"); + baseYangTypes.add("int8"); + baseYangTypes.add("int16"); + baseYangTypes.add("int32"); + baseYangTypes.add("int64"); + baseYangTypes.add("leafref"); + baseYangTypes.add("string"); + baseYangTypes.add("uint8"); + baseYangTypes.add("uint16"); + baseYangTypes.add("uint32"); + baseYangTypes.add("uint64"); + baseYangTypes.add("union"); + } + + public static boolean isBaseYangType(String type) { + return baseYangTypes.contains(type); + } + + public static TypeDefinition javaTypeForBaseYangType(QName typeQName) { + TypeDefinition type = baseYangTypeMap.get(typeQName.getLocalName()); + return type; + } + + public static TypeDefinition javaTypeForBaseYangType(String typeName) { + TypeDefinition type = baseYangTypeMap.get(typeName); + return type; + } + + public static TypeDefinition javaTypeForBaseYangSignedIntegerType( + String typeName, List ranges) { + if (typeName.equals("int8")) { + return new Int8(ranges, null, null); + } else if (typeName.equals("int16")) { + return new Int16(ranges, null, null); + } else if (typeName.equals("int32")) { + return new Int32(ranges, null, null); + } else if (typeName.equals("int64")) { + return new Int64(ranges, null, null); + } + return null; + } + + public static TypeDefinition javaTypeForBaseYangUnsignedIntegerType( + final String typeName, List ranges) { + if (typeName.equals("uint8")) { + return new Uint8(ranges, null, null); + } else if (typeName.equals("uint16")) { + return new Uint16(ranges, null, null); + } else if (typeName.equals("uint32")) { + return new Uint32(ranges, null, null); + } else if (typeName.equals("uint64")) { + return new Uint64(ranges, null, null); + } + return null; + } + + public static TypeDefinition javaTypeForBaseYangDecimal64Type( + List rangeStatements, int fractionDigits) { + return new Decimal64(rangeStatements, fractionDigits); + } + } -- 2.36.6