Search in sources :

Example 1 with DeviateKind

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

the class DeviationStmtTest method testDeviationAndDeviate.

@Test
public void testDeviationAndDeviate() throws ReactorException {
    final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild().addSources(FOO_MODULE, FOO_IMP_MODULE, BAR_MODULE, BAR_IMP_MODULE).buildEffective();
    assertNotNull(schemaContext);
    Module testModule = schemaContext.findModule("foo", Revision.of("2016-06-23")).get();
    Collection<? extends Deviation> deviations = testModule.getDeviations();
    assertEquals(4, deviations.size());
    for (Deviation deviation : deviations) {
        final Collection<? extends DeviateDefinition> deviates = deviation.getDeviates();
        final String targetLocalName = Iterables.getLast(deviation.getTargetPath().getNodeIdentifiers()).getLocalName();
        if ("test-leaf".equals(targetLocalName)) {
            assertEquals(Optional.of("test-leaf is not supported"), deviation.getDescription());
            assertEquals(1, deviates.size());
            assertEquals(DeviateKind.NOT_SUPPORTED, deviates.iterator().next().getDeviateType());
        } else if ("test-leaf-2".equals(targetLocalName)) {
            assertEquals(1, deviates.size());
            assertEquals(DeviateKind.ADD, deviates.iterator().next().getDeviateType());
            assertEquals("added-def-val", deviates.iterator().next().getDeviatedDefault());
            assertFalse(deviates.iterator().next().getDeviatedConfig());
            assertTrue(deviates.iterator().next().getDeviatedMandatory());
        } else if ("test-leaf-list".equals(targetLocalName)) {
            assertEquals(3, deviates.size());
            for (DeviateDefinition deviate : deviates) {
                if (DeviateKind.ADD.equals(deviate.getDeviateType())) {
                    assertEquals(12, deviate.getDeviatedMaxElements().intValue());
                } else if (DeviateKind.REPLACE.equals(deviate.getDeviateType())) {
                    assertEquals(5, deviate.getDeviatedMinElements().intValue());
                    assertTrue(deviate.getDeviatedType() instanceof Uint32TypeDefinition);
                } else {
                    assertEquals(2, deviate.getDeviatedMusts().size());
                    assertEquals("minutes", deviate.getDeviatedUnits());
                }
            }
        } else {
            assertEquals(1, deviation.getDeviates().size());
            assertEquals(DeviateKind.DELETE, deviates.iterator().next().getDeviateType());
            assertEquals(2, deviates.iterator().next().getDeviatedUniques().size());
        }
    }
    testModule = schemaContext.findModule("bar", Revision.of("2016-09-22")).get();
    assertNotNull(testModule);
    deviations = testModule.getDeviations();
    assertEquals(7, deviations.size());
    Deviation deviation1 = null;
    Deviation deviation2 = null;
    Deviation deviation3 = null;
    Deviation deviation4 = null;
    Deviation deviation5 = null;
    Deviation deviation6 = null;
    Deviation deviation7 = null;
    for (Deviation deviation : deviations) {
        final Collection<? extends DeviateDefinition> deviates = deviation.getDeviates();
        final String targetLocalName = Iterables.getLast(deviation.getTargetPath().getNodeIdentifiers()).getLocalName();
        if ("bar-container-1".equals(targetLocalName)) {
            deviation1 = deviation;
        }
        if ("bar-container-2".equals(targetLocalName)) {
            DeviateKind deviateKind = deviates.iterator().next().getDeviateType();
            if (deviateKind.equals(DeviateKind.DELETE)) {
                deviation2 = deviation;
            } else if (deviateKind.equals(DeviateKind.ADD)) {
                deviation3 = deviation;
            }
        }
        if ("bar-leaf-1".equals(targetLocalName)) {
            if (Optional.of("desc").equals(deviation.getDescription())) {
                deviation4 = deviation;
            } else {
                deviation5 = deviation;
            }
        }
        if ("bar-leaf-2".equals(targetLocalName)) {
            if (Optional.of("ref").equals(deviation.getReference())) {
                deviation6 = deviation;
            } else {
                deviation7 = deviation;
            }
        }
    }
    assertEquals(0, deviation1.getUnknownSchemaNodes().size());
    assertEquals(1, deviation1.asEffectiveStatement().getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
    assertTrue(deviation1.equals(deviation1));
    assertFalse(deviation1.equals(null));
    assertFalse(deviation1.equals("str"));
    DeviateDefinition deviate = deviation1.getDeviates().iterator().next();
    assertTrue(deviate.equals(deviate));
    assertFalse(deviate.equals(null));
    assertFalse(deviate.equals("str"));
    assertFalse(deviation1.equals(deviation2));
    assertFalse(deviation2.equals(deviation3));
    assertFalse(deviation4.equals(deviation5));
    assertFalse(deviation6.equals(deviation7));
}
Also used : DeviateDefinition(org.opendaylight.yangtools.yang.model.api.DeviateDefinition) DeviateKind(org.opendaylight.yangtools.yang.model.api.DeviateKind) Uint32TypeDefinition(org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) Deviation(org.opendaylight.yangtools.yang.model.api.Deviation) Test(org.junit.Test)

Example 2 with DeviateKind

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

the class AbstractDeviateStatementSupport method onFullDefinitionDeclared.

@Override
public final void onFullDefinitionDeclared(final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx) {
    final DeviateKind deviateKind = deviateStmtCtx.argument();
    getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
    final SchemaNodeIdentifier deviationTarget = (SchemaNodeIdentifier) deviateStmtCtx.coerceParentContext().argument();
    if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
        return;
    }
    final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
    final Prerequisite<StmtContext<DeviateKind, DeviateStatement, DeviateEffectiveStatement>> sourceCtxPrerequisite = deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL);
    final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> targetCtxPrerequisite = deviateAction.mutatesEffectiveCtxPath(deviateStmtCtx.getRoot(), SchemaTreeNamespace.class, deviationTarget.getNodeIdentifiers());
    deviateAction.apply(new InferenceAction() {

        @Override
        public void apply(final InferenceContext ctx) {
            if (!deviateStmtCtx.isSupportedToBuildEffective()) {
                // We are not building effective model, hence we should not be performing any effects
                return;
            }
            final var sourceNodeStmtCtx = sourceCtxPrerequisite.resolve(ctx);
            final var targetNodeStmtCtx = targetCtxPrerequisite.resolve(ctx);
            switch(deviateKind) {
                case NOT_SUPPORTED:
                    // FIXME: this can be short-circuited without an inference action
                    targetNodeStmtCtx.setUnsupported();
                    break;
                case ADD:
                    performDeviateAdd(sourceNodeStmtCtx, targetNodeStmtCtx);
                    break;
                case REPLACE:
                    performDeviateReplace(sourceNodeStmtCtx, targetNodeStmtCtx);
                    break;
                case DELETE:
                    performDeviateDelete(sourceNodeStmtCtx, targetNodeStmtCtx);
                    break;
                default:
                    throw new IllegalStateException("Unsupported deviate " + deviateKind);
            }
        }

        @Override
        public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
            throw new InferenceException(deviateStmtCtx.coerceParentContext(), "Deviation target '%s' not found.", deviationTarget);
        }

        @Override
        public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
            if (targetCtxPrerequisite.equals(unavail)) {
                deviateStmtCtx.setUnsupported();
            } else {
                prerequisiteFailed(List.of(unavail));
            }
        }
    });
}
Also used : InferenceContext(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext) InferenceException(org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException) SchemaNodeIdentifier(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier) StmtContext(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext) InferenceAction(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction) Mutable(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable) DeviateKind(org.opendaylight.yangtools.yang.model.api.DeviateKind) ModelActionBuilder(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder)

Aggregations

DeviateKind (org.opendaylight.yangtools.yang.model.api.DeviateKind)2 Test (org.junit.Test)1 DeviateDefinition (org.opendaylight.yangtools.yang.model.api.DeviateDefinition)1 Deviation (org.opendaylight.yangtools.yang.model.api.Deviation)1 Module (org.opendaylight.yangtools.yang.model.api.Module)1 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)1 SchemaNodeIdentifier (org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier)1 Uint32TypeDefinition (org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition)1 InferenceException (org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException)1 ModelActionBuilder (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder)1 InferenceAction (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction)1 InferenceContext (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext)1 StmtContext (org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext)1 Mutable (org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable)1