Search in sources :

Example 6 with DataSchemaNode

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

the class DataStoreAppConfigDefaultXMLReader method createDefaultInstance.

@SuppressWarnings("unchecked")
public T createDefaultInstance(final FallbackConfigProvider fallback) throws ConfigXMLReaderException, URISyntaxException, ParserConfigurationException, XMLStreamException, SAXException, IOException {
    YangInstanceIdentifier yangPath = bindingSerializer.toYangInstanceIdentifier(bindingContext.appConfigPath);
    LOG.debug("{}: Creating app config instance from path {}, Qname: {}", logName, yangPath, bindingContext.bindingQName);
    checkNotNull(schemaService, "%s: Could not obtain the SchemaService OSGi service", logName);
    SchemaContext schemaContext = schemaService.getGlobalContext();
    Module module = schemaContext.findModule(bindingContext.bindingQName.getModule()).orElse(null);
    checkNotNull(module, "%s: Could not obtain the module schema for namespace %s, revision %s", logName, bindingContext.bindingQName.getNamespace(), bindingContext.bindingQName.getRevision());
    DataSchemaNode dataSchema = module.getDataChildByName(bindingContext.bindingQName);
    checkNotNull(dataSchema, "%s: Could not obtain the schema for %s", logName, bindingContext.bindingQName);
    checkCondition(bindingContext.schemaType.isAssignableFrom(dataSchema.getClass()), "%s: Expected schema type %s for %s but actual type is %s", logName, bindingContext.schemaType, bindingContext.bindingQName, dataSchema.getClass());
    NormalizedNode<?, ?> dataNode = parsePossibleDefaultAppConfigXMLFile(schemaContext, dataSchema);
    if (dataNode == null) {
        dataNode = fallback.get(schemaService.getGlobalContext(), dataSchema);
    }
    DataObject appConfig = bindingSerializer.fromNormalizedNode(yangPath, dataNode).getValue();
    // This shouldn't happen but need to handle it in case...
    checkNotNull(appConfig, "%s: Could not create instance for app config binding %s", logName, bindingContext.appConfigBindingClass);
    return (T) appConfig;
}
Also used : DataObject(org.opendaylight.yangtools.yang.binding.DataObject) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 7 with DataSchemaNode

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

the class ModuleMXBeanEntryTest method testThreadsJava.

@Test
public void testThreadsJava() {
    final Map<String, ModuleMXBeanEntry> /* identity local name */
    namesToMBEs = loadThreadsJava();
    {
        // check threadpool-dynamic
        final ModuleMXBeanEntry dynamicThreadPool = namesToMBEs.get(THREADPOOL_DYNAMIC_MXB_NAME);
        final Map<String, AttributeIfc> attributes = dynamicThreadPool.getAttributes();
        // core-size, keepalive, maximum-size
        // threadfactory
        final Set<String> longAttribs = Sets.newHashSet("core-size", "maximum-size");
        for (final String longAttrib : longAttribs) {
            final TypedAttribute attribute = (TypedAttribute) attributes.get(longAttrib);
            assertThat("Failed to check " + longAttrib, attribute.getType(), is((Type) Types.typeForClass(Long.class)));
        }
        // check dependency on thread factory
        final QName threadfactoryQName = QName.create(THREADS_NAMESPACE, THREADS_REVISION_DATE, "threadfactory");
        final ServiceInterfaceEntry threadFactorySIEntry = this.modulesToSIEs.get(threadfactoryQName);
        assertNotNull(threadFactorySIEntry);
        final boolean expectedMandatory = true;
        final TypedAttribute actualThreadFactory = (TypedAttribute) attributes.get("threadfactory");
        final DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
        doReturn(Collections.emptyList()).when(mockedDataSchemaNode).getUnknownSchemaNodes();
        doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
        final AttributeIfc expectedDependencyAttribute = new DependencyAttribute(mockedDataSchemaNode, threadFactorySIEntry, expectedMandatory, "threadfactory description");
        assertThat(actualThreadFactory, is(expectedDependencyAttribute));
        assertThat(dynamicThreadPool.getFullyQualifiedName("DynamicThreadPoolModuleMXBean"), is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
        assertThat(dynamicThreadPool.getNullableDescription(), is("threadpool-dynamic description"));
        assertThat(dynamicThreadPool.getYangModuleName(), is("config-threads-java"));
        assertThat(dynamicThreadPool.getYangModuleLocalname(), is(THREADPOOL_DYNAMIC_MXB_NAME));
        // check root runtime bean
        final Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool.getRuntimeBeans();
        assertThat(runtimeBeans.size(), is(1));
        final RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans, THREADPOOL_DYNAMIC_MXB_NAME);
        assertThat(rootRB.isRoot(), is(true));
        assertThat(rootRB.getAttributes().size(), is(1));
        final JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes().iterator().next();
        assertThat(attribute.getAttributeYangName(), is("created-sessions"));
        assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
        assertThat(attribute.getType().getFullyQualifiedName(), is(Long.class.getName()));
    }
    {
        // check threadfactory-naming
        final ModuleMXBeanEntry threadFactoryNaming = namesToMBEs.get(THREADFACTORY_NAMING_MXB_NAME);
        final Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming.getRuntimeBeans();
        assertThat(runtimeBeans.size(), is(4));
        {
            final RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans, "thread");
            assertNotNull(threadRB);
            assertFalse(threadRB.isRoot());
            assertEquals("name", threadRB.getKeyYangName().get());
            assertEquals("Name", threadRB.getKeyJavaName().get());
            assertThat(threadRB.getAttributes().size(), is(1));
            final AttributeIfc threadNameAttr = threadRB.getAttributes().iterator().next();
            assertThat(threadNameAttr.getAttributeYangName(), is("name"));
            assertTrue(threadNameAttr instanceof JavaAttribute);
            assertThat(((JavaAttribute) threadNameAttr).getType().getFullyQualifiedName(), is(String.class.getName()));
            assertThat(threadRB.getRpcs().size(), is(2));
        }
        {
            final RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans, "ThreadStream");
            assertNotNull(streamRB);
            assertFalse(streamRB.getKeyYangName().isPresent());
            assertFalse(streamRB.getKeyJavaName().isPresent());
            final Map<String, AttributeIfc> attributeMap = streamRB.getYangPropertiesToTypesMap();
            assertEquals(4, attributeMap.size());
            final TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
            assertNotNull(toAttr);
            assertThat(toAttr.getAttributeYangName(), is("peer"));
            assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
            assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
            assertThat(toAttr.getOpenType(), isA(CompositeType.class));
            Set<String> propsExpected = new HashSet<>(2);
            propsExpected.add("port");
            propsExpected.add("core-size");
            assertThat(toAttr.getYangPropertiesToTypesMap().keySet(), is(propsExpected));
            propsExpected = new HashSet<>(2);
            propsExpected.add("Port");
            propsExpected.add("CoreSize");
            assertThat(toAttr.getCapitalizedPropertiesToTypesMap().keySet(), is(propsExpected));
            propsExpected = new HashSet<>(2);
            propsExpected.add("port");
            propsExpected.add("coreSize");
            assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(), is(propsExpected));
            final JavaAttribute timestampAttr = (JavaAttribute) attributeMap.get("timestamp");
            assertNotNull(timestampAttr);
            final JavaAttribute stateAttr = (JavaAttribute) attributeMap.get("state");
            assertNotNull(stateAttr);
            final ListAttribute innerStream = (ListAttribute) attributeMap.get("inner-stream-list");
            assertNotNull(innerStream);
            assertThat(innerStream.getAttributeYangName(), is("inner-stream-list"));
            assertThat(innerStream.getLowerCaseCammelCase(), is("innerStreamList"));
            assertThat(innerStream.getUpperCaseCammelCase(), is("InnerStreamList"));
            assertThat(innerStream.getOpenType(), isA(ArrayType.class));
        }
    }
    {
        // test multiple dependencies
        final ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
        final Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
        assertEquals(1, attributes.size());
        final AttributeIfc threadpoolsAttr = attributes.get("threadpools");
        assertNotNull(threadpoolsAttr);
        assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
        final ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TypedAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) ListDependenciesAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute) TOAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute) ArrayType(javax.management.openmbean.ArrayType) DependencyAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute) CompositeType(javax.management.openmbean.CompositeType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with DataSchemaNode

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

the class ModuleMXBeanEntryBuilder method build.

public Map<String, ModuleMXBeanEntry> build() {
    LOG.debug("Generating ModuleMXBeans of {} to package {}", currentModule.getNamespace(), packageName);
    String configModulePrefix;
    try {
        configModulePrefix = getConfigModulePrefixFromImport(currentModule);
    } catch (IllegalArgumentException e) {
        // this currentModule does not import config currentModule
        return Collections.emptyMap();
    }
    // get identities of base config:currentModule-type
    Map<String, IdentitySchemaNode> moduleIdentities = getIdentityMap();
    Map<String, QName> uniqueGeneratedClassesNames = new HashMap<>();
    // each currentModule name should have an augmentation defined
    Map<String, IdentitySchemaNode> unaugmentedModuleIdentities = new HashMap<>(moduleIdentities);
    Map<String, ModuleMXBeanEntry> result = new HashMap<>();
    for (AugmentationSchemaNode augmentation : currentModule.getAugmentations()) {
        Collection<DataSchemaNode> childNodes = augmentation.getChildNodes();
        if (areAllChildrenCaseSchemaNodes(childNodes)) {
            for (CaseSchemaNode childCase : castChildNodesToChoiceCases(childNodes)) {
                // TODO refactor, extract to standalone builder class
                processCaseSchemaNode(result, uniqueGeneratedClassesNames, configModulePrefix, moduleIdentities, unaugmentedModuleIdentities, augmentation, childCase);
            }
        }
    // skip if child nodes are not all cases
    }
    // clean up nulls
    cleanUpNulls(result);
    // check attributes name uniqueness
    checkAttributeNamesUniqueness(uniqueGeneratedClassesNames, result);
    checkUnaugumentedIdentities(unaugmentedModuleIdentities);
    LOG.debug("Number of ModuleMXBeans to be generated: {}", result.size());
    return result;
}
Also used : AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) HashMap(java.util.HashMap) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) ConfigConstants.createConfigQName(org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.createConfigQName) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)

Example 9 with DataSchemaNode

use of org.opendaylight.yangtools.yang.model.api.DataSchemaNode 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)

Aggregations

DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)9 HashMap (java.util.HashMap)4 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)3 QName (org.opendaylight.yangtools.yang.common.QName)3 HashSet (java.util.HashSet)2 DependencyAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute)2 JavaAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)2 ListAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute)2 ListDependenciesAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute)2 TOAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute)2 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)2 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)2 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)2 IdentitySchemaNode (org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)2 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)2 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)2 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)2 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)2 UnknownSchemaNode (org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode)2 ArrayList (java.util.ArrayList)1