Merge "Refactor Subnet.isSubnetOf - reduce number of 'if' statements. Added unitests."
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / ConfigRegistryImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl;
9
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;
33
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;
49 import java.util.Map;
50 import java.util.Map.Entry;
51 import java.util.Set;
52
53 /**
54  * Singleton that is responsible for creating and committing Config
55  * Transactions. It is registered in Platform MBean Server.
56  */
57 @ThreadSafe
58 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
59     private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
60
61     private final ModuleFactoriesResolver resolver;
62     private final MBeanServer configMBeanServer;
63
64     @GuardedBy("this")
65     private long version = 0;
66     @GuardedBy("this")
67     private long versionCounter = 0;
68
69     /**
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.
73      */
74     @GuardedBy("this")
75     private final ConfigHolder currentConfig = new ConfigHolder();
76
77     /**
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.
81      */
82     @GuardedBy("this")
83     private boolean isHealthy = true;
84
85     /**
86      * Holds Map<transactionName, transactionController> and purges it each time
87      * its content is requested.
88      */
89     @GuardedBy("this")
90     private final TransactionsHolder transactionsHolder = new TransactionsHolder();
91
92     private final BaseJMXRegistrator baseJMXRegistrator;
93
94     private final BeanToOsgiServiceManager beanToOsgiServiceManager;
95
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;
100
101     // Used for finding new factory instances for default module functionality
102     @GuardedBy("this")
103     private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
104
105     @GuardedBy("this") // switched in every 2ndPC
106     private CloseableServiceReferenceReadableRegistry  readableSRRegistry = ServiceReferenceRegistryImpl.createInitialSRLookupRegistry();
107
108     // constructor
109     public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
110             MBeanServer configMBeanServer) {
111         this(resolver, configMBeanServer,
112                 new BaseJMXRegistrator(configMBeanServer));
113     }
114
115     // constructor
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());
127     }
128
129     /**
130      * Create new {@link ConfigTransactionControllerImpl }
131      */
132     @Override
133     public synchronized ObjectName beginConfig() {
134         return beginConfig(false);
135     }
136
137     /**
138      * @param blankTransaction true if this transaction is created automatically by
139      *                         org.opendaylight.controller.config.manager.impl.osgi.BlankTransactionServiceTracker
140      */
141     public synchronized ObjectName beginConfig(boolean blankTransaction) {
142         return beginConfigInternal(blankTransaction).getControllerObjectName();
143     }
144
145     private synchronized ConfigTransactionControllerInternal beginConfigInternal(boolean blankTransaction) {
146         versionCounter++;
147         final String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
148
149         TransactionJMXRegistratorFactory factory = new TransactionJMXRegistratorFactory() {
150             @Override
151             public TransactionJMXRegistrator create() {
152                 return baseJMXRegistrator.createTransactionJMXRegistrator(transactionName);
153             }
154         };
155
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);
162
163         ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
164                 txLookupRegistry, version,
165                 versionCounter, allCurrentFactories, transactionsMBeanServer,
166                 configMBeanServer, blankTransaction, writableRegistry);
167         try {
168             txLookupRegistry.registerMBean(transactionController, transactionController.getControllerObjectName());
169         } catch (InstanceAlreadyExistsException e) {
170             throw new IllegalStateException(e);
171         }
172         transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
173         transactionsHolder.add(transactionName, transactionController);
174         return transactionController;
175     }
176
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
182             throws ConflictingVersionException, ValidationException {
183         final String transactionName = ObjectNameUtil
184                 .getTransactionName(transactionControllerON);
185         logger.info("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
186
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));
193         }
194         // check optimistic lock
195         if (version != configTransactionController.getParentVersion()) {
196             throw new ConflictingVersionException(
197                     String.format(
198                             "Optimistic lock failed. Expected parent version %d, was %d",
199                             version,
200                             configTransactionController.getParentVersion()));
201         }
202         // optimistic lock ok
203
204         CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
205         lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
206         // non recoverable from here:
207         try {
208             return secondPhaseCommit(
209                     configTransactionController, commitInfo);
210         } catch (Throwable t) { // some libs throw Errors: e.g.
211                                 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
212             isHealthy = false;
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) {
217                 throw (Error) t;
218             } else {
219                 throw new RuntimeException(t);
220             }
221         }
222     }
223
224     private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
225                                            CommitInfo commitInfo) {
226
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
234             // registration
235             currentConfig.remove(toBeDestroyed.getIdentifier());
236         }
237
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()
243                 .values()) {
244             RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
245             if (entry.hasOldModule() == false) {
246                 runtimeBeanRegistrator = baseJMXRegistrator
247                         .createRuntimeBeanRegistrator(entry.getIdentifier());
248             } else {
249                 // reuse old JMX registrator
250                 runtimeBeanRegistrator = entry.getOldInternalInfo()
251                         .getRuntimeBeanRegistrator();
252             }
253             // set runtime jmx registrator if required
254             Module module = entry.getModule();
255             if (module instanceof RuntimeBeanRegistratorAwareModule) {
256                 ((RuntimeBeanRegistratorAwareModule) module)
257                         .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
258             }
259             // save it to info so it is accessible afterwards
260             runtimeRegistrators.put(entry.getIdentifier(), runtimeBeanRegistrator);
261         }
262
263         // can register runtime beans
264         List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
265                 .secondPhaseCommit();
266
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<>();
271
272         Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
273
274         int orderingIdx = 0;
275         for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
276             ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
277                     .get(moduleIdentifier);
278             if (entry == null)
279                 throw new NullPointerException("Module not found "
280                         + moduleIdentifier);
281             Module module = entry.getModule();
282             ObjectName primaryReadOnlyON = ObjectNameUtil
283                     .createReadOnlyModuleON(moduleIdentifier);
284
285             // determine if current instance was recreated or reused or is new
286
287             // rules for closing resources:
288             // osgi registration - will be reused if possible.
289             // module jmx registration - will be (re)created every time, needs
290             // to be closed here
291             // runtime jmx registration - should be taken care of by module
292             // itself
293             // instance - is closed only if it was destroyed
294             ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
295                     .createModuleJMXRegistrator();
296
297             OsgiRegistration osgiRegistration = null;
298             if (entry.hasOldModule()) {
299                 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
300                 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo.getReadableModule();
301                 currentConfig.remove(entry.getIdentifier());
302
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();
309                 } else {
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);
314
315                     // close old osgi registration
316                     oldInternalInfo.getOsgiRegistration().close();
317                 }
318
319                 // close old module jmx registrator
320                 oldInternalInfo.getModuleJMXRegistrator().close();
321             } else {
322                 // new instance:
323                 // wrap in readable dynamic mbean
324                 newInstances.add(primaryReadOnlyON);
325             }
326
327             DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
328                     module, module.getInstance(), moduleIdentifier,
329                     registryMBeanServer, configMBeanServer);
330
331             // register to JMX
332             try {
333                 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
334                         primaryReadOnlyON);
335             } catch (InstanceAlreadyExistsException e) {
336                 throw new IllegalStateException(e);
337             }
338
339             // register to OSGi
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);
347                 } else {
348                     throw new NullPointerException(entry.getIdentifier().getFactoryName() + " ModuleFactory not found.");
349                 }
350
351             }
352
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());
359
360             newConfigEntries.put(module, newInfo);
361             orderingIdx++;
362         }
363         currentConfig.addAll(newConfigEntries.values());
364
365         // update version
366         version = configTransactionController.getVersion();
367
368         // switch readable Service Reference Registry
369         this.readableSRRegistry.close();
370         this.readableSRRegistry = ServiceReferenceRegistryImpl.createSRReadableRegistry(
371                 configTransactionController.getWritableRegistry(), this, baseJMXRegistrator);
372
373         return new CommitStatus(newInstances, reusedInstances,
374                 recreatedInstances);
375     }
376
377     /**
378      * {@inheritDoc}
379      */
380     @Override
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
386                 .values()) {
387             result.add(configTransactionController.getControllerObjectName());
388         }
389         return result;
390     }
391
392     /**
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
396      * here.
397      */
398     @Override
399     public synchronized void close() {
400         // abort transactions
401         Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
402                 .getCurrentTransactions();
403         for (ConfigTransactionControllerInternal configTransactionController : transactions
404                 .values()) {
405             try {
406                 configTransactionController.abortConfig();
407             } catch (RuntimeException e) {
408                 logger.warn("Ignoring exception while aborting {}",
409                         configTransactionController, e);
410             }
411         }
412
413         // destroy all live objects one after another in order of the dependency
414         // hierarchy
415         List<DestroyedModule> destroyedModules = currentConfig
416                 .getModulesToBeDestroyed();
417         for (DestroyedModule destroyedModule : destroyedModules) {
418             destroyedModule.close();
419         }
420         // unregister MBeans that failed to unregister properly
421         baseJMXRegistrator.close();
422         // remove jmx servers
423         MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
424         MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
425
426     }
427
428     /**
429      * {@inheritDoc}
430      */
431     @Override
432     public long getVersion() {
433         return version;
434     }
435
436     /**
437      * {@inheritDoc}
438      */
439     @Override
440     public Set<String> getAvailableModuleNames() {
441         return new HierarchicalConfigMBeanFactoriesHolder(
442                 resolver.getAllFactories()).getModuleNames();
443     }
444
445     /**
446      * {@inheritDoc}
447      */
448     @Override
449     public boolean isHealthy() {
450         return isHealthy;
451     }
452
453     // filtering methods
454
455     /**
456      * {@inheritDoc}
457      */
458     @Override
459     public Set<ObjectName> lookupConfigBeans() {
460         return lookupConfigBeans("*", "*");
461     }
462
463     /**
464      * {@inheritDoc}
465      */
466     @Override
467     public Set<ObjectName> lookupConfigBeans(String moduleName) {
468         return lookupConfigBeans(moduleName, "*");
469     }
470
471     /**
472      * {@inheritDoc}
473      */
474     @Override
475     public ObjectName lookupConfigBean(String moduleName, String instanceName)
476             throws InstanceNotFoundException {
477         return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
478     }
479
480     /**
481      * {@inheritDoc}
482      */
483     @Override
484     public Set<ObjectName> lookupConfigBeans(String moduleName,
485             String instanceName) {
486         ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
487                 instanceName);
488         return baseJMXRegistrator.queryNames(namePattern, null);
489     }
490
491     /**
492      * {@inheritDoc}
493      */
494     @Override
495     public Set<ObjectName> lookupRuntimeBeans() {
496         return lookupRuntimeBeans("*", "*");
497     }
498
499     /**
500      * {@inheritDoc}
501      */
502     @Override
503     public Set<ObjectName> lookupRuntimeBeans(String moduleName,
504             String instanceName) {
505         if (moduleName == null)
506             moduleName = "*";
507         if (instanceName == null)
508             instanceName = "*";
509         ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
510                 moduleName, instanceName);
511         return baseJMXRegistrator.queryNames(namePattern, null);
512     }
513
514     @Override
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);
521         }
522         // make sure exactly one match is found:
523         LookupBeansUtil.lookupConfigBean(this, ObjectNameUtil.getFactoryName(objectName), ObjectNameUtil.getInstanceName(objectName));
524     }
525
526     // service reference functionality:
527     @Override
528     public synchronized ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
529         return readableSRRegistry.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
530     }
531
532     @Override
533     public synchronized Map<String, Map<String, ObjectName>> getServiceMapping() {
534         return readableSRRegistry.getServiceMapping();
535     }
536
537     @Override
538     public synchronized Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
539         return readableSRRegistry.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
540     }
541
542     @Override
543     public synchronized Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
544         return readableSRRegistry.lookupServiceInterfaceNames(objectName);
545     }
546
547     @Override
548     public synchronized String getServiceInterfaceName(String namespace, String localName) {
549         return readableSRRegistry.getServiceInterfaceName(namespace, localName);
550     }
551
552     @Override
553     public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
554         readableSRRegistry.checkServiceReferenceExists(objectName);
555     }
556
557     @Override
558     public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
559         return readableSRRegistry.getServiceReference(serviceInterfaceQName, refName);
560     }
561
562     @Override
563     public Set<String> getAvailableModuleFactoryQNames() {
564         return ModuleQNameUtil.getQNames(resolver.getAllFactories());
565     }
566
567     @Override
568     public String toString() {
569         return "ConfigRegistryImpl{" +
570                 "versionCounter=" + versionCounter +
571                 ", version=" + version +
572                 '}';
573     }
574 }
575
576 /**
577  * Holds currently running modules
578  */
579 @NotThreadSafe
580 class ConfigHolder {
581     private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
582
583     /**
584      * Add all modules to the internal map. Also add service instance to OSGi
585      * Service Registry.
586      */
587     public void addAll(Collection<ModuleInternalInfo> configInfos) {
588         if (currentConfig.size() > 0) {
589             throw new IllegalStateException(
590                     "Error - some config entries were not removed: "
591                             + currentConfig);
592         }
593         for (ModuleInternalInfo configInfo : configInfos) {
594             add(configInfo);
595         }
596     }
597
598     private void add(ModuleInternalInfo configInfo) {
599         ModuleInternalInfo oldValue = currentConfig.put(configInfo.getIdentifier(),
600                 configInfo);
601         if (oldValue != null) {
602             throw new IllegalStateException(
603                     "Cannot overwrite module with same name:"
604                             + configInfo.getIdentifier() + ":" + configInfo);
605         }
606     }
607
608     /**
609      * Remove entry from current config.
610      */
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);
616         }
617     }
618
619     public Collection<ModuleInternalInfo> getEntries() {
620         return currentConfig.values();
621     }
622
623     public List<DestroyedModule> getModulesToBeDestroyed() {
624         List<DestroyedModule> result = new ArrayList<>();
625         for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
626             result.add(moduleInternalInfo.toDestroyedModule());
627         }
628         Collections.sort(result);
629         return result;
630     }
631
632
633 }
634
635 /**
636  * Holds Map<transactionName, transactionController> and purges it each time its
637  * content is requested.
638  */
639 @NotThreadSafe
640 class TransactionsHolder {
641     /**
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.
646      *
647      */
648     @GuardedBy("ConfigRegistryImpl.this")
649     private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
650
651     /**
652      * Can only be called from within synchronized method.
653      */
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");
661         }
662     }
663
664     /**
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.
668      *
669      * @return current view on transactions map.
670      */
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
676                     .next();
677             if (entry.getValue().isClosed()) {
678                 it.remove();
679             }
680         }
681         return Collections.unmodifiableMap(transactions);
682     }
683 }