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.JmxAttribute;
16 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
17 import org.opendaylight.controller.config.api.ModuleIdentifier;
18 import org.opendaylight.controller.config.manager.impl.CommitInfo;
19 import org.opendaylight.controller.config.manager.impl.DestroyedModule;
20 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
21 import org.opendaylight.controller.config.spi.Module;
22 import org.opendaylight.controller.config.spi.ModuleFactory;
25 * Represents modules to be committed.
27 class ModulesHolder implements TransactionHolder {
28 private final String transactionName;
30 private final Map<ModuleIdentifier, ModuleInternalTransactionalInfo> commitMap = new HashMap<>();
33 private final Set<ModuleInternalTransactionalInfo> unorderedDestroyedFromPreviousTransactions = new HashSet<>();
35 ModulesHolder(String transactionName) {
36 this.transactionName = transactionName;
40 public CommitInfo toCommitInfo() {
41 List<DestroyedModule> orderedDestroyedFromPreviousTransactions = new ArrayList<>(
42 unorderedDestroyedFromPreviousTransactions.size());
43 for (ModuleInternalTransactionalInfo toBeDestroyed : unorderedDestroyedFromPreviousTransactions) {
44 orderedDestroyedFromPreviousTransactions.add(toBeDestroyed
45 .toDestroyedModule());
47 Collections.sort(orderedDestroyedFromPreviousTransactions);
48 return new CommitInfo(orderedDestroyedFromPreviousTransactions,
52 private ModuleInternalTransactionalInfo findModuleInternalTransactionalInfo(
53 ModuleIdentifier moduleIdentifier,
54 JmxAttribute jmxAttributeForReporting) {
55 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo = commitMap
56 .get(moduleIdentifier);
57 JmxAttributeValidationException.checkNotNull(
58 moduleInternalTransactionalInfo, "Module " + moduleIdentifier
59 + "" + " not found in transaction " + transactionName,
60 jmxAttributeForReporting);
61 return moduleInternalTransactionalInfo;
65 public Module findModule(ModuleIdentifier moduleIdentifier,
66 JmxAttribute jmxAttributeForReporting) {
67 return findModuleInternalTransactionalInfo(moduleIdentifier,
68 jmxAttributeForReporting).getModule();
72 public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
73 JmxAttribute jmxAttributeForReporting) {
74 return findModuleInternalTransactionalInfo(moduleIdentifier,
75 jmxAttributeForReporting).getModuleFactory();
79 public Map<ModuleIdentifier, Module> getAllModules() {
80 Map<ModuleIdentifier, Module> result = new HashMap<>();
81 for (ModuleInternalTransactionalInfo entry : commitMap.values()) {
82 ModuleIdentifier name = entry.getName();
83 result.put(name, entry.getModule());
90 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
91 commitMap.put(moduleInternalTransactionalInfo.getName(),
92 moduleInternalTransactionalInfo);
96 public ModuleInternalTransactionalInfo destroyModule(
97 ModuleIdentifier moduleIdentifier) {
98 ModuleInternalTransactionalInfo found = commitMap
99 .remove(moduleIdentifier);
101 throw new IllegalStateException("Not found:" + moduleIdentifier);
102 if (found.hasOldModule()) {
103 unorderedDestroyedFromPreviousTransactions.add(found);
109 public void assertNotExists(ModuleIdentifier moduleIdentifier)
110 throws InstanceAlreadyExistsException {
111 if (commitMap.containsKey(moduleIdentifier)) {
112 throw new InstanceAlreadyExistsException(
113 "There is an instance registered with name "
118 public Collection<ModuleInternalTransactionalInfo> getAllInfos(){
119 return commitMap.values();