Search in sources :

Example 11 with Module

use of org.opendaylight.controller.config.spi.Module in project controller by opendaylight.

the class ConfigTransactionControllerImpl method secondPhaseCommit.

/**
 * {@inheritDoc}
 */
@Override
public synchronized List<ModuleIdentifier> secondPhaseCommit() {
    transactionStatus.checkNotAborted();
    transactionStatus.checkCommitStarted();
    if (!configBeanModificationDisabled.get()) {
        throw new IllegalStateException("Internal error - validateBeforeCommitAndLockTransaction should be called " + "to obtain a lock");
    }
    LOG.trace("Committing transaction {}", getTransactionIdentifier());
    Map<ModuleIdentifier, Module> allModules = dependencyResolverManager.getAllModules();
    // call getInstance() on all Modules from top to bottom (from source to target
    // of the dependency relation)
    // The source of a dependency closes itself and calls getInstance recursively on
    // the dependencies (in case of reconfiguration)
    // This makes close() calls from top to bottom while createInstance() calls are
    // performed bottom to top
    List<ModuleIdentifier> sortedModuleIdentifiers = Lists.reverse(dependencyResolverManager.getSortedModuleIdentifiers());
    for (ModuleIdentifier moduleIdentifier : sortedModuleIdentifiers) {
        Module module = allModules.get(moduleIdentifier);
        LOG.debug("About to commit {} in transaction {}", moduleIdentifier, getTransactionIdentifier());
        AutoCloseable instance = module.getInstance();
        Preconditions.checkNotNull(instance, "Instance is null:%s in transaction %s", moduleIdentifier, getTransactionIdentifier());
    }
    LOG.trace("Committed configuration {}", getTransactionIdentifier());
    transactionStatus.setCommitted();
    return sortedModuleIdentifiers;
}
Also used : ModuleIdentifier(org.opendaylight.controller.config.api.ModuleIdentifier) AbstractModule(org.opendaylight.controller.config.spi.AbstractModule) Module(org.opendaylight.controller.config.spi.Module)

Example 12 with Module

use of org.opendaylight.controller.config.spi.Module in project controller by opendaylight.

the class ConfigTransactionControllerImpl method processDefaultBeans.

private synchronized void processDefaultBeans(final List<ModuleFactory> lastListOfFactories) {
    transactionStatus.checkNotCommitStarted();
    transactionStatus.checkNotAborted();
    Set<ModuleFactory> oldSet = new HashSet<>(lastListOfFactories);
    Set<ModuleFactory> newSet = new HashSet<>(factoriesHolder.getModuleFactories());
    List<ModuleFactory> toBeAdded = new ArrayList<>();
    List<ModuleFactory> toBeRemoved = new ArrayList<>();
    for (ModuleFactory moduleFactory : factoriesHolder.getModuleFactories()) {
        if (!oldSet.contains(moduleFactory)) {
            toBeAdded.add(moduleFactory);
        }
    }
    for (ModuleFactory moduleFactory : lastListOfFactories) {
        if (!newSet.contains(moduleFactory)) {
            toBeRemoved.add(moduleFactory);
        }
    }
    // add default modules
    for (ModuleFactory moduleFactory : toBeAdded) {
        BundleContext bundleContext = getModuleFactoryBundleContext(moduleFactory.getImplementationName());
        Set<? extends Module> defaultModules = moduleFactory.getDefaultModules(dependencyResolverManager, bundleContext);
        for (Module module : defaultModules) {
            // ensure default module to be registered to jmx even if its module factory does
            // not use dependencyResolverFactory
            DependencyResolver dependencyResolver = dependencyResolverManager.getOrCreate(module.getIdentifier());
            final ObjectName objectName;
            try {
                boolean defaultBean = true;
                objectName = putConfigBeanToJMXAndInternalMaps(module.getIdentifier(), module, moduleFactory, null, dependencyResolver, defaultBean, bundleContext);
            } catch (final InstanceAlreadyExistsException e) {
                throw new IllegalStateException(e);
            }
            // register default module as every possible service
            final Set<ServiceInterfaceAnnotation> serviceInterfaceAnnotations = InterfacesHelper.getServiceInterfaceAnnotations(moduleFactory);
            for (String qname : InterfacesHelper.getQNames(serviceInterfaceAnnotations)) {
                try {
                    saveServiceReference(qname, module.getIdentifier().getInstanceName(), objectName);
                } catch (final InstanceNotFoundException e) {
                    throw new IllegalStateException("Unable to register default module instance " + module + " as a service of " + qname, e);
                }
            }
        }
    }
    // remove modules belonging to removed factories
    for (ModuleFactory removedFactory : toBeRemoved) {
        List<ModuleIdentifier> modulesOfRemovedFactory = dependencyResolverManager.findAllByFactory(removedFactory);
        for (ModuleIdentifier name : modulesOfRemovedFactory) {
            // remove service refs
            final ModuleFactory moduleFactory = dependencyResolverManager.findModuleInternalTransactionalInfo(name).getModuleFactory();
            final Set<ServiceInterfaceAnnotation> serviceInterfaceAnnotations = InterfacesHelper.getServiceInterfaceAnnotations(moduleFactory);
            for (String qname : InterfacesHelper.getQNames(serviceInterfaceAnnotations)) {
                try {
                    removeServiceReference(qname, name.getInstanceName());
                } catch (final InstanceNotFoundException e) {
                    throw new IllegalStateException("Unable to UNregister default module instance " + name + " as a service of " + qname, e);
                }
            }
            // close module
            destroyModule(name);
        }
    }
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ArrayList(java.util.ArrayList) ServiceInterfaceAnnotation(org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation) DependencyResolver(org.opendaylight.controller.config.api.DependencyResolver) ObjectName(javax.management.ObjectName) ModuleFactory(org.opendaylight.controller.config.spi.ModuleFactory) ModuleIdentifier(org.opendaylight.controller.config.api.ModuleIdentifier) AbstractModule(org.opendaylight.controller.config.spi.AbstractModule) Module(org.opendaylight.controller.config.spi.Module) HashSet(java.util.HashSet) BundleContext(org.osgi.framework.BundleContext)

Example 13 with Module

use of org.opendaylight.controller.config.spi.Module in project controller by opendaylight.

the class ConfigTransactionControllerImpl method reCreateModule.

@Override
public synchronized void reCreateModule(final ObjectName objectName) throws InstanceNotFoundException {
    transactionStatus.checkNotCommitStarted();
    transactionStatus.checkNotAborted();
    checkTransactionName(objectName);
    ObjectNameUtil.checkDomain(objectName);
    ModuleIdentifier moduleIdentifier = ObjectNameUtil.fromON(objectName, ObjectNameUtil.TYPE_MODULE);
    ModuleInternalTransactionalInfo txInfo = dependencyResolverManager.findModuleInternalTransactionalInfo(moduleIdentifier);
    Module realModule = txInfo.getRealModule();
    if (realModule instanceof AbstractModule) {
        ((AbstractModule<?>) realModule).setCanReuseInstance(false);
    }
}
Also used : ModuleIdentifier(org.opendaylight.controller.config.api.ModuleIdentifier) ModuleInternalTransactionalInfo(org.opendaylight.controller.config.manager.impl.dependencyresolver.ModuleInternalTransactionalInfo) AbstractModule(org.opendaylight.controller.config.spi.AbstractModule) Module(org.opendaylight.controller.config.spi.Module) AbstractModule(org.opendaylight.controller.config.spi.AbstractModule)

Example 14 with Module

use of org.opendaylight.controller.config.spi.Module in project controller by opendaylight.

the class ModulesHolder method getAllModules.

public Map<ModuleIdentifier, Module> getAllModules() {
    Map<ModuleIdentifier, Module> result = new HashMap<>();
    for (ModuleInternalTransactionalInfo entry : commitMap.values()) {
        ModuleIdentifier name = entry.getIdentifier();
        result.put(name, entry.getProxiedModule());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ModuleIdentifier(org.opendaylight.controller.config.api.ModuleIdentifier) Module(org.opendaylight.controller.config.spi.Module)

Example 15 with Module

use of org.opendaylight.controller.config.spi.Module in project controller by opendaylight.

the class JMXGeneratorTest method generateMBEsTest.

@Test
public void generateMBEsTest() throws Exception {
    // default value for module factory file is true
    map.put(JMXGenerator.MODULE_FACTORY_FILE_BOOLEAN, "randomValue");
    jmxGenerator.setAdditionalConfig(map);
    Collection<File> files = jmxGenerator.generateSources(context, outputBaseDir, Collections.singleton(threadsJavaModule), m -> Optional.empty());
    assertEquals(expectedModuleFileNames, toFileNames(files));
    for (File file : files) {
        final String name = file.getName();
        if (!name.endsWith("java")) {
            continue;
        }
        MbeASTVisitor visitor = new MbeASTVisitor(EXPECTED_PACKAGE_PREFIX + ".threads.java", name);
        verifyFile(file, visitor);
        switch(name) {
            case "AbstractDynamicThreadPoolModule.java":
                assertAbstractDynamicThreadPoolModule(visitor);
                break;
            case "AsyncEventBusModuleMXBean.java":
                assertEquals("Incorrenct number of generated methods", 4, visitor.methods.size());
                break;
            case "AbstractNamingThreadFactoryModuleFactory.java":
                assertAbstractNamingThreadFactoryModuleFactory(visitor);
                break;
            case "AsyncEventBusModule.java":
                assertContains(visitor.extnds, EXPECTED_PACKAGE_PREFIX + ".threads.java.AbstractAsyncEventBusModule");
                visitor.assertFields(0);
                assertEquals("Incorrenct number of generated methods", 2, visitor.methods.size());
                visitor.assertConstructors(2);
                visitor.assertMethodDescriptions(0);
                visitor.assertMethodJavadocs(0);
                break;
            case "EventBusModuleFactory.java":
                assertContains(visitor.extnds, EXPECTED_PACKAGE_PREFIX + ".threads.java.AbstractEventBusModuleFactory");
                visitor.assertFields(0);
                assertEquals("Incorrenct number of generated methods", 0, visitor.methods.size());
                visitor.assertConstructors(0);
                visitor.assertMethodDescriptions(0);
                visitor.assertMethodJavadocs(0);
                break;
        }
    }
    verifyXmlFiles(Collections2.filter(files, input -> input.getName().endsWith("xml")));
    // verify ModuleFactory file
    File moduleFactoryFile = JMXGenerator.concatFolders(generatedResourcesDir, "META-INF", "services", ModuleFactory.class.getName());
    assertTrue(moduleFactoryFile.exists());
    Set<String> lines = ImmutableSet.copyOf(Files.readLines(moduleFactoryFile, StandardCharsets.UTF_8));
    Set<String> expectedLines = ImmutableSet.of(EXPECTED_PACKAGE_PREFIX + ".threads.java.EventBusModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.AsyncEventBusModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.DynamicThreadPoolModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.NamingThreadFactoryModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.ThreadPoolRegistryImplModuleFactory");
    assertEquals(expectedLines, lines);
}
Also used : ConfigConstants(org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants) DynamicMBeanWithInstance(org.opendaylight.controller.config.api.DynamicMBeanWithInstance) HashMap(java.util.HashMap) ModuleFactory(org.opendaylight.controller.config.spi.ModuleFactory) Collections2(com.google.common.collect.Collections2) AbstractModule(org.opendaylight.controller.config.spi.AbstractModule) DependencyResolverFactory(org.opendaylight.controller.config.api.DependencyResolverFactory) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) EXPECTED_PACKAGE_PREFIX(org.opendaylight.controller.config.yangjmxgenerator.PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX) ErrorHandler(org.xml.sax.ErrorHandler) ParseException(com.github.javaparser.ParseException) Files(com.google.common.io.Files) MavenProject(org.apache.maven.project.MavenProject) Assert.fail(org.junit.Assert.fail) ServiceInterfaceEntryTest(org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntryTest) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) InputSource(org.xml.sax.InputSource) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ImmutableSet(com.google.common.collect.ImmutableSet) Module(org.opendaylight.controller.config.spi.Module) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) List(java.util.List) SAXParseException(org.xml.sax.SAXParseException) DependencyResolver(org.opendaylight.controller.config.api.DependencyResolver) Assert.assertFalse(org.junit.Assert.assertFalse) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) JavaParser(com.github.javaparser.JavaParser) ModuleFactory(org.opendaylight.controller.config.spi.ModuleFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) ServiceInterfaceEntryTest(org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntryTest) Test(org.junit.Test)

Aggregations

Module (org.opendaylight.controller.config.spi.Module)15 ModuleIdentifier (org.opendaylight.controller.config.api.ModuleIdentifier)11 AbstractModule (org.opendaylight.controller.config.spi.AbstractModule)7 DependencyResolver (org.opendaylight.controller.config.api.DependencyResolver)5 ModuleFactory (org.opendaylight.controller.config.spi.ModuleFactory)5 BundleContext (org.osgi.framework.BundleContext)5 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)3 ObjectName (javax.management.ObjectName)2 ModuleFactoryNotFoundException (org.opendaylight.controller.config.api.ModuleFactoryNotFoundException)2 ValidationException (org.opendaylight.controller.config.api.ValidationException)2 ServiceInterfaceAnnotation (org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation)2 ModuleInternalTransactionalInfo (org.opendaylight.controller.config.manager.impl.dependencyresolver.ModuleInternalTransactionalInfo)2 JavaParser (com.github.javaparser.JavaParser)1 ParseException (com.github.javaparser.ParseException)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 Collections2 (com.google.common.collect.Collections2)1 ImmutableSet (com.google.common.collect.ImmutableSet)1