Search in sources :

Example 6 with IdentitySchemaNode

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

the class SchemaContextTest method assertAllIdentitiesAreExpected.

private static void assertAllIdentitiesAreExpected(final Module module, final Map<String, /* identity name */
Optional<QName>> expectedIdentitiesToBases) {
    Map<String, Optional<QName>> /* identity name */
    copyOfExpectedNames = new HashMap<>(expectedIdentitiesToBases);
    for (IdentitySchemaNode id : module.getIdentities()) {
        String localName = id.getQName().getLocalName();
        assertTrue("Unexpected identity " + localName, copyOfExpectedNames.containsKey(localName));
        Optional<QName> maybeExpectedBaseQName = copyOfExpectedNames.remove(localName);
        if (maybeExpectedBaseQName.isPresent()) {
            assertEquals("Unexpected base identity of " + localName, maybeExpectedBaseQName.get(), id.getBaseIdentities().iterator().next().getQName());
        }
    }
    assertEquals("Expected identities not found " + copyOfExpectedNames, Collections.emptyMap(), copyOfExpectedNames);
}
Also used : Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)

Example 7 with IdentitySchemaNode

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

the class SchemaContextTest method testReadingIdentities_threadsModule.

@Test
public void testReadingIdentities_threadsModule() {
    IdentitySchemaNode serviceType = findIdentityByQName(configModule, SERVICE_TYPE_Q_NAME);
    Map<String, Optional<QName>> /* identity name */
    expectedIdentitiesToBases = ImmutableMap.of("eventbus", Optional.<QName>absent(), "threadfactory", Optional.<QName>absent(), "threadpool", Optional.<QName>absent(), "scheduled-threadpool", Optional.<QName>absent());
    assertThat(threadsModule.getIdentities().size(), is(expectedIdentitiesToBases.size()));
    assertAllIdentitiesAreExpected(threadsModule, expectedIdentitiesToBases);
    IdentitySchemaNode eventBusSchemaNode = null;
    for (IdentitySchemaNode id : threadsModule.getIdentities()) {
        String localName = id.getQName().getLocalName();
        if (localName.equals("eventbus")) {
            eventBusSchemaNode = id;
        }
        // serviceType
        if (localName.equals("scheduled-threadpool") == false) {
            assertEquals(serviceType, id.getBaseIdentities().iterator().next());
        }
    }
    assertNotNull(eventBusSchemaNode);
    // check unknown schma nodes
    List<UnknownSchemaNode> unknownSchemaNodes = eventBusSchemaNode.getUnknownSchemaNodes();
    assertEquals(1, unknownSchemaNodes.size());
    UnknownSchemaNode usn = unknownSchemaNodes.get(0);
    assertEquals("com.google.common.eventbus.EventBus", usn.getQName().getLocalName());
    assertEquals(ConfigConstants.JAVA_CLASS_EXTENSION_QNAME, usn.getNodeType());
}
Also used : Optional(com.google.common.base.Optional) UnknownSchemaNode(org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) Test(org.junit.Test)

Example 8 with IdentitySchemaNode

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

the class ConfigSubsystemFacade method transformIdentities.

private static Map<String, Map<Optional<Revision>, IdentityMapping>> transformIdentities(final Set<Module> modules) {
    Map<String, Map<Optional<Revision>, IdentityMapping>> mappedIds = new HashMap<>();
    for (Module module : modules) {
        String namespace = module.getNamespace().toString();
        Map<Optional<Revision>, IdentityMapping> revisionsByNamespace = mappedIds.computeIfAbsent(namespace, k -> new HashMap<>());
        Optional<Revision> revision = Optional.fromJavaUtil(module.getRevision());
        IdentityMapping identityMapping = revisionsByNamespace.computeIfAbsent(revision, k -> new IdentityMapping());
        for (IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
            identityMapping.addIdSchemaNode(identitySchemaNode);
        }
    }
    return mappedIds;
}
Also used : IdentityMapping(org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping) Revision(org.opendaylight.yangtools.yang.common.Revision) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) Module(org.opendaylight.yangtools.yang.model.api.Module) HashMap(java.util.HashMap) Map(java.util.Map) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)

Example 9 with IdentitySchemaNode

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

the class JMXGenerator method generateSources.

@Override
public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir, final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver) {
    Preconditions.checkArgument(context != null, "Null context received");
    Preconditions.checkArgument(outputBaseDir != null, "Null outputBaseDir received");
    Preconditions.checkArgument(this.namespaceToPackageMapping != null && !this.namespaceToPackageMapping.isEmpty(), "No namespace to package mapping provided in additionalConfiguration");
    final PackageTranslator packageTranslator = new PackageTranslator(this.namespaceToPackageMapping);
    if (!outputBaseDir.exists()) {
        outputBaseDir.mkdirs();
    }
    final GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker();
    // create SIE structure qNamesToSIEs
    final Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
    final Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
    for (final Module module : context.getModules()) {
        final String packageName = packageTranslator.getPackageName(module);
        final Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry.create(module, packageName, knownSEITracker);
        for (final Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
            // merge value into qNamesToSIEs
            if (qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue()) != null) {
                throw new IllegalStateException("Cannot add two SIE with same qname " + sieEntry.getValue());
            }
        }
        if (currentModules.contains(module)) {
            // write this sie to disk
            for (final ServiceInterfaceEntry sie : namesToSIEntries.values()) {
                try {
                    generatedFiles.addFile(this.codeWriter.writeSie(sie, outputBaseDir));
                } catch (final Exception e) {
                    throw new RuntimeException("Error occurred during SIE source generate phase", e);
                }
            }
        }
    }
    final File mainBaseDir = concatFolders(this.projectBaseDir, "src", "main", "java");
    Preconditions.checkNotNull(this.resourceBaseDir, "resource base dir attribute was null");
    final StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder();
    // create MBEs
    for (final Module module : currentModules) {
        final String packageName = packageTranslator.getPackageName(module);
        final Map<String, ModuleMXBeanEntry> /* MB identity local name */
        namesToMBEs = ModuleMXBeanEntry.create(module, qNamesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context)), packageName);
        for (final Entry<String, ModuleMXBeanEntry> mbeEntry : namesToMBEs.entrySet()) {
            final ModuleMXBeanEntry mbe = mbeEntry.getValue();
            try {
                final List<File> files1 = this.codeWriter.writeMbe(mbe, outputBaseDir, mainBaseDir);
                generatedFiles.addFile(files1);
            } catch (final Exception e) {
                throw new RuntimeException("Error occurred during MBE source generate phase", e);
            }
            fullyQualifiedNamesOfFactories.append(mbe.getFullyQualifiedName(mbe.getStubFactoryName()));
            fullyQualifiedNamesOfFactories.append("\n");
        }
    }
    // create ModuleFactory file if needed
    if (fullyQualifiedNamesOfFactories.length() > 0 && this.generateModuleFactoryFile) {
        final File serviceLoaderFile = JMXGenerator.concatFolders(this.resourceBaseDir, "META-INF", "services", ModuleFactory.class.getName());
        // if this file does not exist, create empty file
        serviceLoaderFile.getParentFile().mkdirs();
        try {
            serviceLoaderFile.createNewFile();
            Files.asCharSink(serviceLoaderFile, StandardCharsets.UTF_8).write(fullyQualifiedNamesOfFactories.toString());
        } catch (final IOException e) {
            final String message = "Cannot write to " + serviceLoaderFile;
            LOG.error(message, e);
            throw new RuntimeException(message, e);
        }
    }
    return generatedFiles.getFiles();
}
Also used : HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) ServiceInterfaceEntry(org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry) IOException(java.io.IOException) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) IOException(java.io.IOException) PackageTranslator(org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator) ModuleFactory(org.opendaylight.controller.config.spi.ModuleFactory) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) Module(org.opendaylight.yangtools.yang.model.api.Module) TypeProviderImpl(org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl) File(java.io.File) TypeProviderWrapper(org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper)

Example 10 with IdentitySchemaNode

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

the class ModuleMXBeanEntryTest method test_jmxImplModule.

@Test
public void test_jmxImplModule() {
    final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
    final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry.create(this.threadsModule, PACKAGE_NAME, identitiesToSIs);
    modulesToSIEs.putAll(ServiceInterfaceEntry.create(this.jmxModule, PACKAGE_NAME, identitiesToSIs));
    final Map<String, ModuleMXBeanEntry> /* identity local name */
    namesToMBEs = ModuleMXBeanEntry.create(this.jmxImplModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context)), PACKAGE_NAME);
    final Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf").getAttributes();
    assertCorrectAttributesSize(namesToMBEs, attributes);
    // 
    final DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes.get("thread-factory");
    assertNotNull(threadFactoryAttribute);
    assertFalse(threadFactoryAttribute.getDependency().isMandatory());
    assertThat(threadFactoryAttribute.getDependency().getSie().getTypeName(), is("ThreadFactoryServiceInterface"));
    assertThat(threadFactoryAttribute.getAttributeYangName(), is("thread-factory"));
    assertThat(threadFactoryAttribute.getLowerCaseCammelCase(), is("threadFactory"));
    assertThat(threadFactoryAttribute.getUpperCaseCammelCase(), is("ThreadFactory"));
    assertThat(threadFactoryAttribute.getOpenType(), isA(SimpleType.class));
    assertNull(threadFactoryAttribute.getNullableDefault());
    assertNull(threadFactoryAttribute.getNullableDescription());
    assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
}
Also used : HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) IdentitySchemaNode(org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode) DependencyAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute) SimpleType(javax.management.openmbean.SimpleType) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) TypeProviderImpl(org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl) Test(org.junit.Test)

Aggregations

IdentitySchemaNode (org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode)12 QName (org.opendaylight.yangtools.yang.common.QName)9 HashMap (java.util.HashMap)8 Optional (com.google.common.base.Optional)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 ConfigConstants.createConfigQName (org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.createConfigQName)2 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)2 TypeProviderImpl (org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl)2 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)2 Module (org.opendaylight.yangtools.yang.model.api.Module)2 UnknownSchemaNode (org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode)2 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 SimpleType (javax.management.openmbean.SimpleType)1 IdentityMapping (org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping)1 ModuleFactory (org.opendaylight.controller.config.spi.ModuleFactory)1