Fix checkstyle warnings in config-manager
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / ModulesHolder.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.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import javax.annotation.concurrent.GuardedBy;
19 import javax.management.InstanceAlreadyExistsException;
20 import org.opendaylight.controller.config.api.JmxAttribute;
21 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
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.TransactionIdentifier;
25 import org.opendaylight.controller.config.spi.Module;
26 import org.opendaylight.controller.config.spi.ModuleFactory;
27
28 /**
29  * Represents modules to be committed.
30  */
31 class ModulesHolder {
32     private final TransactionIdentifier transactionIdentifier;
33     @GuardedBy("this")
34     private final Map<ModuleIdentifier, ModuleInternalTransactionalInfo> commitMap = new HashMap<>();
35
36     @GuardedBy("this")
37     private final Set<ModuleInternalTransactionalInfo> unorderedDestroyedFromPreviousTransactions = new HashSet<>();
38
39     ModulesHolder(TransactionIdentifier transactionIdentifier) {
40         this.transactionIdentifier = transactionIdentifier;
41     }
42
43
44     public CommitInfo toCommitInfo() {
45         List<DestroyedModule> orderedDestroyedFromPreviousTransactions = new ArrayList<>(
46                 unorderedDestroyedFromPreviousTransactions.size());
47         for (ModuleInternalTransactionalInfo toBeDestroyed : unorderedDestroyedFromPreviousTransactions) {
48             orderedDestroyedFromPreviousTransactions.add(toBeDestroyed
49                     .toDestroyedModule());
50         }
51         Collections.sort(orderedDestroyedFromPreviousTransactions);
52         return new CommitInfo(orderedDestroyedFromPreviousTransactions,
53                 commitMap);
54     }
55
56     private ModuleInternalTransactionalInfo findModuleInternalTransactionalInfo(
57             ModuleIdentifier moduleIdentifier,
58             JmxAttribute jmxAttributeForReporting) {
59         ModuleInternalTransactionalInfo moduleInternalTransactionalInfo = commitMap
60                 .get(moduleIdentifier);
61         JmxAttributeValidationException.checkNotNull(
62                 moduleInternalTransactionalInfo, "Module " + moduleIdentifier
63                         + "" + " not found in transaction " + transactionIdentifier,
64                 jmxAttributeForReporting);
65         return moduleInternalTransactionalInfo;
66     }
67
68     public Module findModule(ModuleIdentifier moduleIdentifier,
69             JmxAttribute jmxAttributeForReporting) {
70         return findModuleInternalTransactionalInfo(moduleIdentifier,
71                 jmxAttributeForReporting).getProxiedModule();
72     }
73
74     public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
75             JmxAttribute jmxAttributeForReporting) {
76         return findModuleInternalTransactionalInfo(moduleIdentifier,
77                 jmxAttributeForReporting).getModuleFactory();
78     }
79
80     public Map<ModuleIdentifier, Module> getAllModules() {
81         Map<ModuleIdentifier, Module> result = new HashMap<>();
82         for (ModuleInternalTransactionalInfo entry : commitMap.values()) {
83             ModuleIdentifier name = entry.getIdentifier();
84             result.put(name, entry.getProxiedModule());
85         }
86         return result;
87     }
88
89     public void put(
90             ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
91         commitMap.put(moduleInternalTransactionalInfo.getIdentifier(),
92                 moduleInternalTransactionalInfo);
93     }
94
95     public ModuleInternalTransactionalInfo destroyModule(
96             ModuleIdentifier moduleIdentifier) {
97         ModuleInternalTransactionalInfo found = commitMap.remove(moduleIdentifier);
98         if (found == null) {
99             throw new IllegalStateException("Not found:" + moduleIdentifier);
100         }
101         if (found.hasOldModule()) {
102             unorderedDestroyedFromPreviousTransactions.add(found);
103         }
104         return found;
105     }
106
107     public void assertNotExists(ModuleIdentifier moduleIdentifier)
108             throws InstanceAlreadyExistsException {
109         if (commitMap.containsKey(moduleIdentifier)) {
110             throw new InstanceAlreadyExistsException(
111                     "There is an instance registered with name " + moduleIdentifier);
112         }
113     }
114
115     public Collection<ModuleInternalTransactionalInfo> getAllInfos(){
116         return commitMap.values();
117     }
118
119     public ModuleInternalTransactionalInfo findModuleInternalTransactionalInfo(ModuleIdentifier moduleIdentifier) {
120         ModuleInternalTransactionalInfo found = commitMap.get(moduleIdentifier);
121         if (found == null) {
122             throw new IllegalStateException("Not found:" + moduleIdentifier);
123         }
124         return found;
125     }
126 }