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