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 static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
15 import java.io.Closeable;
16 import java.util.AbstractMap;
17 import java.util.Arrays;
18 import java.util.Dictionary;
19 import java.util.HashMap;
20 import java.util.List;
22 import org.mockito.Matchers;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.config.spi.ModuleFactory;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceRegistration;
28 public class HardcodedModuleFactoriesResolver implements ModuleFactoriesResolver {
29 private Map<String, Map.Entry<ModuleFactory, BundleContext>> factories;
31 public HardcodedModuleFactoriesResolver(BundleContext bundleContext, ModuleFactory... list) {
32 List<ModuleFactory> factoryList = Arrays.asList(list);
33 this.factories = new HashMap<>(factoryList.size());
34 for (ModuleFactory moduleFactory : list) {
35 StringBuffer errors = new StringBuffer();
36 String moduleName = moduleFactory.getImplementationName();
37 if (moduleName == null || moduleName.isEmpty()) {
38 throw new IllegalStateException(
39 "Invalid implementation name for " + moduleFactory);
42 Map.Entry<ModuleFactory, BundleContext> conflicting = factories.get(moduleName);
43 if (conflicting != null) {
45 .format("Module name is not unique. Found two conflicting factories with same name '%s': " +
46 "\n\t%s\n\t%s\n", moduleName, conflicting.getKey(), moduleFactory);
51 factories.put(moduleName, new AbstractMap.SimpleEntry<>(moduleFactory,
56 if (errors.length() > 0) {
57 throw new IllegalArgumentException(errors.toString());
62 private static BundleContext mockBundleContext() {
63 BundleContext bundleContext = Mockito.mock(BundleContext.class);
64 ServiceRegistration<ModuleFactory> serviceRegistration = mock(ServiceRegistration.class);
65 doNothing().when(serviceRegistration).unregister();
66 doReturn(serviceRegistration).when(bundleContext).registerService(
67 Matchers.any(String[].class), any(Closeable.class),
68 any(Dictionary.class));
73 public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {