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 java.lang.management.ManagementFactory;
11 import java.util.Collection;
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.MBeanServer;
16 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
17 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
18 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
19 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RuntimeGeneratedMappingServiceActivator;
20 import org.opendaylight.controller.config.spi.ModuleFactory;
21 import org.opendaylight.yangtools.concepts.ObjectRegistration;
22 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
23 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
24 import org.osgi.framework.BundleActivator;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceRegistration;
27 import org.osgi.util.tracker.ServiceTracker;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class ConfigManagerActivator implements BundleActivator {
32 private static final Logger logger = LoggerFactory.getLogger(ConfigManagerActivator.class);
34 private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
35 private ExtensibleBundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
36 private ConfigRegistryImpl configRegistry;
37 private ConfigRegistryJMXRegistrator configRegistryJMXRegistrator;
38 private ServiceRegistration<?> configRegistryServiceRegistration;
39 private RuntimeGeneratedMappingServiceActivator mappingServiceActivator;
42 public void start(BundleContext context) {
44 // track bundles containing YangModuleInfo
45 ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker();
46 mappingServiceActivator = new RuntimeGeneratedMappingServiceActivator(moduleInfoBundleTracker);
47 CodecRegistry codecRegistry = mappingServiceActivator.startRuntimeMappingService(context).getCodecRegistry();
49 // start config registry
50 BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
52 configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
55 // track bundles containing factories
56 BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
58 ModuleFactoryBundleTracker moduleFactoryBundleTracker = new ModuleFactoryBundleTracker(
59 blankTransactionServiceTracker);
61 // start extensible tracker
62 bundleTracker = new ExtensibleBundleTracker<>(context, moduleInfoBundleTracker, moduleFactoryBundleTracker);
65 // register config registry to OSGi
66 configRegistryServiceRegistration = context.registerService(ConfigRegistryImpl.class, configRegistry, null);
68 // register config registry to jmx
69 configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
71 configRegistryJMXRegistrator.registerToJMX(configRegistry);
72 } catch (InstanceAlreadyExistsException e) {
73 throw new IllegalStateException("Config Registry was already registered to JMX", e);
76 ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
77 blankTransactionServiceTracker);
78 serviceTracker.open();
82 public void stop(BundleContext context) {
84 configRegistry.close();
85 } catch (Exception e) {
86 logger.warn("Exception while closing config registry", e);
89 bundleTracker.close();
90 } catch (Exception e) {
91 logger.warn("Exception while closing extender", e);
94 configRegistryJMXRegistrator.close();
95 } catch (Exception e) {
97 "Exception while closing config registry jmx registrator",
101 configRegistryServiceRegistration.unregister();
102 } catch (Exception e) {
103 logger.warn("Exception while unregistering config registry", e);
106 mappingServiceActivator.close();
107 } catch (Exception e) {
108 logger.warn("Exception while closing mapping service", e);