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