Fix star import and enable checkstyle rule to prevent it.
[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.ArrayList;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 import javax.annotation.concurrent.GuardedBy;
17 import javax.management.InstanceAlreadyExistsException;
18
19 import org.opendaylight.controller.config.api.DependencyResolver;
20 import org.opendaylight.controller.config.api.DependencyResolverFactory;
21 import org.opendaylight.controller.config.api.JmxAttribute;
22 import org.opendaylight.controller.config.api.ModuleIdentifier;
23 import org.opendaylight.controller.config.manager.impl.CommitInfo;
24 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
25 import org.opendaylight.controller.config.manager.impl.TransactionStatus;
26 import org.opendaylight.controller.config.spi.Module;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28
29 /**
30  * Holds information about modules being created and destroyed within this
31  * transaction. Observes usage of DependencyResolver within modules to figure
32  * out dependency tree.
33  */
34 public class DependencyResolverManager implements TransactionHolder, DependencyResolverFactory {
35     @GuardedBy("this")
36     private final Map<ModuleIdentifier, DependencyResolverImpl> moduleIdentifiersToDependencyResolverMap = new HashMap<>();
37     private final ModulesHolder modulesHolder;
38     private final TransactionStatus transactionStatus;
39
40     public DependencyResolverManager(String transactionName,
41             TransactionStatus transactionStatus) {
42         this.modulesHolder = new ModulesHolder(transactionName);
43         this.transactionStatus = transactionStatus;
44     }
45
46     @Override
47     public DependencyResolver createDependencyResolver(ModuleIdentifier moduleIdentifier) {
48         return getOrCreate(moduleIdentifier);
49     }
50
51     public synchronized DependencyResolverImpl getOrCreate(ModuleIdentifier name) {
52         DependencyResolverImpl dependencyResolver = moduleIdentifiersToDependencyResolverMap
53                 .get(name);
54         if (dependencyResolver == null) {
55             transactionStatus.checkNotCommitted();
56             dependencyResolver = new DependencyResolverImpl(name,
57                     transactionStatus, modulesHolder);
58             moduleIdentifiersToDependencyResolverMap.put(name,
59                     dependencyResolver);
60         }
61         return dependencyResolver;
62     }
63
64     /**
65      * Get all dependency resolvers, including those that belong to destroyed
66      * things?
67      */
68     private List<DependencyResolverImpl> getAllSorted() {
69         transactionStatus.checkCommitted();
70         List<DependencyResolverImpl> sorted = new ArrayList<>(
71                 moduleIdentifiersToDependencyResolverMap.values());
72         for (DependencyResolverImpl dri : sorted) {
73             dri.countMaxDependencyDepth(this);
74         }
75         Collections.sort(sorted);
76         return sorted;
77     }
78
79     public List<ModuleIdentifier> getSortedModuleIdentifiers() {
80         List<ModuleIdentifier> result = new ArrayList<>(
81                 moduleIdentifiersToDependencyResolverMap.size());
82         for (DependencyResolverImpl dri : getAllSorted()) {
83             ModuleIdentifier driName = dri.getName();
84             result.add(driName);
85         }
86         return result;
87     }
88
89     @Override
90     public ModuleInternalTransactionalInfo destroyModule(
91             ModuleIdentifier moduleIdentifier) {
92         transactionStatus.checkNotCommitted();
93         ModuleInternalTransactionalInfo found = modulesHolder
94                 .destroyModule(moduleIdentifier);
95         moduleIdentifiersToDependencyResolverMap.remove(moduleIdentifier);
96         return found;
97     }
98
99     // protect write access
100     @Override
101     public void put(
102             ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
103         transactionStatus.checkNotCommitted();
104         modulesHolder.put(moduleInternalTransactionalInfo);
105     }
106
107     // wrapped methods:
108
109     @Override
110     public CommitInfo toCommitInfo() {
111         return modulesHolder.toCommitInfo();
112     }
113
114     @Override
115     public Module findModule(ModuleIdentifier moduleIdentifier,
116             JmxAttribute jmxAttributeForReporting) {
117         return modulesHolder.findModule(moduleIdentifier,
118                 jmxAttributeForReporting);
119     }
120
121     @Override
122     public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
123             JmxAttribute jmxAttributeForReporting) {
124         return modulesHolder.findModuleFactory(moduleIdentifier,
125                 jmxAttributeForReporting);
126     }
127
128     @Override
129     public Map<ModuleIdentifier, Module> getAllModules() {
130         return modulesHolder.getAllModules();
131     }
132
133     @Override
134     public void assertNotExists(ModuleIdentifier moduleIdentifier)
135             throws InstanceAlreadyExistsException {
136         modulesHolder.assertNotExists(moduleIdentifier);
137     }
138
139     public List<ModuleIdentifier> findAllByFactory(ModuleFactory factory) {
140         List<ModuleIdentifier> result = new ArrayList<>();
141         for( ModuleInternalTransactionalInfo  info : modulesHolder.getAllInfos()) {
142             if (factory.equals(info.getModuleFactory())) {
143                 result.add(info.getName());
144             }
145         }
146         return result;
147     }
148 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.