X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2FConfigTransactionControllerImpl.java;h=ad64288ab203a0e9c00d6bbe5964dff33fedf520;hb=refs%2Fchanges%2F73%2F46573%2F5;hp=a58f7a05c65ae9a1f31a407e53c6f54d028e3200;hpb=8161319be53a57e3ac22ba72f267483526e11e0d;p=controller.git diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ConfigTransactionControllerImpl.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ConfigTransactionControllerImpl.java index a58f7a05c6..ad64288ab2 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ConfigTransactionControllerImpl.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ConfigTransactionControllerImpl.java @@ -7,9 +7,8 @@ */ package org.opendaylight.controller.config.manager.impl; -import static com.google.common.base.Preconditions.checkNotNull; -import static java.lang.String.format; - +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -26,8 +25,10 @@ import javax.management.InstanceNotFoundException; import javax.management.MBeanServer; import javax.management.ObjectName; import org.opendaylight.controller.config.api.DependencyResolver; +import org.opendaylight.controller.config.api.ModuleFactoryNotFoundException; import org.opendaylight.controller.config.api.ModuleIdentifier; import org.opendaylight.controller.config.api.ValidationException; +import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation; import org.opendaylight.controller.config.api.jmx.ObjectNameUtil; import org.opendaylight.controller.config.manager.impl.dependencyresolver.DependencyResolverManager; import org.opendaylight.controller.config.manager.impl.dependencyresolver.ModuleInternalTransactionalInfo; @@ -37,13 +38,16 @@ import org.opendaylight.controller.config.manager.impl.dynamicmbean.ReadOnlyAtom import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder; import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator; import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator.TransactionModuleJMXRegistration; +import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider; +import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper; +import org.opendaylight.controller.config.spi.AbstractModule; import org.opendaylight.controller.config.spi.Module; import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.yangtools.concepts.Identifiable; -import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry; import org.osgi.framework.BundleContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** * This is a JMX bean representing current transaction. It contains * transaction identifier, unique version and parent version for @@ -53,12 +57,13 @@ class ConfigTransactionControllerImpl implements ConfigTransactionControllerInternal, ConfigTransactionControllerImplMXBean, Identifiable { - private static final Logger LOGGER = LoggerFactory.getLogger(ConfigTransactionControllerImpl.class); + private static final Logger LOG = LoggerFactory.getLogger(ConfigTransactionControllerImpl.class); private final ConfigTransactionLookupRegistry txLookupRegistry; private final ObjectName controllerON; - private final long parentVersion, currentVersion; + private final long parentVersion; + private final long currentVersion; private final HierarchicalConfigMBeanFactoriesHolder factoriesHolder; private final DependencyResolverManager dependencyResolverManager; private final TransactionStatus transactionStatus; @@ -82,7 +87,7 @@ class ConfigTransactionControllerImpl implements private final SearchableServiceReferenceWritableRegistry writableSRRegistry; public ConfigTransactionControllerImpl(ConfigTransactionLookupRegistry txLookupRegistry, - long parentVersion, CodecRegistry codecRegistry, long currentVersion, + long parentVersion, BindingContextProvider bindingContextProvider, long currentVersion, Map> currentlyRegisteredFactories, MBeanServer transactionsMBeanServer, MBeanServer configMBeanServer, boolean blankTransaction, SearchableServiceReferenceWritableRegistry writableSRRegistry) { @@ -95,7 +100,7 @@ class ConfigTransactionControllerImpl implements this.factoriesHolder = new HierarchicalConfigMBeanFactoriesHolder(currentlyRegisteredFactories); this.transactionStatus = new TransactionStatus(); this.dependencyResolverManager = new DependencyResolverManager(txLookupRegistry.getTransactionIdentifier(), - transactionStatus, writableSRRegistry, codecRegistry, transactionsMBeanServer); + transactionStatus, writableSRRegistry, bindingContextProvider, transactionsMBeanServer); this.transactionsMBeanServer = transactionsMBeanServer; this.configMBeanServer = configMBeanServer; this.blankTransaction = blankTransaction; @@ -125,12 +130,12 @@ class ConfigTransactionControllerImpl implements List toBeAdded = new ArrayList<>(); List toBeRemoved = new ArrayList<>(); for (ModuleFactory moduleFactory : factoriesHolder.getModuleFactories()) { - if (oldSet.contains(moduleFactory) == false) { + if (!oldSet.contains(moduleFactory)) { toBeAdded.add(moduleFactory); } } for (ModuleFactory moduleFactory : lastListOfFactories) { - if (newSet.contains(moduleFactory) == false) { + if (!newSet.contains(moduleFactory)) { toBeRemoved.add(moduleFactory); } } @@ -142,13 +147,24 @@ class ConfigTransactionControllerImpl implements 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; - putConfigBeanToJMXAndInternalMaps(module.getIdentifier(), module, moduleFactory, null, + objectName = putConfigBeanToJMXAndInternalMaps(module.getIdentifier(), module, moduleFactory, null, dependencyResolver, defaultBean, bundleContext); } catch (InstanceAlreadyExistsException e) { throw new IllegalStateException(e); } + + // register default module as every possible service + final Set serviceInterfaceAnnotations = InterfacesHelper.getServiceInterfaceAnnotations(moduleFactory); + for (String qname : InterfacesHelper.getQNames(serviceInterfaceAnnotations)) { + try { + saveServiceReference(qname, module.getIdentifier().getInstanceName(), objectName); + } catch (InstanceNotFoundException e) { + throw new IllegalStateException("Unable to register default module instance " + module + " as a service of " + qname, e); + } + } } } @@ -156,13 +172,26 @@ class ConfigTransactionControllerImpl implements for (ModuleFactory removedFactory : toBeRemoved) { List modulesOfRemovedFactory = dependencyResolverManager.findAllByFactory(removedFactory); for (ModuleIdentifier name : modulesOfRemovedFactory) { + // remove service refs + final ModuleFactory moduleFactory = dependencyResolverManager.findModuleInternalTransactionalInfo(name).getModuleFactory(); + final Set serviceInterfaceAnnotations = InterfacesHelper.getServiceInterfaceAnnotations(moduleFactory); + for (String qname : InterfacesHelper.getQNames(serviceInterfaceAnnotations)) { + try { + removeServiceReference(qname, name.getInstanceName()); + } catch (InstanceNotFoundException e) { + throw new IllegalStateException("Unable to UNregister default module instance " + name + " as a service of " + qname, e); + } + } + + // close module destroyModule(name); } } } - private synchronized void copyExistingModule(ModuleInternalInfo oldConfigBeanInfo) throws InstanceAlreadyExistsException { + private synchronized void copyExistingModule(ModuleInternalInfo oldConfigBeanInfo) + throws InstanceAlreadyExistsException { transactionStatus.checkNotCommitStarted(); transactionStatus.checkNotAborted(); @@ -174,7 +203,7 @@ class ConfigTransactionControllerImpl implements try { moduleFactory = factoriesHolder.findByModuleName(moduleIdentifier.getFactoryName()); bc = getModuleFactoryBundleContext(moduleFactory.getImplementationName()); - } catch (InstanceNotFoundException e) { + } catch (ModuleFactoryNotFoundException e) { throw new IllegalStateException(e); } @@ -186,7 +215,7 @@ class ConfigTransactionControllerImpl implements moduleIdentifier.getInstanceName(), dependencyResolver, oldConfigBeanInfo.getReadableModule(), bc); } catch (Exception e) { - throw new IllegalStateException(format( + throw new IllegalStateException(String.format( "Error while copying old configuration from %s to %s", oldConfigBeanInfo, moduleFactory), e); } @@ -195,8 +224,8 @@ class ConfigTransactionControllerImpl implements } @Override - public synchronized ObjectName createModule(String factoryName, - String instanceName) throws InstanceAlreadyExistsException { + public synchronized ObjectName createModule(String factoryName, String instanceName) + throws InstanceAlreadyExistsException { transactionStatus.checkNotCommitStarted(); transactionStatus.checkNotAborted(); @@ -204,12 +233,8 @@ class ConfigTransactionControllerImpl implements dependencyResolverManager.assertNotExists(moduleIdentifier); // find factory - ModuleFactory moduleFactory; - try { - moduleFactory = factoriesHolder.findByModuleName(factoryName); - } catch (InstanceNotFoundException e) { - throw new IllegalArgumentException(e); - } + ModuleFactory moduleFactory = factoriesHolder.findByModuleName(factoryName); + DependencyResolver dependencyResolver = dependencyResolverManager.getOrCreate(moduleIdentifier); BundleContext bundleContext = getModuleFactoryBundleContext(moduleFactory.getImplementationName()); Module module = moduleFactory.createModule(instanceName, dependencyResolver, @@ -219,6 +244,21 @@ class ConfigTransactionControllerImpl implements moduleFactory, null, dependencyResolver, defaultBean, bundleContext); } + @Override + public synchronized void reCreateModule(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); + } + } + private synchronized ObjectName putConfigBeanToJMXAndInternalMaps( ModuleIdentifier moduleIdentifier, Module module, ModuleFactory moduleFactory, @@ -226,17 +266,17 @@ class ConfigTransactionControllerImpl implements boolean isDefaultBean, BundleContext bundleContext) throws InstanceAlreadyExistsException { - LOGGER.debug("Adding module {} to transaction {}", moduleIdentifier, this); - if (moduleIdentifier.equals(module.getIdentifier()) == false) { + LOG.debug("Adding module {} to transaction {}", moduleIdentifier, this); + if (!moduleIdentifier.equals(module.getIdentifier())) { throw new IllegalStateException("Incorrect name reported by module. Expected " + moduleIdentifier + ", got " + module.getIdentifier()); } - if (dependencyResolver.getIdentifier().equals(moduleIdentifier) == false) { + if (!dependencyResolver.getIdentifier().equals(moduleIdentifier)) { throw new IllegalStateException("Incorrect name reported by dependency resolver. Expected " + moduleIdentifier + ", got " + dependencyResolver.getIdentifier()); } DynamicMBean writableDynamicWrapper = new DynamicWritableWrapper( - module, moduleIdentifier, getTransactionIdentifier(), + module, moduleIdentifier, getTransactionIdentifier().getName(), readOnlyAtomicBoolean, transactionsMBeanServer, configMBeanServer); @@ -264,20 +304,20 @@ class ConfigTransactionControllerImpl implements private void checkTransactionName(ObjectName objectName) { String foundTransactionName = ObjectNameUtil .getTransactionName(objectName); - if (getTransactionIdentifier().getName().equals(foundTransactionName) == false) { + if (!getTransactionIdentifier().getName().equals(foundTransactionName)) { throw new IllegalArgumentException("Wrong transaction name " + objectName); } } private synchronized void destroyModule(ModuleIdentifier moduleIdentifier) { - LOGGER.debug("Destroying module {} in transaction {}", moduleIdentifier, this); + LOG.debug("Destroying module {} in transaction {}", moduleIdentifier, this); transactionStatus.checkNotAborted(); ModuleInternalTransactionalInfo found = dependencyResolverManager.findModuleInternalTransactionalInfo(moduleIdentifier); - if (blankTransaction == false && + if (!blankTransaction && found.isDefaultBean()) { - LOGGER.warn("Warning: removing default bean. This will be forbidden in next version of config-subsystem"); + LOG.warn("Warning: removing default bean. This will be forbidden in next version of config-subsystem"); } // first remove refNames, it checks for objectname existence @@ -285,7 +325,7 @@ class ConfigTransactionControllerImpl implements writableSRRegistry.removeServiceReferences( ObjectNameUtil.createTransactionModuleON(getTransactionName(), moduleIdentifier)); } catch (InstanceNotFoundException e) { - LOGGER.error("Possible code error: cannot find {} in {}", moduleIdentifier, writableSRRegistry); + LOG.error("Possible code error: cannot find {} in {}", moduleIdentifier, writableSRRegistry); throw new IllegalStateException("Possible code error: cannot find " + moduleIdentifier, e); } @@ -319,7 +359,7 @@ class ConfigTransactionControllerImpl implements private void validateNoLocks() throws ValidationException { transactionStatus.checkNotAborted(); - LOGGER.trace("Validating transaction {}", getTransactionIdentifier()); + LOG.trace("Validating transaction {}", getTransactionIdentifier()); // call validate() List collectedExceptions = new ArrayList<>(); for (Entry entry : dependencyResolverManager @@ -329,7 +369,7 @@ class ConfigTransactionControllerImpl implements try { module.validate(); } catch (Exception e) { - LOGGER.warn("Validation exception in {}", getTransactionName(), + LOG.warn("Validation exception in {}", getTransactionName(), e); collectedExceptions.add(ValidationException .createForSingleException(name, e)); @@ -339,7 +379,7 @@ class ConfigTransactionControllerImpl implements throw ValidationException .createFromCollectedValidationExceptions(collectedExceptions); } - LOGGER.trace("Validated transaction {}", getTransactionIdentifier()); + LOG.trace("Validated transaction {}", getTransactionIdentifier()); } /** @@ -358,8 +398,9 @@ class ConfigTransactionControllerImpl implements try { validateNoLocks(); } catch (ValidationException e) { - LOGGER.trace("Commit failed on validation"); - configBeanModificationDisabled.set(false); // recoverable error + LOG.trace("Commit failed on validation"); + // recoverable error + configBeanModificationDisabled.set(false); throw e; } // errors in this state are not recoverable. modules are not mutable @@ -375,55 +416,58 @@ class ConfigTransactionControllerImpl implements public synchronized List secondPhaseCommit() { transactionStatus.checkNotAborted(); transactionStatus.checkCommitStarted(); - if (configBeanModificationDisabled.get() == false) { + if (!configBeanModificationDisabled.get()) { throw new IllegalStateException( "Internal error - validateBeforeCommitAndLockTransaction should be called " + "to obtain a lock"); } - LOGGER.trace("Committing transaction {}", getTransactionIdentifier()); + LOG.trace("Committing transaction {}", getTransactionIdentifier()); + + Map 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 sortedModuleIdentifiers = Lists.reverse(dependencyResolverManager.getSortedModuleIdentifiers()); + for (ModuleIdentifier moduleIdentifier : sortedModuleIdentifiers) { + Module module = allModules.get(moduleIdentifier); - // call getInstance() - for (Entry entry : dependencyResolverManager - .getAllModules().entrySet()) { - Module module = entry.getValue(); - ModuleIdentifier name = entry.getKey(); try { - LOGGER.debug("About to commit {} in transaction {}", - name, getTransactionIdentifier()); + LOG.debug("About to commit {} in transaction {}", + moduleIdentifier, getTransactionIdentifier()); AutoCloseable instance = module.getInstance(); - checkNotNull(instance, "Instance is null:{} in transaction {}", name, getTransactionIdentifier()); + Preconditions.checkNotNull(instance, "Instance is null:{} in transaction {}", moduleIdentifier, getTransactionIdentifier()); } catch (Exception e) { - LOGGER.error("Commit failed on {} in transaction {}", name, + LOG.error("Commit failed on {} in transaction {}", moduleIdentifier, getTransactionIdentifier(), e); internalAbort(); throw new IllegalStateException( - format("Error - getInstance() failed for %s in transaction %s", - name, getTransactionIdentifier()), e); + String.format("Error - getInstance() failed for %s in transaction %s", + moduleIdentifier, getTransactionIdentifier()), e); } } - // count dependency order - - LOGGER.trace("Committed configuration {}", getTransactionIdentifier()); + LOG.trace("Committed configuration {}", getTransactionIdentifier()); transactionStatus.setCommitted(); - return dependencyResolverManager.getSortedModuleIdentifiers(); + return sortedModuleIdentifiers; } @Override - public synchronized void abortConfig() { + public void abortConfig() { transactionStatus.checkNotCommitStarted(); transactionStatus.checkNotAborted(); internalAbort(); } private void internalAbort() { - LOGGER.trace("Aborting {}", this); + LOG.trace("Aborting {}", this); transactionStatus.setAborted(); close(); } + @Override public void close() { dependencyResolverManager.close(); txLookupRegistry.close(); @@ -479,6 +523,25 @@ class ConfigTransactionControllerImpl implements public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException { txLookupRegistry.checkConfigBeanExists(objectName); } + + + /** + * {@inheritDoc} + */ + @Override + public Set lookupRuntimeBeans() { + return txLookupRegistry.lookupRuntimeBeans(); + } + + /** + * {@inheritDoc} + */ + @Override + public Set lookupRuntimeBeans(String moduleName, + String instanceName) { + return txLookupRegistry.lookupRuntimeBeans(moduleName, instanceName); + } + // -- /**