Merge "BUG 2849 : Reduce sending of duplicate replication messages"
[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.manager.impl.ConfigRegistryImpl;
19 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
20 import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
21 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
22 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
23 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
24 import org.opendaylight.controller.config.spi.ModuleFactory;
25 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
26 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
27 import org.osgi.framework.BundleActivator;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.util.tracker.ServiceTracker;
30
31 public class ConfigManagerActivator implements BundleActivator {
32     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
33
34     private AutoCloseable autoCloseable;
35
36     @Override
37     public void start(final BundleContext context) {
38
39         ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl?
40
41         BindingContextProvider bindingContextProvider = new BindingContextProvider();
42
43         RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
44                 moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, bindingContextProvider, context);
45
46         ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
47
48
49         // start config registry
50         BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
51                 context);
52         ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
53                 bindingContextProvider);
54
55         // track bundles containing factories
56         BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
57                 configRegistry);
58         ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker(
59                 blankTransactionServiceTracker);
60
61         // start extensible tracker
62         ExtensibleBundleTracker<?> bundleTracker = new ExtensibleBundleTracker<>(context,
63                 primaryModuleFactoryBundleTracker, moduleInfoBundleTracker);
64         bundleTracker.open();
65
66         // register config registry to OSGi
67         AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, GeneratedClassLoadingStrategy.class);
68         AutoCloseable configRegReg = registerService(context, configRegistry, ConfigRegistryImpl.class);
69
70         // register config registry to jmx
71         ConfigRegistryJMXRegistrator 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         // TODO wire directly via moduleInfoBundleTracker
79         ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
80                 blankTransactionServiceTracker);
81         serviceTracker.open();
82
83         List<AutoCloseable> list = Arrays.asList(
84                 bindingContextProvider, clsReg,configRegistry, wrap(bundleTracker), configRegReg, configRegistryJMXRegistrator, wrap(serviceTracker));
85         autoCloseable = OsgiRegistrationUtil.aggregate(list);
86     }
87
88     @Override
89     public void stop(final BundleContext context) throws Exception {
90         autoCloseable.close();
91     }
92 }