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