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.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
19 import javax.annotation.concurrent.GuardedBy;
20 import javax.management.InstanceAlreadyExistsException;
22 import org.opendaylight.controller.config.api.JmxAttribute;
23 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.opendaylight.controller.config.manager.impl.CommitInfo;
26 import org.opendaylight.controller.config.manager.impl.DestroyedModule;
27 import org.opendaylight.controller.config.manager.impl.ModuleInternalTransactionalInfo;
28 import org.opendaylight.controller.config.spi.Module;
29 import org.opendaylight.controller.config.spi.ModuleFactory;
32 * Represents modules to be committed.
34 class ModulesHolder implements TransactionHolder {
35 private final String transactionName;
37 private final Map<ModuleIdentifier, ModuleInternalTransactionalInfo> commitMap = new HashMap<>();
40 private final Set<ModuleInternalTransactionalInfo> unorderedDestroyedFromPreviousTransactions = new HashSet<>();
42 ModulesHolder(String transactionName) {
43 this.transactionName = transactionName;
47 public CommitInfo toCommitInfo() {
48 List<DestroyedModule> orderedDestroyedFromPreviousTransactions = new ArrayList<>(
49 unorderedDestroyedFromPreviousTransactions.size());
50 for (ModuleInternalTransactionalInfo toBeDestroyed : unorderedDestroyedFromPreviousTransactions) {
51 orderedDestroyedFromPreviousTransactions.add(toBeDestroyed
52 .toDestroyedModule());
54 Collections.sort(orderedDestroyedFromPreviousTransactions);
55 return new CommitInfo(orderedDestroyedFromPreviousTransactions,
59 private ModuleInternalTransactionalInfo findModuleInternalTransactionalInfo(
60 ModuleIdentifier moduleIdentifier,
61 JmxAttribute jmxAttributeForReporting) {
62 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo = commitMap
63 .get(moduleIdentifier);
64 JmxAttributeValidationException.checkNotNull(
65 moduleInternalTransactionalInfo, "Module " + moduleIdentifier
66 + "" + " not found in transaction " + transactionName,
67 jmxAttributeForReporting);
68 return moduleInternalTransactionalInfo;
72 public Module findModule(ModuleIdentifier moduleIdentifier,
73 JmxAttribute jmxAttributeForReporting) {
74 return findModuleInternalTransactionalInfo(moduleIdentifier,
75 jmxAttributeForReporting).getModule();
79 public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
80 JmxAttribute jmxAttributeForReporting) {
81 return findModuleInternalTransactionalInfo(moduleIdentifier,
82 jmxAttributeForReporting).getModuleFactory();
86 public Map<ModuleIdentifier, Module> getAllModules() {
87 Map<ModuleIdentifier, Module> result = new HashMap<>();
88 for (ModuleInternalTransactionalInfo entry : commitMap.values()) {
89 ModuleIdentifier name = entry.getName();
90 result.put(name, entry.getModule());
97 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
98 commitMap.put(moduleInternalTransactionalInfo.getName(),
99 moduleInternalTransactionalInfo);
103 public ModuleInternalTransactionalInfo destroyModule(
104 ModuleIdentifier moduleIdentifier) {
105 ModuleInternalTransactionalInfo found = commitMap
106 .remove(moduleIdentifier);
108 throw new IllegalStateException("Not found:" + moduleIdentifier);
109 if (found.hasOldModule()) {
110 unorderedDestroyedFromPreviousTransactions.add(found);
116 public void assertNotExists(ModuleIdentifier moduleIdentifier)
117 throws InstanceAlreadyExistsException {
118 if (commitMap.containsKey(moduleIdentifier)) {
119 throw new InstanceAlreadyExistsException(
120 "There is an instance registered with name "
125 public Collection<ModuleInternalTransactionalInfo> getAllInfos(){
126 return commitMap.values();