2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl.dependencyresolver;
12 import javax.annotation.concurrent.GuardedBy;
13 import javax.management.InstanceAlreadyExistsException;
15 import org.opendaylight.controller.config.api.DependencyResolver;
16 import org.opendaylight.controller.config.api.DependencyResolverFactory;
17 import org.opendaylight.controller.config.api.JmxAttribute;
18 import org.opendaylight.controller.config.api.ModuleIdentifier;
19 import org.opendaylight.controller.config.manager.impl.CommitInfo;
20 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
21 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
22 import org.opendaylight.controller.config.spi.Module;
23 import org.opendaylight.controller.config.spi.ModuleFactory;
26 * Holds information about modules being created and destroyed within this
27 * transaction. Observes usage of DependencyResolver within modules to figure
28 * out dependency tree.
30 public class DependencyResolverManager implements TransactionHolder, DependencyResolverFactory {
32 private final Map<ModuleIdentifier, DependencyResolverImpl> moduleIdentifiersToDependencyResolverMap = new HashMap<>();
33 private final ModulesHolder modulesHolder;
34 private final TransactionStatus transactionStatus;
36 public DependencyResolverManager(String transactionName,
37 TransactionStatus transactionStatus) {
38 this.modulesHolder = new ModulesHolder(transactionName);
39 this.transactionStatus = transactionStatus;
43 public DependencyResolver createDependencyResolver(ModuleIdentifier moduleIdentifier) {
44 return getOrCreate(moduleIdentifier);
47 public synchronized DependencyResolverImpl getOrCreate(ModuleIdentifier name) {
48 DependencyResolverImpl dependencyResolver = moduleIdentifiersToDependencyResolverMap
50 if (dependencyResolver == null) {
51 transactionStatus.checkNotCommitted();
52 dependencyResolver = new DependencyResolverImpl(name,
53 transactionStatus, modulesHolder);
54 moduleIdentifiersToDependencyResolverMap.put(name,
57 return dependencyResolver;
61 * Get all dependency resolvers, including those that belong to destroyed
64 private List<DependencyResolverImpl> getAllSorted() {
65 transactionStatus.checkCommitted();
66 List<DependencyResolverImpl> sorted = new ArrayList<>(
67 moduleIdentifiersToDependencyResolverMap.values());
68 for (DependencyResolverImpl dri : sorted) {
69 dri.countMaxDependencyDepth(this);
71 Collections.sort(sorted);
75 public List<ModuleIdentifier> getSortedModuleIdentifiers() {
76 List<ModuleIdentifier> result = new ArrayList<>(
77 moduleIdentifiersToDependencyResolverMap.size());
78 for (DependencyResolverImpl dri : getAllSorted()) {
79 ModuleIdentifier driName = dri.getName();
86 public ModuleInternalTransactionalInfo destroyModule(
87 ModuleIdentifier moduleIdentifier) {
88 transactionStatus.checkNotCommitted();
89 ModuleInternalTransactionalInfo found = modulesHolder
90 .destroyModule(moduleIdentifier);
91 moduleIdentifiersToDependencyResolverMap.remove(moduleIdentifier);
95 // protect write access
98 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
99 transactionStatus.checkNotCommitted();
100 modulesHolder.put(moduleInternalTransactionalInfo);
106 public CommitInfo toCommitInfo() {
107 return modulesHolder.toCommitInfo();
111 public Module findModule(ModuleIdentifier moduleIdentifier,
112 JmxAttribute jmxAttributeForReporting) {
113 return modulesHolder.findModule(moduleIdentifier,
114 jmxAttributeForReporting);
118 public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
119 JmxAttribute jmxAttributeForReporting) {
120 return modulesHolder.findModuleFactory(moduleIdentifier,
121 jmxAttributeForReporting);
125 public Map<ModuleIdentifier, Module> getAllModules() {
126 return modulesHolder.getAllModules();
130 public void assertNotExists(ModuleIdentifier moduleIdentifier)
131 throws InstanceAlreadyExistsException {
132 modulesHolder.assertNotExists(moduleIdentifier);
135 public List<ModuleIdentifier> findAllByFactory(ModuleFactory factory) {
136 List<ModuleIdentifier> result = new ArrayList<>();
137 for( ModuleInternalTransactionalInfo info : modulesHolder.getAllInfos()) {
138 if (factory.equals(info.getModuleFactory())) {
139 result.add(info.getName());