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.Registration;
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 ExtensibleBundleTracker<Collection<Registration<YangModuleInfo>>> bundleTracker;
35 private ConfigRegistryImpl configRegistry;
36 private ConfigRegistryJMXRegistrator configRegistryJMXRegistrator;
37 private ServiceRegistration configRegistryServiceRegistration;
39 private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
41 private RuntimeGeneratedMappingServiceActivator mappingServiceActivator;
44 public void start(BundleContext context) {
46 // track bundles containing YangModuleInfo
47 ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker();
48 mappingServiceActivator = new RuntimeGeneratedMappingServiceActivator(moduleInfoBundleTracker);
49 CodecRegistry codecRegistry = mappingServiceActivator.startRuntimeMappingService(context).getCodecRegistry();
51 // start config registry
52 BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
54 configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
57 // track bundles containing factories
58 BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
60 ModuleFactoryBundleTracker moduleFactoryBundleTracker = new ModuleFactoryBundleTracker(
61 blankTransactionServiceTracker);
63 // start extensible tracker
64 bundleTracker = new ExtensibleBundleTracker<>(context, moduleInfoBundleTracker, moduleFactoryBundleTracker);
67 // register config registry to OSGi
68 configRegistryServiceRegistration = context.registerService(ConfigRegistryImpl.class, configRegistry, null);
70 // register config registry to jmx
71 configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
73 configRegistryJMXRegistrator.registerToJMX(configRegistry);
74 } catch (InstanceAlreadyExistsException e) {
75 throw new IllegalStateException("Config Registry was already registered to JMX", e);
78 ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
79 blankTransactionServiceTracker);
80 serviceTracker.open();
84 public void stop(BundleContext context) {
86 configRegistry.close();
87 } catch (Exception e) {
88 logger.warn("Exception while closing config registry", e);
91 bundleTracker.close();
92 } catch (Exception e) {
93 logger.warn("Exception while closing extender", e);
96 configRegistryJMXRegistrator.close();
97 } catch (Exception e) {
99 "Exception while closing config registry jmx registrator",
103 configRegistryServiceRegistration.unregister();
104 } catch (Exception e) {
105 logger.warn("Exception while unregistering config registry", e);
108 mappingServiceActivator.close();
109 } catch (Exception e) {
110 logger.warn("Exception while closing mapping service", e);