Search in sources :

Example 1 with AnydataSchemaNode

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;
}
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 AnydataSchemaNode

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);
}
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 AnydataSchemaNode

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");
}
Also used : AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) Test(org.junit.Test)

Example 4 with AnydataSchemaNode

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)

Example 5 with AnydataSchemaNode

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();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)

Aggregations

AnydataSchemaNode (org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)13 AnyxmlSchemaNode (org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)10 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)7 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)7 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)7 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)6 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)5 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)5 Test (org.junit.Test)4 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)4 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)3 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 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1