Search in sources :

Example 1 with ChoiceEffectiveStatement

use of org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement in project yangtools by opendaylight.

the class SchemaInferenceStack method resolveChoiceSteps.

private void resolveChoiceSteps(@NonNull final ChoiceEffectiveStatement parent, @NonNull final QName nodeIdentifier) {
    for (EffectiveStatement<?, ?> stmt : parent.effectiveSubstatements()) {
        if (stmt instanceof CaseEffectiveStatement) {
            final CaseEffectiveStatement caze = (CaseEffectiveStatement) stmt;
            final SchemaTreeEffectiveStatement<?> found = caze.findSchemaTreeNode(nodeIdentifier).orElse(null);
            if (found instanceof ChoiceEffectiveStatement) {
                deque.push(caze);
                deque.push(found);
                return;
            }
        }
    }
    throw new VerifyException("Failed to resolve " + nodeIdentifier + " in " + parent);
}
Also used : VerifyException(com.google.common.base.VerifyException) CaseEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement) ChoiceEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement)

Example 2 with ChoiceEffectiveStatement

use of org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement in project yangtools by opendaylight.

the class SchemaInferenceStack method enterChoice.

/**
 * Lookup a {@code choice} by its node identifier and push it to the stack. This step is very similar to
 * {@link #enterSchemaTree(QName)}, except it handles the use case where traversal ignores actual {@code case}
 * intermediate schema tree children.
 *
 * @param nodeIdentifier Node identifier of the choice to enter
 * @return Resolved choice
 * @throws NullPointerException if {@code nodeIdentifier} is null
 * @throws IllegalArgumentException if the corresponding choice cannot be found
 */
@NonNull
public ChoiceEffectiveStatement enterChoice(final QName nodeIdentifier) {
    final QName nodeId = requireNonNull(nodeIdentifier);
    final EffectiveStatement<?, ?> parent = deque.peek();
    if (parent instanceof ChoiceEffectiveStatement) {
        return enterChoice((ChoiceEffectiveStatement) parent, nodeId);
    }
    // Fall back to schema tree lookup. Note if it results in non-choice, we rewind before reporting an error
    final SchemaTreeEffectiveStatement<?> result = enterSchemaTree(nodeId);
    if (result instanceof ChoiceEffectiveStatement) {
        return (ChoiceEffectiveStatement) result;
    }
    exit();
    if (parent != null) {
        throw notPresent(parent, "Choice", nodeId);
    }
    throw new IllegalArgumentException("Choice " + nodeId + " not present");
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) AbstractQName(org.opendaylight.yangtools.yang.common.AbstractQName) ChoiceEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement) NonNull(org.eclipse.jdt.annotation.NonNull) Objects.requireNonNull(java.util.Objects.requireNonNull)

Example 3 with ChoiceEffectiveStatement

use of org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement in project yangtools by opendaylight.

the class NormalizedNodeStreamWriterStack method startChoiceNode.

public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
    LOG.debug("Enter choice {}", name);
    final ChoiceEffectiveStatement stmt = dataTree.enterChoice(name.getNodeType());
    verify(stmt instanceof ChoiceSchemaNode, "Node %s is not a choice", stmt);
    final ChoiceSchemaNode ret = (ChoiceSchemaNode) stmt;
    schemaStack.push(ret);
    return ret;
}
Also used : ChoiceEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)

Example 4 with ChoiceEffectiveStatement

use of org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement in project yangtools by opendaylight.

the class YT1208Test method testChoiceStatementReuse.

@Test
public void testChoiceStatementReuse() throws Exception {
    final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1208/choice.yang").getModuleStatement(QNameModule.create(XMLNamespace.of("foo")));
    final NotificationEffectiveStatement notif = module.findFirstEffectiveSubstatement(NotificationEffectiveStatement.class).orElseThrow();
    final ChoiceEffectiveStatement grpBar = notif.findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow().findFirstEffectiveSubstatement(ChoiceEffectiveStatement.class).orElseThrow();
    final ChoiceEffectiveStatement contBar = notif.findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow().findFirstEffectiveSubstatement(ChoiceEffectiveStatement.class).orElseThrow();
    assertSame(contBar, grpBar);
}
Also used : ContainerEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement) GroupingEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement) ChoiceEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement) NotificationEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) Test(org.junit.Test)

Example 5 with ChoiceEffectiveStatement

use of org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement in project yangtools by opendaylight.

the class DeclaredStatementsTest method testDeclaredChoice.

@Test
public void testDeclaredChoice() throws ReactorException {
    final StatementStreamSource choiceStmtModule = sourceForResource("/declared-statements-test/choice-declared-test.yang");
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources(choiceStmtModule);
    assertNotNull(schemaContext);
    final Module testModule = schemaContext.findModules("choice-declared-test").iterator().next();
    assertNotNull(testModule);
    final ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "test-choice"));
    assertNotNull(choiceSchemaNode);
    final ChoiceStatement choiceStatement = ((ChoiceEffectiveStatement) choiceSchemaNode).getDeclared();
    final QName name = choiceStatement.argument();
    assertNotNull(name);
    final DefaultStatement defaultStatement = choiceStatement.getDefault().get();
    assertEquals("case-two", defaultStatement.argument());
    assertTrue(choiceStatement.getConfig().isPresent());
    assertTrue(choiceStatement.getMandatory().isPresent());
    final Collection<? extends CaseStatement> caseStatements = choiceStatement.getCases();
    assertNotNull(caseStatements);
    assertEquals(3, caseStatements.size());
    final CaseStatement caseStatement = caseStatements.iterator().next();
    final QName caseStatementName = caseStatement.argument();
    assertNotNull(caseStatementName);
    final WhenStatement caseStatementWhen = caseStatement.getWhenStatement().get();
    final Collection<? extends IfFeatureStatement> caseStatementIfFeatures = caseStatement.getIfFeatures();
    assertNotNull(caseStatementIfFeatures);
    assertEquals(1, caseStatementIfFeatures.size());
    final Collection<? extends DataDefinitionStatement> caseStatementDataDefinitions = caseStatement.getDataDefinitions();
    assertNotNull(caseStatementDataDefinitions);
    assertEquals(1, caseStatementDataDefinitions.size());
    assertTrue(caseStatement.getStatus().isPresent());
    assertTrue(caseStatement.getDescription().isPresent());
    assertTrue(caseStatement.getReference().isPresent());
    final WhenStatement whenStatement = choiceStatement.getWhenStatement().get();
    final Collection<? extends IfFeatureStatement> ifFeatureStatements = choiceStatement.getIfFeatures();
    assertNotNull(ifFeatureStatements);
    assertEquals(1, ifFeatureStatements.size());
    assertTrue(choiceStatement.getStatus().isPresent());
    assertTrue(choiceStatement.getDescription().isPresent());
    assertTrue(choiceStatement.getReference().isPresent());
}
Also used : StatementStreamSource(org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource) QName(org.opendaylight.yangtools.yang.common.QName) CaseStatement(org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement) ChoiceStatement(org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement) WhenStatement(org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement) ChoiceEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) DefaultStatement(org.opendaylight.yangtools.yang.model.api.stmt.DefaultStatement) Test(org.junit.Test)

Aggregations

ChoiceEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement)5 Test (org.junit.Test)2 QName (org.opendaylight.yangtools.yang.common.QName)2 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)2 VerifyException (com.google.common.base.VerifyException)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 AbstractQName (org.opendaylight.yangtools.yang.common.AbstractQName)1 Module (org.opendaylight.yangtools.yang.model.api.Module)1 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)1 CaseEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement)1 CaseStatement (org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement)1 ChoiceStatement (org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement)1 ContainerEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement)1 DefaultStatement (org.opendaylight.yangtools.yang.model.api.stmt.DefaultStatement)1 GroupingEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement)1 ModuleEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement)1 NotificationEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement)1 WhenStatement (org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement)1 StatementStreamSource (org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource)1