1b695a9bda2fe42a2fcdd042636e9bff4b0a511d
[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.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;
31
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;
47 import java.util.Map;
48 import java.util.Map.Entry;
49 import java.util.Set;
50
51 /**
52  * Singleton that is responsible for creating and committing Config
53  * Transactions. It is registered in Platform MBean Server.
54  */
55 @ThreadSafe
56 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
57     private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
58
59     private final ModuleFactoriesResolver resolver;
60     private final MBeanServer configMBeanServer;
61
62     @GuardedBy("this")
63     private final BundleContext bundleContext;
64
65     @GuardedBy("this")
66     private long version = 0;
67     @GuardedBy("this")
68     private long versionCounter = 0;
69
70     /**
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.
74      */
75     @GuardedBy("this")
76     private final ConfigHolder currentConfig = new ConfigHolder();
77
78     /**
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.
82      */
83     @GuardedBy("this")
84     private boolean isHealthy = true;
85
86     /**
87      * Holds Map<transactionName, transactionController> and purges it each time
88      * its content is requested.
89      */
90     @GuardedBy("this")
91     private final TransactionsHolder transactionsHolder = new TransactionsHolder();
92
93     private final BaseJMXRegistrator baseJMXRegistrator;
94
95     private final BeanToOsgiServiceManager beanToOsgiServiceManager;
96
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;
101
102     @GuardedBy("this")
103     private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
104
105     // constructor
106     public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
107             BundleContext bundleContext, MBeanServer configMBeanServer) {
108         this(resolver, bundleContext, configMBeanServer,
109                 new BaseJMXRegistrator(configMBeanServer));
110     }
111
112     // constructor
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());
125     }
126
127     /**
128      * Create new {@link ConfigTransactionControllerImpl }
129      */
130     @Override
131     public synchronized ObjectName beginConfig() {
132         return beginConfig(false);
133     }
134
135     /**
136      * @param blankTransaction true if this transaction is created automatically by
137      *                         org.opendaylight.controller.config.manager.impl.osgi.BlankTransactionServiceTracker
138      */
139     public synchronized ObjectName beginConfig(boolean blankTransaction) {
140         return beginConfigInternal(blankTransaction).getControllerObjectName();
141     }
142
143     private synchronized ConfigTransactionControllerInternal beginConfigInternal(boolean blankTransaction) {
144         versionCounter++;
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);
152         try {
153             transactionRegistrator.registerMBean(transactionController, transactionController.getControllerObjectName());
154         } catch (InstanceAlreadyExistsException e) {
155             throw new IllegalStateException(e);
156         }
157
158         transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
159
160         transactionsHolder.add(transactionName, transactionController);
161         return transactionController;
162     }
163
164     /**
165      * {@inheritDoc}
166      */
167     @Override
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);
173
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));
180         }
181         // check optimistic lock
182         if (version != configTransactionController.getParentVersion()) {
183             throw new ConflictingVersionException(
184                     String.format(
185                             "Optimistic lock failed. Expected parent version %d, was %d",
186                             version,
187                             configTransactionController.getParentVersion()));
188         }
189         // optimistic lock ok
190
191         CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
192         lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
193         // non recoverable from here:
194         try {
195             return secondPhaseCommit(
196                     configTransactionController, commitInfo);
197         } catch (Throwable t) { // some libs throw Errors: e.g.
198                                 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
199             isHealthy = false;
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) {
204                 throw (Error) t;
205             } else {
206                 throw new RuntimeException(t);
207             }
208         }
209     }
210
211     private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
212                                            CommitInfo commitInfo) {
213
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
221             // registration
222             currentConfig.remove(toBeDestroyed.getIdentifier());
223         }
224
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()
230                 .values()) {
231             RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
232             if (entry.hasOldModule() == false) {
233                 runtimeBeanRegistrator = baseJMXRegistrator
234                         .createRuntimeBeanRegistrator(entry.getIdentifier());
235             } else {
236                 // reuse old JMX registrator
237                 runtimeBeanRegistrator = entry.getOldInternalInfo()
238                         .getRuntimeBeanRegistrator();
239             }
240             // set runtime jmx registrator if required
241             Module module = entry.getModule();
242             if (module instanceof RuntimeBeanRegistratorAwareModule) {
243                 ((RuntimeBeanRegistratorAwareModule) module)
244                         .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
245             }
246             // save it to info so it is accessible afterwards
247             runtimeRegistrators.put(entry.getIdentifier(), runtimeBeanRegistrator);
248         }
249
250         // can register runtime beans
251         List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
252                 .secondPhaseCommit();
253
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<>();
258
259         Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
260
261         int orderingIdx = 0;
262         for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
263             ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
264                     .get(moduleIdentifier);
265             if (entry == null)
266                 throw new NullPointerException("Module not found "
267                         + moduleIdentifier);
268             Module module = entry.getModule();
269             ObjectName primaryReadOnlyON = ObjectNameUtil
270                     .createReadOnlyModuleON(moduleIdentifier);
271
272             // determine if current instance was recreated or reused or is new
273
274             // rules for closing resources:
275             // osgi registration - will be reused if possible.
276             // module jmx registration - will be (re)created every time, needs
277             // to be closed here
278             // runtime jmx registration - should be taken care of by module
279             // itself
280             // instance - is closed only if it was destroyed
281             ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
282                     .createModuleJMXRegistrator();
283
284             OsgiRegistration osgiRegistration = null;
285             if (entry.hasOldModule()) {
286                 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
287                 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo
288                         .getReadableModule();
289                 currentConfig.remove(entry.getIdentifier());
290
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();
297                 } else {
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);
302
303                     // close old osgi registration
304                     oldInternalInfo.getOsgiRegistration().close();
305                 }
306
307                 // close old module jmx registrator
308                 oldInternalInfo.getModuleJMXRegistrator().close();
309             } else {
310                 // new instance:
311                 // wrap in readable dynamic mbean
312                 newInstances.add(primaryReadOnlyON);
313             }
314
315             DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
316                     module, module.getInstance(), moduleIdentifier,
317                     registryMBeanServer, configMBeanServer);
318
319             // register to JMX
320             try {
321                 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
322                         primaryReadOnlyON);
323             } catch (InstanceAlreadyExistsException e) {
324                 throw new IllegalStateException(e);
325             }
326
327             // register to OSGi
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);
335                 } else {
336                     throw new NullPointerException(entry.getIdentifier().getFactoryName() + " ModuleFactory not found.");
337                 }
338
339             }
340
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());
347
348             newConfigEntries.put(module, newInfo);
349             orderingIdx++;
350         }
351         currentConfig.addAll(newConfigEntries.values());
352
353         // update version
354         version = configTransactionController.getVersion();
355         return new CommitStatus(newInstances, reusedInstances,
356                 recreatedInstances);
357     }
358
359     /**
360      * {@inheritDoc}
361      */
362     @Override
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
368                 .values()) {
369             result.add(configTransactionController.getControllerObjectName());
370         }
371         return result;
372     }
373
374     /**
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
378      * here.
379      */
380     @Override
381     public synchronized void close() {
382         // abort transactions
383         Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
384                 .getCurrentTransactions();
385         for (ConfigTransactionControllerInternal configTransactionController : transactions
386                 .values()) {
387             try {
388                 configTransactionController.abortConfig();
389             } catch (RuntimeException e) {
390                 logger.warn("Ignoring exception while aborting {}",
391                         configTransactionController, e);
392             }
393         }
394
395         // destroy all live objects one after another in order of the dependency
396         // hierarchy
397         List<DestroyedModule> destroyedModules = currentConfig
398                 .getModulesToBeDestroyed();
399         for (DestroyedModule destroyedModule : destroyedModules) {
400             destroyedModule.close();
401         }
402         // unregister MBeans that failed to unregister properly
403         baseJMXRegistrator.close();
404         // remove jmx servers
405         MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
406         MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
407
408     }
409
410     /**
411      * {@inheritDoc}
412      */
413     @Override
414     public long getVersion() {
415         return version;
416     }
417
418     /**
419      * {@inheritDoc}
420      */
421     @Override
422     public Set<String> getAvailableModuleNames() {
423         return new HierarchicalConfigMBeanFactoriesHolder(
424                 resolver.getAllFactories()).getModuleNames();
425     }
426
427     /**
428      * {@inheritDoc}
429      */
430     @Override
431     public boolean isHealthy() {
432         return isHealthy;
433     }
434
435     // filtering methods
436
437     /**
438      * {@inheritDoc}
439      */
440     @Override
441     public Set<ObjectName> lookupConfigBeans() {
442         return lookupConfigBeans("*", "*");
443     }
444
445     /**
446      * {@inheritDoc}
447      */
448     @Override
449     public Set<ObjectName> lookupConfigBeans(String moduleName) {
450         return lookupConfigBeans(moduleName, "*");
451     }
452
453     /**
454      * {@inheritDoc}
455      */
456     @Override
457     public ObjectName lookupConfigBean(String moduleName, String instanceName)
458             throws InstanceNotFoundException {
459         return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
460     }
461
462     /**
463      * {@inheritDoc}
464      */
465     @Override
466     public Set<ObjectName> lookupConfigBeans(String moduleName,
467             String instanceName) {
468         ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
469                 instanceName);
470         return baseJMXRegistrator.queryNames(namePattern, null);
471     }
472
473     /**
474      * {@inheritDoc}
475      */
476     @Override
477     public Set<ObjectName> lookupRuntimeBeans() {
478         return lookupRuntimeBeans("*", "*");
479     }
480
481     /**
482      * {@inheritDoc}
483      */
484     @Override
485     public Set<ObjectName> lookupRuntimeBeans(String moduleName,
486             String instanceName) {
487         if (moduleName == null)
488             moduleName = "*";
489         if (instanceName == null)
490             instanceName = "*";
491         ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
492                 moduleName, instanceName);
493         return baseJMXRegistrator.queryNames(namePattern, null);
494     }
495
496 }
497
498 /**
499  * Holds currently running modules
500  */
501 @NotThreadSafe
502 class ConfigHolder {
503     private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
504
505     /**
506      * Add all modules to the internal map. Also add service instance to OSGi
507      * Service Registry.
508      */
509     public void addAll(Collection<ModuleInternalInfo> configInfos) {
510         if (currentConfig.size() > 0) {
511             throw new IllegalStateException(
512                     "Error - some config entries were not removed: "
513                             + currentConfig);
514         }
515         for (ModuleInternalInfo configInfo : configInfos) {
516             add(configInfo);
517         }
518     }
519
520     private void add(ModuleInternalInfo configInfo) {
521         ModuleInternalInfo oldValue = currentConfig.put(configInfo.getName(),
522                 configInfo);
523         if (oldValue != null) {
524             throw new IllegalStateException(
525                     "Cannot overwrite module with same name:"
526                             + configInfo.getName() + ":" + configInfo);
527         }
528     }
529
530     /**
531      * Remove entry from current config.
532      */
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);
538         }
539     }
540
541     public Collection<ModuleInternalInfo> getEntries() {
542         return currentConfig.values();
543     }
544
545     public List<DestroyedModule> getModulesToBeDestroyed() {
546         List<DestroyedModule> result = new ArrayList<>();
547         for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
548             result.add(moduleInternalInfo.toDestroyedModule());
549         }
550         Collections.sort(result);
551         return result;
552     }
553 }
554
555 /**
556  * Holds Map<transactionName, transactionController> and purges it each time its
557  * content is requested.
558  */
559 @NotThreadSafe
560 class TransactionsHolder {
561     /**
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.
566      *
567      */
568     @GuardedBy("ConfigRegistryImpl.this")
569     private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
570
571     /**
572      * Can only be called from within synchronized method.
573      */
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");
581         }
582     }
583
584     /**
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.
588      *
589      * @return current view on transactions map.
590      */
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
596                     .next();
597             if (entry.getValue().isClosed()) {
598                 it.remove();
599             }
600         }
601         return Collections.unmodifiableMap(transactions);
602     }
603 }