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.ValidationException;
14 import org.opendaylight.controller.config.api.jmx.CommitStatus;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
17 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder;
18 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
19 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
20 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
21 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
22 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
23 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager;
24 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
25 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
26 import org.opendaylight.controller.config.spi.Module;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28 import org.osgi.framework.BundleContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import javax.annotation.concurrent.GuardedBy;
33 import javax.annotation.concurrent.NotThreadSafe;
34 import javax.annotation.concurrent.ThreadSafe;
35 import javax.management.InstanceAlreadyExistsException;
36 import javax.management.InstanceNotFoundException;
37 import javax.management.MBeanServer;
38 import javax.management.MBeanServerFactory;
39 import javax.management.ObjectName;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.LinkedList;
46 import java.util.List;
48 import java.util.Map.Entry;
52 * Singleton that is responsible for creating and committing Config
53 * Transactions. It is registered in Platform MBean Server.
56 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
57 private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
59 private final ModuleFactoriesResolver resolver;
60 private final MBeanServer configMBeanServer;
63 private final BundleContext bundleContext;
66 private long version = 0;
68 private long versionCounter = 0;
71 * Contains current configuration in form of {moduleName:{instanceName,read
72 * only module}} for copying state to new transaction. Each running module
73 * is present just once, no matter how many interfaces it exposes.
76 private final ConfigHolder currentConfig = new ConfigHolder();
79 * Will return true unless there was a transaction that succeeded during
80 * validation but failed in second phase of commit. In this case the server
81 * is unstable and its state is undefined.
84 private boolean isHealthy = true;
87 * Holds Map<transactionName, transactionController> and purges it each time
88 * its content is requested.
91 private final TransactionsHolder transactionsHolder = new TransactionsHolder();
93 private final BaseJMXRegistrator baseJMXRegistrator;
95 private final BeanToOsgiServiceManager beanToOsgiServiceManager;
97 // internal jmx server for read only beans
98 private final MBeanServer registryMBeanServer;
99 // internal jmx server shared by all transactions
100 private final MBeanServer transactionsMBeanServer;
103 private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
106 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
107 BundleContext bundleContext, MBeanServer configMBeanServer) {
108 this(resolver, bundleContext, configMBeanServer,
109 new BaseJMXRegistrator(configMBeanServer));
113 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
114 BundleContext bundleContext, MBeanServer configMBeanServer,
115 BaseJMXRegistrator baseJMXRegistrator) {
116 this.resolver = resolver;
117 this.beanToOsgiServiceManager = new BeanToOsgiServiceManager();
118 this.bundleContext = bundleContext;
119 this.configMBeanServer = configMBeanServer;
120 this.baseJMXRegistrator = baseJMXRegistrator;
121 this.registryMBeanServer = MBeanServerFactory
122 .createMBeanServer("ConfigRegistry" + configMBeanServer.getDefaultDomain());
123 this.transactionsMBeanServer = MBeanServerFactory
124 .createMBeanServer("ConfigTransactions" + configMBeanServer.getDefaultDomain());
128 * Create new {@link ConfigTransactionControllerImpl }
131 public synchronized ObjectName beginConfig() {
132 return beginConfig(false);
136 * @param blankTransaction true if this transaction is created automatically by
137 * org.opendaylight.controller.config.manager.impl.osgi.BlankTransactionServiceTracker
139 public synchronized ObjectName beginConfig(boolean blankTransaction) {
140 return beginConfigInternal(blankTransaction).getControllerObjectName();
143 private synchronized ConfigTransactionControllerInternal beginConfigInternal(boolean blankTransaction) {
145 String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
146 TransactionJMXRegistrator transactionRegistrator = baseJMXRegistrator
147 .createTransactionJMXRegistrator(transactionName);
148 Map<String, Map.Entry<ModuleFactory, BundleContext>> allCurrentFactories = Collections.unmodifiableMap(resolver.getAllFactories());
149 ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
150 transactionName, transactionRegistrator, version,
151 versionCounter, allCurrentFactories, transactionsMBeanServer, configMBeanServer, blankTransaction);
153 transactionRegistrator.registerMBean(transactionController, transactionController.getControllerObjectName());
154 } catch (InstanceAlreadyExistsException e) {
155 throw new IllegalStateException(e);
158 transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
160 transactionsHolder.add(transactionName, transactionController);
161 return transactionController;
168 public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
169 throws ConflictingVersionException, ValidationException {
170 final String transactionName = ObjectNameUtil
171 .getTransactionName(transactionControllerON);
172 logger.info("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
174 // find ConfigTransactionController
175 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder.getCurrentTransactions();
176 ConfigTransactionControllerInternal configTransactionController = transactions.get(transactionName);
177 if (configTransactionController == null) {
178 throw new IllegalArgumentException(String.format(
179 "Transaction with name '%s' not found", transactionName));
181 // check optimistic lock
182 if (version != configTransactionController.getParentVersion()) {
183 throw new ConflictingVersionException(
185 "Optimistic lock failed. Expected parent version %d, was %d",
187 configTransactionController.getParentVersion()));
189 // optimistic lock ok
191 CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
192 lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
193 // non recoverable from here:
195 return secondPhaseCommit(
196 configTransactionController, commitInfo);
197 } catch (Throwable t) { // some libs throw Errors: e.g.
198 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
200 logger.error("Configuration Transaction failed on 2PC, server is unhealthy", t);
201 if (t instanceof RuntimeException) {
202 throw (RuntimeException) t;
203 } else if (t instanceof Error) {
206 throw new RuntimeException(t);
211 private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
212 CommitInfo commitInfo) {
214 // close instances which were destroyed by the user, including
215 // (hopefully) runtime beans
216 for (DestroyedModule toBeDestroyed : commitInfo
217 .getDestroyedFromPreviousTransactions()) {
218 toBeDestroyed.close(); // closes instance (which should close
219 // runtime jmx registrator),
220 // also closes osgi registration and ModuleJMXRegistrator
222 currentConfig.remove(toBeDestroyed.getIdentifier());
225 // set RuntimeBeanRegistrators on beans implementing
226 // RuntimeBeanRegistratorAwareModule, each module
227 // should have exactly one runtime jmx registrator.
228 Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
229 for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted()
231 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
232 if (entry.hasOldModule() == false) {
233 runtimeBeanRegistrator = baseJMXRegistrator
234 .createRuntimeBeanRegistrator(entry.getIdentifier());
236 // reuse old JMX registrator
237 runtimeBeanRegistrator = entry.getOldInternalInfo()
238 .getRuntimeBeanRegistrator();
240 // set runtime jmx registrator if required
241 Module module = entry.getModule();
242 if (module instanceof RuntimeBeanRegistratorAwareModule) {
243 ((RuntimeBeanRegistratorAwareModule) module)
244 .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
246 // save it to info so it is accessible afterwards
247 runtimeRegistrators.put(entry.getIdentifier(), runtimeBeanRegistrator);
250 // can register runtime beans
251 List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
252 .secondPhaseCommit();
254 // copy configuration to read only mode
255 List<ObjectName> newInstances = new LinkedList<>();
256 List<ObjectName> reusedInstances = new LinkedList<>();
257 List<ObjectName> recreatedInstances = new LinkedList<>();
259 Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
262 for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
263 ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
264 .get(moduleIdentifier);
266 throw new NullPointerException("Module not found "
268 Module module = entry.getModule();
269 ObjectName primaryReadOnlyON = ObjectNameUtil
270 .createReadOnlyModuleON(moduleIdentifier);
272 // determine if current instance was recreated or reused or is new
274 // rules for closing resources:
275 // osgi registration - will be reused if possible.
276 // module jmx registration - will be (re)created every time, needs
278 // runtime jmx registration - should be taken care of by module
280 // instance - is closed only if it was destroyed
281 ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
282 .createModuleJMXRegistrator();
284 OsgiRegistration osgiRegistration = null;
285 if (entry.hasOldModule()) {
286 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
287 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo
288 .getReadableModule();
289 currentConfig.remove(entry.getIdentifier());
291 // test if old instance == new instance
292 if (oldReadableConfigBean.getInstance().equals(module.getInstance())) {
293 // reused old instance:
294 // wrap in readable dynamic mbean
295 reusedInstances.add(primaryReadOnlyON);
296 osgiRegistration = oldInternalInfo.getOsgiRegistration();
298 // recreated instance:
299 // it is responsibility of module to call the old instance -
300 // we just need to unregister configbean
301 recreatedInstances.add(primaryReadOnlyON);
303 // close old osgi registration
304 oldInternalInfo.getOsgiRegistration().close();
307 // close old module jmx registrator
308 oldInternalInfo.getModuleJMXRegistrator().close();
311 // wrap in readable dynamic mbean
312 newInstances.add(primaryReadOnlyON);
315 DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
316 module, module.getInstance(), moduleIdentifier,
317 registryMBeanServer, configMBeanServer);
321 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
323 } catch (InstanceAlreadyExistsException e) {
324 throw new IllegalStateException(e);
328 if (osgiRegistration == null) {
329 ModuleFactory moduleFactory = entry.getModuleFactory();
330 if(moduleFactory != null) {
331 BundleContext bc = configTransactionController.
332 getModuleFactoryBundleContext(moduleFactory.getImplementationName());
333 osgiRegistration = beanToOsgiServiceManager.registerToOsgi(module.getClass(),
334 newReadableConfigBean.getInstance(), entry.getIdentifier(), bc);
336 throw new NullPointerException(entry.getIdentifier().getFactoryName() + " ModuleFactory not found.");
341 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators
342 .get(entry.getIdentifier());
343 ModuleInternalInfo newInfo = new ModuleInternalInfo(
344 entry.getIdentifier(), newReadableConfigBean, osgiRegistration,
345 runtimeBeanRegistrator, newModuleJMXRegistrator,
346 orderingIdx, entry.isDefaultBean());
348 newConfigEntries.put(module, newInfo);
351 currentConfig.addAll(newConfigEntries.values());
354 version = configTransactionController.getVersion();
355 return new CommitStatus(newInstances, reusedInstances,
363 public synchronized List<ObjectName> getOpenConfigs() {
364 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
365 .getCurrentTransactions();
366 List<ObjectName> result = new ArrayList<>(transactions.size());
367 for (ConfigTransactionControllerInternal configTransactionController : transactions
369 result.add(configTransactionController.getControllerObjectName());
375 * Abort open transactions and unregister read only modules. Since this
376 * class is not responsible for registering itself under
377 * {@link org.opendaylight.controller.config.api.ConfigRegistry#OBJECT_NAME}, it will not unregister itself
381 public synchronized void close() {
382 // abort transactions
383 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
384 .getCurrentTransactions();
385 for (ConfigTransactionControllerInternal configTransactionController : transactions
388 configTransactionController.abortConfig();
389 } catch (RuntimeException e) {
390 logger.warn("Ignoring exception while aborting {}",
391 configTransactionController, e);
395 // destroy all live objects one after another in order of the dependency
397 List<DestroyedModule> destroyedModules = currentConfig
398 .getModulesToBeDestroyed();
399 for (DestroyedModule destroyedModule : destroyedModules) {
400 destroyedModule.close();
402 // unregister MBeans that failed to unregister properly
403 baseJMXRegistrator.close();
404 // remove jmx servers
405 MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
406 MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
414 public long getVersion() {
422 public Set<String> getAvailableModuleNames() {
423 return new HierarchicalConfigMBeanFactoriesHolder(
424 resolver.getAllFactories()).getModuleNames();
431 public boolean isHealthy() {
441 public Set<ObjectName> lookupConfigBeans() {
442 return lookupConfigBeans("*", "*");
449 public Set<ObjectName> lookupConfigBeans(String moduleName) {
450 return lookupConfigBeans(moduleName, "*");
457 public ObjectName lookupConfigBean(String moduleName, String instanceName)
458 throws InstanceNotFoundException {
459 return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
466 public Set<ObjectName> lookupConfigBeans(String moduleName,
467 String instanceName) {
468 ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
470 return baseJMXRegistrator.queryNames(namePattern, null);
477 public Set<ObjectName> lookupRuntimeBeans() {
478 return lookupRuntimeBeans("*", "*");
485 public Set<ObjectName> lookupRuntimeBeans(String moduleName,
486 String instanceName) {
487 if (moduleName == null)
489 if (instanceName == null)
491 ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
492 moduleName, instanceName);
493 return baseJMXRegistrator.queryNames(namePattern, null);
499 * Holds currently running modules
503 private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
506 * Add all modules to the internal map. Also add service instance to OSGi
509 public void addAll(Collection<ModuleInternalInfo> configInfos) {
510 if (currentConfig.size() > 0) {
511 throw new IllegalStateException(
512 "Error - some config entries were not removed: "
515 for (ModuleInternalInfo configInfo : configInfos) {
520 private void add(ModuleInternalInfo configInfo) {
521 ModuleInternalInfo oldValue = currentConfig.put(configInfo.getName(),
523 if (oldValue != null) {
524 throw new IllegalStateException(
525 "Cannot overwrite module with same name:"
526 + configInfo.getName() + ":" + configInfo);
531 * Remove entry from current config.
533 public void remove(ModuleIdentifier name) {
534 ModuleInternalInfo removed = currentConfig.remove(name);
535 if (removed == null) {
536 throw new IllegalStateException(
537 "Cannot remove from ConfigHolder - name not found:" + name);
541 public Collection<ModuleInternalInfo> getEntries() {
542 return currentConfig.values();
545 public List<DestroyedModule> getModulesToBeDestroyed() {
546 List<DestroyedModule> result = new ArrayList<>();
547 for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
548 result.add(moduleInternalInfo.toDestroyedModule());
550 Collections.sort(result);
556 * Holds Map<transactionName, transactionController> and purges it each time its
557 * content is requested.
560 class TransactionsHolder {
562 * This map keeps transaction names and
563 * {@link ConfigTransactionControllerInternal} instances, because platform
564 * MBeanServer transforms mbeans into another representation. Map is cleaned
565 * every time current transactions are requested.
568 @GuardedBy("ConfigRegistryImpl.this")
569 private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
572 * Can only be called from within synchronized method.
574 public void add(String transactionName,
575 ConfigTransactionControllerInternal transactionController) {
576 Object oldValue = transactions.put(transactionName,
577 transactionController);
578 if (oldValue != null) {
579 throw new IllegalStateException(
580 "Error: two transactions with same name");
585 * Purges closed transactions from transactions map. Can only be called from
586 * within synchronized method. Calling this method more than once within the
587 * method can modify the resulting map that was obtained in previous calls.
589 * @return current view on transactions map.
591 public Map<String, ConfigTransactionControllerInternal> getCurrentTransactions() {
592 // first, remove closed transaction
593 for (Iterator<Entry<String, ConfigTransactionControllerInternal>> it = transactions
594 .entrySet().iterator(); it.hasNext();) {
595 Entry<String, ConfigTransactionControllerInternal> entry = it
597 if (entry.getValue().isClosed()) {
601 return Collections.unmodifiableMap(transactions);