Merge "Use ObjectRegistration instead of Registration"
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ConfigManagerActivator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.osgi;
9
10 import java.lang.management.ManagementFactory;
11 import java.util.Collection;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.MBeanServer;
15
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;
30
31 public class ConfigManagerActivator implements BundleActivator {
32     private static final Logger logger = LoggerFactory.getLogger(ConfigManagerActivator.class);
33
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;
40
41     @Override
42     public void start(BundleContext context) {
43
44         // track bundles containing YangModuleInfo
45         ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker();
46         mappingServiceActivator = new RuntimeGeneratedMappingServiceActivator(moduleInfoBundleTracker);
47         CodecRegistry codecRegistry = mappingServiceActivator.startRuntimeMappingService(context).getCodecRegistry();
48
49         // start config registry
50         BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
51                 context);
52         configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
53                 codecRegistry);
54
55         // track bundles containing factories
56         BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
57                 configRegistry);
58         ModuleFactoryBundleTracker moduleFactoryBundleTracker = new ModuleFactoryBundleTracker(
59                 blankTransactionServiceTracker);
60
61         // start extensible tracker
62         bundleTracker = new ExtensibleBundleTracker<>(context, moduleInfoBundleTracker, moduleFactoryBundleTracker);
63         bundleTracker.open();
64
65         // register config registry to OSGi
66         configRegistryServiceRegistration = context.registerService(ConfigRegistryImpl.class, configRegistry, null);
67
68         // register config registry to jmx
69         configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
70         try {
71             configRegistryJMXRegistrator.registerToJMX(configRegistry);
72         } catch (InstanceAlreadyExistsException e) {
73             throw new IllegalStateException("Config Registry was already registered to JMX", e);
74         }
75
76         ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
77                 blankTransactionServiceTracker);
78         serviceTracker.open();
79     }
80
81     @Override
82     public void stop(BundleContext context) {
83         try {
84             configRegistry.close();
85         } catch (Exception e) {
86             logger.warn("Exception while closing config registry", e);
87         }
88         try {
89             bundleTracker.close();
90         } catch (Exception e) {
91             logger.warn("Exception while closing extender", e);
92         }
93         try {
94             configRegistryJMXRegistrator.close();
95         } catch (Exception e) {
96             logger.warn(
97                     "Exception while closing config registry jmx registrator",
98                     e);
99         }
100         try {
101             configRegistryServiceRegistration.unregister();
102         } catch (Exception e) {
103             logger.warn("Exception while unregistering config registry", e);
104         }
105         try {
106             mappingServiceActivator.close();
107         } catch (Exception e) {
108             logger.warn("Exception while closing mapping service", e);
109         }
110     }
111 }