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.Collection;
15 import java.util.List;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.MBeanServer;
18 import org.opendaylight.controller.config.api.ConfigSystemService;
19 import org.opendaylight.controller.config.api.ConfigRegistry;
20 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
21 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
22 import org.opendaylight.controller.config.manager.impl.jmx.JMXNotifierConfigRegistry;
23 import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
24 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
25 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
26 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28 import org.opendaylight.yangtools.concepts.ObjectRegistration;
29 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
30 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
31 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
32 import org.osgi.framework.Bundle;
33 import org.osgi.framework.BundleActivator;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.BundleEvent;
36 import org.osgi.framework.SynchronousBundleListener;
37 import org.osgi.util.tracker.BundleTracker;
38 import org.osgi.util.tracker.ServiceTracker;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class ConfigManagerActivator implements BundleActivator, SynchronousBundleListener, ConfigSystemService {
44 private static final Logger LOG = LoggerFactory.getLogger(ConfigManagerActivator.class);
46 private static final long SYSTEM_BUNDLE_ID = 0;
48 private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
50 private AutoCloseable autoCloseable;
52 private ConfigRegistryImpl configRegistry;
55 public void start(final BundleContext context) {
57 ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl?
59 BindingContextProvider bindingContextProvider = new BindingContextProvider();
61 RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
62 moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, bindingContextProvider, context);
64 ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(context, moduleInfoRegistryWrapper);
66 // start config registry
67 BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
69 configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
70 bindingContextProvider);
72 // track bundles containing factories
73 BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
75 ModuleFactoryBundleTracker moduleFactoryTracker = new ModuleFactoryBundleTracker(
76 blankTransactionServiceTracker);
78 boolean scanResolvedBundlesForModuleInfo = true;
79 BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker = null;
80 ExtensibleBundleTracker<?> moduleFactoryBundleTracker;
81 if(scanResolvedBundlesForModuleInfo) {
82 moduleInfoResolvedBundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING |
83 Bundle.STOPPING | Bundle.ACTIVE, moduleInfoBundleTracker);
84 moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context, moduleFactoryTracker);
86 moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
87 moduleFactoryTracker, moduleInfoBundleTracker);
90 moduleInfoBundleTracker.open(moduleInfoResolvedBundleTracker);
92 // start extensible tracker
93 moduleFactoryBundleTracker.open();
95 // Wrap config registry with JMX notification publishing adapter
96 final JMXNotifierConfigRegistry notifyingConfigRegistry =
97 new JMXNotifierConfigRegistry(configRegistry, configMBeanServer);
99 // register config registry to OSGi
100 AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, ClassLoadingStrategy.class);
101 AutoCloseable configRegReg = registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
103 // register config registry to jmx
104 ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
106 configRegistryJMXRegistrator.registerToJMXNoNotifications(configRegistry);
107 } catch (InstanceAlreadyExistsException e) {
108 configRegistryJMXRegistrator.close();
109 throw new IllegalStateException("Config Registry was already registered to JMX", e);
112 // register config registry to jmx
113 final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = new ConfigRegistryJMXRegistrator(configMBeanServer);
115 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
116 } catch (InstanceAlreadyExistsException e) {
117 configRegistryJMXRegistrator.close();
118 configRegistryJMXRegistratorWithNotifications.close();
119 throw new IllegalStateException("Config Registry was already registered to JMX", e);
122 // TODO wire directly via moduleInfoBundleTracker
123 ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
124 blankTransactionServiceTracker);
125 serviceTracker.open();
127 AutoCloseable configMgrReg = registerService(context, this, ConfigSystemService.class);
129 List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg,
130 wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
131 configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications,
132 wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry, configMgrReg);
133 autoCloseable = OsgiRegistrationUtil.aggregate(list);
135 context.addBundleListener(this);
136 } catch(Exception e) {
137 LOG.warn("Error starting config manager", e);
139 // Log JVM Error and re-throw. The OSGi container may silently fail the bundle and not always log
140 // the exception. This has been seen on initial feature install.
141 LOG.error("Error starting config manager", e);
147 public void stop(final BundleContext context) throws Exception {
148 context.removeBundleListener(this);
149 autoCloseable.close();
153 public void bundleChanged(BundleEvent event) {
154 if(configRegistry == null) {
158 // If the system bundle (id 0) is stopping close the ConfigRegistry so it destroys all modules. On
159 // shutdown the system bundle is stopped first.
160 if(event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) {
161 configRegistry.close();
166 public void closeAllConfigModules() {
167 if(configRegistry != null) {
168 configRegistry.close();