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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.