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=d5d82dd1cb581563c11104035b07f9760f87182c;hb=refs%2Fchanges%2F54%2F57354%2F8;hp=97c42803ef618662efacb522244762088eb4f13a;hpb=cbcc2b61265e903959f11d44c292771e76b3926e;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 97c42803ef..d5d82dd1cb 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -49,14 +49,11 @@ 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 - * optimistic locking. + * This is a JMX bean representing current transaction. It contains transaction + * identifier, unique version and parent version for optimistic locking. */ -class ConfigTransactionControllerImpl implements - ConfigTransactionControllerInternal, - ConfigTransactionControllerImplMXBean, - Identifiable { +public class ConfigTransactionControllerImpl implements ConfigTransactionControllerInternal, + ConfigTransactionControllerImplMXBean, Identifiable { private static final Logger LOG = LoggerFactory.getLogger(ConfigTransactionControllerImpl.class); private final ConfigTransactionLookupRegistry txLookupRegistry; @@ -75,8 +72,7 @@ class ConfigTransactionControllerImpl implements * during validation. */ @GuardedBy("this") - private final AtomicBoolean configBeanModificationDisabled = new AtomicBoolean( - false); + private final AtomicBoolean configBeanModificationDisabled = new AtomicBoolean(false); private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl( configBeanModificationDisabled); private final MBeanServer configMBeanServer; @@ -87,10 +83,10 @@ class ConfigTransactionControllerImpl implements private final SearchableServiceReferenceWritableRegistry writableSRRegistry; public ConfigTransactionControllerImpl(final ConfigTransactionLookupRegistry txLookupRegistry, - final long parentVersion, final BindingContextProvider bindingContextProvider, final long currentVersion, - final Map> currentlyRegisteredFactories, - final MBeanServer transactionsMBeanServer, final MBeanServer configMBeanServer, - final boolean blankTransaction, final SearchableServiceReferenceWritableRegistry writableSRRegistry) { + final long parentVersion, final BindingContextProvider bindingContextProvider, final long currentVersion, + final Map> currentlyRegisteredFactories, + final MBeanServer transactionsMBeanServer, final MBeanServer configMBeanServer, + final boolean blankTransaction, final SearchableServiceReferenceWritableRegistry writableSRRegistry) { this.txLookupRegistry = txLookupRegistry; String transactionName = txLookupRegistry.getTransactionIdentifier().getName(); this.controllerON = ObjectNameUtil.createTransactionControllerON(transactionName); @@ -108,7 +104,8 @@ class ConfigTransactionControllerImpl implements } @Override - public void copyExistingModulesAndProcessFactoryDiff(final Collection existingModules, final List lastListOfFactories) { + public void copyExistingModulesAndProcessFactoryDiff(final Collection existingModules, + final List lastListOfFactories) { // copy old configuration to this server for (ModuleInternalInfo oldConfigInfo : existingModules) { try { @@ -145,7 +142,8 @@ class ConfigTransactionControllerImpl implements Set 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 + // 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 { @@ -155,14 +153,16 @@ class ConfigTransactionControllerImpl implements } catch (final InstanceAlreadyExistsException e) { throw new IllegalStateException(e); } - // register default module as every possible service - final Set serviceInterfaceAnnotations = InterfacesHelper.getServiceInterfaceAnnotations(moduleFactory); + final Set 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); + throw new IllegalStateException( + "Unable to register default module instance " + module + " as a service of " + qname, + e); } } } @@ -173,23 +173,26 @@ class ConfigTransactionControllerImpl implements 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); + 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 (final InstanceNotFoundException e) { - throw new IllegalStateException("Unable to UNregister default module instance " + name + " as a service of " + qname, e); + throw new IllegalStateException( + "Unable to UNregister default module instance " + name + " as a service of " + qname, + e); } } - // close module destroyModule(name); } } } - + @SuppressWarnings("IllegalCatch") private synchronized void copyExistingModule(final ModuleInternalInfo oldConfigBeanInfo) throws InstanceAlreadyExistsException { @@ -211,16 +214,14 @@ class ConfigTransactionControllerImpl implements DependencyResolver dependencyResolver = dependencyResolverManager.getOrCreate(moduleIdentifier); try { - module = moduleFactory.createModule( - moduleIdentifier.getInstanceName(), dependencyResolver, + module = moduleFactory.createModule(moduleIdentifier.getInstanceName(), dependencyResolver, oldConfigBeanInfo.getReadableModule(), bc); } catch (final Exception e) { - throw new IllegalStateException(String.format( - "Error while copying old configuration from %s to %s", + throw new IllegalStateException(String.format("Error while copying old configuration from %s to %s", oldConfigBeanInfo, moduleFactory), e); } - putConfigBeanToJMXAndInternalMaps(moduleIdentifier, module, moduleFactory, oldConfigBeanInfo, dependencyResolver, - oldConfigBeanInfo.isDefaultBean(), bc); + putConfigBeanToJMXAndInternalMaps(moduleIdentifier, module, moduleFactory, oldConfigBeanInfo, + dependencyResolver, oldConfigBeanInfo.isDefaultBean(), bc); } @Override @@ -237,11 +238,10 @@ class ConfigTransactionControllerImpl implements DependencyResolver dependencyResolver = dependencyResolverManager.getOrCreate(moduleIdentifier); BundleContext bundleContext = getModuleFactoryBundleContext(moduleFactory.getImplementationName()); - Module module = moduleFactory.createModule(instanceName, dependencyResolver, - bundleContext); + Module module = moduleFactory.createModule(instanceName, dependencyResolver, bundleContext); boolean defaultBean = false; - return putConfigBeanToJMXAndInternalMaps(moduleIdentifier, module, - moduleFactory, null, dependencyResolver, defaultBean, bundleContext); + return putConfigBeanToJMXAndInternalMaps(moduleIdentifier, module, moduleFactory, null, dependencyResolver, + defaultBean, bundleContext); } @Override @@ -252,43 +252,40 @@ class ConfigTransactionControllerImpl implements ObjectNameUtil.checkDomain(objectName); ModuleIdentifier moduleIdentifier = ObjectNameUtil.fromON(objectName, ObjectNameUtil.TYPE_MODULE); - ModuleInternalTransactionalInfo txInfo = dependencyResolverManager.findModuleInternalTransactionalInfo(moduleIdentifier); + ModuleInternalTransactionalInfo txInfo = dependencyResolverManager + .findModuleInternalTransactionalInfo(moduleIdentifier); Module realModule = txInfo.getRealModule(); - if(realModule instanceof AbstractModule) { - ((AbstractModule)realModule).setCanReuseInstance(false); + if (realModule instanceof AbstractModule) { + ((AbstractModule) realModule).setCanReuseInstance(false); } } - private synchronized ObjectName putConfigBeanToJMXAndInternalMaps( - final ModuleIdentifier moduleIdentifier, final Module module, - final ModuleFactory moduleFactory, + private synchronized ObjectName putConfigBeanToJMXAndInternalMaps(final ModuleIdentifier moduleIdentifier, + final Module module, final ModuleFactory moduleFactory, @Nullable final ModuleInternalInfo maybeOldConfigBeanInfo, final DependencyResolver dependencyResolver, - final boolean isDefaultBean, final BundleContext bundleContext) - throws InstanceAlreadyExistsException { + final boolean isDefaultBean, final BundleContext bundleContext) throws InstanceAlreadyExistsException { 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()); + throw new IllegalStateException("Incorrect name reported by module. Expected " + moduleIdentifier + ", got " + + module.getIdentifier()); } 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().getName(), - readOnlyAtomicBoolean, transactionsMBeanServer, + DynamicMBean writableDynamicWrapper = new DynamicWritableWrapper(module, moduleIdentifier, + getTransactionIdentifier().getName(), readOnlyAtomicBoolean, transactionsMBeanServer, configMBeanServer); - ObjectName writableON = ObjectNameUtil.createTransactionModuleON( - getTransactionIdentifier().getName(), moduleIdentifier); + ObjectName writableON = ObjectNameUtil.createTransactionModuleON(getTransactionIdentifier().getName(), + moduleIdentifier); // put wrapper to jmx TransactionModuleJMXRegistration transactionModuleJMXRegistration = getTxModuleJMXRegistrator() .registerMBean(writableDynamicWrapper, writableON); - dependencyResolverManager.put( - moduleIdentifier, module, moduleFactory, - maybeOldConfigBeanInfo, transactionModuleJMXRegistration, isDefaultBean, bundleContext); + dependencyResolverManager.put(moduleIdentifier, module, moduleFactory, maybeOldConfigBeanInfo, + transactionModuleJMXRegistration, isDefaultBean, bundleContext); return writableON; } @@ -296,27 +293,17 @@ class ConfigTransactionControllerImpl implements public synchronized void destroyModule(final ObjectName objectName) throws InstanceNotFoundException { checkTransactionName(objectName); ObjectNameUtil.checkDomain(objectName); - ModuleIdentifier moduleIdentifier = ObjectNameUtil.fromON(objectName, - ObjectNameUtil.TYPE_MODULE); + ModuleIdentifier moduleIdentifier = ObjectNameUtil.fromON(objectName, ObjectNameUtil.TYPE_MODULE); destroyModule(moduleIdentifier); } - private void checkTransactionName(final ObjectName objectName) { - String foundTransactionName = ObjectNameUtil - .getTransactionName(objectName); - if (!getTransactionIdentifier().getName().equals(foundTransactionName)) { - throw new IllegalArgumentException("Wrong transaction name " - + objectName); - } - } - private synchronized void destroyModule(final ModuleIdentifier moduleIdentifier) { LOG.debug("Destroying module {} in transaction {}", moduleIdentifier, this); transactionStatus.checkNotAborted(); - ModuleInternalTransactionalInfo found = dependencyResolverManager.findModuleInternalTransactionalInfo(moduleIdentifier); - if (!blankTransaction && - found.isDefaultBean()) { + ModuleInternalTransactionalInfo found = dependencyResolverManager + .findModuleInternalTransactionalInfo(moduleIdentifier); + if (!blankTransaction && found.isDefaultBean()) { LOG.warn("Warning: removing default bean. This will be forbidden in next version of config-subsystem"); } // first remove refNames, it checks for objectname existence @@ -334,6 +321,13 @@ class ConfigTransactionControllerImpl implements removedTInfo.getTransactionModuleJMXRegistration().close(); } + private void checkTransactionName(final ObjectName objectName) { + String foundTransactionName = ObjectNameUtil.getTransactionName(objectName); + if (!getTransactionIdentifier().getName().equals(foundTransactionName)) { + throw new IllegalArgumentException("Wrong transaction name " + objectName); + } + } + @Override public long getParentVersion() { return parentVersion; @@ -357,27 +351,24 @@ class ConfigTransactionControllerImpl implements } } + @SuppressWarnings("IllegalCatch") private void validateNoLocks() throws ValidationException { transactionStatus.checkNotAborted(); LOG.trace("Validating transaction {}", getTransactionIdentifier()); // call validate() List collectedExceptions = new ArrayList<>(); - for (Entry entry : dependencyResolverManager - .getAllModules().entrySet()) { + for (Entry entry : dependencyResolverManager.getAllModules().entrySet()) { ModuleIdentifier name = entry.getKey(); Module module = entry.getValue(); try { module.validate(); } catch (final Exception e) { - LOG.warn("Validation exception in {}", getTransactionName(), - e); - collectedExceptions.add(ValidationException - .createForSingleException(name, e)); + LOG.warn("Validation exception in {}", getTransactionName(), e); + collectedExceptions.add(ValidationException.createForSingleException(name, e)); } } if (!collectedExceptions.isEmpty()) { - throw ValidationException - .createFromCollectedValidationExceptions(collectedExceptions); + throw ValidationException.createFromCollectedValidationExceptions(collectedExceptions); } LOG.trace("Validated transaction {}", getTransactionIdentifier()); } @@ -385,13 +376,12 @@ class ConfigTransactionControllerImpl implements /** * If this method passes validation, it will grab * {@link TransactionStatus#secondPhaseCommitStarted} lock. This lock will - * prevent calling @{link #validateBeforeCommitAndLockTransaction}, - * effectively only allowing to call {@link #secondPhaseCommit} after - * successful return of this method. + * prevent calling @{link #validateBeforeCommitAndLockTransaction}, effectively + * only allowing to call {@link #secondPhaseCommit} after successful return of + * this method. */ @Override - public synchronized CommitInfo validateBeforeCommitAndLockTransaction() - throws ValidationException { + public synchronized CommitInfo validateBeforeCommitAndLockTransaction() throws ValidationException { transactionStatus.checkNotAborted(); transactionStatus.checkNotCommitStarted(); configBeanModificationDisabled.set(true); @@ -418,34 +408,28 @@ class ConfigTransactionControllerImpl implements transactionStatus.checkCommitStarted(); if (!configBeanModificationDisabled.get()) { throw new IllegalStateException( - "Internal error - validateBeforeCommitAndLockTransaction should be called " - + "to obtain a lock"); + "Internal error - validateBeforeCommitAndLockTransaction should be called " + "to obtain a lock"); } 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()); + // 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); - try { - LOG.debug("About to commit {} in transaction {}", - moduleIdentifier, getTransactionIdentifier()); - AutoCloseable instance = module.getInstance(); - Preconditions.checkNotNull(instance, "Instance is null:{} in transaction {}", moduleIdentifier, getTransactionIdentifier()); - } catch (final Exception e) { - LOG.error("Commit failed on {} in transaction {}", moduleIdentifier, - getTransactionIdentifier(), e); - internalAbort(); - throw new IllegalStateException( - String.format("Error - getInstance() failed for %s in transaction %s", - moduleIdentifier, getTransactionIdentifier()), e); - } + 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()); @@ -503,17 +487,17 @@ class ConfigTransactionControllerImpl implements * {@inheritDoc} */ @Override - public ObjectName lookupConfigBean(final String moduleName, final String instanceName) - throws InstanceNotFoundException { - return txLookupRegistry.lookupConfigBean(moduleName, instanceName); + public Set lookupConfigBeans(final String moduleName, final String instanceName) { + return txLookupRegistry.lookupConfigBeans(moduleName, instanceName); } /** * {@inheritDoc} */ @Override - public Set lookupConfigBeans(final String moduleName, final String instanceName) { - return txLookupRegistry.lookupConfigBeans(moduleName, instanceName); + public ObjectName lookupConfigBean(final String moduleName, final String instanceName) + throws InstanceNotFoundException { + return txLookupRegistry.lookupConfigBean(moduleName, instanceName); } /** @@ -524,7 +508,6 @@ class ConfigTransactionControllerImpl implements txLookupRegistry.checkConfigBeanExists(objectName); } - /** * {@inheritDoc} */ @@ -537,8 +520,7 @@ class ConfigTransactionControllerImpl implements * {@inheritDoc} */ @Override - public Set lookupRuntimeBeans(final String moduleName, - final String instanceName) { + public Set lookupRuntimeBeans(final String moduleName, final String instanceName) { return txLookupRegistry.lookupRuntimeBeans(moduleName, instanceName); } @@ -559,10 +541,7 @@ class ConfigTransactionControllerImpl implements @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("transactionName="); - sb.append(getTransactionName()); - return sb.toString(); + return "transactionName=" + getTransactionName(); } // @VisibleForTesting @@ -587,7 +566,8 @@ class ConfigTransactionControllerImpl implements @Override public BundleContext getModuleFactoryBundleContext(final String factoryName) { - Map.Entry factoryBundleContextEntry = this.currentlyRegisteredFactories.get(factoryName); + Map.Entry factoryBundleContextEntry = this.currentlyRegisteredFactories + .get(factoryName); if (factoryBundleContextEntry == null || factoryBundleContextEntry.getValue() == null) { throw new NullPointerException("Bundle context of " + factoryName + " ModuleFactory not found."); } @@ -596,9 +576,9 @@ class ConfigTransactionControllerImpl implements // service reference functionality: - @Override - public synchronized ObjectName lookupConfigBeanByServiceInterfaceName(final String serviceInterfaceQName, final String refName) { + public synchronized ObjectName lookupConfigBeanByServiceInterfaceName(final String serviceInterfaceQName, + final String refName) { return writableSRRegistry.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName); } @@ -608,12 +588,14 @@ class ConfigTransactionControllerImpl implements } @Override - public synchronized Map lookupServiceReferencesByServiceInterfaceName(final String serviceInterfaceQName) { + public synchronized Map lookupServiceReferencesByServiceInterfaceName( + final String serviceInterfaceQName) { return writableSRRegistry.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName); } @Override - public synchronized Set lookupServiceInterfaceNames(final ObjectName objectName) throws InstanceNotFoundException { + public synchronized Set lookupServiceInterfaceNames(final ObjectName objectName) + throws InstanceNotFoundException { return writableSRRegistry.lookupServiceInterfaceNames(objectName); } @@ -623,12 +605,14 @@ class ConfigTransactionControllerImpl implements } @Override - public synchronized ObjectName saveServiceReference(final String serviceInterfaceName, final String refName, final ObjectName moduleON) throws InstanceNotFoundException { + public synchronized ObjectName saveServiceReference(final String serviceInterfaceName, final String refName, + final ObjectName moduleON) throws InstanceNotFoundException { return writableSRRegistry.saveServiceReference(serviceInterfaceName, refName, moduleON); } @Override - public synchronized void removeServiceReference(final String serviceInterfaceName, final String refName) throws InstanceNotFoundException { + public synchronized void removeServiceReference(final String serviceInterfaceName, final String refName) + throws InstanceNotFoundException { writableSRRegistry.removeServiceReference(serviceInterfaceName, refName); } @@ -643,7 +627,7 @@ class ConfigTransactionControllerImpl implements } @Override - public SearchableServiceReferenceWritableRegistry getWritableRegistry() { + public SearchableServiceReferenceWritableRegistry getWritableRegistry() { return writableSRRegistry; } @@ -663,7 +647,8 @@ class ConfigTransactionControllerImpl implements } @Override - public ObjectName getServiceReference(final String serviceInterfaceQName, final String refName) throws InstanceNotFoundException { + public ObjectName getServiceReference(final String serviceInterfaceQName, final String refName) + throws InstanceNotFoundException { return writableSRRegistry.getServiceReference(serviceInterfaceQName, refName); } }