Decouple config and netconf subsystems.
[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 static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.registerService;
11 import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.wrap;
12
13 import java.lang.management.ManagementFactory;
14 import java.util.Arrays;
15 import java.util.List;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.MBeanServer;
18 import org.opendaylight.controller.config.api.ConfigRegistry;
19 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
20 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
21 import org.opendaylight.controller.config.manager.impl.jmx.JMXNotifierConfigRegistry;
22 import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
23 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
24 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
25 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
26 import org.opendaylight.controller.config.spi.ModuleFactory;
27 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
28 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.util.tracker.ServiceTracker;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class ConfigManagerActivator implements BundleActivator {
36
37     private static final Logger LOG = LoggerFactory.getLogger(ConfigManagerActivator.class);
38
39     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
40
41     private AutoCloseable autoCloseable;
42
43     @Override
44     public void start(final BundleContext context) {
45         try {
46             ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl?
47
48             BindingContextProvider bindingContextProvider = new BindingContextProvider();
49
50             RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
51                     moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, bindingContextProvider, context);
52
53             ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
54
55             // start config registry
56             BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
57                     context);
58             ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
59                     bindingContextProvider);
60
61             // track bundles containing factories
62             BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
63                     configRegistry);
64             ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker(
65                     blankTransactionServiceTracker);
66
67             // start extensible tracker
68             ExtensibleBundleTracker<?> bundleTracker = new ExtensibleBundleTracker<>(context,
69                     primaryModuleFactoryBundleTracker, moduleInfoBundleTracker);
70             bundleTracker.open();
71
72             // Wrap config registry with JMX notification publishing adapter
73             final JMXNotifierConfigRegistry notifyingConfigRegistry =
74                     new JMXNotifierConfigRegistry(configRegistry, configMBeanServer);
75
76             // register config registry to OSGi
77             AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, GeneratedClassLoadingStrategy.class);
78             AutoCloseable configRegReg = registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
79
80             // register config registry to jmx
81             ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
82             try {
83                 configRegistryJMXRegistrator.registerToJMXNoNotifications(configRegistry);
84             } catch (InstanceAlreadyExistsException e) {
85                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
86             }
87
88             // register config registry to jmx
89             final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = new ConfigRegistryJMXRegistrator(configMBeanServer);
90             try {
91                 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
92             } catch (InstanceAlreadyExistsException e) {
93                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
94             }
95
96             // TODO wire directly via moduleInfoBundleTracker
97             ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
98                     blankTransactionServiceTracker);
99             serviceTracker.open();
100
101             List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg, configRegistry, wrap(bundleTracker),
102                     configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications, wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry);
103             autoCloseable = OsgiRegistrationUtil.aggregate(list);
104         } catch(Exception e) {
105             LOG.warn("Error starting config manager", e);
106         }
107     }
108
109     @Override
110     public void stop(final BundleContext context) throws Exception {
111         autoCloseable.close();
112     }
113 }