Modify ModuleInfoBundleTracker to track RESOLVED bundles
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BundleContextBackedModuleFactoriesResolver.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.osgi;
9
10 import java.util.AbstractMap;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
15 import org.opendaylight.controller.config.spi.ModuleFactory;
16 import org.osgi.framework.BundleContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Retrieves list of currently registered Module Factories using bundlecontext.
22  */
23 public class BundleContextBackedModuleFactoriesResolver implements
24         ModuleFactoriesResolver {
25     private static final Logger LOG = LoggerFactory
26             .getLogger(BundleContextBackedModuleFactoriesResolver.class);
27     private ModuleFactoryBundleTracker moduleFactoryBundleTracker;
28
29     public BundleContextBackedModuleFactoriesResolver() {
30     }
31
32     public void setModuleFactoryBundleTracker(ModuleFactoryBundleTracker moduleFactoryBundleTracker) {
33         this.moduleFactoryBundleTracker = moduleFactoryBundleTracker;
34     }
35
36     @Override
37     public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
38         Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>();
39         for(Entry<ModuleFactory, BundleContext> entry: moduleFactoryBundleTracker.getModuleFactoryEntries()) {
40             ModuleFactory factory = entry.getKey();
41             BundleContext bundleContext = entry.getValue();
42             String moduleName = factory .getImplementationName();
43             if (moduleName == null || moduleName.isEmpty()) {
44                 throw new IllegalStateException("Invalid implementation name for " + factory);
45             }
46
47             LOG.debug("Processing factory {} {}", moduleName, factory);
48
49             Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
50             if (conflicting != null) {
51                 String error = String
52                         .format("Module name is not unique. Found two conflicting factories with same name '%s': '%s' '%s'",
53                                 moduleName, conflicting.getKey(), factory);
54                 LOG.error(error);
55                 throw new IllegalArgumentException(error);
56             } else {
57                 result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory, bundleContext));
58             }
59         }
60
61         return result;
62     }
63 }