Add ModuleFactory#getDefaultModules method to config-api.
[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 java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
20
21 import javax.annotation.concurrent.GuardedBy;
22 import javax.annotation.concurrent.NotThreadSafe;
23 import javax.annotation.concurrent.ThreadSafe;
24 import javax.management.InstanceAlreadyExistsException;
25 import javax.management.InstanceNotFoundException;
26 import javax.management.MBeanServer;
27 import javax.management.MBeanServerFactory;
28 import javax.management.ObjectName;
29
30 import org.opendaylight.controller.config.api.ConflictingVersionException;
31 import org.opendaylight.controller.config.api.ModuleIdentifier;
32 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
33 import org.opendaylight.controller.config.api.ValidationException;
34 import org.opendaylight.controller.config.api.jmx.CommitStatus;
35 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
36 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
37 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
38 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder;
39 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
40 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
41 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
42 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
43 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
44 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager;
45 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
46 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
47 import org.opendaylight.controller.config.spi.Module;
48 import org.opendaylight.controller.config.spi.ModuleFactory;
49 import org.osgi.framework.BundleContext;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
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     @GuardedBy("this")
64     private long version = 0;
65     @GuardedBy("this")
66     private long versionCounter = 0;
67
68     /**
69      * Contains current configuration in form of {moduleName:{instanceName,read
70      * only module}} for copying state to new transaction. Each running module
71      * is present just once, no matter how many interfaces it exposes.
72      */
73     @GuardedBy("this")
74     private final ConfigHolder currentConfig = new ConfigHolder();
75
76     /**
77      * Will return true unless there was a transaction that succeeded during
78      * validation but failed in second phase of commit. In this case the server
79      * is unstable and its state is undefined.
80      */
81     @GuardedBy("this")
82     private boolean isHealthy = true;
83
84     /**
85      * Holds Map<transactionName, transactionController> and purges it each time
86      * its content is requested.
87      */
88     @GuardedBy("this")
89     private final TransactionsHolder transactionsHolder = new TransactionsHolder();
90
91     private final BaseJMXRegistrator baseJMXRegistrator;
92
93     private final BeanToOsgiServiceManager beanToOsgiServiceManager;
94
95     // internal jmx server for read only beans
96     private final MBeanServer registryMBeanServer;
97     // internal jmx server shared by all transactions
98     private final MBeanServer transactionsMBeanServer;
99
100     @GuardedBy("this")
101     private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
102
103     // constructor
104     public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
105             BundleContext bundleContext, MBeanServer configMBeanServer) {
106         this(resolver, bundleContext, configMBeanServer,
107                 new BaseJMXRegistrator(configMBeanServer));
108     }
109
110     // constructor
111     public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
112             BundleContext bundleContext, MBeanServer configMBeanServer,
113             BaseJMXRegistrator baseJMXRegistrator) {
114         this.resolver = resolver;
115         this.beanToOsgiServiceManager = new BeanToOsgiServiceManager(
116                 bundleContext);
117         this.configMBeanServer = configMBeanServer;
118         this.baseJMXRegistrator = baseJMXRegistrator;
119         this.registryMBeanServer = MBeanServerFactory
120                 .createMBeanServer("ConfigRegistry" + configMBeanServer.getDefaultDomain());
121         this.transactionsMBeanServer = MBeanServerFactory
122                 .createMBeanServer("ConfigTransactions" + configMBeanServer.getDefaultDomain());
123     }
124
125     /**
126      * Create new {@link ConfigTransactionControllerImpl }
127      */
128     @Override
129     public synchronized ObjectName beginConfig() {
130         return beginConfigInternal().getControllerObjectName();
131     }
132
133     private synchronized ConfigTransactionControllerInternal beginConfigInternal() {
134         versionCounter++;
135         String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
136         TransactionJMXRegistrator transactionRegistrator = baseJMXRegistrator
137                 .createTransactionJMXRegistrator(transactionName);
138         List<ModuleFactory> allCurrentFactories = Collections.unmodifiableList(resolver.getAllFactories());
139         ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
140                 transactionName, transactionRegistrator, version,
141                 versionCounter, allCurrentFactories, transactionsMBeanServer, configMBeanServer);
142         try {
143             transactionRegistrator.registerMBean(transactionController, transactionController.getControllerObjectName());
144         } catch (InstanceAlreadyExistsException e) {
145             throw new IllegalStateException(e);
146         }
147
148         transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
149
150         transactionsHolder.add(transactionName, transactionController);
151         return transactionController;
152     }
153
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
159             throws ConflictingVersionException, ValidationException {
160         final String transactionName = ObjectNameUtil
161                 .getTransactionName(transactionControllerON);
162         logger.info("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
163
164         // find ConfigTransactionController
165         Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder.getCurrentTransactions();
166         ConfigTransactionControllerInternal configTransactionController = transactions.get(transactionName);
167         if (configTransactionController == null) {
168             throw new IllegalArgumentException(String.format(
169                     "Transaction with name '%s' not found", transactionName));
170         }
171         // check optimistic lock
172         if (version != configTransactionController.getParentVersion()) {
173             throw new ConflictingVersionException(
174                     String.format(
175                             "Optimistic lock failed. Expected parent version %d, was %d",
176                             version,
177                             configTransactionController.getParentVersion()));
178         }
179         // optimistic lock ok
180
181         CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
182         lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
183         // non recoverable from here:
184         try {
185             return secondPhaseCommit(
186                     configTransactionController, commitInfo);
187         } catch (Throwable t) { // some libs throw Errors: e.g.
188                                 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
189             isHealthy = false;
190             logger.error("Configuration Transaction failed on 2PC, server is unhealthy", t);
191             if (t instanceof RuntimeException) {
192                 throw (RuntimeException) t;
193             } else if (t instanceof Error) {
194                 throw (Error) t;
195             } else {
196                 throw new RuntimeException(t);
197             }
198         }
199     }
200
201     private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
202                                            CommitInfo commitInfo) {
203
204         // close instances which were destroyed by the user, including
205         // (hopefully) runtime beans
206         for (DestroyedModule toBeDestroyed : commitInfo
207                 .getDestroyedFromPreviousTransactions()) {
208             toBeDestroyed.close(); // closes instance (which should close
209                                    // runtime jmx registrator),
210             // also closes osgi registration and ModuleJMXRegistrator
211             // registration
212             currentConfig.remove(toBeDestroyed.getName());
213         }
214
215         // set RuntimeBeanRegistrators on beans implementing
216         // RuntimeBeanRegistratorAwareModule, each module
217         // should have exactly one runtime jmx registrator.
218         Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
219         for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted()
220                 .values()) {
221             RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
222             if (entry.hasOldModule() == false) {
223                 runtimeBeanRegistrator = baseJMXRegistrator
224                         .createRuntimeBeanRegistrator(entry.getName());
225             } else {
226                 // reuse old JMX registrator
227                 runtimeBeanRegistrator = entry.getOldInternalInfo()
228                         .getRuntimeBeanRegistrator();
229             }
230             // set runtime jmx registrator if required
231             Module module = entry.getModule();
232             if (module instanceof RuntimeBeanRegistratorAwareModule) {
233                 ((RuntimeBeanRegistratorAwareModule) module)
234                         .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
235             }
236             // save it to info so it is accessible afterwards
237             runtimeRegistrators.put(entry.getName(), runtimeBeanRegistrator);
238         }
239
240         // can register runtime beans
241         List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
242                 .secondPhaseCommit();
243
244         // copy configuration to read only mode
245         List<ObjectName> newInstances = new LinkedList<>();
246         List<ObjectName> reusedInstances = new LinkedList<>();
247         List<ObjectName> recreatedInstances = new LinkedList<>();
248
249         Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
250
251         int orderingIdx = 0;
252         for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
253             ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
254                     .get(moduleIdentifier);
255             if (entry == null)
256                 throw new NullPointerException("Module not found "
257                         + moduleIdentifier);
258             Module module = entry.getModule();
259             ObjectName primaryReadOnlyON = ObjectNameUtil
260                     .createReadOnlyModuleON(moduleIdentifier);
261
262             // determine if current instance was recreated or reused or is new
263
264             // rules for closing resources:
265             // osgi registration - will be (re)created every time, so it needs
266             // to be closed here
267             // module jmx registration - will be (re)created every time, needs
268             // to be closed here
269             // runtime jmx registration - should be taken care of by module
270             // itself
271             // instance - is closed only if it was destroyed
272             ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
273                     .createModuleJMXRegistrator();
274
275             if (entry.hasOldModule()) {
276                 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
277                 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo
278                         .getReadableModule();
279                 currentConfig.remove(entry.getName());
280
281                 // test if old instance == new instance
282                 if (oldReadableConfigBean.getInstance().equals(
283                         module.getInstance())) {
284                     // reused old instance:
285                     // wrap in readable dynamic mbean
286                     reusedInstances.add(primaryReadOnlyON);
287                 } else {
288                     // recreated instance:
289                     // it is responsibility of module to call the old instance -
290                     // we just need to unregister configbean
291                     recreatedInstances.add(primaryReadOnlyON);
292                 }
293                 // close old osgi registration in any case
294                 oldInternalInfo.getOsgiRegistration().close();
295                 // close old module jmx registrator
296                 oldInternalInfo.getModuleJMXRegistrator().close();
297             } else {
298                 // new instance:
299                 // wrap in readable dynamic mbean
300                 newInstances.add(primaryReadOnlyON);
301             }
302
303             DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
304                     module, module.getInstance(), moduleIdentifier,
305                     registryMBeanServer, configMBeanServer);
306
307             // register to JMX
308             try {
309                 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
310                         primaryReadOnlyON);
311             } catch (InstanceAlreadyExistsException e) {
312                 throw new IllegalStateException(e);
313             }
314
315             // register to OSGi
316             OsgiRegistration osgiRegistration = beanToOsgiServiceManager
317                     .registerToOsgi(module.getClass(),
318                             newReadableConfigBean.getInstance(),
319                             entry.getName());
320
321             RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators
322                     .get(entry.getName());
323             ModuleInternalInfo newInfo = new ModuleInternalInfo(
324                     entry.getName(), newReadableConfigBean, osgiRegistration,
325                     runtimeBeanRegistrator, newModuleJMXRegistrator,
326                     orderingIdx);
327
328             newConfigEntries.put(module, newInfo);
329             orderingIdx++;
330         }
331         currentConfig.addAll(newConfigEntries.values());
332
333         // update version
334         version = configTransactionController.getVersion();
335         return new CommitStatus(newInstances, reusedInstances,
336                 recreatedInstances);
337     }
338
339     /**
340      * {@inheritDoc}
341      */
342     @Override
343     public synchronized List<ObjectName> getOpenConfigs() {
344         Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
345                 .getCurrentTransactions();
346         List<ObjectName> result = new ArrayList<>(transactions.size());
347         for (ConfigTransactionControllerInternal configTransactionController : transactions
348                 .values()) {
349             result.add(configTransactionController.getControllerObjectName());
350         }
351         return result;
352     }
353
354     /**
355      * Abort open transactions and unregister read only modules. Since this
356      * class is not responsible for registering itself under
357      * {@link ConfigRegistryMXBean#OBJECT_NAME}, it will not unregister itself
358      * here.
359      */
360     @Override
361     public synchronized void close() {
362         // abort transactions
363         Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
364                 .getCurrentTransactions();
365         for (ConfigTransactionControllerInternal configTransactionController : transactions
366                 .values()) {
367             try {
368                 configTransactionController.abortConfig();
369             } catch (RuntimeException e) {
370                 logger.warn("Ignoring exception while aborting {}",
371                         configTransactionController, e);
372             }
373         }
374
375         // destroy all live objects one after another in order of the dependency
376         // hierarchy
377         List<DestroyedModule> destroyedModules = currentConfig
378                 .getModulesToBeDestroyed();
379         for (DestroyedModule destroyedModule : destroyedModules) {
380             destroyedModule.close();
381         }
382         // unregister MBeans that failed to unregister properly
383         baseJMXRegistrator.close();
384         // remove jmx servers
385         MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
386         MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
387
388     }
389
390     /**
391      * {@inheritDoc}
392      */
393     @Override
394     public long getVersion() {
395         return version;
396     }
397
398     /**
399      * {@inheritDoc}
400      */
401     @Override
402     public Set<String> getAvailableModuleNames() {
403         return new HierarchicalConfigMBeanFactoriesHolder(
404                 resolver.getAllFactories()).getModuleNames();
405     }
406
407     /**
408      * {@inheritDoc}
409      */
410     @Override
411     public boolean isHealthy() {
412         return isHealthy;
413     }
414
415     // filtering methods
416
417     /**
418      * {@inheritDoc}
419      */
420     @Override
421     public Set<ObjectName> lookupConfigBeans() {
422         return lookupConfigBeans("*", "*");
423     }
424
425     /**
426      * {@inheritDoc}
427      */
428     @Override
429     public Set<ObjectName> lookupConfigBeans(String moduleName) {
430         return lookupConfigBeans(moduleName, "*");
431     }
432
433     /**
434      * {@inheritDoc}
435      */
436     @Override
437     public ObjectName lookupConfigBean(String moduleName, String instanceName)
438             throws InstanceNotFoundException {
439         return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
440     }
441
442     /**
443      * {@inheritDoc}
444      */
445     @Override
446     public Set<ObjectName> lookupConfigBeans(String moduleName,
447             String instanceName) {
448         ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
449                 instanceName);
450         return baseJMXRegistrator.queryNames(namePattern, null);
451     }
452
453     /**
454      * {@inheritDoc}
455      */
456     @Override
457     public Set<ObjectName> lookupRuntimeBeans() {
458         return lookupRuntimeBeans("*", "*");
459     }
460
461     /**
462      * {@inheritDoc}
463      */
464     @Override
465     public Set<ObjectName> lookupRuntimeBeans(String moduleName,
466             String instanceName) {
467         if (moduleName == null)
468             moduleName = "*";
469         if (instanceName == null)
470             instanceName = "*";
471         ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
472                 moduleName, instanceName);
473         return baseJMXRegistrator.queryNames(namePattern, null);
474     }
475
476 }
477
478 /**
479  * Holds currently running modules
480  */
481 @NotThreadSafe
482 class ConfigHolder {
483     private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
484
485     /**
486      * Add all modules to the internal map. Also add service instance to OSGi
487      * Service Registry.
488      */
489     public void addAll(Collection<ModuleInternalInfo> configInfos) {
490         if (currentConfig.size() > 0) {
491             throw new IllegalStateException(
492                     "Error - some config entries were not removed: "
493                             + currentConfig);
494         }
495         for (ModuleInternalInfo configInfo : configInfos) {
496             add(configInfo);
497         }
498     }
499
500     private void add(ModuleInternalInfo configInfo) {
501         ModuleInternalInfo oldValue = currentConfig.put(configInfo.getName(),
502                 configInfo);
503         if (oldValue != null) {
504             throw new IllegalStateException(
505                     "Cannot overwrite module with same name:"
506                             + configInfo.getName() + ":" + configInfo);
507         }
508     }
509
510     /**
511      * Remove entry from current config.
512      */
513     public void remove(ModuleIdentifier name) {
514         ModuleInternalInfo removed = currentConfig.remove(name);
515         if (removed == null) {
516             throw new IllegalStateException(
517                     "Cannot remove from ConfigHolder - name not found:" + name);
518         }
519     }
520
521     public Collection<ModuleInternalInfo> getEntries() {
522         return currentConfig.values();
523     }
524
525     public List<DestroyedModule> getModulesToBeDestroyed() {
526         List<DestroyedModule> result = new ArrayList<>();
527         for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
528             result.add(moduleInternalInfo.toDestroyedModule());
529         }
530         Collections.sort(result);
531         return result;
532     }
533 }
534
535 /**
536  * Holds Map<transactionName, transactionController> and purges it each time its
537  * content is requested.
538  */
539 @NotThreadSafe
540 class TransactionsHolder {
541     /**
542      * This map keeps transaction names and
543      * {@link ConfigTransactionControllerInternal} instances, because platform
544      * MBeanServer transforms mbeans into another representation. Map is cleaned
545      * every time current transactions are requested.
546      *
547      */
548     @GuardedBy("ConfigRegistryImpl.this")
549     private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
550
551     /**
552      * Can only be called from within synchronized method.
553      */
554     public void add(String transactionName,
555             ConfigTransactionControllerInternal transactionController) {
556         Object oldValue = transactions.put(transactionName,
557                 transactionController);
558         if (oldValue != null) {
559             throw new IllegalStateException(
560                     "Error: two transactions with same name");
561         }
562     }
563
564     /**
565      * Purges closed transactions from transactions map. Can only be called from
566      * within synchronized method. Calling this method more than once within the
567      * method can modify the resulting map that was obtained in previous calls.
568      *
569      * @return current view on transactions map.
570      */
571     public Map<String, ConfigTransactionControllerInternal> getCurrentTransactions() {
572         // first, remove closed transaction
573         for (Iterator<Entry<String, ConfigTransactionControllerInternal>> it = transactions
574                 .entrySet().iterator(); it.hasNext();) {
575             Entry<String, ConfigTransactionControllerInternal> entry = it
576                     .next();
577             if (entry.getValue().isClosed()) {
578                 it.remove();
579             }
580         }
581         return Collections.unmodifiableMap(transactions);
582     }
583 }