Merge "Integration test added to netconf-it to test identity (de)serialization."
[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.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;
30
31 public class ConfigManagerActivator implements BundleActivator {
32     private static final Logger logger = LoggerFactory.getLogger(ConfigManagerActivator.class);
33
34     private ExtensibleBundleTracker<Collection<Registration<YangModuleInfo>>> bundleTracker;
35     private ConfigRegistryImpl configRegistry;
36     private ConfigRegistryJMXRegistrator configRegistryJMXRegistrator;
37     private ServiceRegistration configRegistryServiceRegistration;
38
39     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
40
41     private RuntimeGeneratedMappingServiceActivator mappingServiceActivator;
42
43     @Override
44     public void start(BundleContext context) {
45
46         // track bundles containing YangModuleInfo
47         ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker();
48         mappingServiceActivator = new RuntimeGeneratedMappingServiceActivator(moduleInfoBundleTracker);
49         CodecRegistry codecRegistry = mappingServiceActivator.startRuntimeMappingService(context).getCodecRegistry();
50
51         // start config registry
52         BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
53                 context);
54         configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
55                 codecRegistry);
56
57         // track bundles containing factories
58         BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
59                 configRegistry);
60         ModuleFactoryBundleTracker moduleFactoryBundleTracker = new ModuleFactoryBundleTracker(
61                 blankTransactionServiceTracker);
62
63         // start extensible tracker
64         bundleTracker = new ExtensibleBundleTracker<>(context, moduleInfoBundleTracker, moduleFactoryBundleTracker);
65         bundleTracker.open();
66
67         // register config registry to OSGi
68         configRegistryServiceRegistration = context.registerService(ConfigRegistryImpl.class, configRegistry, null);
69
70         // register config registry to jmx
71         configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
72         try {
73             configRegistryJMXRegistrator.registerToJMX(configRegistry);
74         } catch (InstanceAlreadyExistsException e) {
75             throw new IllegalStateException("Config Registry was already registered to JMX", e);
76         }
77
78         ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
79                 blankTransactionServiceTracker);
80         serviceTracker.open();
81     }
82
83     @Override
84     public void stop(BundleContext context) {
85         try {
86             configRegistry.close();
87         } catch (Exception e) {
88             logger.warn("Exception while closing config registry", e);
89         }
90         try {
91             bundleTracker.close();
92         } catch (Exception e) {
93             logger.warn("Exception while closing extender", e);
94         }
95         try {
96             configRegistryJMXRegistrator.close();
97         } catch (Exception e) {
98             logger.warn(
99                     "Exception while closing config registry jmx registrator",
100                     e);
101         }
102         try {
103             configRegistryServiceRegistration.unregister();
104         } catch (Exception e) {
105             logger.warn("Exception while unregistering config registry", e);
106         }
107         try {
108             mappingServiceActivator.close();
109         } catch (Exception e) {
110             logger.warn("Exception while closing mapping service", e);
111         }
112     }
113 }