Merge "Bug 809: Enhancements to the toaster example"
[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
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.TreeSet;
18 import javax.management.InstanceNotFoundException;
19 import org.opendaylight.controller.config.spi.ModuleFactory;
20 import org.osgi.framework.BundleContext;
21
22 /**
23  * Hold sorted ConfigMBeanFactories by their module names. Check that module
24  * names are globally unique.
25  */
26 public class HierarchicalConfigMBeanFactoriesHolder {
27
28     private final Map<String, Map.Entry<ModuleFactory, BundleContext>> moduleNamesToConfigBeanFactories;
29     private final Set<String> moduleNames;
30     private final List<ModuleFactory> moduleFactories;
31
32     /**
33      * Create instance.
34      *
35      * @throws IllegalArgumentException
36      *             if unique constraint on module names is violated
37      */
38     public HierarchicalConfigMBeanFactoriesHolder(
39             Map<String, Map.Entry<ModuleFactory, BundleContext>> factoriesMap) {
40         this.moduleNamesToConfigBeanFactories = Collections
41                 .unmodifiableMap(factoriesMap);
42         moduleNames = Collections.unmodifiableSet(new TreeSet<>(
43                 moduleNamesToConfigBeanFactories.keySet()));
44         List<ModuleFactory> factories = new ArrayList<>(this.moduleNamesToConfigBeanFactories.size());
45         Collection<Map.Entry<ModuleFactory, BundleContext>> entryCollection = this.moduleNamesToConfigBeanFactories.values();
46         for (Map.Entry<ModuleFactory, BundleContext> entry : entryCollection) {
47             factories.add(entry.getKey());
48         }
49         this.moduleFactories = Collections.unmodifiableList(factories);
50     }
51
52     /**
53      * Get ModuleFactory by their name.
54      *
55      * @throws IllegalArgumentException
56      *             if factory is not found
57      */
58     public ModuleFactory findByModuleName(String moduleName) throws InstanceNotFoundException {
59         Map.Entry<ModuleFactory, BundleContext> result = moduleNamesToConfigBeanFactories.get(moduleName);
60         if (result == null) {
61             throw new InstanceNotFoundException(
62                     "ModuleFactory not found with module name: " + moduleName);
63         }
64         return result.getKey();
65     }
66
67     public Set<String> getModuleNames() {
68         return moduleNames;
69     }
70
71     public List<ModuleFactory> getModuleFactories() {
72         return moduleFactories;
73     }
74
75 }