Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / factoriesresolver / HierarchicalConfigMBeanFactoriesHolder.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.factoriesresolver;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.TreeSet;
16
17 import org.opendaylight.controller.config.spi.ModuleFactory;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Hold sorted ConfigMBeanFactories by their module names. Check that module
23  * names are globally unique.
24  */
25 public class HierarchicalConfigMBeanFactoriesHolder {
26     private static final Logger logger = LoggerFactory
27             .getLogger(HierarchicalConfigMBeanFactoriesHolder.class);
28
29     private final Map<String, ModuleFactory> moduleNamesToConfigBeanFactories;
30     private final Set<String> moduleNames;
31
32     /**
33      * Create instance.
34      *
35      * @throws IllegalArgumentException
36      *             if unique constraint on module names is violated
37      */
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);
47             }
48             logger.debug("Reading factory {} {}", moduleName, factory);
49             String error = null;
50             ModuleFactory conflicting = moduleNamesToConfigBeanFactories
51                     .get(moduleName);
52             if (conflicting != null) {
53                 error = String
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);
56
57             }
58
59             if (error == null) {
60                 moduleNamesToConfigBeanFactories.put(moduleName, factory);
61             } else {
62                 errors.append(error);
63             }
64
65         }
66         if (errors.length() > 0) {
67             throw new IllegalArgumentException(errors.toString());
68         }
69         this.moduleNamesToConfigBeanFactories = Collections
70                 .unmodifiableMap(moduleNamesToConfigBeanFactories);
71         moduleNames = Collections.unmodifiableSet(new TreeSet<>(
72                 moduleNamesToConfigBeanFactories.keySet()));
73     }
74
75     /**
76      * Get ModuleFactory by their name.
77      *
78      * @throws IllegalArgumentException
79      *             if factory is not found
80      */
81     public ModuleFactory findByModuleName(String moduleName) {
82         ModuleFactory result = moduleNamesToConfigBeanFactories.get(moduleName);
83         if (result == null) {
84             throw new IllegalArgumentException(
85                     "ModuleFactory not found with module name: " + moduleName);
86         }
87         return result;
88     }
89
90     public Set<String> getModuleNames() {
91         return moduleNames;
92     }
93
94 }