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;
}
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);
}
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);
}
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);
}
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());
}
Aggregations