X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fosgi%2FConfigManagerActivator.java;h=d1c3d767bfebfdf7bc2e72b1a292af464ca0aadf;hp=6381836af856f06c6eba19eccfaa4529da6c1194;hb=f43b01b81319959b1907e3e04537f5169e7f33d8;hpb=e467b27c2487308a0e4b12f3565b6e1077e103e2 diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ConfigManagerActivator.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ConfigManagerActivator.java index 6381836af8..d1c3d767bf 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ConfigManagerActivator.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ConfigManagerActivator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -7,84 +7,165 @@ */ package org.opendaylight.controller.config.manager.impl.osgi; +import java.lang.management.ManagementFactory; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import javax.management.InstanceAlreadyExistsException; +import javax.management.MBeanServer; +import org.opendaylight.controller.config.api.ConfigRegistry; +import org.opendaylight.controller.config.api.ConfigSystemService; import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl; import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator; -import org.opendaylight.controller.config.manager.impl.osgi.mapping.CodecRegistryProvider; +import org.opendaylight.controller.config.manager.impl.jmx.JMXNotifierConfigRegistry; +import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider; import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker; import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry; import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil; import org.opendaylight.controller.config.spi.ModuleFactory; -import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext; +import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy; +import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext; +import org.opendaylight.yangtools.concepts.ObjectRegistration; +import org.opendaylight.yangtools.yang.binding.YangModuleInfo; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.SynchronousBundleListener; +import org.osgi.util.tracker.BundleTracker; import org.osgi.util.tracker.ServiceTracker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import javax.management.InstanceAlreadyExistsException; -import javax.management.MBeanServer; -import java.lang.management.ManagementFactory; -import java.util.Arrays; -import java.util.List; +public class ConfigManagerActivator implements BundleActivator, SynchronousBundleListener, ConfigSystemService { + + private static final Logger LOG = LoggerFactory.getLogger(ConfigManagerActivator.class); -import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.registerService; -import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.wrap; + private static final long SYSTEM_BUNDLE_ID = 0; -public class ConfigManagerActivator implements BundleActivator { private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer(); private AutoCloseable autoCloseable; - @Override - public void start(BundleContext context) { - - ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl? - - RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry( - moduleInfoBackedContext, moduleInfoBackedContext, context); + private ConfigRegistryImpl configRegistry; - ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper); - - CodecRegistryProvider codecRegistryProvider = new CodecRegistryProvider(moduleInfoBackedContext, context); - - // start config registry - BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver( - context); - ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer, - codecRegistryProvider.getCodecRegistry()); - - // track bundles containing factories - BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker( - configRegistry); - ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker( - blankTransactionServiceTracker); + @Override + public void start(final BundleContext context) { + LOG.info("Config manager starting..."); + try { + // the inner strategy is backed by thread context cl? + final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create(); + + final BindingContextProvider bindingContextProvider = new BindingContextProvider(); + + final RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry( + moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, + bindingContextProvider, context); + + final ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker( + moduleInfoRegistryWrapper); + + // start config registry + final BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = + new BundleContextBackedModuleFactoriesResolver(context); + this.configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, + this.configMBeanServer, bindingContextProvider); + + // track bundles containing factories + final BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker( + this.configRegistry); + final ModuleFactoryBundleTracker moduleFactoryTracker = new ModuleFactoryBundleTracker( + blankTransactionServiceTracker); + + BundleTracker>> moduleInfoResolvedBundleTracker = + new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE, + moduleInfoBundleTracker); + ExtensibleBundleTracker moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context, + moduleFactoryTracker); + moduleInfoBundleTracker.open(moduleInfoResolvedBundleTracker); + + // start extensible tracker + moduleFactoryBundleTracker.open(); + + // Wrap config registry with JMX notification publishing adapter + final JMXNotifierConfigRegistry notifyingConfigRegistry = new JMXNotifierConfigRegistry(this.configRegistry, + this.configMBeanServer); + + // register config registry to OSGi + final AutoCloseable clsReg = OsgiRegistrationUtil.registerService(context, moduleInfoBackedContext, + ClassLoadingStrategy.class); + final AutoCloseable configRegReg = OsgiRegistrationUtil.registerService(context, notifyingConfigRegistry, + ConfigRegistry.class); + + // register config registry to jmx + final ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator( + this.configMBeanServer); + try { + configRegistryJMXRegistrator.registerToJMXNoNotifications(this.configRegistry); + } catch (final InstanceAlreadyExistsException e) { + configRegistryJMXRegistrator.close(); + throw new IllegalStateException("Config Registry was already registered to JMX", e); + } + + // register config registry to jmx + final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = + new ConfigRegistryJMXRegistrator(this.configMBeanServer); + try { + configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry); + } catch (final InstanceAlreadyExistsException e) { + configRegistryJMXRegistrator.close(); + configRegistryJMXRegistratorWithNotifications.close(); + throw new IllegalStateException("Config Registry was already registered to JMX", e); + } + + // TODO wire directly via moduleInfoBundleTracker + final ServiceTracker serviceTracker = new ServiceTracker<>(context, + ModuleFactory.class, blankTransactionServiceTracker); + serviceTracker.open(); + + final AutoCloseable configMgrReg = OsgiRegistrationUtil.registerService(context, this, + ConfigSystemService.class); + + final List list = Arrays.asList(bindingContextProvider, clsReg, + OsgiRegistrationUtil.wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker, configRegReg, + configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications, + OsgiRegistrationUtil.wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry, + configMgrReg); + this.autoCloseable = OsgiRegistrationUtil.aggregate(list); + + context.addBundleListener(this); + } catch (final IllegalStateException e) { + LOG.error("Error starting config manager", e); + } - // start extensible tracker - ExtensibleBundleTracker bundleTracker = new ExtensibleBundleTracker<>(context, - primaryModuleFactoryBundleTracker, moduleInfoBundleTracker); - bundleTracker.open(); + LOG.info("Config manager start complete"); + } - // register config registry to OSGi - AutoCloseable configRegReg = registerService(context, configRegistry, ConfigRegistryImpl.class); + @Override + public void stop(final BundleContext context) throws Exception { + LOG.info("Config manager stopping"); + context.removeBundleListener(this); + this.autoCloseable.close(); + } - // register config registry to jmx - ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer); - try { - configRegistryJMXRegistrator.registerToJMX(configRegistry); - } catch (InstanceAlreadyExistsException e) { - throw new IllegalStateException("Config Registry was already registered to JMX", e); + @Override + public void bundleChanged(final BundleEvent event) { + if (this.configRegistry == null) { + return; } - // TODO wire directly via moduleInfoBundleTracker - ServiceTracker serviceTracker = new ServiceTracker<>(context, ModuleFactory.class, - blankTransactionServiceTracker); - serviceTracker.open(); - - List list = Arrays.asList( - codecRegistryProvider, configRegistry, wrap(bundleTracker), configRegReg, configRegistryJMXRegistrator, wrap(serviceTracker)); - autoCloseable = OsgiRegistrationUtil.aggregate(list); + // If the system bundle (id 0) is stopping close the ConfigRegistry so it + // destroys all modules. On + // shutdown the system bundle is stopped first. + if (event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) { + this.configRegistry.close(); + } } @Override - public void stop(BundleContext context) throws Exception { - autoCloseable.close(); + public void closeAllConfigModules() { + if (this.configRegistry != null) { + this.configRegistry.close(); + } } }