use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode 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.AnydataSchemaNode 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.AnydataSchemaNode in project yangtools by opendaylight.
the class Bug6883Test method test.
@Test
public void test() throws Exception {
final AnydataSchemaNode topAnyData = assertAnyData("top");
assertEquals(Status.DEPRECATED, topAnyData.getStatus());
assertEquals(Optional.of("top anydata"), topAnyData.getDescription());
assertAnyData("root", "root-anydata");
assertAnyData("root", "aug-anydata");
assertAnyData("root", "grp-anydata");
assertAnyData("my-list", "list-anydata");
assertAnyData("sub-data");
assertAnyData("my-rpc", "input", "input-anydata");
assertAnyData("my-rpc", "output", "output-anydata");
assertAnyData("my-notification", "notification-anydata");
assertAnyData("my-choice", "one", "case-anydata");
assertAnyData("my-choice", "case-shorthand-anydata", "case-shorthand-anydata");
}
use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project netconf by opendaylight.
the class DefinitionGenerator method processChildren.
/**
* Processes the nodes.
*/
private ObjectNode processChildren(final ObjectNode parentNode, final Collection<? extends DataSchemaNode> nodes, final String parentName, final ObjectNode definitions, final DefinitionNames definitionNames, final boolean isConfig, final SchemaInferenceStack stack, final OAversion oaversion) throws IOException {
final ObjectNode properties = JsonNodeFactory.instance.objectNode();
final ArrayNode required = JsonNodeFactory.instance.arrayNode();
for (final DataSchemaNode node : nodes) {
stack.enterSchemaTree(node.getQName());
if (!isConfig || node.isConfiguration()) {
/*
Add module name prefix to property name, when needed, when ServiceNow can process colons,
use RestDocGenUtil#resolveNodesName for creating property name
*/
final String propertyName = node.getQName().getLocalName();
final ObjectNode property;
if (node instanceof LeafSchemaNode) {
processLeafNode((LeafSchemaNode) node, propertyName, properties, required, stack, definitions, definitionNames, oaversion);
} else if (node instanceof AnyxmlSchemaNode) {
processAnyXMLNode((AnyxmlSchemaNode) node, propertyName, properties, required);
} else if (node instanceof AnydataSchemaNode) {
processAnydataNode((AnydataSchemaNode) node, propertyName, properties, required);
} else {
if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
property = processDataNodeContainer((DataNodeContainer) node, parentName, definitions, definitionNames, isConfig, stack, oaversion);
if (!isConfig) {
processActionNodeContainer(node, parentName, definitions, definitionNames, stack, oaversion);
}
} else if (node instanceof LeafListSchemaNode) {
property = processLeafListNode((LeafListSchemaNode) node, stack, definitions, definitionNames, oaversion);
} else if (node instanceof ChoiceSchemaNode) {
for (final CaseSchemaNode variant : ((ChoiceSchemaNode) node).getCases()) {
stack.enterSchemaTree(variant.getQName());
processChoiceNode(variant.getChildNodes(), parentName, definitions, definitionNames, isConfig, stack, properties, oaversion);
stack.exit();
}
stack.exit();
// FIXME dangerous statement here! Try to rework without continue.
continue;
} else {
throw new IllegalArgumentException("Unknown DataSchemaNode type: " + node.getClass());
}
properties.set(propertyName, property);
}
}
stack.exit();
}
parentNode.set(PROPERTIES_KEY, properties);
setRequiredIfNotEmpty(parentNode, required);
return properties;
}
use of org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode in project netconf by opendaylight.
the class DefinitionGenerator method processChoiceNode.
private void processChoiceNode(final Iterable<? extends DataSchemaNode> nodes, final String parentName, final ObjectNode definitions, final DefinitionNames definitionNames, final boolean isConfig, final SchemaInferenceStack stack, final ObjectNode properties, final OAversion oaversion) throws IOException {
for (final DataSchemaNode node : nodes) {
stack.enterSchemaTree(node.getQName());
/*
Add module name prefix to property name, when needed, when ServiceNow can process colons,
use RestDocGenUtil#resolveNodesName for creating property name
*/
final String name = node.getQName().getLocalName();
final ObjectNode property;
/*
Ignore mandatoriness(passing unreferenced arrayNode to process...Node), because choice produces multiple
properties
*/
if (node instanceof LeafSchemaNode) {
processLeafNode((LeafSchemaNode) node, name, properties, JsonNodeFactory.instance.arrayNode(), stack, definitions, definitionNames, oaversion);
} else if (node instanceof AnyxmlSchemaNode) {
processAnyXMLNode((AnyxmlSchemaNode) node, name, properties, JsonNodeFactory.instance.arrayNode());
} else if (node instanceof AnydataSchemaNode) {
processAnydataNode((AnydataSchemaNode) node, name, properties, JsonNodeFactory.instance.arrayNode());
} else {
if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
property = processDataNodeContainer((DataNodeContainer) node, parentName, definitions, definitionNames, isConfig, stack, oaversion);
if (!isConfig) {
processActionNodeContainer(node, parentName, definitions, definitionNames, stack, oaversion);
}
} else if (node instanceof LeafListSchemaNode) {
property = processLeafListNode((LeafListSchemaNode) node, stack, definitions, definitionNames, oaversion);
} else if (node instanceof ChoiceSchemaNode) {
for (final CaseSchemaNode variant : ((ChoiceSchemaNode) node).getCases()) {
processChoiceNode(variant.getChildNodes(), parentName, definitions, definitionNames, isConfig, stack, properties, oaversion);
}
continue;
} else {
throw new IllegalArgumentException("Unknown DataSchemaNode type: " + node.getClass());
}
properties.set(name, property);
}
stack.exit();
}
}
Aggregations