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