Search in sources :

Example 1 with AnyxmlSchemaNode

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

the class DataObjectStreamerGenerator method createStream.

private ChildStream createStream(final CodecClassLoader loader, final ImmutableMap<String, Type> props, final DataSchemaNode childSchema, final Method getter) {
    if (childSchema instanceof LeafSchemaNode) {
        return qnameChildStream(STREAM_LEAF, getter, childSchema);
    }
    if (childSchema instanceof ContainerSchemaNode) {
        return containerChildStream(getter);
    }
    if (childSchema instanceof ListSchemaNode) {
        final String getterName = getter.getName();
        final Type childType = props.get(getterName);
        verify(childType instanceof ParameterizedType, "Unexpected type %s for %s", childType, getterName);
        final Type[] params = ((ParameterizedType) childType).getActualTypeArguments();
        final ListSchemaNode listSchema = (ListSchemaNode) childSchema;
        final Class<?> valueClass;
        if (!listSchema.isUserOrdered() && !listSchema.getKeyDefinition().isEmpty()) {
            loadTypeClass(loader, params[0]);
            valueClass = loadTypeClass(loader, params[1]);
        } else {
            valueClass = loadTypeClass(loader, params[0]);
        }
        return listChildStream(getter, valueClass.asSubclass(DataObject.class), listSchema);
    }
    if (childSchema instanceof ChoiceSchemaNode) {
        return choiceChildStream(getter);
    }
    if (childSchema instanceof AnydataSchemaNode) {
        return qnameChildStream(STREAM_ANYDATA, getter, childSchema);
    }
    if (childSchema instanceof AnyxmlSchemaNode) {
        return qnameChildStream(STREAM_ANYXML, getter, childSchema);
    }
    if (childSchema instanceof LeafListSchemaNode) {
        return qnameChildStream(((LeafListSchemaNode) childSchema).isUserOrdered() ? STREAM_ORDERED_LEAF_LIST : STREAM_LEAF_LIST, getter, childSchema);
    }
    LOG.debug("Ignoring {} due to unhandled schema {}", getter, childSchema);
    return null;
}
Also used : LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) ParameterizedType(org.opendaylight.mdsal.binding.model.api.ParameterizedType) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) ParameterizedType(org.opendaylight.mdsal.binding.model.api.ParameterizedType) Type(org.opendaylight.mdsal.binding.model.api.Type) InstrumentedType(net.bytebuddy.dynamic.scaffold.InstrumentedType) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)

Example 2 with AnyxmlSchemaNode

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

the class BindingCodecContext method getLeafNodesUsingReflection.

private ImmutableMap<Method, ValueNodeCodecContext> getLeafNodesUsingReflection(final Class<?> parentClass, final Map<String, DataSchemaNode> getterToLeafSchema) {
    final Map<Method, ValueNodeCodecContext> leaves = new HashMap<>();
    for (final Method method : parentClass.getMethods()) {
        // Only consider non-bridge methods with no arguments
        if (method.getParameterCount() == 0 && !method.isBridge()) {
            final DataSchemaNode schema = getterToLeafSchema.get(method.getName());
            final ValueNodeCodecContext valueNode;
            if (schema instanceof LeafSchemaNode) {
                final LeafSchemaNode leafSchema = (LeafSchemaNode) schema;
                // FIXME: MDSAL-670: this is not right as we need to find a concrete type, but this may return
                // Object.class
                final Class<?> valueType = method.getReturnType();
                final IllegalArgumentCodec<Object, Object> codec = getCodec(valueType, leafSchema.getType());
                valueNode = LeafNodeCodecContext.of(leafSchema, codec, method.getName(), valueType, context.getEffectiveModelContext());
            } else if (schema instanceof LeafListSchemaNode) {
                final Optional<Type> optType = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
                checkState(optType.isPresent(), "Failed to find return type for %s", method);
                final Class<?> valueType;
                final Type genericType = optType.get();
                if (genericType instanceof Class<?>) {
                    valueType = (Class<?>) genericType;
                } else if (genericType instanceof ParameterizedType) {
                    valueType = (Class<?>) ((ParameterizedType) genericType).getRawType();
                } else if (genericType instanceof WildcardType) {
                    // FIXME: MDSAL-670: this is not right as we need to find a concrete type
                    valueType = Object.class;
                } else {
                    throw new IllegalStateException("Unexpected return type " + genericType);
                }
                final LeafListSchemaNode leafListSchema = (LeafListSchemaNode) schema;
                final IllegalArgumentCodec<Object, Object> codec = getCodec(valueType, leafListSchema.getType());
                valueNode = new LeafSetNodeCodecContext(leafListSchema, codec, method.getName());
            } else if (schema instanceof AnyxmlSchemaNode) {
                valueNode = new OpaqueNodeCodecContext.Anyxml<>((AnyxmlSchemaNode) schema, method.getName(), opaqueReturnType(method), loader);
            } else if (schema instanceof AnydataSchemaNode) {
                valueNode = new OpaqueNodeCodecContext.Anydata<>((AnydataSchemaNode) schema, method.getName(), opaqueReturnType(method), loader);
            } else {
                verify(schema == null, "Unhandled schema %s for method %s", schema, method);
                // We do not have schema for leaf, so we will ignore it (e.g. getClass).
                continue;
            }
            leaves.put(method, valueNode);
        }
    }
    return ImmutableMap.copyOf(leaves);
}
Also used : LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) Optional(java.util.Optional) HashMap(java.util.HashMap) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) Method(java.lang.reflect.Method) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) IllegalArgumentCodec(org.opendaylight.yangtools.concepts.IllegalArgumentCodec) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) ParameterizedType(java.lang.reflect.ParameterizedType) ListRuntimeType(org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) OpaqueObject(org.opendaylight.yangtools.yang.binding.OpaqueObject) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)

Example 3 with AnyxmlSchemaNode

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

the class NormalizedNodeStreamWriterStack method startAnyxmlNode.

public void startAnyxmlNode(final NodeIdentifier name) {
    final SchemaNode schema = enterDataTree(name);
    checkArgument(schema instanceof AnyxmlSchemaNode, "Node %s is not anyxml", schema);
    schemaStack.push(schema);
}
Also used : AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)

Example 4 with AnyxmlSchemaNode

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

the class AugmentProcessTest method readAndParseYangFileTest.

@Test
public void readAndParseYangFileTest() throws ReactorException {
    final SchemaContext root = RFC7950Reactors.defaultReactor().newBuild().addSources(AUGMENTED, ROOT).buildEffective();
    assertNotNull(root);
    final Module augmentedModule = root.findModules("augmented").iterator().next();
    assertNotNull(augmentedModule);
    final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
    final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
    final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
    assertEquals(3, targetContNode.getChildNodes().size());
    final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
    final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
    final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
    final AnyxmlSchemaNode axmlNode = (AnyxmlSchemaNode) contAdded2Node.getDataChildByName(axml);
    final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
    final AnyxmlSchemaNode axmlGrpNode = (AnyxmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
    final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
    final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
    final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
    final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
    final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
}
Also used : ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) Test(org.junit.Test)

Example 5 with AnyxmlSchemaNode

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

the class DeclaredStatementsTest method testDeclaredAnyXml.

@Test
public void testDeclaredAnyXml() throws ReactorException {
    final StatementStreamSource anyxmlStmtModule = sourceForResource("/declared-statements-test/anyxml-declared-test.yang");
    final SchemaContext schemaContext = StmtTestUtils.parseYangSources(anyxmlStmtModule);
    assertNotNull(schemaContext);
    final Module testModule = schemaContext.findModules("anyxml-declared-test").iterator().next();
    assertNotNull(testModule);
    final AnyxmlSchemaNode anyxmlSchemaNode = (AnyxmlSchemaNode) testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "foobar"));
    assertNotNull(anyxmlSchemaNode);
    final AnyxmlStatement anyxmlStatement = ((AnyxmlEffectiveStatement) anyxmlSchemaNode).getDeclared();
    final QName name = anyxmlStatement.argument();
    assertNotNull(name);
    final WhenStatement whenStatement = anyxmlStatement.getWhenStatement().get();
    assertNotNull(whenStatement.argument());
    final DescriptionStatement whenStatementDescription = whenStatement.getDescription().get();
    assertTrue(whenStatement.getReference().isPresent());
    final Collection<? extends IfFeatureStatement> ifFeatureStatements = anyxmlStatement.getIfFeatures();
    assertNotNull(ifFeatureStatements);
    assertEquals(1, ifFeatureStatements.size());
    final Predicate<Set<QName>> ifFeaturePredicate = ifFeatureStatements.iterator().next().getIfFeaturePredicate();
    assertNotNull(ifFeaturePredicate);
    final Collection<? extends MustStatement> mustStatements = anyxmlStatement.getMustStatements();
    assertNotNull(mustStatements);
    assertEquals(1, mustStatements.size());
    final MustStatement mustStatement = mustStatements.iterator().next();
    assertNotNull(mustStatement.argument());
    assertTrue(mustStatement.getErrorAppTagStatement().isPresent());
    assertTrue(mustStatement.getErrorMessageStatement().isPresent());
    assertTrue(mustStatement.getDescription().isPresent());
    assertTrue(mustStatement.getReference().isPresent());
    final ConfigStatement configStatement = anyxmlStatement.getConfig().get();
    assertFalse(configStatement.getValue());
    final StatusStatement statusStatement = anyxmlStatement.getStatus().get();
    final Status status = statusStatement.argument();
    assertNotNull(status);
    final DescriptionStatement descriptionStatement = anyxmlStatement.getDescription().get();
    assertEquals("anyxml description", descriptionStatement.argument());
    final ReferenceStatement referenceStatement = anyxmlStatement.getReference().get();
    assertEquals("anyxml reference", referenceStatement.argument());
    assertTrue(anyxmlStatement.getMandatory().isPresent());
}
Also used : MustStatement(org.opendaylight.yangtools.yang.model.api.stmt.MustStatement) Status(org.opendaylight.yangtools.yang.model.api.Status) ConfigStatement(org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement) Set(java.util.Set) StatementStreamSource(org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource) QName(org.opendaylight.yangtools.yang.common.QName) ReferenceStatement(org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement) StatusStatement(org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) WhenStatement(org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement) DescriptionStatement(org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement) AnyxmlEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) AnyxmlStatement(org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlStatement) Test(org.junit.Test)

Aggregations

AnyxmlSchemaNode (org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)22 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)15 Test (org.junit.Test)13 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)13 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)12 AnydataSchemaNode (org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)10 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)10 Module (org.opendaylight.yangtools.yang.model.api.Module)10 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)8 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)7 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)6 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)5 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)4 GroupingDefinition (org.opendaylight.yangtools.yang.model.api.GroupingDefinition)4 DataNodeContainer (org.opendaylight.yangtools.yang.model.api.DataNodeContainer)3 TypedDataSchemaNode (org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DataObject (org.opendaylight.yangtools.yang.binding.DataObject)2 QName (org.opendaylight.yangtools.yang.common.QName)2 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)2