config-manager: final parameters
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / factoriesresolver / HardcodedModuleFactoriesResolver.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 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;
14
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;
21 import java.util.Map;
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;
27
28 public class HardcodedModuleFactoriesResolver implements ModuleFactoriesResolver {
29     private Map<String, Map.Entry<ModuleFactory, BundleContext>> factories;
30
31     public HardcodedModuleFactoriesResolver(final BundleContext bundleContext, final 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);
40             }
41             String error = null;
42             Map.Entry<ModuleFactory, BundleContext> conflicting = factories.get(moduleName);
43             if (conflicting != null) {
44                 error = String
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);
47
48             }
49
50             if (error == null) {
51                 factories.put(moduleName, new AbstractMap.SimpleEntry<>(moduleFactory,
52                         bundleContext));
53             } else {
54                 errors.append(error);
55             }
56             if (errors.length() > 0) {
57                 throw new IllegalArgumentException(errors.toString());
58             }
59         }
60     }
61
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));
69         return bundleContext;
70     }
71
72     @Override
73     public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
74         return factories;
75     }
76
77 }