Search in sources :

Example 6 with ModuleMXBeanEntry

use of org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry in project controller by opendaylight.

the class RpcFacade method mapRpcs.

public Rpcs mapRpcs() {
    final Map<String, Map<String, ModuleRpcs>> map = new HashMap<>();
    for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
        Map<String, ModuleRpcs> namespaceToModules = map.computeIfAbsent(namespaceToModuleEntry.getKey(), k -> new HashMap<>());
        for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
            ModuleRpcs rpcMapping = namespaceToModules.computeIfAbsent(moduleEntry.getKey(), k -> new ModuleRpcs(yangStoreService.getEnumResolver()));
            final ModuleMXBeanEntry entry = moduleEntry.getValue();
            for (final RuntimeBeanEntry runtimeEntry : entry.getRuntimeBeans()) {
                rpcMapping.addNameMapping(runtimeEntry);
                for (final RuntimeBeanEntry.Rpc rpc : runtimeEntry.getRpcs()) {
                    rpcMapping.addRpc(runtimeEntry, rpc);
                }
            }
        }
    }
    return new Rpcs(map);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) Rpcs(org.opendaylight.controller.config.facade.xml.rpc.Rpcs) ModuleRpcs(org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs) ModuleRpcs(org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with ModuleMXBeanEntry

use of org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry 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 8 with ModuleMXBeanEntry

use of org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry in project controller by opendaylight.

the class ModuleMXBeanEntryPluginTest method testThreadsJavaPlugin.

@Test
public void testThreadsJavaPlugin() {
    Map<String, ModuleMXBeanEntry> /* identity local name */
    namesToMBEs = loadThreadsJava();
    {
        // check threadfactory-naming
        ModuleMXBeanEntry threadFactoryNaming = namesToMBEs.get(THREADFACTORY_NAMING_MXB_NAME);
        Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming.getRuntimeBeans();
        assertThat(runtimeBeans.size(), is(4));
        // first one should be root
        {
            RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans, THREADFACTORY_NAMING_MXB_NAME);
            assertThat(rootRB.isRoot(), is(true));
            assertThat(rootRB.getAttributes().size(), is(1));
            JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes().iterator().next();
            assertThat(attribute.getAttributeYangName(), is("created-sessions"));
            assertThat(rootRB.getYangName(), is(THREADFACTORY_NAMING_MXB_NAME));
            Map<String, FtlTemplate> ftlMap = TemplateFactory.getTOAndMXInterfaceFtlFiles(rootRB);
            assertThat(ftlMap.size(), is(1));
            GeneralInterfaceTemplate rootGeneratorInterface = (GeneralInterfaceTemplate) ftlMap.get("NamingThreadFactoryRuntimeMXBean.java");
            assertNotNull(rootGeneratorInterface);
            assertThat(rootGeneratorInterface.getPackageName(), is(PACKAGE_NAME));
            assertThat(rootGeneratorInterface.getFullyQualifiedName(), is(PACKAGE_NAME + ".NamingThreadFactoryRuntimeMXBean"));
            assertThat(rootGeneratorInterface.getTypeDeclaration().getExtended(), is(Arrays.asList("org.opendaylight.controller.config.api.runtime.RuntimeBean")));
            assertThat(rootGeneratorInterface.getMethods().size(), is(1));
            Method getCreatedSessions = findFirstMethodByName(rootGeneratorInterface.getMethods(), "getCreatedSessions");
            assertThat(getCreatedSessions.getName(), is("getCreatedSessions"));
            assertThat(getCreatedSessions.getParameters().isEmpty(), is(true));
            assertThat(getCreatedSessions.getReturnType(), is(Long.class.getName()));
        }
    }
    {
        ModuleMXBeanEntry threadFactoryNaming = namesToMBEs.get(THREADFACTORY_NAMING_MXB_NAME);
        Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming.getRuntimeBeans();
        assertThat(runtimeBeans.size(), is(4));
        {
            RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans, "ThreadStream");
            assertNotNull(streamRB);
            assertFalse(streamRB.getKeyYangName().isPresent());
            assertFalse(streamRB.getKeyJavaName().isPresent());
            Map<String, AttributeIfc> attributeMap = streamRB.getYangPropertiesToTypesMap();
            assertEquals(4, attributeMap.size());
            TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
            assertNotNull(toAttr);
            JavaAttribute timestampAttr = (JavaAttribute) attributeMap.get("timestamp");
            assertNotNull(timestampAttr);
            JavaAttribute stateAttr = (JavaAttribute) attributeMap.get("state");
            assertNotNull(stateAttr);
            ListAttribute innerStreamList = (ListAttribute) attributeMap.get("inner-stream-list");
            assertNotNull(innerStreamList);
            Map<String, FtlTemplate> ftlMap = TemplateFactory.getTOAndMXInterfaceFtlFiles(streamRB);
            assertThat(ftlMap.size(), is(3));
            GeneralInterfaceTemplate streamGeneralInterface = (GeneralInterfaceTemplate) ftlMap.get("ThreadStreamRuntimeMXBean.java");
            assertThat(streamGeneralInterface.getMethods().size(), is(4));
            Method getPeer = findFirstMethodByName(streamGeneralInterface.getMethods(), "getPeer");
            assertNotNull(getPeer);
            assertThat(getPeer.getReturnType(), is(PACKAGE_NAME + ".Peer"));
            // test TO
            GeneralClassTemplate peerTO = (GeneralClassTemplate) ftlMap.get("pack2.Peer");
            assertThat(peerTO.getPackageName(), is(PACKAGE_NAME));
            assertThat(peerTO.getTypeDeclaration().getExtended().isEmpty(), is(true));
            assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME + ".Peer"));
            assertThat(peerTO.getMethods().size(), is(5 + 2));
            Method getPort = findFirstMethodByName(peerTO.getMethods(), "getPort");
            assertNotNull(getPort);
            Method setPort = findFirstMethodByName(peerTO.getMethods(), "setPort");
            assertNotNull(setPort);
            Method getCoreSize = findFirstMethodByName(peerTO.getMethods(), "getCoreSize");
            Method setCoreSize = findFirstMethodByName(peerTO.getMethods(), "setCoreSize");
            assertNotNull(setCoreSize);
            assertNotNull(getCoreSize);
        }
    }
}
Also used : GeneralInterfaceTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralInterfaceTemplate) RuntimeBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry) GeneralClassTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.GeneralClassTemplate) Method(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Method) TOAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) Collection(java.util.Collection) AttributeIfc(org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc) FtlTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.FtlTemplate) JavaAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute) ListAttribute(org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute) Test(org.junit.Test) ModuleMXBeanEntryTest(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntryTest)

Example 9 with ModuleMXBeanEntry

use of org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry in project controller by opendaylight.

the class ModuleMXBeanEntryTemplatesTest method test.

@Test
public void test() {
    final ModuleMXBeanEntry mbe = mockMbe("package");
    final AbstractFactoryTemplate template = TemplateFactory.abstractFactoryTemplateFromMbe(mbe);
    assertNotNull(template);
}
Also used : AbstractFactoryTemplate(org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.AbstractFactoryTemplate) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) Test(org.junit.Test)

Example 10 with ModuleMXBeanEntry

use of org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry in project controller by opendaylight.

the class AbsModuleGeneratedObjectFactoryTest method test.

@Test
public void test() throws IOException {
    Map<QName, ServiceInterfaceEntry> serviceInterfaceEntryMap = loadThreadsServiceInterfaceEntries("packages.sis");
    Map<String, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava(serviceInterfaceEntryMap, "packages.pack2");
    ModuleMXBeanEntry dynamicThreadPool = namesToMBEs.get(THREADPOOL_DYNAMIC_MXB_NAME);
    parseGeneratedFile(dynamicThreadPool);
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) ServiceInterfaceEntry(org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry) AbstractGeneratedObjectTest(org.opendaylight.controller.config.yangjmxgenerator.plugin.module.AbstractGeneratedObjectTest) Test(org.junit.Test)

Aggregations

ModuleMXBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry)10 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Map (java.util.Map)3 RuntimeBeanEntry (org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry)3 File (java.io.File)2 InstanceConfig (org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfig)2 ServiceInterfaceEntry (org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry)2 AttributeIfc (org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc)2 JavaAttribute (org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute)2 AbstractGeneratedObjectTest (org.opendaylight.controller.config.yangjmxgenerator.plugin.module.AbstractGeneratedObjectTest)2 QName (org.opendaylight.yangtools.yang.common.QName)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)1 Matchers.anyString (org.mockito.Matchers.anyString)1 ModuleConfig (org.opendaylight.controller.config.facade.xml.mapping.config.ModuleConfig)1 ModuleRpcs (org.opendaylight.controller.config.facade.xml.rpc.ModuleRpcs)1