Search in sources :

Example 1 with SchemaNode

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

the class RuntimeBeanEntry method extractSubtree.

/**
 * Get direct descendants of this subtree, together with attributes defined
 * in subtree.
 */
private static AttributesRpcsAndRuntimeBeans extractSubtree(final String packageName, final DataNodeContainer subtree, final TypeProviderWrapper typeProviderWrapper, final Module currentModule, final SchemaContext ctx) {
    Multimap<QName, RpcDefinition> identitiesToRpcs = getIdentitiesToRpcs(ctx);
    List<AttributeIfc> attributes = Lists.newArrayList();
    List<RuntimeBeanEntry> runtimeBeanEntries = new ArrayList<>();
    for (DataSchemaNode child : subtree.getChildNodes()) {
        // runtime beans
        if (child instanceof LeafSchemaNode) {
            // just save the attribute
            LeafSchemaNode leaf = (LeafSchemaNode) child;
            attributes.add(new JavaAttribute(leaf, typeProviderWrapper));
        } else if (child instanceof ContainerSchemaNode) {
            ContainerSchemaNode container = (ContainerSchemaNode) child;
            // this can be either TO or hierarchical RB
            TOAttribute toAttribute = TOAttribute.create(container, typeProviderWrapper, packageName);
            attributes.add(toAttribute);
        } else if (child instanceof ListSchemaNode) {
            if (isInnerStateBean(child)) {
                ListSchemaNode listSchemaNode = (ListSchemaNode) child;
                RuntimeBeanEntry hierarchicalChild = createHierarchical(packageName, listSchemaNode, typeProviderWrapper, currentModule, ctx);
                runtimeBeanEntries.add(hierarchicalChild);
            } else /* ordinary list attribute */
            {
                ListAttribute listAttribute = ListAttribute.create((ListSchemaNode) child, typeProviderWrapper, packageName);
                attributes.add(listAttribute);
            }
        } else if (child instanceof LeafListSchemaNode) {
            ListAttribute listAttribute = ListAttribute.create((LeafListSchemaNode) child, typeProviderWrapper);
            attributes.add(listAttribute);
        } else {
            throw new IllegalStateException("Unexpected running-data node " + child);
        }
    }
    Set<Rpc> rpcs = new HashSet<>();
    SchemaNode subtreeSchemaNode = (SchemaNode) subtree;
    for (UnknownSchemaNode unknownSchemaNode : subtreeSchemaNode.getUnknownSchemaNodes()) {
        if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME.equals(unknownSchemaNode.getNodeType())) {
            String localIdentityName = unknownSchemaNode.getNodeParameter();
            QName identityQName = unknownSchemaNode.isAddedByUses() ? findQNameFromGrouping(subtree, ctx, unknownSchemaNode, localIdentityName) : QName.create(currentModule.getNamespace(), currentModule.getRevision(), localIdentityName);
            // convert RpcDefinition to Rpc
            for (RpcDefinition rpcDefinition : identitiesToRpcs.get(identityQName)) {
                String name = TypeProviderWrapper.findJavaParameter(rpcDefinition);
                AttributeIfc returnType;
                if (rpcDefinition.getOutput() == null || rpcDefinition.getOutput().getChildNodes().isEmpty()) {
                    returnType = VoidAttribute.getInstance();
                } else if (rpcDefinition.getOutput().getChildNodes().size() == 1) {
                    DataSchemaNode returnDSN = rpcDefinition.getOutput().getChildNodes().iterator().next();
                    returnType = getReturnTypeAttribute(returnDSN, typeProviderWrapper, packageName);
                } else {
                    throw new IllegalArgumentException("More than one child node in rpc output is not supported. " + "Error occured in " + rpcDefinition);
                }
                List<JavaAttribute> parameters = new ArrayList<>();
                for (DataSchemaNode childNode : sortAttributes(rpcDefinition.getInput().getChildNodes())) {
                    if (childNode.isAddedByUses() == false) {
                        // skip
                        // refined
                        // context-instance
                        checkArgument(childNode instanceof LeafSchemaNode, "Unexpected type of rpc input type. " + "Currently only leafs and empty output nodes are supported, got " + childNode);
                        JavaAttribute javaAttribute = new JavaAttribute((LeafSchemaNode) childNode, typeProviderWrapper);
                        parameters.add(javaAttribute);
                    }
                }
                Rpc newRpc = new Rpc(returnType, name, rpcDefinition.getQName().getLocalName(), parameters);
                rpcs.add(newRpc);
            }
        }
    }
    return new AttributesRpcsAndRuntimeBeans(runtimeBeanEntries, attributes, rpcs);
}
Also used : RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ArrayList(java.util.ArrayList) TOAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) HashSet(java.util.HashSet) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)

Example 2 with SchemaNode

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

the class RuntimeBeanEntry method getIdentitiesToRpcs.

private static Multimap<QName, /* of identity */
RpcDefinition> getIdentitiesToRpcs(final SchemaContext schemaCtx) {
    Multimap<QName, RpcDefinition> result = HashMultimap.create();
    for (Module currentModule : schemaCtx.getModules()) {
        // Find all identities in current module for later identity->rpc mapping
        Set<QName> allIdentitiesInModule = Sets.newHashSet(Collections2.transform(currentModule.getIdentities(), SchemaNode::getQName));
        for (RpcDefinition rpc : currentModule.getRpcs()) {
            ContainerSchemaNode input = rpc.getInput();
            if (input != null) {
                for (UsesNode uses : input.getUses()) {
                    // Check if the rpc is config rpc by looking for input argument rpc-context-ref
                    Iterator<QName> pathFromRoot = uses.getGroupingPath().getPathFromRoot().iterator();
                    if (!pathFromRoot.hasNext() || !pathFromRoot.next().equals(ConfigConstants.RPC_CONTEXT_REF_GROUPING_QNAME)) {
                        continue;
                    }
                    for (SchemaNode refinedNode : uses.getRefines().values()) {
                        for (UnknownSchemaNode unknownSchemaNode : refinedNode.getUnknownSchemaNodes()) {
                            if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME.equals(unknownSchemaNode.getNodeType())) {
                                String localIdentityName = unknownSchemaNode.getNodeParameter();
                                QName identityQName = QName.create(currentModule.getNamespace(), currentModule.getRevision(), localIdentityName);
                                Preconditions.checkArgument(allIdentitiesInModule.contains(identityQName), "Identity referenced by rpc not found. Identity: %s, rpc: %s", localIdentityName, rpc);
                                result.put(identityQName, rpc);
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) QName(org.opendaylight.yangtools.yang.common.QName) Module(org.opendaylight.yangtools.yang.model.api.Module) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) UsesNode(org.opendaylight.yangtools.yang.model.api.UsesNode)

Example 3 with SchemaNode

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

the class ConfigLoaderImpl method parseDefaultConfigFile.

private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename) throws IOException, XMLStreamException {
    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
    final File newFile = new File(this.path, filename);
    FileChannel channel = new RandomAccessFile(newFile, READ).getChannel();
    FileLock lock = null;
    final Stopwatch stopwatch = Stopwatch.createStarted();
    while (lock == null || stopwatch.elapsed(TimeUnit.SECONDS) > TIMEOUT_SECONDS) {
        try {
            lock = channel.tryLock();
        } catch (final IllegalStateException e) {
        // Ignore
        }
        if (lock == null) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                LOG.warn("Failed to lock xml", e);
            }
        }
    }
    try (InputStream resourceAsStream = new FileInputStream(newFile)) {
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
        final SchemaNode schemaNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, config.getSchemaPath());
        try (XmlParserStream xmlParser = XmlParserStream.create(streamWriter, this.schemaContext, schemaNode)) {
            xmlParser.parse(reader);
        } catch (final URISyntaxException | XMLStreamException | IOException | ParserConfigurationException | SAXException e) {
            LOG.warn("Failed to parse xml", e);
        } finally {
            reader.close();
            channel.close();
        }
    }
    return result.getResult();
}
Also used : XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) FileChannel(java.nio.channels.FileChannel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Stopwatch(com.google.common.base.Stopwatch) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) RandomAccessFile(java.io.RandomAccessFile) XMLStreamException(javax.xml.stream.XMLStreamException) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) FileLock(java.nio.channels.FileLock) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 4 with SchemaNode

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

the class ModuleMXBeanEntryBuilder method extractDependency.

private static Optional<? extends AbstractDependencyAttribute> extractDependency(final DataNodeContainer dataNodeContainer, final DataSchemaNode attrNode, final Module currentModule, final Map<QName, ServiceInterfaceEntry> qNamesToSIEs, final SchemaContext schemaContext) {
    if (isDependencyContainer(dataNodeContainer)) {
        // reference
        UsesNode usesNode = dataNodeContainer.getUses().iterator().next();
        for (SchemaNode refineNode : usesNode.getRefines().values()) {
            // this will ignore name nodes, since they are not needed here
            if (TYPE.equals(refineNode.getQName().getLocalName())) {
                checkState(refineNode.getUnknownSchemaNodes().size() == 1, "Unexpected unknown schema node size of " + refineNode);
                UnknownSchemaNode requiredIdentity = refineNode.getUnknownSchemaNodes().iterator().next();
                checkState(ConfigConstants.REQUIRED_IDENTITY_EXTENSION_QNAME.equals(requiredIdentity.getNodeType()), "Unexpected language extension " + requiredIdentity);
                String prefixAndIdentityLocalName = requiredIdentity.getNodeParameter();
                // import should point to a module
                ServiceInterfaceEntry serviceInterfaceEntry = findSIE(prefixAndIdentityLocalName, currentModule, qNamesToSIEs, schemaContext);
                LeafSchemaNode refine = (LeafSchemaNode) usesNode.getRefines().values().iterator().next();
                boolean mandatory = refine.isMandatory();
                AbstractDependencyAttribute reference;
                if (dataNodeContainer instanceof ContainerSchemaNode) {
                    reference = new DependencyAttribute(attrNode, serviceInterfaceEntry, mandatory, attrNode.getDescription().orElse(null));
                } else {
                    reference = new ListDependenciesAttribute(attrNode, serviceInterfaceEntry, mandatory, attrNode.getDescription().orElse(null));
                }
                return Optional.of(reference);
            }
        }
    }
    return Optional.absent();
}
Also used : CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) DependencyAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute) AbstractDependencyAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) AbstractDependencyAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) ListDependenciesAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) UsesNode(org.opendaylight.yangtools.yang.model.api.UsesNode)

Example 5 with SchemaNode

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

the class RuntimeBeanEntry method findQNameFromGrouping.

/**
 * Find "proper" qname of unknown node in case it comes from a grouping
 */
private static QName findQNameFromGrouping(final DataNodeContainer subtree, final SchemaContext ctx, final UnknownSchemaNode unknownSchemaNode, final String localIdentityName) {
    QName identityQName = null;
    for (UsesNode usesNode : subtree.getUses()) {
        SchemaNode dataChildByName = SchemaContextUtil.findDataSchemaNode(ctx, usesNode.getGroupingPath());
        Module m = SchemaContextUtil.findParentModule(ctx, dataChildByName);
        List<UnknownSchemaNode> unknownSchemaNodes = dataChildByName.getUnknownSchemaNodes();
        if (Collections2.transform(unknownSchemaNodes, UNKNOWN_NODE_TO_STRING).contains(UNKNOWN_NODE_TO_STRING.apply(unknownSchemaNode))) {
            identityQName = QName.create(dataChildByName.getQName(), localIdentityName);
        }
    }
    return identityQName;
}
Also used : LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) Module(org.opendaylight.yangtools.yang.model.api.Module) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) UsesNode(org.opendaylight.yangtools.yang.model.api.UsesNode)

Aggregations

SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)5 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)4 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)4 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)4 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)4 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)4 UnknownSchemaNode (org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode)4 QName (org.opendaylight.yangtools.yang.common.QName)3 UsesNode (org.opendaylight.yangtools.yang.model.api.UsesNode)3 Module (org.opendaylight.yangtools.yang.model.api.Module)2 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)2 Stopwatch (com.google.common.base.Stopwatch)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 URISyntaxException (java.net.URISyntaxException)1 FileChannel (java.nio.channels.FileChannel)1 FileLock (java.nio.channels.FileLock)1