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;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
16 import javax.annotation.concurrent.GuardedBy;
17 import javax.management.InstanceAlreadyExistsException;
19 import org.opendaylight.controller.config.api.JmxAttribute;
20 import org.opendaylight.controller.config.api.ModuleIdentifier;
21 import org.opendaylight.controller.config.manager.impl.CommitInfo;
22 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
23 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
24 import org.opendaylight.controller.config.spi.Module;
25 import org.opendaylight.controller.config.spi.ModuleFactory;
28 * Holds information about modules being created and destroyed within this
29 * transaction. Observes usage of DependencyResolver within modules to figure
30 * out dependency tree.
32 public class DependencyResolverManager implements TransactionHolder {
34 private final Map<ModuleIdentifier, DependencyResolverImpl> moduleIdentifiersToDependencyResolverMap = new HashMap<>();
35 private final ModulesHolder modulesHolder;
36 private final TransactionStatus transactionStatus;
38 public DependencyResolverManager(String transactionName,
39 TransactionStatus transactionStatus) {
40 this.modulesHolder = new ModulesHolder(transactionName);
41 this.transactionStatus = transactionStatus;
44 public synchronized DependencyResolverImpl getOrCreate(ModuleIdentifier name) {
45 DependencyResolverImpl dependencyResolver = moduleIdentifiersToDependencyResolverMap
47 if (dependencyResolver == null) {
48 transactionStatus.checkNotCommitted();
49 dependencyResolver = new DependencyResolverImpl(name,
50 transactionStatus, modulesHolder);
51 moduleIdentifiersToDependencyResolverMap.put(name,
54 return dependencyResolver;
58 * Get all dependency resolvers, including those that belong to destroyed
61 private List<DependencyResolverImpl> getAllSorted() {
62 transactionStatus.checkCommitted();
63 List<DependencyResolverImpl> sorted = new ArrayList<>(
64 moduleIdentifiersToDependencyResolverMap.values());
65 for (DependencyResolverImpl dri : sorted) {
66 dri.countMaxDependencyDepth(this);
68 Collections.sort(sorted);
72 public List<ModuleIdentifier> getSortedModuleIdentifiers() {
73 List<ModuleIdentifier> result = new ArrayList<>(
74 moduleIdentifiersToDependencyResolverMap.size());
75 for (DependencyResolverImpl dri : getAllSorted()) {
76 ModuleIdentifier driName = dri.getName();
83 public ModuleInternalTransactionalInfo destroyModule(
84 ModuleIdentifier moduleIdentifier) {
85 transactionStatus.checkNotCommitted();
86 ModuleInternalTransactionalInfo found = modulesHolder
87 .destroyModule(moduleIdentifier);
88 moduleIdentifiersToDependencyResolverMap.remove(moduleIdentifier);
92 // protect write access
95 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
96 transactionStatus.checkNotCommitted();
97 modulesHolder.put(moduleInternalTransactionalInfo);
103 public CommitInfo toCommitInfo() {
104 return modulesHolder.toCommitInfo();
108 public Module findModule(ModuleIdentifier moduleIdentifier,
109 JmxAttribute jmxAttributeForReporting) {
110 return modulesHolder.findModule(moduleIdentifier,
111 jmxAttributeForReporting);
115 public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
116 JmxAttribute jmxAttributeForReporting) {
117 return modulesHolder.findModuleFactory(moduleIdentifier,
118 jmxAttributeForReporting);
122 public Map<ModuleIdentifier, Module> getAllModules() {
123 return modulesHolder.getAllModules();
127 public void assertNotExists(ModuleIdentifier moduleIdentifier)
128 throws InstanceAlreadyExistsException {
129 modulesHolder.assertNotExists(moduleIdentifier);