Merge "Update toaster example to emit lifecycle logs"
[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 org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
11 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
12 import org.opendaylight.controller.config.manager.impl.osgi.mapping.CodecRegistryProvider;
13 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
14 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
15 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
16 import org.opendaylight.controller.config.spi.ModuleFactory;
17 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
18 import org.osgi.framework.BundleActivator;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.util.tracker.ServiceTracker;
21
22 import javax.management.InstanceAlreadyExistsException;
23 import javax.management.MBeanServer;
24 import java.lang.management.ManagementFactory;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.registerService;
29 import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.wrap;
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(BundleContext context) {
38
39         ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl?
40
41         RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
42                 moduleInfoBackedContext, moduleInfoBackedContext, context);
43
44         ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
45
46         CodecRegistryProvider codecRegistryProvider = new CodecRegistryProvider(moduleInfoBackedContext, context);
47
48         // start config registry
49         BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
50                 context);
51         ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
52                 codecRegistryProvider.getCodecRegistry());
53
54         // track bundles containing factories
55         BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
56                 configRegistry);
57         ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker(
58                 blankTransactionServiceTracker);
59
60         // start extensible tracker
61         ExtensibleBundleTracker<?> bundleTracker = new ExtensibleBundleTracker<>(context,
62                 primaryModuleFactoryBundleTracker, moduleInfoBundleTracker);
63         bundleTracker.open();
64
65         // register config registry to OSGi
66         AutoCloseable configRegReg = registerService(context, configRegistry, ConfigRegistryImpl.class);
67
68         // register config registry to jmx
69         ConfigRegistryJMXRegistrator 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         // TODO wire directly via moduleInfoBundleTracker
77         ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
78                 blankTransactionServiceTracker);
79         serviceTracker.open();
80
81         List<AutoCloseable> list = Arrays.asList(
82                 codecRegistryProvider, configRegistry, wrap(bundleTracker), configRegReg, configRegistryJMXRegistrator, wrap(serviceTracker));
83         autoCloseable = OsgiRegistrationUtil.aggregate(list);
84     }
85
86     @Override
87     public void stop(BundleContext context) throws Exception {
88         autoCloseable.close();
89     }
90 }