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 org.opendaylight.controller.config.api.JmxAttribute;
11 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
12 import org.opendaylight.controller.config.api.ModuleIdentifier;
13 import org.opendaylight.controller.config.manager.impl.CommitInfo;
14 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
15 import org.opendaylight.controller.config.spi.Module;
16 import org.opendaylight.controller.config.spi.ModuleFactory;
18 import javax.annotation.concurrent.GuardedBy;
19 import javax.management.InstanceAlreadyExistsException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
30 * Represents modules to be committed.
33 private final TransactionIdentifier transactionIdentifier;
35 private final Map<ModuleIdentifier, ModuleInternalTransactionalInfo> commitMap = new HashMap<>();
38 private final Set<ModuleInternalTransactionalInfo> unorderedDestroyedFromPreviousTransactions = new HashSet<>();
40 ModulesHolder(TransactionIdentifier transactionIdentifier) {
41 this.transactionIdentifier = transactionIdentifier;
45 public CommitInfo toCommitInfo() {
46 List<DestroyedModule> orderedDestroyedFromPreviousTransactions = new ArrayList<>(
47 unorderedDestroyedFromPreviousTransactions.size());
48 for (ModuleInternalTransactionalInfo toBeDestroyed : unorderedDestroyedFromPreviousTransactions) {
49 orderedDestroyedFromPreviousTransactions.add(toBeDestroyed
50 .toDestroyedModule());
52 Collections.sort(orderedDestroyedFromPreviousTransactions);
53 return new CommitInfo(orderedDestroyedFromPreviousTransactions,
57 private ModuleInternalTransactionalInfo findModuleInternalTransactionalInfo(
58 ModuleIdentifier moduleIdentifier,
59 JmxAttribute jmxAttributeForReporting) {
60 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo = commitMap
61 .get(moduleIdentifier);
62 JmxAttributeValidationException.checkNotNull(
63 moduleInternalTransactionalInfo, "Module " + moduleIdentifier
64 + "" + " not found in transaction " + transactionIdentifier,
65 jmxAttributeForReporting);
66 return moduleInternalTransactionalInfo;
69 public Module findModule(ModuleIdentifier moduleIdentifier,
70 JmxAttribute jmxAttributeForReporting) {
71 return findModuleInternalTransactionalInfo(moduleIdentifier,
72 jmxAttributeForReporting).getProxiedModule();
75 public ModuleFactory findModuleFactory(ModuleIdentifier moduleIdentifier,
76 JmxAttribute jmxAttributeForReporting) {
77 return findModuleInternalTransactionalInfo(moduleIdentifier,
78 jmxAttributeForReporting).getModuleFactory();
81 public Map<ModuleIdentifier, Module> getAllModules() {
82 Map<ModuleIdentifier, Module> result = new HashMap<>();
83 for (ModuleInternalTransactionalInfo entry : commitMap.values()) {
84 ModuleIdentifier name = entry.getIdentifier();
85 result.put(name, entry.getProxiedModule());
91 ModuleInternalTransactionalInfo moduleInternalTransactionalInfo) {
92 commitMap.put(moduleInternalTransactionalInfo.getIdentifier(),
93 moduleInternalTransactionalInfo);
96 public ModuleInternalTransactionalInfo destroyModule(
97 ModuleIdentifier moduleIdentifier) {
98 ModuleInternalTransactionalInfo found = commitMap.remove(moduleIdentifier);
100 throw new IllegalStateException("Not found:" + moduleIdentifier);
102 if (found.hasOldModule()) {
103 unorderedDestroyedFromPreviousTransactions.add(found);
108 public void assertNotExists(ModuleIdentifier moduleIdentifier)
109 throws InstanceAlreadyExistsException {
110 if (commitMap.containsKey(moduleIdentifier)) {
111 throw new InstanceAlreadyExistsException(
112 "There is an instance registered with name " + moduleIdentifier);
116 public Collection<ModuleInternalTransactionalInfo> getAllInfos(){
117 return commitMap.values();
120 public ModuleInternalTransactionalInfo findModuleInternalTransactionalInfo(ModuleIdentifier moduleIdentifier) {
121 ModuleInternalTransactionalInfo found = commitMap.get(moduleIdentifier);
123 throw new IllegalStateException("Not found:" + moduleIdentifier);