fe94af89fa3b6b8479c82bcc9c9fa1e9cafa1a5a
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / DependencyResolverManager.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.dependencyresolver;
9
10 import java.util.*;
11
12 import javax.annotation.concurrent.GuardedBy;
13 import javax.management.InstanceAlreadyExistsException;
14
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;
24
25 /**
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.
29  */
30 public class DependencyResolverManager implements TransactionHolder, DependencyResolverFactory {
31     @GuardedBy("this")
32     private final Map<ModuleIdentifier, DependencyResolverImpl> moduleIdentifiersToDependencyResolverMap = new HashMap<>();
33     private final ModulesHolder modulesHolder;
34     private final TransactionStatus transactionStatus;
35
36     public DependencyResolverManager(String transactionName,
37             TransactionStatus transactionStatus) {
38         this.modulesHolder = new ModulesHolder(transactionName);
39         this.transactionStatus = transactionStatus;
40     }
41
42     @Override
43     public DependencyResolver createDependencyResolver(ModuleIdentifier moduleIdentifier) {
44         return getOrCreate(moduleIdentifier);
45     }
46
47     public synchronized DependencyResolverImpl getOrCreate(ModuleIdentifier name) {
48         DependencyResolverImpl dependencyResolver = moduleIdentifiersToDependencyResolverMap
49                 .get(name);
50         if (dependencyResolver == null) {
51             transactionStatus.checkNotCommitted();
52             dependencyResolver = new DependencyResolverImpl(name,
53                     transactionStatus, modulesHolder);
54             moduleIdentifiersToDependencyResolverMap.put(name,
55                     dependencyResolver);
56         }
57         return dependencyResolver;
58     }
59
60     /**
61      * Get all dependency resolvers, including those that belong to destroyed
62      * things?
63      */
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);
70         }
71         Collections.sort(sorted);
72         return sorted;
73     }
74
75     public List<ModuleIdentifier> getSortedModuleIdentifiers() {
76         List<ModuleIdentifier> result = new ArrayList<>(
77                 moduleIdentifiersToDependencyResolverMap.size());
78         for (DependencyResolverImpl dri : getAllSorted()) {
79             ModuleIdentifier driName = dri.getName();
80             result.add(driName);
81         }
82         return result;
83     }
84
85     @Override
86     public ModuleInternalTransactionalInfo destroyModule(
87             ModuleIdentifier moduleIdentifier) {
88         transactionStatus.checkNotCommitted();
89         ModuleInternalTransactionalInfo found = modulesHolder
90                 .destroyModule(moduleIdentifier);
91         moduleIdentifiersToDependencyResolverMap.remove(moduleIdentifier);
92         return found;
93     }
94
95     // protect write access
96     @Override
97     public void put(
98             ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
99         transactionStatus.checkNotCommitted();
100         modulesHolder.put(moduleInternalTransactionalInfo);
101     }
102
103     // wrapped methods:
104
105     @Override
106     public CommitInfo toCommitInfo() {
107         return modulesHolder.toCommitInfo();
108     }
109
110     @Override
111     public Module findModule(ModuleIdentifier moduleIdentifier,
112             JmxAttribute jmxAttributeForReporting) {
113         return modulesHolder.findModule(moduleIdentifier,
114                 jmxAttributeForReporting);
115     }
116
117     @Override
118     public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
119             JmxAttribute jmxAttributeForReporting) {
120         return modulesHolder.findModuleFactory(moduleIdentifier,
121                 jmxAttributeForReporting);
122     }
123
124     @Override
125     public Map<ModuleIdentifier, Module> getAllModules() {
126         return modulesHolder.getAllModules();
127     }
128
129     @Override
130     public void assertNotExists(ModuleIdentifier moduleIdentifier)
131             throws InstanceAlreadyExistsException {
132         modulesHolder.assertNotExists(moduleIdentifier);
133     }
134
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());
140             }
141         }
142         return result;
143     }
144 }