use of org.opendaylight.controller.config.api.ModuleIdentifier in project controller by opendaylight.
the class DynamicWritableWrapperTest method testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON.
/*
* Try to call setter with ObjectName containing transaction name. Verify that
* ObjectName without transaction name was actually passed on the config bean.
*/
@Test
public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON() throws Exception {
TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory.createModule("", null, null);
ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp", "parallel");
ObjectName dynON2 = ObjectNameUtil.createReadOnlyModuleON(moduleIdentifier2);
AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean, moduleIdentifier2);
platformMBeanServer.registerMBean(dyn, dynON2);
try {
TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(platformMBeanServer, dynON2, TestingParallelAPSPConfigMXBean.class);
ObjectName withTransactionName = ObjectNameUtil.createTransactionModuleON("transaction1", "moduleName", "instanceName");
proxy.setThreadPool(withTransactionName);
ObjectName withoutTransactionName = ObjectNameUtil.withoutTransactionName(withTransactionName);
assertEquals(withoutTransactionName, proxy.getThreadPool());
} finally {
platformMBeanServer.unregisterMBean(dynON2);
}
}
use of org.opendaylight.controller.config.api.ModuleIdentifier in project controller by opendaylight.
the class ObjectNameUtilTest method testModuleIdentifier.
@Test
public void testModuleIdentifier() throws Exception {
ModuleIdentifier mi = new ModuleIdentifier(moduleName, instanceName);
ObjectName on = ObjectNameUtil.createReadOnlyModuleON(mi);
assertEquals(moduleName, ObjectNameUtil.getFactoryName(on));
assertEquals(instanceName, ObjectNameUtil.getInstanceName(on));
assertEquals(mi, ObjectNameUtil.fromON(on, ObjectNameUtil.TYPE_MODULE));
}
use of org.opendaylight.controller.config.api.ModuleIdentifier in project controller by opendaylight.
the class AbsFactoryGeneratedObjectFactory method toGeneratedObject.
public GeneratedObject toGeneratedObject(FullyQualifiedName absFactoryFQN, Optional<String> copyright, Optional<String> header, Optional<String> classJavaDoc, QName yangModuleQName, String globallyUniqueName, List<FullyQualifiedName> providedServices, FullyQualifiedName moduleFQN, List<Field> moduleFields) {
JavaFileInputBuilder b = new JavaFileInputBuilder();
b.addImportFQN(new FullyQualifiedName(Module.class));
b.addImportFQN(new FullyQualifiedName(ModuleIdentifier.class));
Annotation moduleQNameAnnotation = Annotation.createModuleQNameANnotation(yangModuleQName);
b.addClassAnnotation(moduleQNameAnnotation);
b.setFqn(absFactoryFQN);
b.setTypeName(TypeName.absClassType);
b.setCopyright(copyright);
b.setHeader(header);
b.setClassJavaDoc(classJavaDoc);
b.addImplementsFQN(new FullyQualifiedName(ModuleFactory.class));
if (classJavaDoc.isPresent()) {
b.addClassAnnotation(format("@%s(value=\"%s\")", Description.class.getCanonicalName(), classJavaDoc.get()));
}
b.addToBody(format("public static final java.lang.String NAME = \"%s\";", globallyUniqueName));
b.addToBody(format("private static final java.util.Set<Class<? extends %s>> serviceIfcs;", AbstractServiceInterface.class.getCanonicalName()));
b.addToBody("@Override\n public final String getImplementationName() { \n return NAME; \n}");
b.addToBody(getServiceIfcsInitialization(providedServices));
// createModule
b.addToBody(format("\n" + "@Override\n" + "public %s createModule(String instanceName, %s dependencyResolver, %s bundleContext) {\n" + "return instantiateModule(instanceName, dependencyResolver, bundleContext);\n" + "}\n", Module.class.getCanonicalName(), DependencyResolver.class.getCanonicalName(), BUNDLE_CONTEXT));
b.addToBody(getCreateModule(moduleFQN, moduleFields));
b.addToBody(format("\n" + "public %s instantiateModule(String instanceName, %s dependencyResolver, %s oldModule, %s oldInstance, %s bundleContext) {\n" + "return new %s(new %s(NAME, instanceName), dependencyResolver, oldModule, oldInstance);\n" + "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), moduleFQN, AutoCloseable.class.getCanonicalName(), BUNDLE_CONTEXT, moduleFQN, ModuleIdentifier.class.getCanonicalName()));
b.addToBody(format("\n" + "public %s instantiateModule(String instanceName, %s dependencyResolver, %s bundleContext) {\n" + "return new %s(new %s(NAME, instanceName), dependencyResolver);\n" + "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), BUNDLE_CONTEXT, moduleFQN, ModuleIdentifier.class.getCanonicalName()));
b.addToBody(format("\n" + "public %s handleChangedClass(%s dependencyResolver, %s old, %s bundleContext) throws Exception {\n" + // "// @Deprecated return handleChangedClass(old);\n" +
"String instanceName = old.getModule().getIdentifier().getInstanceName();\n" + "%1$s newModule = new %1$s(new ModuleIdentifier(NAME, instanceName), dependencyResolver);\n" + "Module oldModule = old.getModule();\n" + "Class<? extends Module> oldModuleClass = oldModule.getClass();\n" + genCodeToCopyAttributes(moduleFields) + "return newModule;\n" + "}\n", moduleFQN, DependencyResolver.class.getCanonicalName(), DynamicMBeanWithInstance.class.getCanonicalName(), BUNDLE_CONTEXT));
b.addToBody(format("\n@Deprecated\n" + "public %s handleChangedClass(%s old) throws Exception {\n" + "throw new UnsupportedOperationException(\"Class reloading is not supported\");\n" + "}\n", moduleFQN, DynamicMBeanWithInstance.class.getCanonicalName()));
b.addToBody(format("\n" + "@Override\n" + "public java.util.Set<%s> getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory dependencyResolverFactory, %s bundleContext) {\n" + "return new java.util.HashSet<>();\n" + "}\n", moduleFQN, BUNDLE_CONTEXT));
return new GeneratedObjectBuilder(b.build()).toGeneratedObject();
}
use of org.opendaylight.controller.config.api.ModuleIdentifier in project controller by opendaylight.
the class ConfigRegistryImpl method secondPhaseCommit.
@GuardedBy("configTransactionLock")
private CommitStatus secondPhaseCommit(final ConfigTransactionControllerInternal configTransactionController, final CommitInfo commitInfo, final ConfigTransactionLookupRegistry txLookupRegistry) {
// (hopefully) runtime beans
for (DestroyedModule toBeDestroyed : commitInfo.getDestroyedFromPreviousTransactions()) {
// closes instance (which should close
// runtime jmx registrator),
// also closes osgi registration and ModuleJMXRegistrator
// registration
toBeDestroyed.close();
currentConfig.remove(toBeDestroyed.getIdentifier());
}
// set RuntimeBeanRegistrators on beans implementing
// RuntimeBeanRegistratorAwareModule
Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted().values()) {
// set runtime jmx registrator if required
Module module = entry.getProxiedModule();
RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = null;
if (module instanceof RuntimeBeanRegistratorAwareModule) {
if (entry.hasOldModule()) {
if (module.canReuse(entry.getOldInternalInfo().getReadableModule().getModule())) {
runtimeBeanRegistrator = entry.getOldInternalInfo().getRuntimeBeanRegistrator();
((RuntimeBeanRegistratorAwareModule) module).setRuntimeBeanRegistrator(runtimeBeanRegistrator);
} else {
runtimeBeanRegistrator = baseJMXRegistrator.createRuntimeBeanRegistrator(entry.getIdentifier());
entry.getOldInternalInfo().getRuntimeBeanRegistrator().close();
((RuntimeBeanRegistratorAwareModule) module).setRuntimeBeanRegistrator(runtimeBeanRegistrator);
}
} else {
runtimeBeanRegistrator = baseJMXRegistrator.createRuntimeBeanRegistrator(entry.getIdentifier());
((RuntimeBeanRegistratorAwareModule) module).setRuntimeBeanRegistrator(runtimeBeanRegistrator);
}
}
// save it to info so it is accessible afterwards
if (runtimeBeanRegistrator != null) {
runtimeRegistrators.put(entry.getIdentifier(), runtimeBeanRegistrator);
}
}
// can register runtime beans
List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController.secondPhaseCommit();
txLookupRegistry.close();
configTransactionController.close();
// copy configuration to read only mode
List<ObjectName> newInstances = new LinkedList<>();
List<ObjectName> reusedInstances = new LinkedList<>();
List<ObjectName> recreatedInstances = new LinkedList<>();
Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
int orderingIdx = 0;
for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
LOG.trace("Registering {}", moduleIdentifier);
ModuleInternalTransactionalInfo entry = commitInfo.getCommitted().get(moduleIdentifier);
if (entry == null) {
throw new NullPointerException("Module not found " + moduleIdentifier);
}
ObjectName primaryReadOnlyON = ObjectNameUtil.createReadOnlyModuleON(moduleIdentifier);
// determine if current instance was recreated or reused or is new
// rules for closing resources:
// osgi registration - will be reused if possible.
// module jmx registration - will be (re)created every time, needs
// to be closed here
// runtime jmx registration - should be taken care of by module
// itself
// instance - is closed only if it was destroyed
ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator.createModuleJMXRegistrator();
OsgiRegistration osgiRegistration = null;
AutoCloseable instance = entry.getProxiedModule().getInstance();
if (entry.hasOldModule()) {
ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo.getReadableModule();
currentConfig.remove(entry.getIdentifier());
// test if old instance == new instance
if (oldReadableConfigBean.getInstance().equals(instance)) {
// reused old instance:
// wrap in readable dynamic mbean
reusedInstances.add(primaryReadOnlyON);
osgiRegistration = oldInternalInfo.getOsgiRegistration();
} else {
// recreated instance:
// it is responsibility of module to call the old instance -
// we just need to unregister configbean
recreatedInstances.add(primaryReadOnlyON);
// close old osgi registration
oldInternalInfo.getOsgiRegistration().close();
}
// close old module jmx registrator
oldInternalInfo.getModuleJMXRegistrator().close();
// We no longer need old internal info. Clear it out, so we do not create a
// serial leak evidenced
// by BUG-4514. The reason is that modules retain their resolver, which retains
// modules. If we retain
// the old module, we would have the complete reconfiguration history held in
// heap for no good reason.
entry.clearOldInternalInfo();
} else {
// new instance:
// wrap in readable dynamic mbean
newInstances.add(primaryReadOnlyON);
}
Module realModule = entry.getRealModule();
DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(realModule, instance, moduleIdentifier, registryMBeanServer, configMBeanServer);
// register to JMX
try {
newModuleJMXRegistrator.registerMBean(newReadableConfigBean, primaryReadOnlyON);
} catch (final InstanceAlreadyExistsException e) {
throw new IllegalStateException("Possible code error, already registered:" + primaryReadOnlyON, e);
}
// register services to OSGi
Map<ServiceInterfaceAnnotation, String> annotationMapping = configTransactionController.getWritableRegistry().findServiceInterfaces(moduleIdentifier);
BundleContext bc = configTransactionController.getModuleFactoryBundleContext(entry.getModuleFactory().getImplementationName());
if (osgiRegistration == null) {
osgiRegistration = beanToOsgiServiceManager.registerToOsgi(newReadableConfigBean.getInstance(), moduleIdentifier, bc, annotationMapping);
} else {
osgiRegistration.updateRegistrations(annotationMapping, bc, instance);
}
RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators.get(entry.getIdentifier());
ModuleInternalInfo newInfo = new ModuleInternalInfo(entry.getIdentifier(), newReadableConfigBean, osgiRegistration, runtimeBeanRegistrator, newModuleJMXRegistrator, orderingIdx, entry.isDefaultBean(), entry.getModuleFactory(), entry.getBundleContext());
newConfigEntries.put(realModule, newInfo);
orderingIdx++;
}
currentConfig.addAll(newConfigEntries.values());
// update version
version = configTransactionController.getVersion();
// switch readable Service Reference Registry
synchronized (readableSRRegistryLock) {
readableSRRegistry.close();
readableSRRegistry = ServiceReferenceRegistryImpl.createSRReadableRegistry(configTransactionController.getWritableRegistry(), this, baseJMXRegistrator);
}
return new CommitStatus(newInstances, reusedInstances, recreatedInstances);
}
use of org.opendaylight.controller.config.api.ModuleIdentifier 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;
}
Aggregations