From d1f606bf178d536ab6681bc9dbf8205906bcf40d Mon Sep 17 00:00:00 2001 From: Peter Kajsa Date: Thu, 20 Aug 2015 11:21:38 +0200 Subject: [PATCH] Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin Parser fixes: - error message fix - SchemaContextUtilTest fix - module source path fix - module source prep. - import of multiple revision module without revision specifying fix - minor fixes of BaseStatementImpl class - adjustment of decimal64 type to original yang parser behavior - restrictions to leaf return type fix - range constraints in decimal64 typedef fix - xpath validation bug fix - case short hand augmenting fix - augment order fix - module dependency sort in effective schema context fix - get original statement context fix - statement context copy type fix - unknown statements QName resolution fix - extension definition fix in test models - moduleImport default revision replace - default revision fix - sourcePath resolved correctly via NamedFileInputStream Change-Id: I54656ff50f681bb1357f7df4e606af3c925341eb Signed-off-by: Peter Kajsa --- .../impl/YangStatementParserListenerImpl.java | 56 +- .../yang/parser/spi/meta/StmtContext.java | 10 +- .../parser/spi/meta/StmtContextUtils.java | 107 +-- .../spi/source/StmtOrderingNamespace.java | 22 + .../reactor/CrossSourceStatementReactor.java | 15 +- .../stmt/reactor/RootStatementContext.java | 12 +- .../stmt/reactor/SourceSpecificContext.java | 3 - .../stmt/reactor/StatementContextBase.java | 36 +- .../stmt/reactor/SubstatementContext.java | 12 +- .../stmt/rfc6020/AugmentStatementImpl.java | 34 +- .../stmt/rfc6020/BaseStatementImpl.java | 10 +- .../stmt/rfc6020/BinarySpecificationImpl.java | 65 -- .../stmt/rfc6020/DeviationStatementImpl.java | 2 +- .../rfc6020/ImportStatementDefinition.java | 91 +- .../stmt/rfc6020/MustStatementImpl.java | 2 +- .../stmt/rfc6020/PathStatementImpl.java | 2 +- .../stmt/rfc6020/RefineStatementImpl.java | 2 +- .../stmt/rfc6020/TypeStatementImpl.java | 5 +- .../yang/parser/stmt/rfc6020/TypeUtils.java | 168 ++-- .../yang/parser/stmt/rfc6020/Utils.java | 192 ++-- .../stmt/rfc6020/WhenStatementImpl.java | 2 +- .../stmt/rfc6020/YangInferencePipeline.java | 4 +- .../stmt/rfc6020/YangStatementSourceImpl.java | 58 +- .../AnyXmlEffectiveStatementImpl.java | 9 +- .../AugmentEffectiveStatementImpl.java | 2 +- .../effective/CaseEffectiveStatementImpl.java | 6 +- .../rfc6020/effective/CaseShorthandImpl.java | 99 +- .../ChoiceEffectiveStatementImpl.java | 48 +- .../ContainerEffectiveStatementImpl.java | 8 +- .../effective/EffectiveSchemaContext.java | 7 +- .../effective/EffectiveStatementBase.java | 15 + .../ExtendedTypeEffectiveStatementImpl.java | 35 +- .../GroupingEffectiveStatementImpl.java | 5 +- .../InputEffectiveStatementImpl.java | 6 +- .../effective/LeafEffectiveStatementImpl.java | 9 +- .../LeafListEffectiveStatementImpl.java | 9 +- .../effective/ListEffectiveStatementImpl.java | 7 +- .../ModuleEffectiveStatementImpl.java | 83 +- .../OutputEffectiveStatementImpl.java | 6 +- .../SubmoduleEffectiveStatementImpl.java | 36 +- .../TypeDefEffectiveStatementImpl.java | 152 ++- .../UnknownEffectiveStatementImpl.java | 9 +- .../effective/UsesEffectiveStatementImpl.java | 4 +- .../type/BinaryEffectiveStatementImpl.java | 4 +- ...64SpecificationEffectiveStatementImpl.java | 117 ++- .../parser/util/ModuleDependencySort.java | 2 +- .../yang/parser/impl/AugmentTest.java | 18 + .../parser/util/SchemaContextUtilTest.java | 8 +- .../yang/stmt/retest/Bug2291Test.java | 28 + .../stmt/retest/SchemaContextUtilTest.java | 866 ++++++++++++++++++ .../yangtools/yang/stmt/retest/TestUtils.java | 126 ++- .../yang/stmt/test/AugmentProcessTest.java | 29 + .../yang/stmt/test/MoreRevisionsTest.java | 18 +- .../yang/stmt/test/StmtTestUtils.java | 59 +- .../stmt/test/TypedefConstraintsTest.java | 73 ++ .../yang/stmt/test/YangTypes2StmtTest.java | 8 +- .../augment-monitoring@2013-07-01.yang | 25 + .../choice-monitoring@2013-07-01.yang | 30 + .../multiple-revisions/importedtestrev.yang | 13 + .../importedtestrev@2015-05-02.yang | 13 + .../importedtestrev@2015-06-02.yang | 13 + .../importedtestrev@2015-08-02.yang | 13 + .../importedtestrev@2015-09-02.yang | 13 + .../multiple-revisions/testrev.yang | 19 + .../resources/stmt-test/constraints/foo.yang | 29 + 65 files changed, 2376 insertions(+), 613 deletions(-) create mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/StmtOrderingNamespace.java delete mode 100644 yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BinarySpecificationImpl.java create mode 100644 yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug2291Test.java create mode 100644 yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/SchemaContextUtilTest.java create mode 100644 yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java create mode 100644 yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/augment-monitoring@2013-07-01.yang create mode 100644 yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/choice-monitoring@2013-07-01.yang create mode 100644 yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev.yang create mode 100644 yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-05-02.yang create mode 100644 yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-06-02.yang create mode 100644 yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-08-02.yang create mode 100644 yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-09-02.yang create mode 100644 yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/testrev.yang create mode 100644 yang/yang-parser-impl/src/test/resources/stmt-test/constraints/foo.yang diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangStatementParserListenerImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangStatementParserListenerImpl.java index 04f54c624c..526f9d1adc 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangStatementParserListenerImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangStatementParserListenerImpl.java @@ -9,26 +9,25 @@ package org.opendaylight.yangtools.yang.parser.impl; import java.util.ArrayList; import java.util.List; - -import javax.annotation.concurrent.Immutable; - +import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase; +import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.antlr.v4.runtime.tree.ParseTree; import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser; import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; -import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase; -import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource; import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule; import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference; import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; + +import javax.annotation.concurrent.Immutable; @Immutable public class YangStatementParserListenerImpl extends YangStatementParserBaseListener { @@ -43,9 +42,6 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList public YangStatementParserListenerImpl(String sourceName) { this.sourceName = sourceName; - // TODO - // this.sourceName = Preconditions.checkNotNull(sourceName, "YangStatementParserListenerImpl#sourceName cannot " - // + "be null"); } public void setAttributes(StatementWriter writer, QNameToStatementDefinition stmtDef) { @@ -61,8 +57,8 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList @Override public void enterStatement(YangStatementParser.StatementContext ctx) { - final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), - ctx.getStart().getCharPositionInLine()); + final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx + .getStart().getLine(), ctx.getStart().getCharPositionInLine()); boolean action = true; QName identifier; for (int i = 0; i < ctx.getChildCount(); i++) { @@ -71,8 +67,7 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList try { identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, ((YangStatementParser.KeywordContext) child).children.get(0).getText()); - if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) - && toBeSkipped.isEmpty()) { + if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) { if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) { isType = true; } else { @@ -80,10 +75,9 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList } } else { if (writer.getPhase().equals(ModelProcessingPhase.FULL_DECLARATION)) { - throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement " - + "or use of extension. Source: " + ref); - } - else { + throw new IllegalArgumentException(identifier.getLocalName() + " is not a YANG statement " + + "or use of extension. Source: " + ref); + } else { action = false; toBeSkipped.add(((YangStatementParser.KeywordContext) child).children.get(0).getText()); } @@ -95,17 +89,16 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList try { final String argument = Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child); if (isType) { - if (TypeUtils.isYangTypeBodyStmt(argument)) { - writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, argument), ref); - } else { - writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, Rfc6020Mapping.TYPE - .getStatementName().getLocalName()), ref); - } + if (TypeUtils.isYangTypeBodyStmtString(argument)) { + writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, argument), ref); + } else { + writer.startStatement(new QName(YangConstants.RFC6020_YIN_NAMESPACE, Rfc6020Mapping + .TYPE.getStatementName().getLocalName()), ref); + } writer.argumentValue(argument, ref); isType = false; } else if (action) { - writer.argumentValue( - Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref); + writer.argumentValue(Utils.stringFromStringContext((YangStatementParser.ArgumentContext) child), ref); } else { action = true; } @@ -119,16 +112,15 @@ public class YangStatementParserListenerImpl extends YangStatementParserBaseList @Override public void exitStatement(YangStatementParser.StatementContext ctx) { - final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), - ctx.getStart().getCharPositionInLine()); + final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx.getStart().getLine(), ctx + .getStart().getCharPositionInLine()); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); if (child instanceof YangStatementParser.KeywordContext) { try { String statementName = ((YangStatementParser.KeywordContext) child).children.get(0).getText(); QName identifier = new QName(YangConstants.RFC6020_YIN_NAMESPACE, statementName); - if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) - && toBeSkipped.isEmpty()) { + if (stmtDef != null && Utils.isValidStatementDefinition(prefixes, stmtDef, identifier) && toBeSkipped.isEmpty()) { writer.endStatement(ref); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java index d46e2716a7..46cdb16628 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java @@ -88,9 +88,11 @@ public interface StmtContext, E extends Effect ORIGINAL, ADDED_BY_USES, ADDED_BY_AUGMENTATION, ADDED_BY_USES_AUGMENTATION } - TypeOfCopy getTypeOfCopy(); + List getCopyHistory(); - void setTypeOfCopy(TypeOfCopy typeOfCopy); + void addAllToCopyHistory(List typeOfCopyList); + + void addToCopyHistory(TypeOfCopy typeOfCopy); StatementContextBase getOriginalCtx(); @@ -98,6 +100,10 @@ public interface StmtContext, E extends Effect boolean isRootContext(); + void setOrder(int order); + + int getOrder(); + void setCompletedPhase(ModelProcessingPhase completedPhase); ModelProcessingPhase getCompletedPhase(); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index eccbaca07d..0c1169a5f9 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -7,42 +7,41 @@ */ package org.opendaylight.yangtools.yang.parser.spi.meta; -import com.google.common.base.Function; -import com.google.common.base.Predicate; +import org.opendaylight.yangtools.yang.common.QName; import com.google.common.base.Splitter; -import java.util.Collection; -import java.util.HashSet; import java.util.List; -import java.util.Set; -import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; import org.opendaylight.yangtools.yang.common.QNameModule; -import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; +import java.util.HashSet; +import java.util.Set; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl; +import com.google.common.base.Predicate; +import java.util.Collection; +import com.google.common.base.Function; +import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; public final class StmtContextUtils { public static final char LIST_KEY_SEPARATOR = ' '; private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults(); - private static final Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { + private static final Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { @Override public DeclaredStatement apply(final StmtContext input) { return input.buildDeclared(); } }; - private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { + private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { @Override public EffectiveStatement apply(final StmtContext input) { return input.buildEffective(); } }; - public static final Predicate> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate>() { + public static final Predicate> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate>() { @Override public boolean apply(StmtContext input) { return input.isSupportedToBuildEffective(); @@ -65,7 +64,8 @@ public final class StmtContextUtils { @SuppressWarnings("unchecked") public static final > AT firstAttributeOf( - final Iterable> contexts, final Class
declaredType) { + final Iterable> contexts, + final Class
declaredType) { for (StmtContext ctx : contexts) { if (producesDeclared(ctx, declaredType)) { return (AT) ctx.getStatementArgument(); @@ -75,8 +75,8 @@ public final class StmtContextUtils { } @SuppressWarnings("unchecked") - public static final > AT firstAttributeOf(final StmtContext ctx, - final Class
declaredType) { + public static final > AT firstAttributeOf( + final StmtContext ctx, final Class
declaredType) { if (producesDeclared(ctx, declaredType)) { return (AT) ctx.getStatementArgument(); @@ -86,31 +86,34 @@ public final class StmtContextUtils { } @SuppressWarnings("unchecked") - public static final > StmtContext findFirstDeclaredSubstatement( + public static final > StmtContext findFirstDeclaredSubstatement( StmtContext stmtContext, Class
declaredType) { - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); + Collection> declaredSubstatements = stmtContext + .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext, declaredType)) { + if (producesDeclared(subStmtContext,declaredType)) { return (StmtContext) subStmtContext; } } return null; } - public static final StmtContext findFirstDeclaredSubstatement(final StmtContext stmtContext, - int startIndex, final Class>... types) { + public static final StmtContext findFirstDeclaredSubstatement( + final StmtContext stmtContext, int startIndex, final Class>... types) { if (startIndex >= types.length) { return null; } - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); + Collection> declaredSubstatements = stmtContext + .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext, types[startIndex])) { + if (producesDeclared(subStmtContext,types[startIndex])) { if (startIndex + 1 == types.length) { return subStmtContext; } else { - return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types); + return findFirstDeclaredSubstatement(subStmtContext, + ++startIndex, types); } } } @@ -118,15 +121,17 @@ public final class StmtContextUtils { } public static final
> StmtContext findFirstDeclaredSubstatementOnSublevel( - final StmtContext stmtContext, final Class
declaredType, int sublevel) { - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); + final StmtContext stmtContext, final Class
declaredType, + int sublevel) { + Collection> declaredSubstatements = stmtContext + .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { - if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) { + if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } else { if (sublevel > 1) { - StmtContext result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType, - --sublevel); + StmtContext result = findFirstDeclaredSubstatementOnSublevel( + subStmtContext, declaredType, --sublevel); if (result != null) { return result; } @@ -139,13 +144,15 @@ public final class StmtContextUtils { public static final
> StmtContext findDeepFirstDeclaredSubstatement( final StmtContext stmtContext, final Class
declaredType) { - Collection> declaredSubstatements = stmtContext.declaredSubstatements(); + Collection> declaredSubstatements = stmtContext + .declaredSubstatements(); for (StmtContext subStmtContext : declaredSubstatements) { - if (producesDeclared(subStmtContext, declaredType)) { + if (producesDeclared(subStmtContext,declaredType)) { return subStmtContext; } else { - StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); + StmtContext result = findDeepFirstDeclaredSubstatement( + subStmtContext, declaredType); if (result != null) { return result; } @@ -157,15 +164,17 @@ public final class StmtContextUtils { public static final boolean producesDeclared(final StmtContext ctx, final Class> type) { - return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass()); + return type.isAssignableFrom(ctx.getPublicDefinition() + .getDeclaredRepresentationClass()); } - public static boolean isInExtensionBody(StmtContext stmtCtx) { + public static boolean isInExtensionBody( + StmtContext stmtCtx) { - StmtContext current = stmtCtx; - while (!current.getParentContext().isRootContext()) { + StmtContext current = stmtCtx; + while(!current.getParentContext().isRootContext()) { current = current.getParentContext(); - if (producesDeclared(current, UnknownStatementImpl.class)) { + if(producesDeclared(current, UnknownStatementImpl.class)) { return true; } } @@ -174,24 +183,15 @@ public final class StmtContextUtils { } public static boolean isUnknownStatement(StmtContext stmtCtx) { - return producesDeclared(stmtCtx, UnknownStatementImpl.class); - } - - public static Set getCopyTypesFromOriginal(StmtContext ctx) { - - Set copyTypesFromOriginal = new HashSet<>(); - StmtContext current = ctx; - - while (current.getOriginalCtx() != null) { - copyTypesFromOriginal.add(current.getTypeOfCopy()); - current = current.getOriginalCtx(); + if (producesDeclared(stmtCtx, UnknownStatementImpl.class)) { + return true; + } else { + return false; } - - return copyTypesFromOriginal; } - public static Collection replaceModuleQNameForKey( - StmtContext, KeyStatement, ?> keyStmtCtx, QNameModule newQNameModule) { + public static Collection replaceModuleQNameForKey(StmtContext, KeyStatement, ?> keyStmtCtx, + QNameModule newQNameModule) { List keyTokens = KEY_SPLITTER.splitToList(keyStmtCtx.rawStatementArgument()); @@ -199,7 +199,8 @@ public final class StmtContextUtils { for (String keyToken : keyTokens) { QName keyQName = QName.create(newQNameModule, keyToken); - SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier.create(false, keyQName); + SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier + .create(false, keyQName); newKeys.add(keyIdentifier); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/StmtOrderingNamespace.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/StmtOrderingNamespace.java new file mode 100644 index 0000000000..200870abb9 --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/StmtOrderingNamespace.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2015 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.yangtools.yang.parser.spi.source; + +import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; + +import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace; + +/** + * + * Source-specific mapping of prefixes to namespaces + * + */ +public interface StmtOrderingNamespace extends + IdentifierNamespace { + +} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/CrossSourceStatementReactor.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/CrossSourceStatementReactor.java index c804080994..fc3b31222e 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/CrossSourceStatementReactor.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/CrossSourceStatementReactor.java @@ -7,8 +7,12 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.reactor; -import java.util.Collection; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import java.io.InputStream; +import java.util.List; +import java.util.Collection; import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext; import com.google.common.collect.ImmutableMap; @@ -94,6 +98,15 @@ public class CrossSourceStatementReactor { return context.buildEffective(); } + public SchemaContext buildEffective(List yangInputStreams) throws SourceException, ReactorException { + + for(InputStream yangInputStream : yangInputStreams) { + addSource(new YangStatementSourceImpl(yangInputStream)); + } + + return buildEffective(); + } + } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java index 6b876c71f6..8108cd810e 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java @@ -137,8 +137,16 @@ public class RootStatementContext, E extends E public StatementContextBase createCopy(QNameModule newQNameModule, StatementContextBase newParent, TypeOfCopy typeOfCopy) throws SourceException { RootStatementContext copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy); - copy.setTypeOfCopy(typeOfCopy); - copy.setOriginalCtx(this); + + copy.addAllToCopyHistory(this.getCopyHistory()); + copy.addToCopyHistory(typeOfCopy); + + if(this.getOriginalCtx() != null) { + copy.setOriginalCtx(this.getOriginalCtx()); + } else { + copy.setOriginalCtx(this); + } + return copy; } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java index 24315021f3..a5b7f53dc7 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java @@ -8,7 +8,6 @@ package org.opendaylight.yangtools.yang.parser.stmt.reactor; import javax.annotation.Nonnull; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.BinarySpecificationImpl; import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import com.google.common.base.Preconditions; @@ -341,8 +340,6 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh return new StatementDefinitionContext<>(new IdentityRefSpecificationImpl.Definition()); case TypeUtils.INSTANCE_IDENTIFIER: return new StatementDefinitionContext<>(new InstanceIdentifierSpecificationImpl.Definition()); - case TypeUtils.BINARY: - return new StatementDefinitionContext<>(new BinarySpecificationImpl.Definition()); default: return null; } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java index 02e37329d8..83356ed615 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java @@ -7,6 +7,9 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.reactor; +import java.util.LinkedList; + +import java.util.List; import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import com.google.common.collect.HashMultimap; @@ -71,6 +74,7 @@ public abstract class StatementContextBase, E private final StatementDefinitionContext definition; private final StatementIdentifier identifier; private final StatementSourceReference statementDeclSource; + private int order = 0; private Map> substatements = new LinkedHashMap<>(); @@ -95,7 +99,7 @@ public abstract class StatementContextBase, E private E effectiveInstance; private StatementContextBase originalCtx; - private TypeOfCopy typeOfCopy = TypeOfCopy.ORIGINAL; + private List copyHistory; private boolean isSupportedToBuildEffective = true; @@ -110,13 +114,18 @@ public abstract class StatementContextBase, E } @Override - public TypeOfCopy getTypeOfCopy() { - return typeOfCopy; + public List getCopyHistory() { + return copyHistory; + } + + @Override + public void addToCopyHistory(TypeOfCopy typeOfCopy) { + this.copyHistory.add(typeOfCopy); } @Override - public void setTypeOfCopy(TypeOfCopy typeOfCopy) { - this.typeOfCopy = typeOfCopy; + public void addAllToCopyHistory(List typeOfCopyList) { + this.copyHistory.addAll(typeOfCopyList); } @Override @@ -129,6 +138,16 @@ public abstract class StatementContextBase, E this.originalCtx = originalCtx; } + @Override + public void setOrder(int order) { + this.order = order; + } + + @Override + public int getOrder() { + return order; + } + @Override public ModelProcessingPhase getCompletedPhase() { return completedPhase; @@ -144,6 +163,7 @@ public abstract class StatementContextBase, E this.identifier = builder.createIdentifier(); this.statementDeclSource = builder.getStamementSource(); this.completedPhase = null; + initCopyHistory(); } StatementContextBase(StatementContextBase original) { @@ -154,6 +174,12 @@ public abstract class StatementContextBase, E this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource, "Statement context statementDeclSource cannot be null"); this.completedPhase = null; + initCopyHistory(); + } + + private void initCopyHistory() { + this.copyHistory = new LinkedList<>(); + this.copyHistory.add(TypeOfCopy.ORIGINAL); } /** diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java index 76e3a45baa..d0a6967f42 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java @@ -139,8 +139,16 @@ class SubstatementContext, E extends Effective throws SourceException { SubstatementContext copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy); - copy.setTypeOfCopy(typeOfCopy); - copy.setOriginalCtx(this); + + copy.addAllToCopyHistory(this.getCopyHistory()); + copy.addToCopyHistory(typeOfCopy); + + if(this.getOriginalCtx() != null) { + copy.setOriginalCtx(this.getOriginalCtx()); + } else { + copy.setOriginalCtx(this); + } + return copy; } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java index ec649af866..f1219b7504 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java @@ -7,6 +7,7 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; +import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import java.util.Collection; import org.slf4j.Logger; @@ -52,7 +53,7 @@ public class AugmentStatementImpl extends StmtContext ctx, String value) throws SourceException { return SchemaNodeIdentifier.create( AugmentUtils.parseAugmentPath(ctx, value), - Utils.isXPathAbsolute(value)); + Utils.isXPathAbsolute(ctx, value)); } @Override @@ -72,7 +73,7 @@ public class AugmentStatementImpl extends final StmtContext.Mutable> augmentNode) throws SourceException { - if(StmtContextUtils.isInExtensionBody(augmentNode)) { + if (StmtContextUtils.isInExtensionBody(augmentNode)) { return; } @@ -91,7 +92,10 @@ public class AugmentStatementImpl extends .getAugmentTargetCtx(augmentNode); if (augmentTargetCtx == null) { - throw new InferenceException("Augment target not found: "+augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference()); + throw new InferenceException( + "Augment target not found: " + + augmentNode.getStatementArgument(), + augmentNode.getStatementSourceReference()); } if (StmtContextUtils.isInExtensionBody(augmentTargetCtx)) { augmentNode.setIsSupportedToBuildEffective(false); @@ -103,19 +107,37 @@ public class AugmentStatementImpl extends try { AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx); - augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx); + augmentTargetCtx + .addEffectiveSubstatement(augmentSourceCtx); + updateAugmentOrder(augmentSourceCtx); } catch (SourceException e) { LOG.warn(e.getMessage(), e); } } + private void updateAugmentOrder( + final StatementContextBase augmentSourceCtx) { + Integer currentOrder = augmentSourceCtx + .getFromNamespace(StmtOrderingNamespace.class, + Rfc6020Mapping.AUGMENT); + if (currentOrder == null) { + currentOrder = 1; + } else { + currentOrder++; + } + augmentSourceCtx.setOrder(currentOrder); + augmentSourceCtx.addToNs(StmtOrderingNamespace.class, + Rfc6020Mapping.AUGMENT, currentOrder); + } + @Override public void prerequisiteFailed( final Collection> failed) throws InferenceException { - throw new InferenceException("Augment target not found: "+augmentNode.getStatementArgument(), - augmentNode.getStatementSourceReference()); + throw new InferenceException("Augment target not found: " + + augmentNode.getStatementArgument(), augmentNode + .getStatementSourceReference()); } }); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BaseStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BaseStatementImpl.java index aa4085a509..3a80844b2b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BaseStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BaseStatementImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the @@ -8,16 +8,15 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import java.util.Collection; +import java.util.LinkedList; +import java.util.List; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; - import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite; import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase; import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace; -import java.util.LinkedList; -import java.util.List; import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; @@ -73,7 +72,8 @@ public class BaseStatementImpl extends AbstractDeclaredStatement final QName baseIdentityQName = baseStmtCtx.getStatementArgument(); ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(ModelProcessingPhase.STATEMENT_DEFINITION); final Prerequisite> requiresPrereq = baseIdentityAction.requiresCtx(baseStmtCtx, IdentityNamespace.class, baseIdentityQName, ModelProcessingPhase.STATEMENT_DEFINITION); - final Prerequisite mutatesPrereq = baseIdentityAction.mutatesCtx(baseParentCtx, ModelProcessingPhase.STATEMENT_DEFINITION); + final Prerequisite> mutatesPrereq = baseIdentityAction.mutatesCtx + (baseParentCtx, ModelProcessingPhase.STATEMENT_DEFINITION); baseIdentityAction.apply( new InferenceAction() { diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BinarySpecificationImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BinarySpecificationImpl.java deleted file mode 100644 index 6410b05fa4..0000000000 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BinarySpecificationImpl.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2015 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.yangtools.yang.parser.stmt.rfc6020; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.LengthStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; -import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement; -import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryEffectiveStatementImpl; - -public class BinarySpecificationImpl extends AbstractDeclaredStatement implements - TypeStatement.BinarySpecification { - - protected BinarySpecificationImpl(StmtContext context) { - super(context); - } - - public static class Definition extends - AbstractStatementSupport> { - - public Definition() { - super(Rfc6020Mapping.TYPE); - } - - @Override - public String parseArgumentValue(StmtContext ctx, String value) throws SourceException { - return value; - } - - @Override - public BinarySpecification createDeclared(StmtContext ctx) { - return new BinarySpecificationImpl(ctx); - } - - @Override - public EffectiveStatement createEffective( - StmtContext> ctx) { - return new BinaryEffectiveStatementImpl(ctx); - } - } - - @Nullable - @Override - public LengthStatement getLength() { - return firstDeclared(LengthStatement.class); - } - - @Nonnull - @Override - public String getName() { - return argument(); - } -} diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/DeviationStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/DeviationStatementImpl.java index 8dea301b71..8ba3ef03ac 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/DeviationStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/DeviationStatementImpl.java @@ -34,7 +34,7 @@ public class DeviationStatementImpl extends AbstractDeclaredStatement ctx, String value) throws SourceException { - return SchemaNodeIdentifier.create(Utils.parseXPath(ctx, value), Utils.isXPathAbsolute(value)); + return SchemaNodeIdentifier.create(Utils.parseXPath(ctx, value), Utils.isXPathAbsolute(ctx, value)); } @Override public DeviationStatement createDeclared( diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ImportStatementDefinition.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ImportStatementDefinition.java index c1fc892c0e..05b7431690 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ImportStatementDefinition.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ImportStatementDefinition.java @@ -9,6 +9,12 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE; import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf; + +import java.util.Set; + +import java.util.Map.Entry; +import java.util.Map; +import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement; import com.google.common.base.Optional; import java.net.URI; import java.util.Collection; @@ -33,7 +39,8 @@ import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdenti import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl; -public class ImportStatementDefinition extends +public class ImportStatementDefinition + extends AbstractStatementSupport> { public ImportStatementDefinition() { @@ -46,7 +53,8 @@ public class ImportStatementDefinition extends } @Override - public ImportStatement createDeclared(StmtContext ctx) { + public ImportStatement createDeclared( + StmtContext ctx) { return new ImportStatementImpl(ctx); } @@ -61,43 +69,94 @@ public class ImportStatementDefinition extends final Mutable> stmt) throws SourceException { final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt); - ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE); + ModelActionBuilder importAction = stmt + .newInferenceAction(SOURCE_LINKAGE); final Prerequisite> imported; final Prerequisite> linkageTarget; - imported = importAction.requiresCtx(stmt, ModuleNamespace.class, impIdentifier, SOURCE_LINKAGE); + imported = importAction.requiresCtx(stmt, ModuleNamespace.class, + impIdentifier, SOURCE_LINKAGE); linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE); - String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class); - stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, impIdentifier); - importAction.apply(new InferenceAction() { @Override public void apply() throws InferenceException { - StmtContext importedModule = imported.get(); - linkageTarget.get().addToNs(ImportedModuleContext.class, impIdentifier, importedModule); + StmtContext importedModule = null; + ModuleIdentifier importedModuleIdentifier = null; + if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) { + Entry>> recentModuleEntry = findRecentModule( + impIdentifier, + stmt.getAllFromNamespace(ModuleNamespace.class)); + if (recentModuleEntry != null) { + importedModuleIdentifier = recentModuleEntry.getKey(); + importedModule = recentModuleEntry.getValue(); + } + } + + if(importedModule == null || importedModuleIdentifier == null) { + importedModule = imported.get(); + importedModuleIdentifier = impIdentifier; + } + + linkageTarget.get().addToNs(ImportedModuleContext.class, + importedModuleIdentifier, importedModule); + String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), + PrefixStatement.class); + stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, + importedModuleIdentifier); + } + + private Entry>> findRecentModule( + ModuleIdentifier impIdentifier, + Map>> allModules) { + + ModuleIdentifier recentModuleIdentifier = impIdentifier; + Entry>> recentModuleEntry = null; + + Set>>> moduleEntrySet = allModules + .entrySet(); + for (Entry>> moduleEntry : moduleEntrySet) { + if (moduleEntry.getKey().getName() + .equals(impIdentifier.getName()) + && moduleEntry + .getKey() + .getRevision() + .compareTo( + recentModuleIdentifier + .getRevision()) > 0) { + recentModuleIdentifier = moduleEntry.getKey(); + recentModuleEntry = moduleEntry; + } + } + + return recentModuleEntry; } @Override - public void prerequisiteFailed(Collection> failed) throws InferenceException { + public void prerequisiteFailed( + Collection> failed) + throws InferenceException { if (failed.contains(imported)) { - throw new InferenceException(String.format("Imported module [%s] was not found.", impIdentifier), - stmt.getStatementSourceReference()); + throw new InferenceException(String.format( + "Imported module [%s] was not found.", + impIdentifier), stmt.getStatementSourceReference()); } } }); } - private static ModuleIdentifier getImportedModuleIdentifier(Mutable stmt) - throws SourceException { + private static ModuleIdentifier getImportedModuleIdentifier( + Mutable stmt) throws SourceException { String moduleName = stmt.getStatementArgument(); - Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class); + Date revision = firstAttributeOf(stmt.declaredSubstatements(), + RevisionDateStatement.class); if (revision == null) { revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP; } - return new ModuleIdentifierImpl(moduleName, Optional. absent(), Optional. of(revision)); + return new ModuleIdentifierImpl(moduleName, Optional. absent(), + Optional. of(revision)); } } \ No newline at end of file diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/MustStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/MustStatementImpl.java index 0db8c69f41..1768a2d37d 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/MustStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/MustStatementImpl.java @@ -41,7 +41,7 @@ public class MustStatementImpl extends AbstractDeclaredStatement ctx, String value) throws SourceException { - return new RevisionAwareXPathImpl(value, Utils.isXPathAbsolute(value)); + return new RevisionAwareXPathImpl(value, Utils.isXPathAbsolute(ctx, value)); } @Override public MustStatement createDeclared( diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PathStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PathStatementImpl.java index 990d33ac24..2fc61b4727 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PathStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/PathStatementImpl.java @@ -36,7 +36,7 @@ public class PathStatementImpl extends AbstractDeclaredStatement ctx, String value) throws SourceException { - return new RevisionAwareXPathImpl(value, Utils.isXPathAbsolute(value)); + return new RevisionAwareXPathImpl(value, Utils.isXPathAbsolute(ctx, value)); } @Override diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/RefineStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/RefineStatementImpl.java index 9c433f20ef..29232ff956 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/RefineStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/RefineStatementImpl.java @@ -38,7 +38,7 @@ public class RefineStatementImpl extends AbstractDeclaredStatement ctx, String value) throws SourceException { - return SchemaNodeIdentifier.create(Utils.parseXPath(ctx, value), Utils.isXPathAbsolute(value)); + return SchemaNodeIdentifier.create(Utils.parseXPath(ctx, value), Utils.isXPathAbsolute(ctx, value)); } @Override diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java index 199c6ee897..e3a4cf58db 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeStatementImpl.java @@ -7,9 +7,10 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryEffectiveStatementImpl; + import java.util.Collection; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; - import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ExtendedTypeEffectiveStatementImpl; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; @@ -94,6 +95,8 @@ public class TypeStatementImpl extends AbstractDeclaredStatement return new BooleanEffectiveStatementImpl(ctx); case TypeUtils.EMPTY: return new EmptyEffectiveStatementImpl(ctx); + case TypeUtils.BINARY: + return new BinaryEffectiveStatementImpl(ctx); default: // :FIXME try to resolve original typedef context here and // return buildEffective of original typedef context diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java index 9aa2e1d360..6dc11f6d42 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java @@ -9,10 +9,7 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import com.google.common.base.Optional; import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; - import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; @@ -24,7 +21,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; - import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; @@ -71,9 +67,9 @@ public final class TypeUtils { public static final String UINT64 = "uint64"; public static final String UNION = "union"; - private static final ImmutableSet BUILT_IN_TYPES = initBuiltInTypesSet(); - private static final ImmutableSet TYPE_BODY_STMTS = initTypeBodyStmtsSet(); - private static final ImmutableMap BASE_TYPES_MAP = initBaseTypesMap(); + private static final Set BUILT_IN_TYPES = new HashSet<>(); + public static final Set TYPE_BODY_STMTS = new HashSet<>(); + public static final Map> PRIMITIVE_TYPES_MAP = new HashMap<>(); private static final Comparator> TYPE_SORT_COMPARATOR = new Comparator>() { @Override @@ -87,65 +83,68 @@ public final class TypeUtils { return 0; } }; - - private static ImmutableSet initBuiltInTypesSet() { - - final Set builtInTypesInit = new HashSet<>(); - builtInTypesInit.add(BINARY); - builtInTypesInit.add(BITS); - builtInTypesInit.add(BOOLEAN); - builtInTypesInit.add(DECIMAL64); - builtInTypesInit.add(EMPTY); - builtInTypesInit.add(ENUMERATION); - builtInTypesInit.add(IDENTITY_REF); - builtInTypesInit.add(INSTANCE_IDENTIFIER); - builtInTypesInit.add(INT8); - builtInTypesInit.add(INT16); - builtInTypesInit.add(INT32); - builtInTypesInit.add(INT64); - builtInTypesInit.add(LEAF_REF); - builtInTypesInit.add(STRING); - builtInTypesInit.add(UINT8); - builtInTypesInit.add(UINT16); - builtInTypesInit.add(UINT32); - builtInTypesInit.add(UINT64); - builtInTypesInit.add(UNION); - - return ImmutableSet.copyOf(builtInTypesInit); - } - - private static ImmutableSet initTypeBodyStmtsSet() { - - final Set typeBodyStmtsInit = new HashSet<>(); - typeBodyStmtsInit.add(DECIMAL64); - typeBodyStmtsInit.add(ENUMERATION); - typeBodyStmtsInit.add(LEAF_REF); - typeBodyStmtsInit.add(IDENTITY_REF); - typeBodyStmtsInit.add(INSTANCE_IDENTIFIER); - typeBodyStmtsInit.add(BITS); - typeBodyStmtsInit.add(UNION); - typeBodyStmtsInit.add(BINARY); - - return ImmutableSet.copyOf(typeBodyStmtsInit); - } - - private static ImmutableMap initBaseTypesMap() { - - final Map baseTypesMapInit = new HashMap<>(); - baseTypesMapInit.put(BINARY, BinaryType.getInstance()); - baseTypesMapInit.put(BOOLEAN, BooleanType.getInstance()); - baseTypesMapInit.put(EMPTY, EmptyType.getInstance()); - baseTypesMapInit.put(INT8, Int8.getInstance()); - baseTypesMapInit.put(INT16, Int16.getInstance()); - baseTypesMapInit.put(INT32, Int32.getInstance()); - baseTypesMapInit.put(INT64, Int64.getInstance()); - baseTypesMapInit.put(STRING, StringType.getInstance()); - baseTypesMapInit.put(UINT8, Uint8.getInstance()); - baseTypesMapInit.put(UINT16, Uint16.getInstance()); - baseTypesMapInit.put(UINT32, Uint32.getInstance()); - baseTypesMapInit.put(UINT64, Uint64.getInstance()); - - return ImmutableMap.copyOf(baseTypesMapInit); + static { + +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,BINARY)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,BITS)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,BOOLEAN)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,DECIMAL64)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,EMPTY)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,ENUMERATION)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,IDENTITY_REF)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,INSTANCE_IDENTIFIER)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,INT8)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,INT16)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,INT32)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,INT64)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,LEAF_REF)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,STRING)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,UINT8)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,UINT16)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,UINT32)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,UINT64)); +// BUILT_IN_TYPES.add(QName.create(YangConstants.RFC6020_YANG_MODULE,UNION)); + + BUILT_IN_TYPES.add(BINARY); + BUILT_IN_TYPES.add(BITS); + BUILT_IN_TYPES.add(BOOLEAN); + BUILT_IN_TYPES.add(DECIMAL64); + BUILT_IN_TYPES.add(EMPTY); + BUILT_IN_TYPES.add(ENUMERATION); + BUILT_IN_TYPES.add(IDENTITY_REF); + BUILT_IN_TYPES.add(INSTANCE_IDENTIFIER); + BUILT_IN_TYPES.add(INT8); + BUILT_IN_TYPES.add(INT16); + BUILT_IN_TYPES.add(INT32); + BUILT_IN_TYPES.add(INT64); + BUILT_IN_TYPES.add(LEAF_REF); + BUILT_IN_TYPES.add(STRING); + BUILT_IN_TYPES.add(UINT8); + BUILT_IN_TYPES.add(UINT16); + BUILT_IN_TYPES.add(UINT32); + BUILT_IN_TYPES.add(UINT64); + BUILT_IN_TYPES.add(UNION); + + TYPE_BODY_STMTS.add(DECIMAL64); + TYPE_BODY_STMTS.add(ENUMERATION); + TYPE_BODY_STMTS.add(LEAF_REF); + TYPE_BODY_STMTS.add(IDENTITY_REF); + TYPE_BODY_STMTS.add(INSTANCE_IDENTIFIER); + TYPE_BODY_STMTS.add(BITS); + TYPE_BODY_STMTS.add(UNION); + + PRIMITIVE_TYPES_MAP.put(BINARY, BinaryType.getInstance()); + PRIMITIVE_TYPES_MAP.put(BOOLEAN, BooleanType.getInstance()); + PRIMITIVE_TYPES_MAP.put(EMPTY, EmptyType.getInstance()); + PRIMITIVE_TYPES_MAP.put(INT8, Int8.getInstance()); + PRIMITIVE_TYPES_MAP.put(INT16, Int16.getInstance()); + PRIMITIVE_TYPES_MAP.put(INT32, Int32.getInstance()); + PRIMITIVE_TYPES_MAP.put(INT64, Int64.getInstance()); + PRIMITIVE_TYPES_MAP.put(STRING, StringType.getInstance()); + PRIMITIVE_TYPES_MAP.put(UINT8, Uint8.getInstance()); + PRIMITIVE_TYPES_MAP.put(UINT16, Uint16.getInstance()); + PRIMITIVE_TYPES_MAP.put(UINT32, Uint32.getInstance()); + PRIMITIVE_TYPES_MAP.put(UINT64, Uint64.getInstance()); } private TypeUtils() { @@ -166,7 +165,7 @@ public final class TypeUtils { } } - private static int compareNumbers(Number n1, Number n2) { + public static int compareNumbers(Number n1, Number n2) { final BigDecimal num1 = yangConstraintToBigDecimal(n1); final BigDecimal num2 = yangConstraintToBigDecimal(n2); @@ -292,38 +291,41 @@ public final class TypeUtils { return rangeConstraints; } - public static boolean isYangBaseTypeString(String typeName) { + public static boolean isBuiltInType(TypeDefinition o1) { + return BUILT_IN_TYPES.contains(o1.getQName().getLocalName()); + } + + public static boolean isYangBuiltInTypeString(String typeName) { return BUILT_IN_TYPES.contains(typeName); } - public static boolean isYangTypeBodyStmt(String typeName) { + public static boolean isYangPrimitiveTypeString(String typeName) { + return PRIMITIVE_TYPES_MAP.containsKey(typeName); + } + + public static boolean isYangTypeBodyStmtString(String typeName) { return TYPE_BODY_STMTS.contains(typeName); } + public static TypeDefinition getYangPrimitiveTypeFromString(String typeName) { + if (PRIMITIVE_TYPES_MAP.containsKey(typeName)) { + return PRIMITIVE_TYPES_MAP.get(typeName); + } + return null; + } + public static TypeDefinition getTypeFromEffectiveStatement(EffectiveStatement effectiveStatement) { if (effectiveStatement instanceof TypeDefinitionEffectiveBuilder) { TypeDefinitionEffectiveBuilder typeDefEffectiveBuilder = (TypeDefinitionEffectiveBuilder) effectiveStatement; return typeDefEffectiveBuilder.buildType(); } else { - final String typeName = ((TypeDefinition) effectiveStatement).getQName().getLocalName(); - return BASE_TYPES_MAP.get(typeName); - } - } - - public static TypeDefinition getYangBaseTypeFromString(String typeName) { - - if (BASE_TYPES_MAP.containsKey(typeName)) { - return BASE_TYPES_MAP.get(typeName); + final String typeName = ((TypeDefinition) effectiveStatement).getQName().getLocalName(); + return PRIMITIVE_TYPES_MAP.get(typeName); } - - return null; } public static void sortTypes(List> typesInit) { Collections.sort(typesInit, TYPE_SORT_COMPARATOR); } - public static boolean isBuiltInType(TypeDefinition o1) { - return BUILT_IN_TYPES.contains(o1.getQName().getLocalName()); - } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java index 1d6a8e84ff..6efbe5ffe7 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java @@ -9,10 +9,10 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf; -import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; - import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -22,16 +22,19 @@ import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.Set; + import javax.annotation.Nullable; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; + import org.antlr.v4.runtime.tree.TerminalNode; import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.common.YangConstants; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.Deviation; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaPath; @@ -43,10 +46,13 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative; import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName; import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName; import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule; @@ -61,8 +67,7 @@ public final class Utils { private static final Logger LOG = LoggerFactory.getLogger(Utils.class); private static final CharMatcher DOUBLE_QUOTE_MATCHER = CharMatcher.is('"'); - private static final CharMatcher SINGLE_QUOTE_MATCHER = CharMatcher - .is('\''); + private static final CharMatcher SINGLE_QUOTE_MATCHER = CharMatcher.is('\''); public static final QName EMPTY_QNAME = QName.create("empty", "empty"); @@ -75,8 +80,8 @@ public final class Utils { private Utils() { } - public static Collection transformKeysStringToKeyNodes(StmtContext ctx, String - value) { + public static Collection transformKeysStringToKeyNodes(StmtContext ctx, + String value) { Splitter keySplitter = Splitter.on(SEPARATOR).omitEmptyStrings().trimResults(); List keyTokens = keySplitter.splitToList(value); @@ -89,8 +94,8 @@ public final class Utils { for (String keyToken : keyTokens) { - SchemaNodeIdentifier.Relative keyNode = (Relative) SchemaNodeIdentifier.Relative - .create(false, Utils.qNameFromArgument(ctx, keyToken)); + SchemaNodeIdentifier.Relative keyNode = (Relative) SchemaNodeIdentifier.Relative.create(false, + Utils.qNameFromArgument(ctx, keyToken)); keyNodes.add(keyNode); } @@ -99,26 +104,29 @@ public final class Utils { public static List splitPathToNodeNames(String path) { - Splitter keySplitter = Splitter.on(SEPARATOR_NODENAME) - .omitEmptyStrings().trimResults(); + Splitter keySplitter = Splitter.on(SEPARATOR_NODENAME).omitEmptyStrings().trimResults(); return keySplitter.splitToList(path); } - public static void validateXPath(String path) { + public static void validateXPath(final StmtContext ctx, String path) { final XPath xPath = XPathFactory.newInstance().newXPath(); try { xPath.compile(path); } catch (XPathExpressionException e) { - throw new IllegalArgumentException( - "Argument is not valid XPath string", e); + throw new IllegalArgumentException(String.format("Argument %s is not valid XPath string at %s", path, ctx + .getStatementSourceReference().toString()), e); } } - public static boolean isXPathAbsolute(String path) { + private static String trimSingleLastSlashFromXPath(final String path) { + return path.replaceAll("/$", ""); + } - validateXPath(path); + public static boolean isXPathAbsolute(final StmtContext ctx, String path) { + + validateXPath(ctx, trimSingleLastSlashFromXPath(path)); return path.matches(REGEX_PATH_ABS); } @@ -143,8 +151,8 @@ public final class Utils { return null; } - public static boolean isValidStatementDefinition(PrefixToModule prefixes, - QNameToStatementDefinition stmtDef, QName identifier) { + public static boolean isValidStatementDefinition(PrefixToModule prefixes, QNameToStatementDefinition stmtDef, + QName identifier) { if (stmtDef.get(identifier) != null) { return true; } else { @@ -154,16 +162,11 @@ public final class Utils { if (namesParts.length == 2) { String prefix = namesParts[0]; String localName = namesParts[1]; - if (prefixes != null - && prefixes.get(prefix) != null - && stmtDef - .get(new QName( - YangConstants.RFC6020_YIN_NAMESPACE, - localName)) != null) { + if (prefixes != null && prefixes.get(prefix) != null + && stmtDef.get(new QName(YangConstants.RFC6020_YIN_NAMESPACE, localName)) != null) { return true; } else { - if (stmtDef.get(new QName( - YangConstants.RFC6020_YIN_NAMESPACE, localName)) != null) { + if (stmtDef.get(new QName(YangConstants.RFC6020_YIN_NAMESPACE, localName)) != null) { return true; } } @@ -172,12 +175,13 @@ public final class Utils { return false; } - public static Iterable parseXPath(StmtContext ctx, - String path) { + public static Iterable parseXPath(StmtContext ctx, String path) { + + String trimmedPath = trimSingleLastSlashFromXPath(path); - validateXPath(path); + validateXPath(ctx, trimmedPath); - List nodeNames = splitPathToNodeNames(path); + List nodeNames = splitPathToNodeNames(trimmedPath); List qNames = new ArrayList<>(); for (String nodeName : nodeNames) { @@ -192,8 +196,7 @@ public final class Utils { return qNames; } - public static String stringFromStringContext( - final YangStatementParser.ArgumentContext context) { + public static String stringFromStringContext(final YangStatementParser.ArgumentContext context) { StringBuilder sb = new StringBuilder(); List strings = context.STRING(); if (strings.isEmpty()) { @@ -236,14 +239,15 @@ public final class Utils { prefix = namesParts[0]; localName = namesParts[1]; qNameModule = getModuleQNameByPrefix(ctx, prefix); - //in case of unknown statement argument, we're not going to parse it - if (qNameModule == null && ctx.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom - (UnknownStatementImpl.class)) { + // in case of unknown statement argument, we're not going to parse it + if (qNameModule == null + && ctx.getPublicDefinition().getDeclaredRepresentationClass() + .isAssignableFrom(UnknownStatementImpl.class)) { localName = value; qNameModule = getRootModuleQName(ctx); } - //:FIXME test and verify this... - if(qNameModule == null && ctx.getTypeOfCopy() == StmtContext.TypeOfCopy.ADDED_BY_AUGMENTATION) { + if (qNameModule == null + && Iterables.getLast(ctx.getCopyHistory()) == StmtContext.TypeOfCopy.ADDED_BY_AUGMENTATION) { ctx = ctx.getOriginalCtx(); qNameModule = getModuleQNameByPrefix(ctx, prefix); } @@ -253,33 +257,24 @@ public final class Utils { } if (qNameModule == null) { - throw new IllegalArgumentException("Error in module '" - + ctx.getRoot().rawStatementArgument() + throw new IllegalArgumentException("Error in module '" + ctx.getRoot().rawStatementArgument() + "': can not resolve QNameModule for '" + value + "'."); } - QNameModule resultQNameModule = qNameModule.getRevision() == null ? QNameModule - .create(qNameModule.getNamespace(), - SimpleDateFormatUtil.DEFAULT_DATE_REV) : qNameModule; + QNameModule resultQNameModule = qNameModule.getRevision() == null ? QNameModule.create( + qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV) : qNameModule; return QName.create(resultQNameModule, localName); } - public static QNameModule getModuleQNameByPrefix(StmtContext ctx, - String prefix) { + public static QNameModule getModuleQNameByPrefix(StmtContext ctx, String prefix) { QNameModule qNameModule; - ModuleIdentifier impModIdentifier = ctx.getRoot().getFromNamespace( - ImpPrefixToModuleIdentifier.class, prefix); - qNameModule = ctx.getFromNamespace(ModuleIdentifierToModuleQName.class, - impModIdentifier); - - if (qNameModule == null - && StmtContextUtils.producesDeclared(ctx.getRoot(), - SubmoduleStatement.class)) { - String moduleName = ctx.getRoot().getFromNamespace( - BelongsToPrefixToModuleName.class, prefix); - qNameModule = ctx.getFromNamespace(ModuleNameToModuleQName.class, - moduleName); + ModuleIdentifier impModIdentifier = ctx.getRoot().getFromNamespace(ImpPrefixToModuleIdentifier.class, prefix); + qNameModule = ctx.getFromNamespace(ModuleIdentifierToModuleQName.class, impModIdentifier); + + if (qNameModule == null && StmtContextUtils.producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) { + String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix); + qNameModule = ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName); } return qNameModule; } @@ -294,25 +289,19 @@ public final class Utils { QNameModule qNameModule = null; if (StmtContextUtils.producesDeclared(rootCtx, ModuleStatement.class)) { - qNameModule = rootCtx.getFromNamespace( - ModuleCtxToModuleQName.class, - rootCtx); - } else if (StmtContextUtils.producesDeclared(rootCtx, - SubmoduleStatement.class)) { - String belongsToModuleName = firstAttributeOf(ctx.getRoot() - .declaredSubstatements(), BelongsToStatement.class); - qNameModule = rootCtx.getFromNamespace( - ModuleNameToModuleQName.class, belongsToModuleName); + qNameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx); + } else if (StmtContextUtils.producesDeclared(rootCtx, SubmoduleStatement.class)) { + String belongsToModuleName = firstAttributeOf(ctx.getRoot().declaredSubstatements(), + BelongsToStatement.class); + qNameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName); } - return qNameModule.getRevision() == null ? QNameModule.create( - qNameModule.getNamespace(), + return qNameModule.getRevision() == null ? QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV) : qNameModule; } @Nullable - public static StatementContextBase findNode( - StatementContextBase rootStmtCtx, + public static StatementContextBase findNode(StatementContextBase rootStmtCtx, final Iterable path) { StatementContextBase parent = rootStmtCtx; @@ -320,8 +309,7 @@ public final class Utils { Iterator pathIter = path.iterator(); while (pathIter.hasNext()) { QName nextPathQName = pathIter.next(); - StatementContextBase foundSubstatement = getSubstatementByQName( - parent, nextPathQName); + StatementContextBase foundSubstatement = getSubstatementByQName(parent, nextPathQName); if (foundSubstatement == null) { return null; @@ -336,13 +324,11 @@ public final class Utils { return null; } - public static StatementContextBase getSubstatementByQName( - StatementContextBase parent, QName nextPathQName) { + public static StatementContextBase getSubstatementByQName(StatementContextBase parent, + QName nextPathQName) { - Collection> declaredSubstatement = parent - .declaredSubstatements(); - Collection> effectiveSubstatement = parent - .effectiveSubstatements(); + Collection> declaredSubstatement = parent.declaredSubstatements(); + Collection> effectiveSubstatement = parent.effectiveSubstatements(); Collection> allSubstatements = new LinkedList<>(); allSubstatements.addAll(declaredSubstatement); @@ -358,20 +344,18 @@ public final class Utils { } @Nullable - public static StatementContextBase findNode( - StatementContextBase rootStmtCtx, + public static StatementContextBase findNode(StatementContextBase rootStmtCtx, final SchemaNodeIdentifier node) { return findNode(rootStmtCtx, node.getPathFromRoot()); } public static SchemaPath getSchemaPath(StmtContext ctx) { - if(ctx == null) { + if (ctx == null) { return null; } - Iterator> iteratorFromRoot = ctx - .getStmtContextsFromRoot().iterator(); + Iterator> iteratorFromRoot = ctx.getStmtContextsFromRoot().iterator(); if (iteratorFromRoot.hasNext()) { iteratorFromRoot.next(); // skip root argument @@ -383,8 +367,10 @@ public final class Utils { Object nextStmtArgument = nextStmtCtx.getStatementArgument(); if (nextStmtArgument instanceof QName) { QName qname = (QName) nextStmtArgument; - if (StmtContextUtils.producesDeclared( - nextStmtCtx.getParentContext(), ChoiceStatement.class) + if (StmtContextUtils.producesDeclared(nextStmtCtx, UsesStatement.class)) { + continue; + } + if (StmtContextUtils.producesDeclared(nextStmtCtx.getParentContext(), ChoiceStatement.class) && isSupportedAsShorthandCase(nextStmtCtx)) { qNamesFromRoot.add(qname); } @@ -392,14 +378,11 @@ public final class Utils { } else if (nextStmtArgument instanceof String) { final QName qName = qNameFromArgument(ctx, (String) nextStmtArgument); qNamesFromRoot.add(qName); - } - else if (StmtContextUtils.producesDeclared(nextStmtCtx, - AugmentStatement.class) + } else if (StmtContextUtils.producesDeclared(nextStmtCtx, AugmentStatement.class) && nextStmtArgument instanceof SchemaNodeIdentifier) { - addQNamesFromSchemaNodeIdentifierToList(qNamesFromRoot, - (SchemaNodeIdentifier) nextStmtArgument); - } else if (nextStmtCtx.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom - (UnknownStatementImpl.class)) { + addQNamesFromSchemaNodeIdentifierToList(qNamesFromRoot, (SchemaNodeIdentifier) nextStmtArgument); + } else if (nextStmtCtx.getPublicDefinition().getDeclaredRepresentationClass() + .isAssignableFrom(UnknownStatementImpl.class)) { qNamesFromRoot.add(nextStmtCtx.getPublicDefinition().getStatementName()); } else { return SchemaPath.SAME; @@ -410,22 +393,17 @@ public final class Utils { return schemaPath; } - private static boolean isSupportedAsShorthandCase( - StmtContext statementCtx) { + private static boolean isSupportedAsShorthandCase(StmtContext statementCtx) { - Collection supportedCaseShorthands = statementCtx.getFromNamespace( - ValidationBundlesNamespace.class, + Collection supportedCaseShorthands = statementCtx.getFromNamespace(ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_CASE_SHORTHANDS); - return supportedCaseShorthands == null - || supportedCaseShorthands.contains(statementCtx - .getPublicDefinition()); + return supportedCaseShorthands == null || supportedCaseShorthands.contains(statementCtx.getPublicDefinition()); } - private static void addQNamesFromSchemaNodeIdentifierToList( - List qNamesFromRoot, SchemaNodeIdentifier augmentTargetPath) { - Iterator augmentTargetPathIterator = augmentTargetPath - .getPathFromRoot().iterator(); + private static void addQNamesFromSchemaNodeIdentifierToList(List qNamesFromRoot, + SchemaNodeIdentifier augmentTargetPath) { + Iterator augmentTargetPathIterator = augmentTargetPath.getPathFromRoot().iterator(); while (augmentTargetPathIterator.hasNext()) { qNamesFromRoot.add(augmentTargetPathIterator.next()); } @@ -437,16 +415,14 @@ public final class Utils { // suit this String deviateUpper = deviate.toUpperCase(); if (Objects.equals(deviate, deviateUpper)) { - throw new IllegalArgumentException(String.format( - "String %s is not valid deviate argument", deviate)); + throw new IllegalArgumentException(String.format("String %s is not valid deviate argument", deviate)); } // but Java enum is uppercase so we cannot use lowercase here try { return Deviation.Deviate.valueOf(deviateUpper); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException(String.format( - "String %s is not valid deviate argument", deviate), e); + throw new IllegalArgumentException(String.format("String %s is not valid deviate argument", deviate), e); } } @@ -470,10 +446,8 @@ public final class Utils { return status; } - public static SchemaPath SchemaNodeIdentifierToSchemaPath( - SchemaNodeIdentifier identifier) { - return SchemaPath.create(identifier.getPathFromRoot(), - identifier.isAbsolute()); + public static SchemaPath SchemaNodeIdentifierToSchemaPath(SchemaNodeIdentifier identifier) { + return SchemaPath.create(identifier.getPathFromRoot(), identifier.isAbsolute()); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/WhenStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/WhenStatementImpl.java index 78d60b1932..20a4417711 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/WhenStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/WhenStatementImpl.java @@ -39,7 +39,7 @@ public class WhenStatementImpl extends AbstractDeclaredStatement ctx, String value) throws SourceException { - return new RevisionAwareXPathImpl(value, Utils.isXPathAbsolute(value)); + return new RevisionAwareXPathImpl(value, Utils.isXPathAbsolute(ctx, value)); } @Override public WhenStatement createDeclared( diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangInferencePipeline.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangInferencePipeline.java index ddf81a8292..fd990136a0 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangInferencePipeline.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangInferencePipeline.java @@ -12,8 +12,9 @@ import static org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour import static org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.treeScoped; import static org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType; -import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; +import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition; import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace; @@ -141,6 +142,7 @@ public final class YangInferencePipeline { .addSupport(new FeatureStatementImpl.Definition()) .addSupport(new PositionStatementImpl.Definition()) .addSupport(new ValueStatementImpl.Definition()) + .addSupport(global(StmtOrderingNamespace.class)) .build(); public static final Map RFC6020_BUNDLES = ImmutableMap diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangStatementSourceImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangStatementSourceImpl.java index 96746e8b51..9bcc6717ed 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangStatementSourceImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangStatementSourceImpl.java @@ -7,9 +7,8 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; +import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; @@ -25,6 +24,7 @@ import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinit import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter; +import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,13 +40,15 @@ public final class YangStatementSourceImpl implements StatementStreamSource { private YangStatementParser.StatementContext statementContext; private ParseTreeWalker walker; private String sourceName; +// private String source; +// private InputStream sourceStream; private static final Logger LOG = LoggerFactory.getLogger(YangStatementSourceImpl.class); public YangStatementSourceImpl(final String fileName, boolean isAbsolute) { try { statementContext = parseYangSource(loadFile(fileName, isAbsolute)); walker = new ParseTreeWalker(); - yangStatementModelParser = new YangStatementParserListenerImpl(fileName); + yangStatementModelParser = new YangStatementParserListenerImpl(sourceName); } catch (Exception e) { LOG.warn(e.getMessage(), e); } @@ -62,6 +64,17 @@ public final class YangStatementSourceImpl implements StatementStreamSource { } } + public YangStatementSourceImpl(SourceIdentifier identifier, YangStatementParser.StatementContext statementContext) { + try { + this.statementContext = statementContext; + this.sourceName = identifier.getName(); + walker = new ParseTreeWalker(); + yangStatementModelParser = new YangStatementParserListenerImpl(sourceName); + } catch (Exception e) { + LOG.warn(e.getMessage(), e); + } + } + @Override public void writeLinkage(final StatementWriter writer, final QNameToStatementDefinition stmtDef) throws SourceException { yangStatementModelParser.setAttributes(writer, stmtDef); @@ -80,15 +93,46 @@ public final class YangStatementSourceImpl implements StatementStreamSource { walker.walk(yangStatementModelParser, statementContext); } - private FileInputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException, FileNotFoundException { - return isAbsolute ? new FileInputStream(new File(fileName)) : new FileInputStream(new File(getClass().getResource(fileName).toURI())); + private NamedFileInputStream loadFile(final String fileName, boolean isAbsolute) throws URISyntaxException, + IOException { + //TODO: we need absolute path first! + return isAbsolute ? new NamedFileInputStream(new File(fileName), fileName) : new NamedFileInputStream(new File + (getClass().getResource(fileName).toURI()), fileName); + +// final File file = new File(fileName); +// final ByteSource byteSource = BuilderUtils.fileToByteSource(file); +// source = byteSource.asCharSource(Charsets.UTF_8).read(); +// return isAbsolute ? new NamedFileInputStream(file, fileName) : new NamedFileInputStream(new File +// (getClass().getResource(fileName).toURI()), fileName); } - private YangStatementParser.StatementContext parseYangSource(final InputStream stream) throws IOException, YangSyntaxErrorException { + private YangStatementParser.StatementContext parseYangSource(final InputStream stream) throws IOException, + YangSyntaxErrorException { final YangStatementLexer lexer = new YangStatementLexer(new ANTLRInputStream(stream)); final CommonTokenStream tokens = new CommonTokenStream(lexer); final YangStatementParser parser = new YangStatementParser(tokens); - sourceName = parser.getSourceName(); + + if(stream instanceof NamedFileInputStream) { + sourceName = stream.toString(); + } else { + sourceName = null; + } + +// sourceStream = stream; +// sourceName = parser.getSourceName(); +// +// if (sourceName == null) { +// sourceName = stream.toString(); +// } + return parser.statement(); } + + public YangStatementParser.StatementContext getYangAST() { + return statementContext; + } + +// public InputStream getSourceStream() { +// return sourceStream; +// } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AnyXmlEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AnyXmlEffectiveStatementImpl.java index ed1e6db53b..f69bc0dfde 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AnyXmlEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AnyXmlEffectiveStatementImpl.java @@ -7,9 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import java.util.Set; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import java.util.Collection; import java.util.LinkedList; @@ -37,7 +34,7 @@ public class AnyXmlEffectiveStatementImpl extends AnyXmlSchemaNode original; ConstraintDefinition constraintsDef; boolean augmenting; - boolean addedByUses; + private boolean addedByUses; ImmutableList unknownNodes; @@ -55,7 +52,7 @@ public class AnyXmlEffectiveStatementImpl extends private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -67,7 +64,7 @@ public class AnyXmlEffectiveStatementImpl extends addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (AnyXmlSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AugmentEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AugmentEffectiveStatementImpl.java index 425ab74123..9b790100db 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AugmentEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AugmentEffectiveStatementImpl.java @@ -59,7 +59,7 @@ public class AugmentEffectiveStatementImpl this.namespace = rootModuleQName.getNamespace(); this.revision = rootModuleQName.getRevision(); - this.order = 1; + this.order = ctx.getOrder(); initCopyOf(ctx); initSubstatementCollections(); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java index 8cbd413590..b57ae3c02f 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java @@ -7,8 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import java.util.Collection; import java.util.HashSet; @@ -58,7 +56,7 @@ public class CaseEffectiveStatementImpl extends private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -70,7 +68,7 @@ public class CaseEffectiveStatementImpl extends addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (ChoiceCaseNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseShorthandImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseShorthandImpl.java index 8554da34df..54d927f617 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseShorthandImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseShorthandImpl.java @@ -7,6 +7,10 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; +import com.google.common.collect.ImmutableList; +import com.google.common.base.Optional; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; +import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode; import com.google.common.collect.Iterables; import java.util.Collections; import java.util.Arrays; @@ -25,12 +29,23 @@ import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; -public class CaseShorthandImpl implements ChoiceCaseNode { +public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode { private final DataSchemaNode caseShorthandNode; private final QName qName; private final SchemaPath path; + private final String description; + private final String reference; + private final Status status; + + private final boolean augmenting; + private final boolean addedByUses; + ConstraintDefinition constraints; + private ChoiceCaseNode original; + + ImmutableList unknownNodes; + public CaseShorthandImpl(DataSchemaNode caseShorthandNode) { this.caseShorthandNode = caseShorthandNode; this.qName = caseShorthandNode.getQName(); @@ -41,31 +56,40 @@ public class CaseShorthandImpl implements ChoiceCaseNode { .create(Iterables.limit(pathFromRoot, Iterables.size(pathFromRoot) - 1), caseShorthandNodePath.isAbsolute()); + + this.description = caseShorthandNode.getDescription(); + this.reference = caseShorthandNode.getReference(); + this.status = caseShorthandNode.getStatus(); + + this.augmenting = caseShorthandNode.isAugmenting(); + this.addedByUses = caseShorthandNode.isAddedByUses(); + this.constraints = caseShorthandNode.getConstraints(); + this.unknownNodes = ImmutableList.copyOf(caseShorthandNode.getUnknownSchemaNodes()); } @Override public boolean isAugmenting() { - return caseShorthandNode.isAugmenting(); + return augmenting; } @Override public boolean isAddedByUses() { - return caseShorthandNode.isAddedByUses(); + return addedByUses; } @Override public boolean isConfiguration() { - return caseShorthandNode.isConfiguration(); + return false; } @Override public ConstraintDefinition getConstraints() { - return caseShorthandNode.getConstraints(); + return constraints; } @Override public QName getQName() { - return caseShorthandNode.getQName(); + return qName; } @Override @@ -75,22 +99,22 @@ public class CaseShorthandImpl implements ChoiceCaseNode { @Override public List getUnknownSchemaNodes() { - return caseShorthandNode.getUnknownSchemaNodes(); + return unknownNodes; } @Override public String getDescription() { - return caseShorthandNode.getDescription(); + return description; } @Override public String getReference() { - return caseShorthandNode.getReference(); + return reference; } @Override public Status getStatus() { - return caseShorthandNode.getStatus(); + return status; } @Override @@ -135,4 +159,59 @@ public class CaseShorthandImpl implements ChoiceCaseNode { public Set getAvailableAugmentations() { return Collections.emptySet(); } + + @Override + public Optional getOriginal() { + return Optional.fromNullable(original); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((qName == null) ? 0 : qName.hashCode()); + result = prime * result + ((path == null) ? 0 : path.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + CaseShorthandImpl other = (CaseShorthandImpl) obj; + if (qName == null) { + if (other.qName != null) { + return false; + } + } else if (!qName.equals(other.qName)) { + return false; + } + if (path == null) { + if (other.path != null) { + return false; + } + } else if (!path.equals(other.path)) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder( + CaseShorthandImpl.class.getSimpleName()); + sb.append("["); + sb.append("qname="); + sb.append(qName); + sb.append("]"); + return sb.toString(); + } + } \ No newline at end of file diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java index fc87e1bd2a..6b3f848ec7 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ChoiceEffectiveStatementImpl.java @@ -7,14 +7,15 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - +import java.util.SortedSet; +import java.util.TreeSet; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.parser.builder.util.Comparators; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import java.util.Collection; import java.util.HashSet; @@ -62,26 +63,27 @@ public class ChoiceEffectiveStatementImpl extends this.path = Utils.getSchemaPath(ctx); this.constraints = new EffectiveConstraintDefinitionImpl(this); - initSubstatementCollectionsAndFields(); initCopyType(ctx); + initSubstatementCollectionsAndFields(); } private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); - if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { + if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; } - if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) { + if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) { addedByUses = true; } - if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) { + if (copyTypesFromOriginal + .contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) { addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective(); } } @@ -91,7 +93,7 @@ public class ChoiceEffectiveStatementImpl extends List unknownNodesInit = new LinkedList<>(); Set augmentationsInit = new HashSet<>(); - Set casesInit = new HashSet<>(); + SortedSet casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP); boolean configurationInit = false; boolean defaultInit = false; @@ -113,9 +115,16 @@ public class ChoiceEffectiveStatementImpl extends || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode || effectiveStatement instanceof LeafSchemaNode) { + + DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement; ChoiceCaseNode shorthandCase = new CaseShorthandImpl( - (DataSchemaNode) effectiveStatement); + dataSchemaNode); casesInit.add(shorthandCase); + + if (dataSchemaNode.isAugmenting() == true + && this.augmenting == false) { + resetAugmenting(dataSchemaNode); + } } if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) { @@ -136,6 +145,25 @@ public class ChoiceEffectiveStatementImpl extends this.cases = ImmutableSet.copyOf(casesInit); } + private void resetAugmenting(DataSchemaNode dataSchemaNode) { + if (dataSchemaNode instanceof LeafEffectiveStatementImpl) { + LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode; + leaf.augmenting = false; + } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) { + ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode; + container.augmenting = false; + } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) { + LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode; + leafList.augmenting = false; + } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) { + ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode; + list.augmenting = false; + } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) { + AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode; + anyXml.augmenting = false; + } + } + @Override public QName getQName() { return qname; diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ContainerEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ContainerEffectiveStatementImpl.java index 58b31c72d4..7f73373a1d 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ContainerEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ContainerEffectiveStatementImpl.java @@ -7,8 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableList; @@ -38,7 +36,7 @@ public class ContainerEffectiveStatementImpl extends private final SchemaPath path; private boolean presence; - private boolean augmenting; + boolean augmenting; private boolean addedByUses; private boolean configuration = true; private ContainerSchemaNode original; @@ -62,7 +60,7 @@ public class ContainerEffectiveStatementImpl extends private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -74,7 +72,7 @@ public class ContainerEffectiveStatementImpl extends addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (ContainerSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java index ff89f81d14..2f885e788b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; +import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort; + import java.util.HashSet; import com.google.common.collect.ImmutableList; import java.util.List; @@ -49,7 +51,10 @@ public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext { modulesInit.add(module); } } - this.modules = ImmutableSet.copyOf(modulesInit); + + Module[] moduleArray = new Module[modulesInit.size()]; + List sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray)); + this.modules = ImmutableSet.copyOf(sortedModuleList); final SetMultimap nsMap = Multimaps.newSetMultimap( new TreeMap>(), MODULE_SET_SUPPLIER); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java index b0a3429166..5b0c94cc98 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java @@ -165,4 +165,19 @@ abstract public class EffectiveStatementBase> } return result; } + + protected final R firstSubstatementOfType(Class type, + Class returnType) { + R result = null; + try { + result = returnType.cast(Iterables.find( + substatements, + Predicates.and(Predicates.instanceOf(type), + Predicates.instanceOf(returnType)))); + } catch (NoSuchElementException e) { + result = null; + } + return result; + } + } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java index bc379501a7..8ae39ae7de 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ExtendedTypeEffectiveStatementImpl.java @@ -22,7 +22,6 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement; import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; @@ -124,8 +123,8 @@ public class ExtendedTypeEffectiveStatementImpl extends EffectiveStatementBase baseType; final QName baseTypeQName = Utils.qNameFromArgument(ctx, ctx.getStatementArgument()); - if (TypeUtils.isYangBaseTypeString(baseTypeQName.getLocalName())) { - baseType = TypeUtils.getYangBaseTypeFromString(baseTypeQName.getLocalName()); + if (TypeUtils.isYangPrimitiveTypeString(baseTypeQName.getLocalName())) { + baseType = TypeUtils.getYangPrimitiveTypeFromString(baseTypeQName.getLocalName()); } else { StmtContext> baseTypeCtx = ctx .getParentContext().getFromNamespace(TypeNamespace.class, baseTypeQName); @@ -163,37 +162,39 @@ public class ExtendedTypeEffectiveStatementImpl extends EffectiveStatementBase> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) { addedByUses = true; diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/InputEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/InputEffectiveStatementImpl.java index 293f0ebd56..6757ba567e 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/InputEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/InputEffectiveStatementImpl.java @@ -7,8 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import java.util.HashSet; import java.util.LinkedList; @@ -62,7 +60,7 @@ public class InputEffectiveStatementImpl extends private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -74,7 +72,7 @@ public class InputEffectiveStatementImpl extends addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (ContainerSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java index 0676661c2f..7926636dc1 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafEffectiveStatementImpl.java @@ -7,9 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import java.util.Set; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; @@ -34,7 +31,7 @@ public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode< private final QName qname; private final SchemaPath path; - private boolean augmenting; + boolean augmenting; private boolean addedByUses; private LeafSchemaNode original; private boolean configuration = true; @@ -58,7 +55,7 @@ public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode< private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -70,7 +67,7 @@ public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode< addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (LeafSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java index 8840670ea3..218e845f6b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/LeafListEffectiveStatementImpl.java @@ -7,9 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import java.util.Set; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; @@ -34,7 +31,7 @@ public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedN private final QName qname; private final SchemaPath path; - private boolean augmenting; + boolean augmenting; private boolean addedByUses; private LeafListSchemaNode original; private boolean configuration = true; @@ -60,7 +57,7 @@ public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedN private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -72,7 +69,7 @@ public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedN addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (LeafListSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ListEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ListEffectiveStatementImpl.java index 84451a4c57..1f9644cf0b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ListEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ListEffectiveStatementImpl.java @@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -39,7 +38,7 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN private final SchemaPath path; boolean augmenting; - boolean addedByUses; + private boolean addedByUses; ListSchemaNode original; boolean configuration = true; ConstraintDefinition constraints; @@ -65,7 +64,7 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -77,7 +76,7 @@ public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataN addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (ListSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ModuleEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ModuleEffectiveStatementImpl.java index db3f31110a..95f6a15c8d 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ModuleEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ModuleEffectiveStatementImpl.java @@ -7,8 +7,10 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName; +import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -27,6 +29,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import java.util.Map; +import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource; import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier; import java.net.URI; import java.util.Collection; @@ -51,12 +54,12 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; public class ModuleEffectiveStatementImpl extends - AbstractEffectiveDocumentedNode - implements Module, Immutable { + AbstractEffectiveDocumentedNode implements + Module, Immutable { private final QNameModule qNameModule; private final String name; - private String sourcePath; // TODO fill + private String sourcePath; private String prefix; private String yangVersion; private String organization; @@ -72,7 +75,7 @@ public class ModuleEffectiveStatementImpl extends private ImmutableSet identities; private ImmutableList unknownNodes; private String source; - private ImmutableList> substatementsOfSubmodules; + private ImmutableList> substatementsOfSubmodules; private ImmutableMap childNodes; private ImmutableSet groupings; @@ -111,8 +114,14 @@ public class ModuleEffectiveStatementImpl extends } } - // TODO init source, sourcePath - source = ctx.getStatementSource().name(); + if (yangVersion == null) { + yangVersion = "1"; + } + + sourcePath = ((DeclarationInTextSource) ctx + .getStatementSourceReference()).getSourceName(); + // TODO source + // source = initSubmodules(ctx); initSubstatementCollections(ctx); @@ -133,21 +142,25 @@ public class ModuleEffectiveStatementImpl extends .values(); Set submodulesInit = new HashSet<>(); - List> substatementsOfSubmodulesInit = new LinkedList<>(); + List> substatementsOfSubmodulesInit = new LinkedList<>(); for (ModuleIdentifier submoduleIdentifier : includedSubmodules) { Mutable> submoduleCtx = (Mutable>) ctx .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier); - SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective(); + SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx + .buildEffective(); submodulesInit.add(submodule); - substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements()); + substatementsOfSubmodulesInit.addAll(submodule + .effectiveSubstatements()); } this.submodules = ImmutableSet.copyOf(submodulesInit); - this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit); + this.substatementsOfSubmodules = ImmutableList + .copyOf(substatementsOfSubmodulesInit); } - private void initSubstatementCollections(StmtContext> ctx) { + private void initSubstatementCollections( + StmtContext> ctx) { List> effectiveSubstatements = new LinkedList<>(); effectiveSubstatements.addAll(effectiveSubstatements()); @@ -206,7 +219,9 @@ public class ModuleEffectiveStatementImpl extends dataSchemaNode); mutablePublicChildNodes.add(dataSchemaNode); } else { - throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); + throw EffectiveStmtUtils + .createNameCollisionSourceException(ctx, + effectiveStatement); } } if (effectiveStatement instanceof UsesNode) { @@ -214,7 +229,9 @@ public class ModuleEffectiveStatementImpl extends if (!mutableUses.contains(usesNode)) { mutableUses.add(usesNode); } else { - throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); + throw EffectiveStmtUtils + .createNameCollisionSourceException(ctx, + effectiveStatement); } } if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) { @@ -223,7 +240,9 @@ public class ModuleEffectiveStatementImpl extends if (!mutableTypeDefinitions.contains(extendedType)) { mutableTypeDefinitions.add(extendedType); } else { - throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); + throw EffectiveStmtUtils + .createNameCollisionSourceException(ctx, + effectiveStatement); } } if (effectiveStatement instanceof GroupingDefinition) { @@ -231,14 +250,17 @@ public class ModuleEffectiveStatementImpl extends if (!mutableGroupings.contains(grp)) { mutableGroupings.add(grp); } else { - throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); + throw EffectiveStmtUtils + .createNameCollisionSourceException(ctx, + effectiveStatement); } } } this.unknownNodes = ImmutableList.copyOf(unknownNodesInit); this.augmentations = ImmutableSet.copyOf(augmentationsInit); - this.imports = ImmutableSet.copyOf(importsInit); + this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, + ctx)); this.notifications = ImmutableSet.copyOf(notificationsInit); this.rpcs = ImmutableSet.copyOf(rpcsInit); this.deviations = ImmutableSet.copyOf(deviationsInit); @@ -253,6 +275,30 @@ public class ModuleEffectiveStatementImpl extends this.uses = ImmutableSet.copyOf(mutableUses); } + private Set resolveModuleImports( + Set importsInit, + StmtContext> ctx) { + Set resolvedModuleImports = new LinkedHashSet<>(); + for (ModuleImport moduleImport : importsInit) { + if (moduleImport.getRevision().equals( + SimpleDateFormatUtil.DEFAULT_DATE_IMP)) { + QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, + moduleImport.getPrefix()); + if (!impModuleQName.getRevision().equals( + SimpleDateFormatUtil.DEFAULT_DATE_REV)) { + ModuleImport resolvedModuleImport = new ModuleImportImpl( + moduleImport.getModuleName(), + impModuleQName.getRevision(), + moduleImport.getPrefix()); + resolvedModuleImports.add(resolvedModuleImport); + } + } else { + resolvedModuleImports.add(moduleImport); + } + } + return resolvedModuleImports; + } + @Override public String getModuleSourcePath() { return sourcePath; @@ -430,7 +476,8 @@ public class ModuleEffectiveStatementImpl extends @Override public String toString() { - StringBuilder sb = new StringBuilder(ModuleEffectiveStatementImpl.class.getSimpleName()); + StringBuilder sb = new StringBuilder( + ModuleEffectiveStatementImpl.class.getSimpleName()); sb.append("["); sb.append("name=").append(name); sb.append(", namespace=").append(getNamespace()); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/OutputEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/OutputEffectiveStatementImpl.java index 7f8133cad4..56d74d17df 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/OutputEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/OutputEffectiveStatementImpl.java @@ -7,8 +7,6 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; - import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy; import java.util.HashSet; import java.util.LinkedList; @@ -62,7 +60,7 @@ public class OutputEffectiveStatementImpl extends private void initCopyType( StmtContext> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -74,7 +72,7 @@ public class OutputEffectiveStatementImpl extends addedByUses = augmenting = true; } - if (ctx.getTypeOfCopy() != TypeOfCopy.ORIGINAL) { + if (ctx.getOriginalCtx() != null) { original = (ContainerSchemaNode) ctx.getOriginalCtx().buildEffective(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/SubmoduleEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/SubmoduleEffectiveStatementImpl.java index 4af5525e95..b530c55c50 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/SubmoduleEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/SubmoduleEffectiveStatementImpl.java @@ -7,8 +7,10 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; +import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; import java.util.LinkedHashSet; import java.util.LinkedHashMap; import com.google.common.collect.ImmutableMap; @@ -22,6 +24,7 @@ import java.util.Map; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; +import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource; import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier; import java.net.URI; import java.util.Collection; @@ -119,8 +122,9 @@ public class SubmoduleEffectiveStatementImpl } } - //:TODO init source, sourcePath - source = ctx.getStatementSource().name(); + //:TODO init source +// source = ctx.getStatementSource().name(); + sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName(); initSubmodules(ctx); initSubstatementCollections(ctx); @@ -247,7 +251,7 @@ public class SubmoduleEffectiveStatementImpl this.unknownNodes = ImmutableList.copyOf(unknownNodesInit); this.augmentations = ImmutableSet.copyOf(augmentationsInit); - this.imports = ImmutableSet.copyOf(importsInit); + this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,ctx)); this.notifications = ImmutableSet.copyOf(notificationsInit); this.rpcs = ImmutableSet.copyOf(rpcsInit); this.deviations = ImmutableSet.copyOf(deviationsInit); @@ -262,6 +266,30 @@ public class SubmoduleEffectiveStatementImpl this.uses = ImmutableSet.copyOf(mutableUses); } + private Set resolveModuleImports( + Set importsInit, + StmtContext> ctx) { + Set resolvedModuleImports = new LinkedHashSet<>(); + for (ModuleImport moduleImport : importsInit) { + if (moduleImport.getRevision().equals( + SimpleDateFormatUtil.DEFAULT_DATE_IMP)) { + QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, + moduleImport.getPrefix()); + if (!impModuleQName.getRevision().equals( + SimpleDateFormatUtil.DEFAULT_DATE_REV)) { + ModuleImport resolvedModuleImport = new ModuleImportImpl( + moduleImport.getModuleName(), + impModuleQName.getRevision(), + moduleImport.getPrefix()); + resolvedModuleImports.add(resolvedModuleImport); + } + } else { + resolvedModuleImports.add(moduleImport); + } + } + return resolvedModuleImports; + } + @Override public String getModuleSourcePath() { return sourcePath; diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java index ee0ec2007a..fe9e1389c7 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/TypeDefEffectiveStatementImpl.java @@ -7,6 +7,14 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; +import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; + +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Decimal64SpecificationEffectiveStatementImpl; +import java.util.ArrayList; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.PatternEffectiveStatementImpl; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeEffectiveStatementImpl; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import java.util.Collections; @@ -17,7 +25,6 @@ import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; @@ -26,12 +33,12 @@ import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder; import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.TypeDefinitionEffectiveBuilder; -public class TypeDefEffectiveStatementImpl extends EffectiveStatementBase implements +public class TypeDefEffectiveStatementImpl extends + EffectiveStatementBase implements TypeDefinition>, TypeDefinitionEffectiveBuilder { private final QName qName; @@ -54,72 +61,132 @@ public class TypeDefEffectiveStatementImpl extends EffectiveStatementBase ctx) { + public TypeDefEffectiveStatementImpl( + StmtContext ctx) { super(ctx); qName = ctx.getStatementArgument(); path = Utils.getSchemaPath(ctx); + baseType = parseBaseTypeFromCtx(ctx); + + EffectiveStatementBase typeEffectiveStmt = firstSubstatementOfType( + TypeDefinition.class, EffectiveStatementBase.class); + + ranges = initRanges(typeEffectiveStmt); + lengths = initLengths(typeEffectiveStmt); + patterns = initPatterns(typeEffectiveStmt); - ExtendedTypeEffectiveStatementImpl type = null; + //due to compatibility problems with original yang parser + //:FIXME try to find out better solution + if (typeEffectiveStmt.argument().equals(TypeUtils.DECIMAL64) + && ranges.isEmpty()) { + fractionDigits = null; + } else { + fractionDigits = initFractionDigits(typeEffectiveStmt); + } for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { - if (effectiveStatement instanceof ExtendedTypeEffectiveStatementImpl) { - type = ((ExtendedTypeEffectiveStatementImpl) effectiveStatement); - } if (effectiveStatement instanceof DefaultEffectiveStatementImpl) { - defaultValue = ((DefaultEffectiveStatementImpl) effectiveStatement).argument(); + defaultValue = ((DefaultEffectiveStatementImpl) effectiveStatement) + .argument(); } if (effectiveStatement instanceof UnitsEffectiveStatementImpl) { - units = ((UnitsEffectiveStatementImpl) effectiveStatement).argument(); + units = ((UnitsEffectiveStatementImpl) effectiveStatement) + .argument(); } if (effectiveStatement instanceof DescriptionEffectiveStatementImpl) { - description = ((DescriptionEffectiveStatementImpl) effectiveStatement).argument(); + description = ((DescriptionEffectiveStatementImpl) effectiveStatement) + .argument(); } if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) { - reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument(); + reference = ((ReferenceEffectiveStatementImpl) effectiveStatement) + .argument(); } if (effectiveStatement instanceof StatusEffectiveStatementImpl) { - status = ((StatusEffectiveStatementImpl) effectiveStatement).argument(); + status = ((StatusEffectiveStatementImpl) effectiveStatement) + .argument(); } } + } - if (type != null) { + private TypeDefinition parseBaseTypeFromCtx( + final StmtContext ctx) { - ranges = ImmutableList.copyOf(type.getRangeConstraints()); - lengths = ImmutableList.copyOf(type.getLengthConstraints()); - patterns = ImmutableList.copyOf(type.getPatternConstraints()); - fractionDigits = type.getFractionDigits(); - } else { + TypeDefinition baseType; - ranges = Collections.emptyList(); - lengths = Collections.emptyList(); - patterns = Collections.emptyList(); - fractionDigits = null; + QName baseTypeQName = Utils.qNameFromArgument(ctx, StmtContextUtils + .firstAttributeOf(ctx.declaredSubstatements(), + TypeStatement.class)); + + if (TypeUtils.isYangBuiltInTypeString(baseTypeQName.getLocalName())) { + baseType = TypeUtils.getYangPrimitiveTypeFromString(baseTypeQName + .getLocalName()); + if (baseType == null) { + baseType = firstSubstatementOfType(TypeDefinition.class); + + //due to compatibility problems with original yang parser + //:FIXME try to find out better solution + if(baseType instanceof Decimal64SpecificationEffectiveStatementImpl) { + Decimal64SpecificationEffectiveStatementImpl decimal64 = (Decimal64SpecificationEffectiveStatementImpl) baseType; + if(decimal64.isExtended()) { + baseType = decimal64.getBaseType(); + } + } + } + } else { + StmtContext> baseTypeCtx = ctx + .getParentContext().getFromNamespace(TypeNamespace.class, + baseTypeQName); + baseType = (TypeDefEffectiveStatementImpl) baseTypeCtx + .buildEffective(); } - baseType = parseBaseTypeFromCtx(ctx); + return baseType; } - private TypeDefinition parseBaseTypeFromCtx(final StmtContext ctx) { + protected Integer initFractionDigits( + EffectiveStatementBase typeEffectiveStmt) { + final FractionDigitsEffectiveStatementImpl fractionDigitsEffStmt = typeEffectiveStmt + .firstEffective(FractionDigitsEffectiveStatementImpl.class); + return fractionDigitsEffStmt != null ? fractionDigitsEffStmt.argument() + : null; + } - TypeDefinition baseType; + protected List initRanges( + EffectiveStatementBase typeEffectiveStmt) { + final RangeEffectiveStatementImpl rangeConstraints = typeEffectiveStmt + .firstEffective(RangeEffectiveStatementImpl.class); + return rangeConstraints != null ? rangeConstraints.argument() + : Collections. emptyList(); + } - QName baseTypeQName = Utils.qNameFromArgument(ctx, - StmtContextUtils.firstAttributeOf(ctx.declaredSubstatements(), TypeStatement.class)); + protected List initLengths( + EffectiveStatementBase typeEffectiveStmt) { + final LengthEffectiveStatementImpl lengthConstraints = typeEffectiveStmt + .firstEffective(LengthEffectiveStatementImpl.class); + return lengthConstraints != null ? lengthConstraints.argument() + : Collections. emptyList(); + } - if (TypeUtils.isYangBaseTypeString(baseTypeQName.getLocalName())) { - baseType = TypeUtils.getYangBaseTypeFromString(baseTypeQName.getLocalName()); - } else { - StmtContext> baseTypeCtx = ctx - .getParentContext().getFromNamespace(TypeNamespace.class, baseTypeQName); - baseType = (TypeDefEffectiveStatementImpl) baseTypeCtx.buildEffective(); - } + protected List initPatterns( + EffectiveStatementBase typeEffectiveStmt) { + final List patternConstraints = new ArrayList<>(); - if (baseType == null) { - baseType = firstSubstatementOfType(TypeDefinition.class); + for (final EffectiveStatement effectiveStatement : typeEffectiveStmt + .effectiveSubstatements()) { + if (effectiveStatement instanceof PatternEffectiveStatementImpl) { + final PatternConstraint pattern = ((PatternEffectiveStatementImpl) effectiveStatement) + .argument(); + + if (pattern != null) { + patternConstraints.add(pattern); + } + } } - return baseType; + return !patternConstraints.isEmpty() ? ImmutableList + .copyOf(patternConstraints) : Collections + . emptyList(); } @Override @@ -193,10 +260,13 @@ public class TypeDefEffectiveStatementImpl extends EffectiveStatementBase, ?> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) { augmenting = true; @@ -85,7 +82,7 @@ public class UnknownEffectiveStatementImpl extends EffectiveStatementBase> ctx) { - Set copyTypesFromOriginal = StmtContextUtils.getCopyTypesFromOriginal(ctx); + List copyTypesFromOriginal = ctx.getCopyHistory(); if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) { addedByUses = true; } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java index 5306b32664..821edb9fa3 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BinaryEffectiveStatementImpl.java @@ -25,7 +25,7 @@ import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; public class BinaryEffectiveStatementImpl extends - EffectiveStatementBase implements + EffectiveStatementBase implements BinaryTypeDefinition { private static final String DESCRIPTION = "The binary built-in type represents any binary data, i.e., a sequence of octets."; @@ -40,7 +40,7 @@ public class BinaryEffectiveStatementImpl extends private final List lengthConstraints; public BinaryEffectiveStatementImpl( - final StmtContext> ctx) { + final StmtContext> ctx) { super(ctx); final LengthConstraint lengthConstraint = new LengthConstraintEffectiveImpl( diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java index 73d6d34ad0..6dddaafa30 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/Decimal64SpecificationEffectiveStatementImpl.java @@ -7,8 +7,10 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase; - import org.opendaylight.yangtools.yang.model.util.Decimal64; import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement; import com.google.common.base.Optional; @@ -31,11 +33,13 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.FractionDigitsEffectiveStatementImpl; public class Decimal64SpecificationEffectiveStatementImpl extends - EffectiveStatementBase implements DecimalTypeDefinition, TypeDefinitionEffectiveBuilder { + EffectiveStatementBase + implements DecimalTypeDefinition, TypeDefinitionEffectiveBuilder { private static final String UNITS = ""; private static final BigDecimal DEFAULT_VALUE = null; - private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, TypeUtils.DECIMAL64); + private static final QName QNAME = QName.create( + YangConstants.RFC6020_YANG_MODULE, TypeUtils.DECIMAL64); private static final String DESCRIPTION = "The decimal64 type represents a subset of the real numbers, which can " + "be represented by decimal numerals. The value space of decimal64 is the set of numbers that can " @@ -43,39 +47,84 @@ public class Decimal64SpecificationEffectiveStatementImpl extends + "'i x 10^-n' where i is an integer64 and n is an integer between 1 and 18, inclusively."; private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.3"; - private static final BigDecimal MIN_VALUE = new BigDecimal("-922337203685477580.8"); - private static final BigDecimal MAX_VALUE = new BigDecimal("922337203685477580.7"); + private static final BigDecimal MIN_VALUE = new BigDecimal( + "-922337203685477580.8"); + private static final BigDecimal MAX_VALUE = new BigDecimal( + "922337203685477580.7"); private List rangeConstraints; private Integer fractionDigits; private SchemaPath path; + private QName extendedTypeQName; - public Decimal64SpecificationEffectiveStatementImpl(StmtContext> ctx) { - super(ctx); + private ExtendedType extendedType; + private final boolean isExtended; - path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME); + public Decimal64SpecificationEffectiveStatementImpl( + StmtContext> ctx) { + super(ctx); for (final EffectiveStatement effectiveStatement : effectiveSubstatements()) { if (effectiveStatement instanceof FractionDigitsEffectiveStatementImpl) { - fractionDigits = ((FractionDigitsEffectiveStatementImpl) effectiveStatement).argument(); + fractionDigits = ((FractionDigitsEffectiveStatementImpl) effectiveStatement) + .argument(); + } + } + + List initRanges = initRanges(); + + if (!initRanges.isEmpty() && validateRanges(initRanges)) { + isExtended = true; + rangeConstraints = ImmutableList.copyOf(initRanges); + SchemaPath parentPath = Utils.getSchemaPath(ctx.getParentContext()); + extendedTypeQName = QName.create(parentPath.getLastComponent().getModule(), QNAME.getLocalName()); + path = parentPath.createChild(extendedTypeQName); + } else { + isExtended = false; + rangeConstraints = defaultRangeStatements(); + path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME); + } + } + + private boolean validateRanges(List initRanges) { + for (RangeConstraint rangeConstraint : initRanges) { + + String maxValueString = rangeConstraint.getMax().toString(); + String minValueString = rangeConstraint.getMin().toString(); + + if ((!maxValueString.equals("max") && new BigDecimal(maxValueString) + .compareTo(MAX_VALUE) > 0) + || (!minValueString.equals("min") && new BigDecimal( + minValueString).compareTo(MIN_VALUE) < 0)) { + return false; } } - - rangeConstraints = defaultRangeStatements(); + return true; + } + + protected List initRanges() { + final RangeEffectiveStatementImpl rangeConstraints = firstEffective(RangeEffectiveStatementImpl.class); + return rangeConstraints != null ? rangeConstraints.argument() + : Collections. emptyList(); } private List defaultRangeStatements() { final List rangeStmts = new ArrayList<>(); - final String rangeDescription = "Integer values between " + MIN_VALUE + " and " + MAX_VALUE + ", inclusively."; + final String rangeDescription = "Integer values between " + MIN_VALUE + + " and " + MAX_VALUE + ", inclusively."; final String rangeReference = RangeConstraintEffectiveImpl.DEFAULT_REFERENCE; - rangeStmts.add(new RangeConstraintEffectiveImpl(MIN_VALUE, MAX_VALUE, Optional.of(rangeDescription), Optional - .of(rangeReference))); + rangeStmts.add(new RangeConstraintEffectiveImpl(MIN_VALUE, MAX_VALUE, + Optional.of(rangeDescription), Optional.of(rangeReference))); return ImmutableList.copyOf(rangeStmts); } + public boolean isExtended() { + return isExtended; + } + @Override public List getRangeConstraints() { return rangeConstraints; @@ -88,7 +137,14 @@ public class Decimal64SpecificationEffectiveStatementImpl extends @Override public DecimalTypeDefinition getBaseType() { - return null; + if(isExtended) { + if (decimal64Instance == null) { + decimal64Instance = Decimal64.create(path, fractionDigits); + } + return decimal64Instance; + } else { + return null; + } } @Override @@ -164,20 +220,39 @@ public class Decimal64SpecificationEffectiveStatementImpl extends @Override public String toString() { - return Decimal64SpecificationEffectiveStatementImpl.class.getSimpleName() + "[qName=" + QNAME - + ", fractionDigits=" + fractionDigits + "]"; + return Decimal64SpecificationEffectiveStatementImpl.class + .getSimpleName() + + "[qName=" + + QNAME + + ", fractionDigits=" + + fractionDigits + "]"; } private Decimal64 decimal64Instance = null; @Override - public Decimal64 buildType() { + public TypeDefinition buildType() { - if (decimal64Instance != null) { + if (decimal64Instance == null) { + decimal64Instance = Decimal64.create(path, fractionDigits); + } + + if(!isExtended) { return decimal64Instance; } - decimal64Instance = Decimal64.create(path, fractionDigits); - return decimal64Instance; + if (extendedType != null) { + return extendedType; + } + + Builder extendedTypeBuilder = ExtendedType.builder(path.getLastComponent(), decimal64Instance, Optional.absent(), + Optional.absent(), path); + + extendedTypeBuilder.fractionDigits(fractionDigits); + extendedTypeBuilder.ranges(rangeConstraints); + + extendedType = extendedTypeBuilder.build(); + + return extendedType; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java index 3df9231c6f..03979a1a47 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java @@ -42,7 +42,7 @@ import org.slf4j.LoggerFactory; */ public final class ModuleDependencySort { - private static final Date DEFAULT_REVISION = new Date(0); + private static final Date DEFAULT_REVISION = SimpleDateFormatUtil.DEFAULT_DATE_REV; private static final Logger LOGGER = LoggerFactory.getLogger(ModuleDependencySort.class); private static final Function TOPOLOGY_FUNCTION = new Function() { @Override diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java index db1278b48a..e53afc975d 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java @@ -436,4 +436,22 @@ public class AugmentTest { assertTrue(id.isAugmenting()); } + @Test + public void caseShortHandAugmentingTest() throws Exception { + modules = TestUtils.loadModules(getClass().getResource("/choice-case-type-test-models").toURI()); + + Module findModule = TestUtils.findModule(modules, "choice-monitoring"); + + ContainerSchemaNode netconf = (ContainerSchemaNode) findModule.getDataChildByName("netconf-state"); + ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName("datastores"); + ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName("datastore"); + ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName("locks"); + ChoiceSchemaNode lockType = (ChoiceSchemaNode)locks.getDataChildByName("lock-type"); + + ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case"); + assertTrue(leafAugCase.isAugmenting()); + DataSchemaNode leafAug = leafAugCase.getDataChildByName("leaf-aug-case"); + assertFalse(leafAug.isAugmenting()); + } + } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java index cc8950d244..3b7bcb60fb 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java @@ -133,7 +133,7 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - GroupingDefinition grouping = myModule.getGroupings().iterator().next(); + GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping"); testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping")) .getDataChildByName("my-leaf-in-grouping"); @@ -235,7 +235,7 @@ public class SchemaContextUtilTest { assertNull(testNode); assertNull(foundNode); - GroupingDefinition grouping = myModule.getGroupings().iterator().next(); + GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping"); testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping")) .getDataChildByName("no-leaf-in-grouping"); @@ -327,7 +327,7 @@ public class SchemaContextUtilTest { assertNotNull(foundNode); assertEquals(testNode, foundNode); - testNode = myModule.getGroupings().iterator().next(); + testNode = getGroupingByName(myModule,"my-grouping"); path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping")); foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); @@ -459,7 +459,7 @@ public class SchemaContextUtilTest { Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), QName.parseRevision("2014-10-07")); - GroupingDefinition grouping = module.getGroupings().iterator().next(); + GroupingDefinition grouping = getGroupingByName(module,"my-grouping"); SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2"); RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug2291Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug2291Test.java new file mode 100644 index 0000000000..ad6a0dd0dc --- /dev/null +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/Bug2291Test.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.stmt.retest; + +import static org.junit.Assert.assertNotNull; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; + +import java.io.File; +import org.junit.Test; + +public class Bug2291Test { + + @Test + public void testRevisionWithExt() throws Exception { + File extdef = new File(getClass().getResource("/bugs/bug2291/bug2291-ext.yang").toURI()); + File bug = new File(getClass().getResource("/bugs/bug2291/bug2291.yang").toURI()); + File inet = new File(getClass().getResource("/ietf/ietf-inet-types@2010-09-24.yang").toURI()); + + SchemaContext context = TestUtils.parseYangSources(extdef, bug, inet); + assertNotNull(context); + } + +} diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/SchemaContextUtilTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/SchemaContextUtilTest.java new file mode 100644 index 0000000000..bd614c1381 --- /dev/null +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/SchemaContextUtilTest.java @@ -0,0 +1,866 @@ +/* + * Copyright (c) 2014 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.yangtools.yang.stmt.retest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collections; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; +import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; +import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.util.Int32; +import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl; +import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; + +public class SchemaContextUtilTest { + @Mock + private SchemaContext mockSchemaContext; + @Mock + private Module mockModule; + + @Test + public void testFindDummyData() { + MockitoAnnotations.initMocks(this); + + QName qName = QName.create("TestQName"); + SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true); + assertEquals("Should be null. Module TestQName not found", null, + SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath)); + + RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true); + assertEquals("Should be null. Module bookstore not found", null, + SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath)); + + SchemaNode schemaNode = Int32.getInstance(); + RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false); + assertEquals("Should be null, Module prefix not found", null, + SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode, + xPathRelative)); + + assertEquals("Should be null. Module TestQName not found", null, + SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName))); + + assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode)); + } + + @Test + public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, + SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-leaf-in-container"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-leaf-in-container")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + RpcDefinition rpc = getRpcByName(myModule,"my-rpc"); + testNode = rpc.getInput().getDataChildByName("my-input-leaf"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "my-input-leaf")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + rpc = getRpcByName(myModule,"my-rpc"); + testNode = rpc.getOutput().getDataChildByName("my-output-leaf"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "output"), + QName.create(myModule.getQNameModule(), "my-output-leaf")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + NotificationDefinition notification = myModule.getNotifications().iterator().next(); + testNode = notification.getDataChildByName("my-notification-leaf"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), + QName.create(myModule.getQNameModule(), "my-notification-leaf")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping"); + testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping")) + .getDataChildByName("my-leaf-in-grouping"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), + QName.create(myModule.getQNameModule(), "my-container-in-grouping"), + QName.create(myModule.getQNameModule(), "my-leaf-in-grouping")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName( + "my-choice-leaf-one"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "one"), + QName.create(myModule.getQNameModule(), "my-choice-leaf-one")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-list"); + + testNode = listNode.getDataChildByName("my-leaf-in-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "my-leaf-in-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-list"); + + testNode = listNode.getDataChildByName("my-leaf-list-in-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "my-leaf-list-in-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + } + + @Test + public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, + SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-leaf-not-in-container"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-leaf-not-in-container")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + RpcDefinition rpc = getRpcByName(myModule,"my-rpc"); + testNode = rpc.getInput().getDataChildByName("no-input-leaf"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "no-input-leaf")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + NotificationDefinition notification = myModule.getNotifications().iterator().next(); + testNode = notification.getDataChildByName("no-notification-leaf"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), + QName.create(myModule.getQNameModule(), "no-notification-leaf")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + GroupingDefinition grouping = getGroupingByName(myModule,"my-grouping"); + testNode = ((ContainerSchemaNode) grouping.getDataChildByName("my-container-in-grouping")) + .getDataChildByName("no-leaf-in-grouping"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), + QName.create(myModule.getQNameModule(), "my-container-in-grouping"), + QName.create(myModule.getQNameModule(), "no-leaf-in-grouping")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + testNode = ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName("one").getDataChildByName( + "no-choice-leaf"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "one"), + QName.create(myModule.getQNameModule(), "no-choice-leaf")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-list"); + + testNode = listNode.getDataChildByName("no-leaf-in-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "no-leaf-in-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-list"); + + testNode = listNode.getDataChildByName("no-leaf-list-in-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "no-leaf-list-in-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + } + + @Test + public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, + SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + SchemaNode testNode = myModule.getDataChildByName("my-container"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = getRpcByName(myModule,"my-rpc"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = myModule.getNotifications().iterator().next(); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = getGroupingByName(myModule,"my-grouping"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = myModule.getDataChildByName("my-choice"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((ContainerSchemaNode) myModule.getDataChildByName("my-container")).getDataChildByName("my-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + } + + @Test + public void findParentModuleTest() throws URISyntaxException, IOException, SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + DataSchemaNode node = myModule.getDataChildByName("my-container"); + + Module foundModule = SchemaContextUtil.findParentModule(context, node); + + assertEquals(myModule, foundModule); + } + + @Test(expected = IllegalArgumentException.class) + public void findParentModuleIllegalArgumentTest() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + SchemaContextUtil.findParentModule(mockContext, null); + + } + + @Test(expected = IllegalArgumentException.class) + public void findParentModuleIllegalArgumentTest2() { + + SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class); + SchemaContextUtil.findParentModule(null, mockSchemaNode); + + } + + @Test(expected = IllegalStateException.class) + public void findParentModuleIllegalStateTest() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class); + Mockito.when(mockSchemaNode.getPath()).thenReturn(null); + SchemaContextUtil.findParentModule(mockContext, mockSchemaNode); + + } + + @Test(expected = IllegalArgumentException.class) + public void findDataSchemaNodeIllegalArgumentTest() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + SchemaContextUtil.findDataSchemaNode(mockContext, null); + + } + + @Test(expected = IllegalArgumentException.class) + public void findDataSchemaNodeIllegalArgumentTest2() { + + SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class); + SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath); + + } + + @Test + public void findDataSchemaNodeTest() throws URISyntaxException, IOException, SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + Module importedModule = context.findModuleByNamespaceAndRevision(new URI("uri:imported-module"), + QName.parseRevision("2014-10-07")); + + SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName("my-imported-container")) + .getDataChildByName("my-imported-leaf"); + + RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf", true); + + SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath); + + assertNotNull(foundNode); + assertNotNull(testNode); + assertEquals(testNode, foundNode); + + } + + @Test + public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module module = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + GroupingDefinition grouping = getGroupingByName(module,"my-grouping"); + SchemaNode testNode = grouping.getDataChildByName("my-leaf-in-gouping2"); + + RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); + + SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath); + + assertNotNull(foundNode); + assertNotNull(testNode); + assertEquals(testNode, foundNode); + + } + + @Test(expected = IllegalArgumentException.class) + public void findDataSchemaNodeFromXPathIllegalArgumentTest() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + Module module = Mockito.mock(Module.class); + + SchemaContextUtil.findDataSchemaNode(mockContext, module, null); + + } + + @Test(expected = IllegalArgumentException.class) + public void findDataSchemaNodeFromXPathIllegalArgumentTest2() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); + + SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath); + + } + + @Test(expected = IllegalArgumentException.class) + public void findDataSchemaNodeFromXPathIllegalArgumentTest3() { + + Module module = Mockito.mock(Module.class); + RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true); + + SchemaContextUtil.findDataSchemaNode(null, module, xpath); + + } + + @Test(expected = IllegalArgumentException.class) + public void findDataSchemaNodeFromXPathIllegalArgumentTest4() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + Module module = Mockito.mock(Module.class); + RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", + true); + + SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath); + + } + + @Test + public void findDataSchemaNodeFromXPathNullTest() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + Module module = Mockito.mock(Module.class); + RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class); + + Mockito.when(xpath.toString()).thenReturn(null); + assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath)); + + } + + @Test + public void findDataSchemaNodeFromXPathNullTest2() { + + SchemaContext mockContext = Mockito.mock(SchemaContext.class); + Module module = Mockito.mock(Module.class); + RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false); + + assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath)); + + } + + @Test + public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException, + SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + // find grouping in container + DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container"); + SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-grouping-in-container")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in list + dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-list"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-list"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "my-grouping-in-list")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in grouping + dataContainer = getGroupingByName(myModule, "my-grouping"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), + QName.create(myModule.getQNameModule(), "my-grouping-in-grouping")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in rpc + RpcDefinition rpc = getRpcByName(myModule, "my-rpc"); + for (GroupingDefinition grouping : rpc.getGroupings()) { + if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) { + testNode = grouping; + } + } + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "my-grouping-in-rpc")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in output + dataContainer = getRpcByName(myModule, "my-rpc").getOutput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-output"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "output"), + QName.create(myModule.getQNameModule(), "my-grouping-in-output")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in input + dataContainer = getRpcByName(myModule, "my-rpc").getInput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-input"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "my-grouping-in-input")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in notification + dataContainer = getNotificationByName(myModule, "my-notification"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-notification"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), + QName.create(myModule.getQNameModule(), "my-grouping-in-notification")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + // find grouping in case + dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName( + "one").getDataChildByName("my-container-in-case"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-case"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "one"), + QName.create(myModule.getQNameModule(), "my-container-in-case"), + QName.create(myModule.getQNameModule(), "my-grouping-in-case")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case"); + path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case")); + + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + } + + @Test + public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException, + SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + // find grouping in container + DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container"); + SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-grouping-in-container2")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in list + dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container")) + .getDataChildByName("my-list"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-list2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"), + QName.create(myModule.getQNameModule(), "my-list"), + QName.create(myModule.getQNameModule(), "my-grouping-in-list2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in grouping + dataContainer = getGroupingByName(myModule, "my-grouping"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"), + QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in rpc + RpcDefinition rpc = getRpcByName(myModule, "my-rpc"); + for (GroupingDefinition grouping : rpc.getGroupings()) { + if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) { + testNode = grouping; + } + } + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in output + dataContainer = getRpcByName(myModule, "my-rpc").getOutput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-output2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "output"), + QName.create(myModule.getQNameModule(), "my-grouping-in-output2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in input + dataContainer = getRpcByName(myModule, "my-rpc").getInput(); + testNode = getGroupingByName(dataContainer, "my-grouping-in-input2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "my-grouping-in-input2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in notification + dataContainer = getNotificationByName(myModule, "my-notification"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"), + QName.create(myModule.getQNameModule(), "my-grouping-in-notification2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + // find grouping in case + dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName( + "one").getDataChildByName("my-container-in-case"); + testNode = getGroupingByName(dataContainer, "my-grouping-in-case2"); + + path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "one"), + QName.create(myModule.getQNameModule(), "my-container-in-case"), + QName.create(myModule.getQNameModule(), "my-grouping-in-case2")); + foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNull(testNode); + assertNull(foundNode); + + } + + private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) { + for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) { + if (grouping.getQName().getLocalName().equals(name)) { + return grouping; + } + } + return null; + } + + private static RpcDefinition getRpcByName(final Module module, final String name) { + for (RpcDefinition rpc : module.getRpcs()) { + if (rpc.getQName().getLocalName().equals(name)) { + return rpc; + } + } + return null; + } + + private static NotificationDefinition getNotificationByName(final Module module, final String name) { + for (NotificationDefinition notification : module.getNotifications()) { + if (notification.getQName().getLocalName().equals(name)) { + return notification; + } + } + return null; + } + + @Test + public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException, + SourceException, ReactorException { + + SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test"); + + Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"), + QName.parseRevision("2014-10-07")); + + ChoiceSchemaNode choice = (ChoiceSchemaNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice"); + SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two"); + + SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"), + QName.create(myModule.getQNameModule(), "input"), + QName.create(myModule.getQNameModule(), "my-choice"), + QName.create(myModule.getQNameModule(), "case-two"), + QName.create(myModule.getQNameModule(), "two")); + SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot()); + + assertNotNull(testNode); + assertNotNull(foundNode); + assertEquals(testNode, foundNode); + + } + +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/TestUtils.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/TestUtils.java index 21326d67c8..5878f492aa 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/TestUtils.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/TestUtils.java @@ -8,6 +8,16 @@ package org.opendaylight.yangtools.yang.stmt.retest; import static org.junit.Assert.assertEquals; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Collection; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; +import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils; + import java.io.File; import java.io.InputStream; import java.net.URI; @@ -40,20 +50,25 @@ final class TestUtils { private TestUtils() { } - public static Set loadModules(final URI resourceDirectory) throws SourceException, ReactorException { - final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(); + public static Set loadModules(final URI resourceDirectory) + throws SourceException, ReactorException { + final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR + .newBuild(); File[] files = new File(resourceDirectory).listFiles(); for (File file : files) { - addSources(reactor, new YangStatementSourceImpl(file.getPath(), true)); + addSources(reactor, new YangStatementSourceImpl(file.getPath(), + true)); } EffectiveSchemaContext ctx = reactor.buildEffective(); return ctx.getModules(); } - public static Set loadModules(final List streams) throws SourceException, ReactorException { - final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(); + public static Set loadModules(final List streams) + throws SourceException, ReactorException { + final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR + .newBuild(); for (InputStream inputStream : streams) { addSources(reactor, new YangStatementSourceImpl(inputStream)); } @@ -62,14 +77,17 @@ final class TestUtils { return ctx.getModules(); } - public static Module loadModule(final InputStream stream) throws SourceException, ReactorException { - final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(); + public static Module loadModule(final InputStream stream) + throws SourceException, ReactorException { + final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR + .newBuild(); addSources(reactor, new YangStatementSourceImpl(stream)); EffectiveSchemaContext ctx = reactor.buildEffective(); return ctx.getModules().iterator().next(); } - public static Module findModule(final Set modules, final String moduleName) { + public static Module findModule(final Set modules, + final String moduleName) { Module result = null; for (Module module : modules) { if (module.getName().equals(moduleName)) { @@ -80,7 +98,8 @@ final class TestUtils { return result; } - public static ModuleImport findImport(final Set imports, final String prefix) { + public static ModuleImport findImport(final Set imports, + final String prefix) { ModuleImport result = null; for (ModuleImport moduleImport : imports) { if (moduleImport.getPrefix().equals(prefix)) { @@ -91,7 +110,8 @@ final class TestUtils { return result; } - public static TypeDefinition findTypedef(final Set> typedefs, final String name) { + public static TypeDefinition findTypedef( + final Set> typedefs, final String name) { TypeDefinition result = null; for (TypeDefinition td : typedefs) { if (td.getQName().getLocalName().equals(name)) { @@ -102,8 +122,9 @@ final class TestUtils { return result; } - public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, - final String prefix, final String... names) { + public static SchemaPath createPath(final boolean absolute, + final URI namespace, final Date revision, final String prefix, + final String... names) { List path = new ArrayList<>(); for (String name : names) { path.add(QName.create(namespace, revision, name)); @@ -123,18 +144,20 @@ final class TestUtils { } /** - * Test if node has augmenting flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its - * child nodes/case nodes too. + * Test if node has augmenting flag set to expected value. In case this is + * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too. * * @param node * node to check * @param expected * expected value */ - public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) { + public static void checkIsAugmenting(final DataSchemaNode node, + final boolean expected) { assertEquals(expected, node.isAugmenting()); if (node instanceof DataNodeContainer) { - for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) { + for (DataSchemaNode child : ((DataNodeContainer) node) + .getChildNodes()) { checkIsAugmenting(child, expected); } } else if (node instanceof ChoiceSchemaNode) { @@ -145,18 +168,20 @@ final class TestUtils { } /** - * Check if node has addedByUses flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its - * child nodes/case nodes too. + * Check if node has addedByUses flag set to expected value. In case this is + * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too. * * @param node * node to check * @param expected * expected value */ - public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) { + public static void checkIsAddedByUses(final DataSchemaNode node, + final boolean expected) { assertEquals(expected, node.isAddedByUses()); if (node instanceof DataNodeContainer) { - for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) { + for (DataSchemaNode child : ((DataNodeContainer) node) + .getChildNodes()) { checkIsAddedByUses(child, expected); } } else if (node instanceof ChoiceSchemaNode) { @@ -166,14 +191,16 @@ final class TestUtils { } } - public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) { + public static void checkIsAddedByUses(final GroupingDefinition node, + final boolean expected) { assertEquals(expected, node.isAddedByUses()); for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) { checkIsAddedByUses(child, expected); } } - public static List findModules(final Set modules, final String moduleName) { + public static List findModules(final Set modules, + final String moduleName) { List result = new ArrayList<>(); for (Module module : modules) { if (module.getName().equals(moduleName)) { @@ -183,10 +210,63 @@ final class TestUtils { return result; } - private static void addSources(CrossSourceStatementReactor.BuildAction reactor, YangStatementSourceImpl... sources) { + private static void addSources( + CrossSourceStatementReactor.BuildAction reactor, + YangStatementSourceImpl... sources) { for (YangStatementSourceImpl source : sources) { reactor.addSource(source); } } + public static SchemaContext parseYangSources( + StatementStreamSource... sources) throws SourceException, + ReactorException { + + CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR + .newBuild(); + reactor.addSources(sources); + + return reactor.buildEffective(); + } + + public static SchemaContext parseYangSources(File... files) + throws SourceException, ReactorException, FileNotFoundException { + + StatementStreamSource[] sources = new StatementStreamSource[files.length]; + + for (int i = 0; i < files.length; i++) { + sources[i] = new YangStatementSourceImpl(new FileInputStream( + files[i])); + } + + return parseYangSources(sources); + } + + public static SchemaContext parseYangSources(Collection files) + throws SourceException, ReactorException, FileNotFoundException { + return parseYangSources(files.toArray(new File[files.size()])); + } + + public static SchemaContext parseYangSources(String yangSourcesDirectoryPath) + throws SourceException, ReactorException, FileNotFoundException, + URISyntaxException { + + URL resourceDir = StmtTestUtils.class + .getResource(yangSourcesDirectoryPath); + File testSourcesDir = new File(resourceDir.toURI()); + + return parseYangSources(testSourcesDir.listFiles()); + } + + public static SchemaContext parseYangSource(String yangSourceFilePath) + throws SourceException, ReactorException, FileNotFoundException, + URISyntaxException { + + URL resourceFile = StmtTestUtils.class + .getResource(yangSourceFilePath); + File testSourcesFile = new File(resourceFile.toURI()); + + return parseYangSources(testSourcesFile); + } + } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/AugmentProcessTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/AugmentProcessTest.java index fb6bd84e33..fdc1802e68 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/AugmentProcessTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/AugmentProcessTest.java @@ -9,9 +9,18 @@ package org.opendaylight.yangtools.yang.stmt.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.net.URISyntaxException; + +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; +import java.io.FileNotFoundException; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; import com.google.common.collect.ImmutableList; import java.net.URI; import org.junit.Test; @@ -247,4 +256,24 @@ public class AugmentProcessTest { reactor.addSource(source); } } + + @Test + public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException { + + SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models"); + + assertNotNull(context); + + ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName("netconf-state"); + ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName("datastores"); + ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName("datastore"); + ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName("locks"); + ChoiceSchemaNode lockType = (ChoiceSchemaNode)locks.getDataChildByName("lock-type"); + + ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case"); + assertTrue(leafAugCase.isAugmenting()); + DataSchemaNode leafAug = leafAugCase.getDataChildByName("leaf-aug-case"); + assertFalse(leafAug.isAugmenting()); + } + } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/MoreRevisionsTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/MoreRevisionsTest.java index e5d774ff3d..b46e65ebbf 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/MoreRevisionsTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/MoreRevisionsTest.java @@ -11,8 +11,11 @@ package org.opendaylight.yangtools.yang.stmt.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.util.Set; +import java.io.FileNotFoundException; +import java.net.URISyntaxException; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import java.util.Set; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.Module; @@ -112,10 +115,21 @@ public class MoreRevisionsTest { .newBuild(); reactor.addSources(TED_20130712, TED_20131021, ISIS_20130712, - ISIS_20131021, L3_20130712, L3_20131021, IETF_TYPES, NETWORK_TOPOLOGY_20130712, NETWORK_TOPOLOGY_20131021); + ISIS_20131021, L3_20130712, L3_20131021, IETF_TYPES, + NETWORK_TOPOLOGY_20130712, NETWORK_TOPOLOGY_20131021); EffectiveSchemaContext result = reactor.buildEffective(); assertNotNull(result); } + @Test + public void multipleRevisionsTest() throws SourceException, + ReactorException, FileNotFoundException, URISyntaxException { + for (int i = 0; i < 25; i++) { + SchemaContext context = StmtTestUtils + .parseYangSources("/semantic-statement-parser/multiple-revisions"); + assertNotNull(context); + } + } + } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/StmtTestUtils.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/StmtTestUtils.java index f612850e44..9cbe4d4dbc 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/StmtTestUtils.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/StmtTestUtils.java @@ -8,9 +8,19 @@ package org.opendaylight.yangtools.yang.stmt.test; +import java.net.URISyntaxException; + +import java.net.URL; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; +import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; import java.util.ArrayList; import java.util.List; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; @@ -23,7 +33,8 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceIm public class StmtTestUtils { - private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class); + private static final Logger LOG = LoggerFactory + .getLogger(StmtTestUtils.class); private StmtTestUtils() { @@ -38,7 +49,8 @@ public class StmtTestUtils { } } - public static List findModules(final Set modules, final String moduleName) { + public static List findModules(final Set modules, + final String moduleName) { List result = new ArrayList<>(); for (Module module : modules) { if (module.getName().equals(moduleName)) { @@ -71,12 +83,49 @@ public class StmtTestUtils { String indent) { for (DataSchemaNode child : childNodes) { - LOG.debug(indent + "Child " - + child.getQName().getLocalName()); + LOG.debug(indent + "Child " + child.getQName().getLocalName()); if (child instanceof DataNodeContainer) { printChilds(((DataNodeContainer) child).getChildNodes(), indent + " "); } } } + + public static SchemaContext parseYangSources( + StatementStreamSource... sources) throws SourceException, + ReactorException { + + CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR + .newBuild(); + reactor.addSources(sources); + + return reactor.buildEffective(); + } + + public static SchemaContext parseYangSources(File... files) + throws SourceException, ReactorException, FileNotFoundException { + + StatementStreamSource[] sources = new StatementStreamSource[files.length]; + + for (int i = 0; i < files.length; i++) { + sources[i] = new YangStatementSourceImpl(new FileInputStream( + files[i])); + } + + return parseYangSources(sources); + } + + public static SchemaContext parseYangSources(Collection files) + throws SourceException, ReactorException, FileNotFoundException { + return parseYangSources(files.toArray(new File[files.size()])); + } + + public static SchemaContext parseYangSources(String yangSourcesDirectoryPath) + throws SourceException, ReactorException, FileNotFoundException, URISyntaxException { + + URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath); + File testSourcesDir = new File(resourceDir.toURI()); + + return parseYangSources(testSourcesDir.listFiles()); + } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java new file mode 100644 index 0000000000..263c179c55 --- /dev/null +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/TypedefConstraintsTest.java @@ -0,0 +1,73 @@ +package org.opendaylight.yangtools.yang.stmt.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.math.BigDecimal; + +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils; +import org.opendaylight.yangtools.yang.model.util.Decimal64; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import java.util.List; +import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import java.util.Set; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import java.io.FileNotFoundException; +import java.net.URISyntaxException; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; +import org.junit.Test; + +public class TypedefConstraintsTest { + + @Test + public void decimalRangeConstraintsTest() throws SourceException, + FileNotFoundException, ReactorException, URISyntaxException { + SchemaContext context = StmtTestUtils + .parseYangSources("/stmt-test/constraints"); + + assertNotNull(context); + + Set> typeDefinitions = context.getTypeDefinitions(); + assertNotNull(typeDefinitions); + assertEquals(1, typeDefinitions.size()); + + TypeDefinition myDecimal = typeDefinitions.iterator().next(); + + assertNotNull(myDecimal); + assertTrue(myDecimal instanceof ExtendedType); + + List rangeConstraints = ((ExtendedType) myDecimal) + .getRangeConstraints(); + + assertNotNull(rangeConstraints); + assertEquals(1, rangeConstraints.size()); + + DataSchemaNode dataNode = context.getDataChildByName("id-decimal64"); + assertNotNull(dataNode); + assertTrue(dataNode instanceof LeafSchemaNode); + + LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode; + TypeDefinition type = leafDecimal.getType(); + + assertTrue(type instanceof ExtendedType); + ExtendedType decType = (ExtendedType) type; + + List decRangeConstraints = decType.getRangeConstraints(); + + assertEquals(1, decRangeConstraints.size()); + + RangeConstraint range = decRangeConstraints.iterator().next(); + assertEquals(new BigDecimal(1.5), range.getMin()); + assertEquals(new BigDecimal(5.5), range.getMax()); + + assertTrue(decType.getQName().getModule().equals(leafDecimal.getQName().getModule())); + assertTrue(decType.getQName().getLocalName().equals(TypeUtils.DECIMAL64)); + assertTrue(decType.getBaseType() instanceof Decimal64); + } + +} diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java index 835eeb13ae..d6203a555b 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/YangTypes2StmtTest.java @@ -9,6 +9,9 @@ package org.opendaylight.yangtools.yang.stmt.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; + +import org.opendaylight.yangtools.yang.model.util.ExtendedType; + import java.net.URI; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; @@ -16,7 +19,6 @@ import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.util.BooleanType; -import org.opendaylight.yangtools.yang.model.util.Decimal64; import org.opendaylight.yangtools.yang.model.util.Int16; import org.opendaylight.yangtools.yang.model.util.Int32; import org.opendaylight.yangtools.yang.model.util.Int64; @@ -68,8 +70,8 @@ public class YangTypes2StmtTest { final LeafSchemaNode lfDecimalNode = (LeafSchemaNode) result.getDataChildByName(lfDecimal); assertNotNull(lfDecimalNode); - final Decimal64 lfDecimalNodeType = (Decimal64) lfDecimalNode.getType(); - assertEquals(Decimal64.class, lfDecimalNodeType.getClass()); + final ExtendedType lfDecimalNodeType = (ExtendedType) lfDecimalNode.getType(); + assertEquals(ExtendedType.class, lfDecimalNodeType.getClass()); assertEquals(2, lfDecimalNodeType.getFractionDigits().intValue()); final LeafSchemaNode lfInt8Node = (LeafSchemaNode) result.getDataChildByName(lfInt8); diff --git a/yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/augment-monitoring@2013-07-01.yang b/yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/augment-monitoring@2013-07-01.yang new file mode 100644 index 0000000000..5c48c1c4db --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/augment-monitoring@2013-07-01.yang @@ -0,0 +1,25 @@ +module augment-monitoring { + yang-version 1; + namespace "urn:ietf:params:xml:ns:yang:augment-monitoring"; + prefix "amon"; + + import choice-monitoring { prefix nm; } + + revision "2013-07-01" { + reference "NO REF"; + } + + augment "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type" { + + case anonymous-lock { + leaf lock-time { + type uint32; + } + } + + leaf leaf-aug-case { + type string; + } + } + +} diff --git a/yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/choice-monitoring@2013-07-01.yang b/yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/choice-monitoring@2013-07-01.yang new file mode 100644 index 0000000000..03a518c7a9 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/choice-case-type-test-models/choice-monitoring@2013-07-01.yang @@ -0,0 +1,30 @@ +module choice-monitoring { + yang-version 1; + namespace "urn:ietf:params:xml:ns:yang:choice-monitoring"; + prefix "ncm"; + + revision 2013-07-01 { + } + + container netconf-state { + config false; + + container datastores { + list datastore { + key name; + + leaf name { + type string; + } + container locks { + choice lock-type { + + case original-lock { + } + + } + } + } + } + } +} diff --git a/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev.yang b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev.yang new file mode 100644 index 0000000000..9be77d0348 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev.yang @@ -0,0 +1,13 @@ +module importedrevtest { + yang-version 1; + namespace "urn:imprevtst:yang"; + prefix "imprevtst"; + + description "This is a test module"; + + revision 2015-07-02; + + typedef date-and-time { + type string; + } +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-05-02.yang b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-05-02.yang new file mode 100644 index 0000000000..283fd414dc --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-05-02.yang @@ -0,0 +1,13 @@ +module importedrevtest { + yang-version 1; + namespace "urn:imprevtst:yang"; + prefix "imprevtst"; + + description "This is a test module"; + + revision 2015-05-02; + + typedef date-and-time { + type string; + } +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-06-02.yang b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-06-02.yang new file mode 100644 index 0000000000..6c025039e6 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-06-02.yang @@ -0,0 +1,13 @@ +module importedrevtest { + yang-version 1; + namespace "urn:imprevtst:yang"; + prefix "imprevtst"; + + description "This is a test module"; + + revision 2015-06-02; + + typedef date-and-time { + type string; + } +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-08-02.yang b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-08-02.yang new file mode 100644 index 0000000000..d532636d77 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-08-02.yang @@ -0,0 +1,13 @@ +module importedrevtest { + yang-version 1; + namespace "urn:imprevtst:yang"; + prefix "imprevtst"; + + description "This is a test module"; + + revision 2015-08-02; + + typedef date-and-time { + type string; + } +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-09-02.yang b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-09-02.yang new file mode 100644 index 0000000000..5ad34ecaeb --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/importedtestrev@2015-09-02.yang @@ -0,0 +1,13 @@ +module importedrevtest { + yang-version 1; + namespace "urn:imprevtst:yang"; + prefix "imprevtst"; + + description "This is a test module"; + + revision 2015-09-02; + + typedef date-and-time { + type string; + } +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/testrev.yang b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/testrev.yang new file mode 100644 index 0000000000..2600dd4726 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/semantic-statement-parser/multiple-revisions/testrev.yang @@ -0,0 +1,19 @@ +module testrev { + yang-version 1; + namespace "urn:tstrev:yang"; + prefix "tstrev"; + + import importedrevtest { + prefix "imprevtst"; + } + + description "This is a test module"; + + revision 2015-07-02; + + container mycontainer { + leaf myleaf { + type imprevtst:date-and-time; + } + } +} \ No newline at end of file diff --git a/yang/yang-parser-impl/src/test/resources/stmt-test/constraints/foo.yang b/yang/yang-parser-impl/src/test/resources/stmt-test/constraints/foo.yang new file mode 100644 index 0000000000..a27f9fe6c3 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/stmt-test/constraints/foo.yang @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 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 + */ +module foo { + yang-version 1; + namespace "urn:opendaylight.foo"; + prefix "foo"; + + revision "2013-10-08" { + } + + typedef my-decimal-type { + type decimal64 { + fraction-digits 6; + range "1.5..5.5"; + } + } + + leaf id-decimal64 { + type decimal64 { + fraction-digits 4; + range "1.5..5.5"; + } + } +} -- 2.36.6