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.factoriesresolver;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
15 import java.util.TreeSet;
17 import org.opendaylight.controller.config.spi.ModuleFactory;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Hold sorted ConfigMBeanFactories by their module names. Check that module
23 * names are globally unique.
25 public class HierarchicalConfigMBeanFactoriesHolder {
26 private static final Logger logger = LoggerFactory
27 .getLogger(HierarchicalConfigMBeanFactoriesHolder.class);
29 private final Map<String, ModuleFactory> moduleNamesToConfigBeanFactories;
30 private final Set<String> moduleNames;
35 * @throws IllegalArgumentException
36 * if unique constraint on module names is violated
38 public HierarchicalConfigMBeanFactoriesHolder(
39 List<? extends ModuleFactory> list) {
40 Map<String, ModuleFactory> moduleNamesToConfigBeanFactories = new HashMap<>();
41 StringBuffer errors = new StringBuffer();
42 for (ModuleFactory factory : list) {
43 String moduleName = factory.getImplementationName();
44 if (moduleName == null || moduleName.isEmpty()) {
45 throw new IllegalStateException(
46 "Invalid implementation name for " + factory);
48 logger.debug("Reading factory {} {}", moduleName, factory);
50 ModuleFactory conflicting = moduleNamesToConfigBeanFactories
52 if (conflicting != null) {
54 .format("Module name is not unique. Found two conflicting factories with same name '%s': " +
55 "\n\t%s\n\t%s\n", moduleName, conflicting, factory);
60 moduleNamesToConfigBeanFactories.put(moduleName, factory);
66 if (errors.length() > 0) {
67 throw new IllegalArgumentException(errors.toString());
69 this.moduleNamesToConfigBeanFactories = Collections
70 .unmodifiableMap(moduleNamesToConfigBeanFactories);
71 moduleNames = Collections.unmodifiableSet(new TreeSet<>(
72 moduleNamesToConfigBeanFactories.keySet()));
76 * Get ModuleFactory by their name.
78 * @throws IllegalArgumentException
79 * if factory is not found
81 public ModuleFactory findByModuleName(String moduleName) {
82 ModuleFactory result = moduleNamesToConfigBeanFactories.get(moduleName);
84 throw new IllegalArgumentException(
85 "ModuleFactory not found with module name: " + moduleName);
90 public Set<String> getModuleNames() {