2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl;
10 import org.opendaylight.controller.config.api.ConflictingVersionException;
11 import org.opendaylight.controller.config.api.ModuleIdentifier;
12 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
13 import org.opendaylight.controller.config.api.ServiceReferenceWritableRegistry;
14 import org.opendaylight.controller.config.api.ValidationException;
15 import org.opendaylight.controller.config.api.jmx.CommitStatus;
16 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
17 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
18 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder;
19 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
20 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
21 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
22 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
23 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
24 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager;
25 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
26 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
27 import org.opendaylight.controller.config.manager.impl.util.ModuleQNameUtil;
28 import org.opendaylight.controller.config.spi.Module;
29 import org.opendaylight.controller.config.spi.ModuleFactory;
30 import org.osgi.framework.BundleContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import javax.annotation.concurrent.GuardedBy;
35 import javax.annotation.concurrent.NotThreadSafe;
36 import javax.annotation.concurrent.ThreadSafe;
37 import javax.management.InstanceAlreadyExistsException;
38 import javax.management.InstanceNotFoundException;
39 import javax.management.MBeanServer;
40 import javax.management.MBeanServerFactory;
41 import javax.management.ObjectName;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.Iterator;
47 import java.util.LinkedList;
48 import java.util.List;
50 import java.util.Map.Entry;
54 * Singleton that is responsible for creating and committing Config
55 * Transactions. It is registered in Platform MBean Server.
58 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
59 private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
61 private final ModuleFactoriesResolver resolver;
62 private final MBeanServer configMBeanServer;
65 private long version = 0;
67 private long versionCounter = 0;
70 * Contains current configuration in form of {moduleName:{instanceName,read
71 * only module}} for copying state to new transaction. Each running module
72 * is present just once, no matter how many interfaces it exposes.
75 private final ConfigHolder currentConfig = new ConfigHolder();
78 * Will return true unless there was a transaction that succeeded during
79 * validation but failed in second phase of commit. In this case the server
80 * is unstable and its state is undefined.
83 private boolean isHealthy = true;
86 * Holds Map<transactionName, transactionController> and purges it each time
87 * its content is requested.
90 private final TransactionsHolder transactionsHolder = new TransactionsHolder();
92 private final BaseJMXRegistrator baseJMXRegistrator;
94 private final BeanToOsgiServiceManager beanToOsgiServiceManager;
96 // internal jmx server for read only beans
97 private final MBeanServer registryMBeanServer;
98 // internal jmx server shared by all transactions
99 private final MBeanServer transactionsMBeanServer;
101 // Used for finding new factory instances for default module functionality
103 private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
105 @GuardedBy("this") // switched in every 2ndPC
106 private CloseableServiceReferenceReadableRegistry readableSRRegistry = ServiceReferenceRegistryImpl.createInitialSRLookupRegistry();
109 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
110 MBeanServer configMBeanServer) {
111 this(resolver, configMBeanServer,
112 new BaseJMXRegistrator(configMBeanServer));
116 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
117 MBeanServer configMBeanServer,
118 BaseJMXRegistrator baseJMXRegistrator) {
119 this.resolver = resolver;
120 this.beanToOsgiServiceManager = new BeanToOsgiServiceManager();
121 this.configMBeanServer = configMBeanServer;
122 this.baseJMXRegistrator = baseJMXRegistrator;
123 this.registryMBeanServer = MBeanServerFactory
124 .createMBeanServer("ConfigRegistry" + configMBeanServer.getDefaultDomain());
125 this.transactionsMBeanServer = MBeanServerFactory
126 .createMBeanServer("ConfigTransactions" + configMBeanServer.getDefaultDomain());
130 * Create new {@link ConfigTransactionControllerImpl }
133 public synchronized ObjectName beginConfig() {
134 return beginConfig(false);
138 * @param blankTransaction true if this transaction is created automatically by
139 * org.opendaylight.controller.config.manager.impl.osgi.BlankTransactionServiceTracker
141 public synchronized ObjectName beginConfig(boolean blankTransaction) {
142 return beginConfigInternal(blankTransaction).getControllerObjectName();
145 private synchronized ConfigTransactionControllerInternal beginConfigInternal(boolean blankTransaction) {
147 final String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
149 TransactionJMXRegistratorFactory factory = new TransactionJMXRegistratorFactory() {
151 public TransactionJMXRegistrator create() {
152 return baseJMXRegistrator.createTransactionJMXRegistrator(transactionName);
156 Map<String, Map.Entry<ModuleFactory, BundleContext>> allCurrentFactories = Collections.unmodifiableMap(
157 resolver.getAllFactories());
158 ConfigTransactionLookupRegistry txLookupRegistry = new ConfigTransactionLookupRegistry(new TransactionIdentifier(
159 transactionName), factory, allCurrentFactories);
160 ServiceReferenceWritableRegistry writableRegistry = ServiceReferenceRegistryImpl.createSRWritableRegistry(
161 readableSRRegistry, txLookupRegistry, allCurrentFactories);
163 ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
164 txLookupRegistry, version,
165 versionCounter, allCurrentFactories, transactionsMBeanServer,
166 configMBeanServer, blankTransaction, writableRegistry);
168 txLookupRegistry.registerMBean(transactionController, transactionController.getControllerObjectName());
169 } catch (InstanceAlreadyExistsException e) {
170 throw new IllegalStateException(e);
172 transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
173 transactionsHolder.add(transactionName, transactionController);
174 return transactionController;
181 public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
182 throws ConflictingVersionException, ValidationException {
183 final String transactionName = ObjectNameUtil
184 .getTransactionName(transactionControllerON);
185 logger.trace("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
187 // find ConfigTransactionController
188 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder.getCurrentTransactions();
189 ConfigTransactionControllerInternal configTransactionController = transactions.get(transactionName);
190 if (configTransactionController == null) {
191 throw new IllegalArgumentException(String.format(
192 "Transaction with name '%s' not found", transactionName));
194 // check optimistic lock
195 if (version != configTransactionController.getParentVersion()) {
196 throw new ConflictingVersionException(
198 "Optimistic lock failed. Expected parent version %d, was %d",
200 configTransactionController.getParentVersion()));
202 // optimistic lock ok
204 CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
205 lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
206 // non recoverable from here:
208 return secondPhaseCommit(
209 configTransactionController, commitInfo);
210 } catch (Throwable t) { // some libs throw Errors: e.g.
211 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
213 logger.error("Configuration Transaction failed on 2PC, server is unhealthy", t);
214 if (t instanceof RuntimeException) {
215 throw (RuntimeException) t;
216 } else if (t instanceof Error) {
219 throw new RuntimeException(t);
224 private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
225 CommitInfo commitInfo) {
227 // close instances which were destroyed by the user, including
228 // (hopefully) runtime beans
229 for (DestroyedModule toBeDestroyed : commitInfo
230 .getDestroyedFromPreviousTransactions()) {
231 toBeDestroyed.close(); // closes instance (which should close
232 // runtime jmx registrator),
233 // also closes osgi registration and ModuleJMXRegistrator
235 currentConfig.remove(toBeDestroyed.getIdentifier());
238 // set RuntimeBeanRegistrators on beans implementing
239 // RuntimeBeanRegistratorAwareModule, each module
240 // should have exactly one runtime jmx registrator.
241 Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
242 for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted()
244 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
245 if (entry.hasOldModule() == false) {
246 runtimeBeanRegistrator = baseJMXRegistrator
247 .createRuntimeBeanRegistrator(entry.getIdentifier());
249 // reuse old JMX registrator
250 runtimeBeanRegistrator = entry.getOldInternalInfo()
251 .getRuntimeBeanRegistrator();
253 // set runtime jmx registrator if required
254 Module module = entry.getModule();
255 if (module instanceof RuntimeBeanRegistratorAwareModule) {
256 ((RuntimeBeanRegistratorAwareModule) module)
257 .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
259 // save it to info so it is accessible afterwards
260 runtimeRegistrators.put(entry.getIdentifier(), runtimeBeanRegistrator);
263 // can register runtime beans
264 List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
265 .secondPhaseCommit();
267 // copy configuration to read only mode
268 List<ObjectName> newInstances = new LinkedList<>();
269 List<ObjectName> reusedInstances = new LinkedList<>();
270 List<ObjectName> recreatedInstances = new LinkedList<>();
272 Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
275 for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
276 ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
277 .get(moduleIdentifier);
279 throw new NullPointerException("Module not found "
281 Module module = entry.getModule();
282 ObjectName primaryReadOnlyON = ObjectNameUtil
283 .createReadOnlyModuleON(moduleIdentifier);
285 // determine if current instance was recreated or reused or is new
287 // rules for closing resources:
288 // osgi registration - will be reused if possible.
289 // module jmx registration - will be (re)created every time, needs
291 // runtime jmx registration - should be taken care of by module
293 // instance - is closed only if it was destroyed
294 ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
295 .createModuleJMXRegistrator();
297 OsgiRegistration osgiRegistration = null;
298 if (entry.hasOldModule()) {
299 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
300 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo.getReadableModule();
301 currentConfig.remove(entry.getIdentifier());
303 // test if old instance == new instance
304 if (oldReadableConfigBean.getInstance().equals(module.getInstance())) {
305 // reused old instance:
306 // wrap in readable dynamic mbean
307 reusedInstances.add(primaryReadOnlyON);
308 osgiRegistration = oldInternalInfo.getOsgiRegistration();
310 // recreated instance:
311 // it is responsibility of module to call the old instance -
312 // we just need to unregister configbean
313 recreatedInstances.add(primaryReadOnlyON);
315 // close old osgi registration
316 oldInternalInfo.getOsgiRegistration().close();
319 // close old module jmx registrator
320 oldInternalInfo.getModuleJMXRegistrator().close();
323 // wrap in readable dynamic mbean
324 newInstances.add(primaryReadOnlyON);
327 DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
328 module, module.getInstance(), moduleIdentifier,
329 registryMBeanServer, configMBeanServer);
333 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
335 } catch (InstanceAlreadyExistsException e) {
336 throw new IllegalStateException(e);
340 if (osgiRegistration == null) {
341 ModuleFactory moduleFactory = entry.getModuleFactory();
342 if(moduleFactory != null) {
343 BundleContext bc = configTransactionController.
344 getModuleFactoryBundleContext(moduleFactory.getImplementationName());
345 osgiRegistration = beanToOsgiServiceManager.registerToOsgi(module.getClass(),
346 newReadableConfigBean.getInstance(), entry.getIdentifier(), bc);
348 throw new NullPointerException(entry.getIdentifier().getFactoryName() + " ModuleFactory not found.");
353 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators
354 .get(entry.getIdentifier());
355 ModuleInternalInfo newInfo = new ModuleInternalInfo(
356 entry.getIdentifier(), newReadableConfigBean, osgiRegistration,
357 runtimeBeanRegistrator, newModuleJMXRegistrator,
358 orderingIdx, entry.isDefaultBean());
360 newConfigEntries.put(module, newInfo);
363 currentConfig.addAll(newConfigEntries.values());
366 version = configTransactionController.getVersion();
368 // switch readable Service Reference Registry
369 this.readableSRRegistry.close();
370 this.readableSRRegistry = ServiceReferenceRegistryImpl.createSRReadableRegistry(
371 configTransactionController.getWritableRegistry(), this, baseJMXRegistrator);
373 return new CommitStatus(newInstances, reusedInstances,
381 public synchronized List<ObjectName> getOpenConfigs() {
382 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
383 .getCurrentTransactions();
384 List<ObjectName> result = new ArrayList<>(transactions.size());
385 for (ConfigTransactionControllerInternal configTransactionController : transactions
387 result.add(configTransactionController.getControllerObjectName());
393 * Abort open transactions and unregister read only modules. Since this
394 * class is not responsible for registering itself under
395 * {@link org.opendaylight.controller.config.api.ConfigRegistry#OBJECT_NAME}, it will not unregister itself
399 public synchronized void close() {
400 // abort transactions
401 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
402 .getCurrentTransactions();
403 for (ConfigTransactionControllerInternal configTransactionController : transactions
406 configTransactionController.abortConfig();
407 } catch (RuntimeException e) {
408 logger.warn("Ignoring exception while aborting {}",
409 configTransactionController, e);
413 // destroy all live objects one after another in order of the dependency
415 List<DestroyedModule> destroyedModules = currentConfig
416 .getModulesToBeDestroyed();
417 for (DestroyedModule destroyedModule : destroyedModules) {
418 destroyedModule.close();
420 // unregister MBeans that failed to unregister properly
421 baseJMXRegistrator.close();
422 // remove jmx servers
423 MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
424 MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
432 public long getVersion() {
440 public Set<String> getAvailableModuleNames() {
441 return new HierarchicalConfigMBeanFactoriesHolder(
442 resolver.getAllFactories()).getModuleNames();
449 public boolean isHealthy() {
459 public Set<ObjectName> lookupConfigBeans() {
460 return lookupConfigBeans("*", "*");
467 public Set<ObjectName> lookupConfigBeans(String moduleName) {
468 return lookupConfigBeans(moduleName, "*");
475 public ObjectName lookupConfigBean(String moduleName, String instanceName)
476 throws InstanceNotFoundException {
477 return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
484 public Set<ObjectName> lookupConfigBeans(String moduleName,
485 String instanceName) {
486 ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
488 return baseJMXRegistrator.queryNames(namePattern, null);
495 public Set<ObjectName> lookupRuntimeBeans() {
496 return lookupRuntimeBeans("*", "*");
503 public Set<ObjectName> lookupRuntimeBeans(String moduleName,
504 String instanceName) {
505 if (moduleName == null)
507 if (instanceName == null)
509 ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
510 moduleName, instanceName);
511 return baseJMXRegistrator.queryNames(namePattern, null);
515 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
516 ObjectNameUtil.checkDomain(objectName);
517 ObjectNameUtil.checkType(objectName, ObjectNameUtil.TYPE_MODULE);
518 String transactionName = ObjectNameUtil.getTransactionName(objectName);
519 if (transactionName != null) {
520 throw new IllegalArgumentException("Transaction attribute not supported in registry, wrong ObjectName: " + objectName);
522 // make sure exactly one match is found:
523 LookupBeansUtil.lookupConfigBean(this, ObjectNameUtil.getFactoryName(objectName), ObjectNameUtil.getInstanceName(objectName));
526 // service reference functionality:
528 public synchronized ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
529 return readableSRRegistry.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
533 public synchronized Map<String, Map<String, ObjectName>> getServiceMapping() {
534 return readableSRRegistry.getServiceMapping();
538 public synchronized Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
539 return readableSRRegistry.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
543 public synchronized Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
544 return readableSRRegistry.lookupServiceInterfaceNames(objectName);
548 public synchronized String getServiceInterfaceName(String namespace, String localName) {
549 return readableSRRegistry.getServiceInterfaceName(namespace, localName);
553 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
554 readableSRRegistry.checkServiceReferenceExists(objectName);
558 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
559 return readableSRRegistry.getServiceReference(serviceInterfaceQName, refName);
563 public Set<String> getAvailableModuleFactoryQNames() {
564 return ModuleQNameUtil.getQNames(resolver.getAllFactories());
568 public String toString() {
569 return "ConfigRegistryImpl{" +
570 "versionCounter=" + versionCounter +
571 ", version=" + version +
577 * Holds currently running modules
581 private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
584 * Add all modules to the internal map. Also add service instance to OSGi
587 public void addAll(Collection<ModuleInternalInfo> configInfos) {
588 if (currentConfig.size() > 0) {
589 throw new IllegalStateException(
590 "Error - some config entries were not removed: "
593 for (ModuleInternalInfo configInfo : configInfos) {
598 private void add(ModuleInternalInfo configInfo) {
599 ModuleInternalInfo oldValue = currentConfig.put(configInfo.getIdentifier(),
601 if (oldValue != null) {
602 throw new IllegalStateException(
603 "Cannot overwrite module with same name:"
604 + configInfo.getIdentifier() + ":" + configInfo);
609 * Remove entry from current config.
611 public void remove(ModuleIdentifier name) {
612 ModuleInternalInfo removed = currentConfig.remove(name);
613 if (removed == null) {
614 throw new IllegalStateException(
615 "Cannot remove from ConfigHolder - name not found:" + name);
619 public Collection<ModuleInternalInfo> getEntries() {
620 return currentConfig.values();
623 public List<DestroyedModule> getModulesToBeDestroyed() {
624 List<DestroyedModule> result = new ArrayList<>();
625 for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
626 result.add(moduleInternalInfo.toDestroyedModule());
628 Collections.sort(result);
636 * Holds Map<transactionName, transactionController> and purges it each time its
637 * content is requested.
640 class TransactionsHolder {
642 * This map keeps transaction names and
643 * {@link ConfigTransactionControllerInternal} instances, because platform
644 * MBeanServer transforms mbeans into another representation. Map is cleaned
645 * every time current transactions are requested.
648 @GuardedBy("ConfigRegistryImpl.this")
649 private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
652 * Can only be called from within synchronized method.
654 public void add(String transactionName,
655 ConfigTransactionControllerInternal transactionController) {
656 Object oldValue = transactions.put(transactionName,
657 transactionController);
658 if (oldValue != null) {
659 throw new IllegalStateException(
660 "Error: two transactions with same name");
665 * Purges closed transactions from transactions map. Can only be called from
666 * within synchronized method. Calling this method more than once within the
667 * method can modify the resulting map that was obtained in previous calls.
669 * @return current view on transactions map.
671 public Map<String, ConfigTransactionControllerInternal> getCurrentTransactions() {
672 // first, remove closed transaction
673 for (Iterator<Entry<String, ConfigTransactionControllerInternal>> it = transactions
674 .entrySet().iterator(); it.hasNext();) {
675 Entry<String, ConfigTransactionControllerInternal> entry = it
677 if (entry.getValue().isClosed()) {
681 return Collections.unmodifiableMap(transactions);