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(context, 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 moduleFactoryTracker = new ModuleFactoryBundleTracker(
64 blankTransactionServiceTracker);
66 // start extensible tracker
67 ExtensibleBundleTracker<?> moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
68 moduleFactoryTracker);
69 moduleFactoryBundleTracker.open();
71 moduleInfoBundleTracker.open();
73 // Wrap config registry with JMX notification publishing adapter
74 final JMXNotifierConfigRegistry notifyingConfigRegistry =
75 new JMXNotifierConfigRegistry(configRegistry, configMBeanServer);
77 // register config registry to OSGi
78 AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, GeneratedClassLoadingStrategy.class);
79 AutoCloseable configRegReg = registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
81 // register config registry to jmx
82 ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
84 configRegistryJMXRegistrator.registerToJMXNoNotifications(configRegistry);
85 } catch (InstanceAlreadyExistsException e) {
86 throw new IllegalStateException("Config Registry was already registered to JMX", e);
89 // register config registry to jmx
90 final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = new ConfigRegistryJMXRegistrator(configMBeanServer);
92 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
93 } catch (InstanceAlreadyExistsException e) {
94 throw new IllegalStateException("Config Registry was already registered to JMX", e);
97 // TODO wire directly via moduleInfoBundleTracker
98 ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
99 blankTransactionServiceTracker);
100 serviceTracker.open();
102 List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg, configRegistry,
103 wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
104 configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications, wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry);
105 autoCloseable = OsgiRegistrationUtil.aggregate(list);
106 } catch(Exception e) {
107 LOG.warn("Error starting config manager", e);
109 // Log JVM Error and re-throw. The OSGi container may silently fail the bundle and not always log
110 // the exception. This has been seen on initial feature install.
111 LOG.error("Error starting config manager", e);
117 public void stop(final BundleContext context) throws Exception {
118 autoCloseable.close();