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.osgi;
10 import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.registerService;
11 import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.wrap;
12 import java.lang.management.ManagementFactory;
13 import java.util.Arrays;
14 import java.util.List;
15 import javax.management.InstanceAlreadyExistsException;
16 import javax.management.MBeanServer;
17 import org.opendaylight.controller.config.api.ConfigRegistry;
18 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
19 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
20 import org.opendaylight.controller.config.manager.impl.jmx.JMXNotifierConfigRegistry;
21 import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
22 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
23 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
24 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
25 import org.opendaylight.controller.config.spi.ModuleFactory;
26 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
27 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
28 import org.osgi.framework.BundleActivator;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.util.tracker.ServiceTracker;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class ConfigManagerActivator implements BundleActivator {
36 private static final Logger LOG = LoggerFactory.getLogger(ConfigManagerActivator.class);
38 private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
40 private AutoCloseable autoCloseable;
43 public void start(final BundleContext context) {
45 ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl?
47 BindingContextProvider bindingContextProvider = new BindingContextProvider();
49 RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
50 moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, bindingContextProvider, context);
52 ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
54 // start config registry
55 BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
57 ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
58 bindingContextProvider);
60 // track bundles containing factories
61 BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
63 ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker(
64 blankTransactionServiceTracker);
66 // start extensible tracker
67 ExtensibleBundleTracker<?> bundleTracker = new ExtensibleBundleTracker<>(context,
68 primaryModuleFactoryBundleTracker, moduleInfoBundleTracker);
71 // Wrap config registry with JMX notification publishing adapter
72 final JMXNotifierConfigRegistry notifyingConfigRegistry =
73 new JMXNotifierConfigRegistry(configRegistry, configMBeanServer);
75 // register config registry to OSGi
76 AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, GeneratedClassLoadingStrategy.class);
77 AutoCloseable configRegReg = registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
79 // register config registry to jmx
80 ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
82 configRegistryJMXRegistrator.registerToJMXNoNotifications(configRegistry);
83 } catch (InstanceAlreadyExistsException e) {
84 throw new IllegalStateException("Config Registry was already registered to JMX", e);
87 // register config registry to jmx
88 final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = new ConfigRegistryJMXRegistrator(configMBeanServer);
90 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
91 } catch (InstanceAlreadyExistsException e) {
92 throw new IllegalStateException("Config Registry was already registered to JMX", e);
95 // TODO wire directly via moduleInfoBundleTracker
96 ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
97 blankTransactionServiceTracker);
98 serviceTracker.open();
100 List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg, configRegistry, wrap(bundleTracker),
101 configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications, wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry);
102 autoCloseable = OsgiRegistrationUtil.aggregate(list);
103 } catch(Exception e) {
104 LOG.warn("Error starting config manager", e);
109 public void stop(final BundleContext context) throws Exception {
110 autoCloseable.close();